fix: handle parameterized generics in field(type=...) to avoid infinite recursion#325
Open
gaoflow wants to merge 1 commit into
Open
fix: handle parameterized generics in field(type=...) to avoid infinite recursion#325gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
…infinite recursion Passing a parameterized generic such as `tuple[int, int]` (types.GenericAlias) or `List[int]` (typing._GenericAlias) as the `type` argument to `field()` caused `maybe_parse_user_type` to loop forever. Both alias types satisfy `isinstance(t, Iterable)`, but iterating them yields starred self-references (e.g. `*tuple[int, int]`) that are themselves Iterable aliases of the same kind, so the recursive expansion never bottoms out. Fix: detect any object with an `__origin__` attribute that is not itself a plain type (covers both `types.GenericAlias` and `typing._GenericAlias`), call `typing.get_origin()` to extract the bare origin class, and return that. This makes `field(type=tuple[int, int])` behave like `field(type=tuple)` at runtime while avoiding the recursion. Closes tobgu#291
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.
Summary
Fixes #291.
Passing a parameterized generic like
tuple[int, int](atypes.GenericAlias) ortyping.List[int](atyping._GenericAlias) as thetype=argument tofield()caused
maybe_parse_user_typeto recurse forever and raise aRecursionError.Root cause: Both alias kinds satisfy
isinstance(t, Iterable), so the codeentered the iterable branch and iterated the alias. Iterating a
types.GenericAliasyields a starred version of itself (e.g.*tuple[int, int]),which is itself a
GenericAliasand alsoIterable— the recursion never bottoms out.Fix: Before falling through to the generic-iterable branch, detect objects that
carry an
__origin__attribute but are not plaintypeinstances (i.e. allGenericAliasforms). Calltyping.get_origin()to extract the bare origin classand return that. This makes
field(type=tuple[int, int])behave likefield(type=tuple)for runtimeisinstancechecks while correctly rejectingnon-tuple values.
Minimal reproducer (before fix → RecursionError; after fix → works)
Test
Added
test_field_type_generic_aliasintests/class_test.pycoveringtuple[int, int],list[str], anddict[str, int]aliases. Full test suite(635 tests) passes.
This pull request was prepared with the assistance of AI, under my direction and review.