| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Expand Up | @@ -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) | ||||||||||||||||||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhy do we need to do this? Shouldn't the local namespace override the type params? Test case: class X[T]:
T = int
x: T
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI 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 globalns, and the globals here are found in the localns variable. This highly confusing situation is because the globals and locals are swapped higher up in the call stack here, for backwards compatibility reasons if you believe the comment: Lines 2419 to 2426 in a3711af
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI saw that comment, maybe we should find a way to get rid of that compatibility hack.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI don't really see a way round it, short of deprecating get_type_hints altogether. Which... I'm not 100% against, but it would be quite disruptive.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThere could be other approaches, like adding new keyword arguments. Will have to think about it more.
Sorry, something went wrong.
AlexWaygood reacted with thumbs up emoji
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIf 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.
Sorry, something went wrong.
All reactions
|
||||||||||||||||||
|
|
||||||||||||||||||
| 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__, | ||||||||||||||||||
| Expand Down | ||||||||||||||||||
| 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. |
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhy do we need the __forward_is_class__ condition? I don't understand why class-scoped forwardrefs are special here.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityFor 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 called globalns) and popping them from the global namespace (which is here called localns). For classes, we only want to give type_params highest priority if they haven't been overridden in the local scope, however.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.