GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-114053: Fix another edge case involving `get_type_hints`, PEP 695 and PEP 563 by AlexWaygood · Pull Request #120272 · python/cpython Conversation
|
Thanks, good catch -- that test failed. I added it and fixed it. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Okay, this still doesn't get things right in all cases :( >>> from __future__ import annotations >>> from typing import get_type_hints >>> Eggs, Spam = int, str >>> def f[Eggs, **Spam](x: Eggs, y: Spam): pass ... >>> get_type_hints(f) {'x': <class 'int'>, 'y': <class 'str'>} |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Should have been fixed in 2fbcd7b. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| 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: |
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem 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 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.
There was an error while loading. Please reload this page.
| 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.
The 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:
class X[T]:
T = int
x: T
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The 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 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
| if localns is None and globalns is None: | |
| # This is surprising, but required. Before Python 3.10, | |
| # get_type_hints only evaluated the globalns of | |
| # a class. To maintain backwards compatibility, we reverse | |
| # the globalns and localns order so that eval() looks into | |
| # *base_globals* first rather than *base_locals*. | |
| # This only affects ForwardRefs. | |
| base_globals, base_locals = base_locals, base_globals |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I 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.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@JelleZijlstra, are you okay with this going in? I'd love to get this into the next 3.13 beta if possible (but at the same time, the bug also exists in Python 3.12, so it wouldn't be a disaster if it waited until beta 4). I agree that the implementation is kinda weird but I'm not sure there's a better way of doing it until we've sorted out the strange globals/locals swapping (which I think would probably be harder to backport to 3.12). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…P 695 and PEP 563 (pythonGH-120272) (cherry picked from commit 2d3187b) Co-authored-by: Alex Waygood <[email protected]>
|
GH-121003 is a backport of this pull request to the 3.13 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…P 695 and PEP 563 (pythonGH-120272) (cherry picked from commit 2d3187b) Co-authored-by: Alex Waygood <[email protected]>
|
GH-121004 is a backport of this pull request to the 3.12 branch. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…EP 695 and PEP 563 (GH-120272) (#121003) Co-authored-by: Alex Waygood <[email protected]>
…EP 695 and PEP 563 (GH-120272) (#121004) Co-authored-by: Alex Waygood <[email protected]>
…P 695 and PEP 563 (python#120272)
…P 695 and PEP 563 (python#120272)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.