Repository navigation
Conversation
…ag#219) On Python 3.14, a forward reference created by annotationlib is evaluated against the namespace of the class that owns the annotation. A method such as `def list(self)` then shadows the builtin `list` in an annotation like `items: list[str]`, and loading the dataclass fails with "TypeError: 'function' object is not subscriptable". When evaluating such a forward reference, use the owner's namespace without members that can never be a type (functions, class/static methods, properties), so those names resolve from the module globals or builtins instead, while nested classes are still resolved from the class namespace. Applied to both the v1 and v0 code paths.
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.
Description
On Python 3.14 (PEP 649/749), a forward reference created by
annotationlibis evaluated against the namespace of the class that owns the annotation. So withdataclassesstoresitemsasForwardRef('list[str]', owner=Inv). Wheneval_forward_ref()resolves it withlocalns=None,ForwardRef.evaluate()falls back tovars(owner), solistbecomes the method and loading fails withTypeError: 'function' object is not subscriptable. Both the v1 and v0 loaders are affected. As noted in the issue,typing.List,dict, and similar names hit the same problem when a method of the same name exists.This PR adds a small
_get_typing_locals()helper (used byeval_forward_ref()in bothutils/_typing_compat.pyandv0/utils/typing_compat.py). On 3.14+, when the forward ref has a class__owner__, the helper passes that class's namespace as locals, minus members that can never be a type: functions, builtin functions,classmethod/staticmethod,property, andcached_property. Names likelist/List/dictthen resolve from the module globals or builtins again, while real class-level types such as nested classes still resolve. On Python < 3.14 the behavior is unchanged (_TYPING_LOCALSas before).Scope note: a bare
items: listnext to alist()method is evaluated by Python itself to the method, and no ForwardRef is involved, so this PR doesn't change that case.Regression tests were added for v1 (
tests/unit/test_loaders.py) and v0 (tests/unit/v0/test_load.py). The v1 test covers alist()method, aList()method, adictproperty, and a nested dataclass, with a round-trip. Both tests fail onmainunder 3.14 with theTypeErrorfrom the issue and pass with this change. They also pass on older Pythons, where the method never interfered.Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it — please feel free to close it, no hard feelings at all 🙂
Related issue
Closes #219
Checklist
pytest --ignore-glob=*integration*): 886 passed, 2 skipped, 6 xfailed, 4 xpassed on CPython 3.14.0rc2, 3.13 and 3.12, and 885 passed on 3.11. The new tests fail on unpatchedmainunder 3.14 and pass with the fix.flake8reports nothing new in the changed modules.CHANGELOG.mdis updated (if applicable) — n/a (HISTORY.rst is written at release time)