Skip to content
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

Document difference between typing.get_type_hints and inspect.get_annotations(eval_str=True) #102405

Open
hauntsaninja opened this issue Mar 3, 2023 · 4 comments
Labels
docs Documentation in the Doc dir topic-typing

Comments

@hauntsaninja
Copy link
Contributor

The differences are subtle, it could be nice to explicitly document them somewhere.

@hauntsaninja hauntsaninja added docs Documentation in the Doc dir topic-typing labels Mar 3, 2023
@ericvsmith
Copy link
Member

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.

@chongkong
Copy link

I'm still having difficulty understanding a diff. Before publishing the doc, can you explain the diffs briefly?

copybara-service bot pushed a commit to tensorflow/tfx that referenced this issue Jun 15, 2023
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
copybara-service bot pushed a commit to tensorflow/tfx that referenced this issue Jun 20, 2023
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
copybara-service bot pushed a commit to tensorflow/tfx that referenced this issue Jun 20, 2023
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
@Hnasar
Copy link

Hnasar commented Apr 26, 2024

I came across a difference— it has to do with how stringified types (or from __future__ import annotations) are evaluated with TypedDicts. When using inspect.get_annotations the type is ForwardRef("str"), but with typing.get_type_hints the type is (a more typical) str. I'm not sure if this is a bug or an intended difference; but having two similar functions sure is confusing!

# 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 ==========================

@Hnasar
Copy link

Hnasar commented May 27, 2024

Another difference: only typing.get_type_hints returns the inherited members:

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'>}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-typing
Projects
None yet
Development

No branches or pull requests

4 participants