[New-bugs-announce] [issue39866] get_type_hints raises inconsistent TypeError
Joey
report at bugs.python.org
Thu Mar 5 15:14:16 EST 2020
New submission from Joey <j.tran4418 at gmail.com>:
If you pass in an instance of an object without type annotations, you get an error that states "XXX is not a module, class, method, or function." This correctly describes the situation
typing.get_type_hints(object())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/typing.py", line 1252, in get_type_hints
raise TypeError('{!r} is not a module, class, method, '
TypeError: <object object at 0x7f577b44ffb0> is not a module, class, method, or function.
However, if you pass in an instance of a class that _does_ have type annotations...
>class Bar:
... foo: int
>typing.get_type_hints(Bar())
{'foo': <class 'int'>}
You don't get an error even though the message of the first exception would suggest you do.
Fix should be pretty easy, either just have the get_type_hints always return a dictionary, and return an empty dictionary if the type of the object has no __annotations__ defined (my preferred solution), or actually check to see if the object is an instance of `_allowed_types` before checking whether the object has annotations.
----------
components: Library (Lib)
messages: 363450
nosy: j.tran4418
priority: normal
severity: normal
status: open
title: get_type_hints raises inconsistent TypeError
versions: Python 3.6, Python 3.7, Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39866>
_______________________________________
More information about the New-bugs-announce
mailing list