Skip to content

get_type_hints(K format=FORWARDREF) raises when K is dynamically created class #124206

Closed
@sobolevn

Description

@sobolevn

Bug report

On main this raises:

>>> from typing import get_type_hints
>>> K = type('K', (), {})
>>> get_type_hints(K)
{}
>>> get_type_hints(K, format=2)
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    get_type_hints(K, format=2)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython2/Lib/typing.py", line 2393, in get_type_hints
    ann = annotationlib.get_annotations(base, format=format)
  File "/Users/sobolev/Desktop/cpython2/Lib/annotationlib.py", line 634, in get_annotations
    annotate = get_annotate_function(obj)
  File "/Users/sobolev/Desktop/cpython2/Lib/annotationlib.py", line 578, in get_annotate_function
    return _BASE_GET_ANNOTATE(obj)
AttributeError: type object 'object' has no attribute '__annotate__'

However, this does not raise:

>>> from inspect import get_annotations
>>> K = type('K', (), {})
>>> get_annotations(K)
{}
>>> get_annotations(K, format=2)
{}

I suppose that this is a bug.

Linked PRs

Metadata

Metadata

Assignees

Labels

3.14bugs and security fixestopic-typingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions