-
-
Notifications
You must be signed in to change notification settings - Fork 32k
typing.get_type_hints
Can Return Incorrect Type for Annotated Metadata
#112618
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
Comments
typing.get_type_hints
Can Return Incorrect Type for Annotated Metadata
Hello! from typing import Annotated, get_type_hints
class Parent:
def __init__(self, num: int) -> None:
self.num = num
class Foo(Parent):
# Realization of hashable protocol: https://docs.python.org/3.12/glossary.html#term-hashable
def __eq__(self, other):
if not isinstance(other, Parent):
return NotImplemented
return self.num == other.num
def __hash__(self):
return hash(self.num)
class Bar(Parent): # Same realization of __eq__ and __hash__ as in Foo but semantically it's another class :)
def __eq__(self, other):
if not isinstance(other, Parent):
return NotImplemented
return self.num == other.num
def __hash__(self):
return hash(self.num)
class Form:
box_a: Annotated[str, Foo(1)]
class Message:
field_a: Annotated[str, Bar(1)]
hints = get_type_hints(Message, include_extras=True)
metadata = hints["field_a"].__metadata__[0]
print(f"MessageCode metadata type: {type(metadata)}")
hints = get_type_hints(Form, include_extras=True)
metadata = hints["box_a"].__metadata__[0]
print(f"DisplayName metadata type: {type(metadata)}") Output: MessageCode metadata type: <class '__main__.Foo'>
DisplayName metadata type: <class '__main__.Foo'> Honestly, I don't know, should it be fixed or not, this behaviour works for years (as I see, from 2018 year). |
I think that there's an easy fix for that which also keeps all tests green: >>> hints = get_type_hints(Message, include_extras=True)
>>> metadata = hints["field_a"].__metadata__[0]
>>> print(f"MessageCode metadata type: {type(metadata)}")
MessageCode metadata type: <class '__main__.MessageCode'>
>>>
>>> hints = get_type_hints(Form, include_extras=True)
>>> metadata = hints["box_a"].__metadata__[0]
>>> print(f"DisplayName metadata type: {type(metadata)}")
DisplayName metadata type: <class '__main__.DisplayName'>
Patch: » git patch
diff --git Lib/typing.py Lib/typing.py
index b3af701f8d5..a7fa763c198 100644
--- Lib/typing.py
+++ Lib/typing.py
@@ -497,6 +497,11 @@ def __getitem__(self, parameters):
return self._getitem(self, *parameters)
+class _AnnotatedSpecialForm(_SpecialForm, _root=True):
+ def __getitem__(self, parameters):
+ return self._getitem(self, parameters)
+
+
class _AnyMeta(type):
def __instancecheck__(self, obj):
if self is Any:
@@ -2005,7 +2010,8 @@ def __mro_entries__(self, bases):
return (self.__origin__,)
-@_SpecialForm
+@_AnnotatedSpecialForm
+@_tp_cache(typed=True)
def Annotated(self, params):
"""Add context-specific metadata to a type. I will experiment a bit with it and decide whether or not I will open a PR for it :) |
Thanks for the bug report @cmg2146! Here's a more minimal repro: Python 3.13.0a2+ (heads/main-dirty:8f71b349de, Nov 27 2023, 22:42:08) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Annotated
>>> Annotated[str, 1]
typing.Annotated[str, 1]
>>> Annotated[str, True]
typing.Annotated[str, 1] @Eclips4, I think this is definitely worth fixing :) I think |
Co-authored-by: Alex Waygood <[email protected]>
…ythonGH-112628) (cherry picked from commit 2a378ca) Co-authored-by: Alex Waygood <[email protected]>
I'd accept a PR adding this to typing-extensions. |
Co-authored-by: Alex Waygood <[email protected]>
Co-authored-by: Alex Waygood <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
When running the following example:
the output will be:
when it should be:
This issue seems to occur only when:
CPython versions tested on:
3.9
Operating systems tested on:
Linux, Windows
Linked PRs
Annotated
cache typed #112619The text was updated successfully, but these errors were encountered: