8000 GH-96079 Fix missing field name for _AnnotatedAlias by iyume · Pull Request #96080 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-96079 Fix missing field name for _AnnotatedAlias #96080

New issue

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

Merged
merged 5 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7131,6 +7131,7 @@ def test_special_attrs(self):
typing.Self: 'Self',
# Subscribed special forms
typing.Annotated[Any, "Annotation"]: 'Annotated',
typing.Annotated[int, 'Annotation']: 'Annotated',
typing.ClassVar[Any]: 'ClassVar',
typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate',
typing.Final[Any]: 'Final',
Expand Down
5 changes: 4 additions & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ def __init__(self, origin, metadata):
if isinstance(origin, _AnnotatedAlias):
metadata = origin.__metadata__ + metadata
origin = origin.__origin__
super().__init__(origin, origin)
super().__init__(origin, origin, name='Annotated')
self.__metadata__ = metadata

def copy_with(self, params):
Expand Down Expand Up @@ -2132,6 +2132,9 @@ def __getattr__(self, attr):
return 'Annotated'
return super().__getattr__(attr)

def __mro_entries__(self, bases):
return (self.__origin__,)
Comment on lines +2135 to +2136
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens without this? It seems kind of random to me (but I didn't try to run the tests).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gvanrossum First about __module__: it is not truly covered by test:

typing.Annotated[Any, "Annotation"]: 'Annotated',

Here typing.Any.__module__ is typing, so passed, but errors in int. It is directly caused by:

cpython/Lib/typing.py

Lines 1357 to 1358 in c7f2686

if not name:
self.__module__ = origin.__module__

The idea of this PR is to add _name attribute to _AnnotatedAlias instance, since _AnnotatedAlias.__origin__ is just the first argument and nowhere to determind where Annotated annotation from.

But with _name attribute, _GenericAlias will think the __origin__ is generic ABCs, Generic will finally add to mros.

cpython/Lib/typing.py

Lines 1270 to 1280 in c7f2686

def __mro_entries__(self, bases):
res = []
if self.__origin__ not in bases:
res.append(self.__origin__)
i = bases.index(self)
for b in bases[i+1:]:
if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
break
else:
res.append(Generic)
return tuple(res)

So __mro_entries__ overridden is required.



class Annotated:
"""Add context specific metadata to a type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In :mod:`typing`, fix missing field ``name`` and incorrect ``__module__`` in _AnnotatedAlias.
0