Fix LocalPythonExecutor support for Enum and other metaclasses by colesmcintosh · Pull Request #1797 · huggingface/smolagents (original) (raw)
Fixes huggingface#1795
The LocalPythonExecutor was using the generic type() function to create
all classes, which fails for classes with custom metaclasses like Enum.
Python's Enum metaclass requires a special _EnumDict namespace object
during class creation, not a regular dict.
This fix:
- Detects when a base class has a custom metaclass
- Uses the metaclass's
__prepare__()method if available to create the proper namespace dictionary - Uses the detected metaclass instead of
type()when instantiating the class
This enables support for Enum and any other class that uses a custom
metaclass with a __prepare__() method.
approved these changes Oct 9, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})