mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
bpo-41370: Evaluate strings as forward refs in PEP 585 generics (GH-30900)
This removes discrepancy between list["int"] and List["int"]. Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
77446d2aa5
commit
b465b60604
3 changed files with 49 additions and 2 deletions
|
@ -336,6 +336,12 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
|
|||
if isinstance(t, ForwardRef):
|
||||
return t._evaluate(globalns, localns, recursive_guard)
|
||||
if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
|
||||
if isinstance(t, GenericAlias):
|
||||
args = tuple(
|
||||
ForwardRef(arg) if isinstance(arg, str) else arg
|
||||
for arg in t.__args__
|
||||
)
|
||||
t = t.__origin__[args]
|
||||
ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
|
||||
if ev_args == t.__args__:
|
||||
return t
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue