-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
gh-114053: Fix another edge case involving get_type_hints
, PEP 695 and PEP 563
#120272
Changes from 9 commits
879abeb
065914e
373b23c
4f95b1c
ffdba91
5505d97
b05ca1a
bccaea2
2fbcd7b
432e749
File filter
Filter by extension
Conversations
Jump to
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) | ||||||||||||||||||
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:
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
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. 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 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. 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. If you decide to add keywords arguments or deprecate things, please tell me in advance so that I can be reactive on Sphinx' side if needed. |
||||||||||||||||||
|
||||||||||||||||||
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.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_params
should 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_params
highest priority if they haven't been overridden in the local scope, however.