| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
…P 695 and PEP 563
There was a problem hiding this comment.
Can you add a test where T also exists in the global scope, ensuring that the type param is picked up over the global?
Sorry, something went wrong.
Thanks, good catch -- that test failed. I added it and fixed it. |
Sorry, something went wrong.
|
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.
Should have been fixed in 2fbcd7b. |
Sorry, something went wrong.
| 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.
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 a problem hiding this comment.
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.
| 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.
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 a problem hiding this comment.
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
Sorry, something went wrong.
There was a problem hiding this comment.
I saw that comment, maybe we should find a way to get rid of that compatibility hack.
Sorry, something went wrong.
There was a problem hiding this comment.
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 a problem hiding this comment.
There could be other approaches, like adding new keyword arguments. Will have to think about it more.
Sorry, something went wrong.
There was a problem hiding this comment.
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.
|
@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.
|
Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
Sorry, something went wrong.
…P 695 and PEP 563 (pythonGH-120272) (cherry picked from commit 2d3187b) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
|
GH-121003 is a backport of this pull request to the 3.13 branch. |
Sorry, something went wrong.
…P 695 and PEP 563 (pythonGH-120272) (cherry picked from commit 2d3187b) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
|
GH-121004 is a backport of this pull request to the 3.12 branch. |
Sorry, something went wrong.
…P 695 and PEP 563 (python#120272)
…P 695 and PEP 563 (python#120272)
…P 695 and PEP 563 (python#120272)
| Back | FazBrowse Home | New Git URL |
This PR fixes a bug in #118009 that can be seen in the following REPL demo, where get_type_hints under from __future__ import annotations semantics won't detect if a variable defined in a type-parameter namespace has been overidden in a local namespace:
The cause of the bug is that get_type_hints does this weird swapping of globals and locals, and the logic added in #118009 doesn't account for that -- it injects the type parameters into the dictionary called localns (which is actually the globals) when it should be injecting the type parameters into globalns (which is actually the locals).
I actually added a test for this exact edge case in #118009, which should have caught the bug. But there was a bug in the test, because the test was trying to be too clever, and I got mixed up between ^ and & when you're using sets. Had I not tried to write such a clever test, I would have caught the bug.