Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-114053: Fix another edge case involving get_type_hints, PEP 695 and PEP 563#120272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
879abeb065914e373b23c4f95b1cffdba915505d97b05ca1abccaea22fbcd7b432e749File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1060,15 +1060,24 @@ def _evaluate(self, globalns, localns, type_params=_sentinel, *, recursive_guard | ||||||||||||||||||
| globalns = getattr( | ||||||||||||||||||
| sys.modules.get(self.__forward_module__, None), '__dict__', globalns | ||||||||||||||||||
| ) | ||||||||||||||||||
| # type parameters require some special handling, | ||||||||||||||||||
| # as they exist in their own scope | ||||||||||||||||||
| # but `eval()` does not have a dedicated parameter for that scope. | ||||||||||||||||||
| # For classes, names in type parameter scopes should override | ||||||||||||||||||
| # names in the global scope (which here are called `localns`!), | ||||||||||||||||||
| # but should in turn be overridden by names in the class scope | ||||||||||||||||||
| # (which here are called `globalns`!) | ||||||||||||||||||
| if type_params: | ||||||||||||||||||
| # "Inject" type parameters into the local namespace | ||||||||||||||||||
| # (unless they are shadowed by assignments *in* the local namespace), | ||||||||||||||||||
| # as a way of emulating annotation scopes when calling `eval()` | ||||||||||||||||||
| locals_to_pass ={param.__name__: param for param in type_params} | localns | ||||||||||||||||||
| else: | ||||||||||||||||||
| locals_to_pass = localns | ||||||||||||||||||
| globalns, localns = dict(globalns), dict(localns) | ||||||||||||||||||
| for param in type_params: | ||||||||||||||||||
| param_name = param.__name__ | ||||||||||||||||||
| if not self.__forward_is_class__ or param_name not in globalns: | ||||||||||||||||||
| globalns[param_name] = param | ||||||||||||||||||
| localns.pop(param_name, None) | ||||||||||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to do this? Shouldn't the local namespace override the type params? Test case: MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already have that test case included in this PR, and it fails unless we have this block of code here. The local namespace shoudl indeed override the type param, but the locals here are found in the Lines 2419 to 2426 in a3711af
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw that comment, maybe we should find a way to get rid of that compatibility hack. MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really see a way round it, short of deprecating Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There could be other approaches, like adding new keyword arguments. Will have to think about it more. Member
| ||||||||||||||||||
| type_ = _type_check( | ||||||||||||||||||
| eval(self.__forward_code__, globalns, locals_to_pass), | ||||||||||||||||||
| eval(self.__forward_code__, globalns, localns), | ||||||||||||||||||
| "Forward references must evaluate to types.", | ||||||||||||||||||
| is_argument=self.__forward_is_argument__, | ||||||||||||||||||
| allow_special_forms=self.__forward_is_class__, | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fix edge-case bug where :func:`typing.get_type_hints` would produce | ||
| incorrect results if type parameters in a class scope were overridden by | ||
| assignments in a class scope and ``from __future__ import annotations`` | ||
| semantics were enabled. Patch by Alex Waygood. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need the
__forward_is_class__condition? I don't understand why class-scoped forwardrefs are special here.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For functions, names in
type_paramsshould always take highest priority, and this is implemented by inserting the type params into the local namespace (which is here calledglobalns) and popping them from the global namespace (which is here calledlocalns). For classes, we only want to givetype_paramshighest priority if they haven't been overridden in the local scope, however.