fix(types): don't treat instance methods on Enum output types as choices#1916
Merged
RobinPicard merged 1 commit intoJul 20, 2026
Merged
Conversation
|
📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1916/ Preview updates automatically with each commit. |
_get_enum_members scans an Enum class's __dict__ for plain FunctionType
values to support enums whose members are functions assigned directly
(e.g. `c = some_function`), since Python's Enum machinery treats those
as methods rather than members under normal iteration.
The scan had no way to tell that apart from an ordinary instance method
defined with def directly in the class body (e.g. describe(self)) - both
end up as plain FunctionType objects in __dict__. Any Enum with a helper
method crashed in python_types_to_terms with a misleading error about
missing type annotations, because the method's self parameter has none.
Use __qualname__ to distinguish the two: a method defined in the class
body is qualified with the class's own name ("Color.describe"), while a
function defined elsewhere and assigned as a member's value keeps its
original qualname ("some_function"). This excludes actual instance
methods while preserving the existing bare-function-member pattern
already covered by test_dsl_python_types_to_terms's SomeEnum.c case.
Fixes dottxt-ai#1915
RobinPicard
force-pushed
the
fix/enum-instance-method-not-a-choice
branch
from
July 20, 2026 09:00
c0994c3 to
af28053
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1915.
_get_enum_membersscans an Enum class's__dict__for plainFunctionTypevalues to support enums whose members are assigned as bare functions (e.g.c = some_function), since Python's Enum machinery treats a plain function assigned in the class body as a method rather than a member - it doesn't show up through normal iteration.The scan had no way to tell that pattern apart from an actual instance method defined with
defdirectly in the class body (e.g.describe(self)). Both end up as plainFunctionTypeobjects in__dict__, so any Enum used as an output type that happened to also define a helper method crashed:describegets swept up as a "member," and building a JSON schema from its signature fails on the unannotatedself- a misleading error unrelated to the actual problem.Fix
__qualname__distinguishes the two cases: a method defined directly in the class body is qualified with the enclosing class's own name ("Color.describe"), while a function defined elsewhere and merely assigned as a member's value keeps its original qualname ("some_function"). Filtering on__qualname__excludes real instance methods while still picking up the bare-function-member pattern thattest_dsl_python_types_to_terms'sSomeEnum.c = funccase already covers.I initially proposed restricting the scan to
functools.partialvalues only (mentioned in the issue), but that turned out to be too narrow - it broke the existingSomeEnum.c = functest, which assigns a plain function directly withoutpartial. Left a correction comment on the issue with the details; the__qualname__approach here preserves that pattern.Testing
Added
test_dsl_enum_with_instance_method_is_not_a_choicetotests/types/test_dsl.py, covering the crash case from the issue.Ran the full
tests/types/suite locally (233 tests, all passing), plustests/types/test_dsl.pyandtests/types/test_types_utils.pyspecifically to make sure the existingSomeEnum/enum-related coverage still passes unchanged.