Repository navigation
gh-160766: Handle nested annotated and generic aliases that contain forward references in annotations - #160769
DavidCEllis wants to merge 6 commits into
Conversation
Special case name replacement to render correct strings
| elts.append(new_elt) | ||
| ast_class = {list: ast.List, tuple: ast.Tuple, set: ast.Set}[type(other)] | ||
| return ast_class(elts), extra_names | ||
| elif type(other) is types.GenericAlias or _is_annotated_alias(other): |
There was a problem hiding this comment.
Should this be
| elif type(other) is types.GenericAlias or _is_annotated_alias(other): | |
| elif isinstance(other, types.GenericAlias) or _is_annotated_alias(other): |
?
GenericAlias can be subclassed.
There was a problem hiding this comment.
As can list/dict/tuple/set. Subclassing could change the constructor and we wouldn't necessarily be able to reconstruct the object correctly.
However I did discover that there's a typing._GenericAlias that is used when you make your own generic classes using the class A[T]: ... syntax which isn't the same as types.GenericAlias that will also need to be handled. It's also not a subclass of types.GenericAlias so checking for subclasses wouldn't catch it either.
This will also need to be handled:
from annotationlib import get_annotations, Format
class Q[T]: ...
class X:
a: Y[Q[Z]]
# Get annotations here to get the ForwardRef
anno = get_annotations(X, format=Format.FORWARDREF)['a']
class Y[T]:
pass
class Z:
pass
print(anno.evaluate(format=Format.STRING))
print(anno.evaluate(format=Format.VALUE))Y[__main__.Q[ForwardRef('Z', is_class=True, owner=<class '__main__.X'>)]]
__main__.Y[__main__.Q[ForwardRef('Z', is_class=True, owner=<class '__main__.X'>)]]There was a problem hiding this comment.
So I'm not to familiar with the stringifier API from annotationlib, but I believe we usually assume subclasses respect the LSP for the constructor. For reference, typing._eval_type() has this logic to rebuild aliases:
Lines 476 to 503 in 3b30937
With _rebuild_generic_alias() defined as:
Lines 437 to 445 in 3b30937
For typing._GenericAlias instances, the copy_with() method is used.
There was a problem hiding this comment.
So I'm not to familiar with the stringifier API from annotationlib, but I believe we usually assume subclasses respect the LSP for the constructor.
I don't think we do this in annotationlib. typing may make the assumption that a value is a valid type hint or obeys LSP, but annotationlib doesn't. To a reasonable extent if an expression is valid in an annotation we currently try to reproduce it correctly even if it's a nonsensical type hint.
As such I'm wary of trying to handle a general case we can't guarantee we can reconstruct without losing information. There may be other specific cases we can and should handle though.
Additional name correction logic for deprecated typing aliases. Logic to skip reconstruction if all values resolve.
This adds another special case to
_Stringifier.__convert_to_astto handletypes.GenericAliasandtyping.Annotatedsimilar to the handling of list, set, tuple, dict that constructs the ast to rebuild theGenericAliasorAnnotatedobject.This is to prevent the
ForwardRefobjects from becoming "hidden" inside theGenericAliasorAnnotatedobjects.For
GenericAliasanast.Callnode is created to reconstruct theGenericAliasas there's no guarantee it was created through subscripting. The call is converted back to subscript format when evaluating theForwardRefinSTRINGformat in order to match the repr ofGenericAliasobjects.For
Annotated, subscripting is used. Are there any other types that need to be handled?Main:
PR:
ForwardRef.evaluate#160766