-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Document difference between typing.get_type_hints and inspect.get_annotations(eval_str=True) #102405
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
It might make sense to delay this until PEP 649 is accepted. Or at least expect to rework it. But I agree it's a good idea. |
I'm still having difficulty understanding a diff. Before publishing the doc, can you explain the diffs briefly? |
Also we're not using `inspect.get_annotations`, not only because it is 3.10+ only, but also we want the additional conversion behavior of `get_type_hints` including: - Resolving string/ForwardRef types - Annotation inheritance - Unwrapping `typing.Annotated` However this is also supported in `inspect.get_annotations(eval_str=True)`, and the decision could be changed in the future. References: - python/cpython#102405 - https://bugs.python.org/issue43817 PiperOrigin-RevId: 540477123
Also we're not using `inspect.get_annotations`, not only because it is 3.10+ only, but also we want the additional conversion behavior of `get_type_hints` including: - Resolving string/ForwardRef types - Annotation inheritance - Unwrapping `typing.Annotated` However this is also supported in `inspect.get_annotations(eval_str=True)`, and the decision could be changed in the future. References: - python/cpython#102405 - https://bugs.python.org/issue43817 PiperOrigin-RevId: 540477123
Also we're not using `inspect.get_annotations`, not only because it is 3.10+ only, but also we want the additional conversion behavior of `get_type_hints` including: - Resolving string/ForwardRef types - Annotation inheritance - Unwrapping `typing.Annotated` However this is also supported in `inspect.get_annotations(eval_str=True)`, and the decision could be changed in the future. References: - python/cpython#102405 - https://bugs.python.org/issue43817 PiperOrigin-RevId: 540477123
I came across a difference— it has to do with how stringified types (or # test_ann.py
import dataclasses
import enum
import inspect
import typing
import pytest
class ATypedDict(typing.TypedDict):
x: str
class ATypedDictWithStr(typing.TypedDict):
x: "str"
class AnEnum(enum.Enum):
x: str = 1
class AnEnumWithStr(enum.Enum):
x: "str" = 1
@dataclasses.dataclass
class ADataClass:
x: str
@dataclasses.dataclass
class ADataClassWithStr:
x: "str"
@pytest.mark.parametrize(
"cls",
(
ATypedDict,
ATypedDictWithStr,
AnEnum,
AnEnumWithStr,
ADataClass,
ADataClassWithStr,
),
)
def test_equality(cls):
assert inspect.get_annotations(cls, eval_str=True) == typing.get_type_hints(cls) $ python3 -m pytest test_ann.py -vvv
============================== test session starts ==============================
platform linux -- Python 3.11.9, pytest-7.4.0, pluggy-1.2.0
test_ann.py::test_equality[ATypedDict] PASSED [ 16%]
test_ann.py::test_equality[ATypedDictWithStr] FAILED [ 33%]
test_ann.py::test_equality[AnEnum] PASSED [ 50%]
test_ann.py::test_equality[AnEnumWithStr] PASSED [ 66%]
test_ann.py::test_equality[ADataClass] PASSED [ 83%]
test_ann.py::test_equality[ADataClassWithStr] PASSED [100%]
=================================== FAILURES ====================================
_______________________ test_equality[ATypedDictWithStr] ________________________
cls = <class 'test_ann.ATypedDictWithStr'>
@pytest.mark.parametrize(
"cls", (…),
)
def test_equality(cls):
> assert inspect.get_annotations(cls, eval_str=True) == typing.get_type_hints(cls)
E AssertionError: assert {'x': ForwardRef('str', module='test_ann')} == {'x': <class 'str'>}
E Differing items:
E {'x': ForwardRef('str', module='test_ann')} != {'x': <class 'str'>}
E Full diff:
E - {'x': <class 'str'>}
E + {'x': ForwardRef('str', module='test_ann')}
test_ann.py:47: AssertionError
========================== 1 failed, 5 passed in 0.04s ========================== |
Another difference: only import inspect, typing
class A:
age: int
class B(A):
name: str
print(inspect.get_annotations(B)) # {'name': <class 'str'>}
print(typing.get_type_hints(B)) # {'age': <class 'int'>, 'name': <class 'str'>} |
Another difference that is not very explicit in the documentation is that
whereas |
https://docs.python.org/3.14/library/typing.html#typing.get_type_hints now documents most of the differences, though we're missing the thing about raising errors on invalid types like For what it's worth, I think most of what |
Agreed. (The currently published Python 3.13 documentation has the same list, btw.) I mentioned the type validation here rather than opening a new issue since this issue is still open and seemingly collecting action items. Earlier, I switched from |
Oh it's worse, it raises on invalid types only if they are wrapped in a ForwardRef:
I'll open a new issue for that; I feel it's a bug. |
See #133959. If we treat that as a bug and fix it, I think the current documentation for |
The differences are subtle, it could be nice to explicitly document them somewhere.
The text was updated successfully, but these errors were encountered: