[New-bugs-announce] [issue45166] get_type_hints + Final + future annotations = TypeError

Timothee Mazzucotelli report at bugs.python.org
Fri Sep 10 12:19:49 EDT 2021


New submission from Timothee Mazzucotelli <pawamoy at pm.me>:

This is my first issue on bugs.python.org, let me know if I can improve it in any way.

---

Originally reported and investigated here: https://github.com/mkdocstrings/mkdocstrings/issues/314

```
# final.py
from __future__ import annotations

from typing import Final, get_type_hints

name: Final[str] = "final"

class Class:
    value: Final = 3000

get_type_hints(Class)
```

Run `python final.py`, and you'll get this traceback:

```
Traceback (most recent call last):
  File "final.py", line 11, in <module>
    get_type_hints(Class)
  File "/.../lib/python3.8/typing.py", line 1232, in get_type_hints
    value = _eval_type(value, base_globals, localns)
  File "/.../lib/python3.8/typing.py", line 270, in _eval_type
    return t._evaluate(globalns, localns)
  File "/.../lib/python3.8/typing.py", line 517, in _evaluate
    self.__forward_value__ = _type_check(
  File "/.../lib/python3.8/typing.py", line 145, in _type_check
    raise TypeError(f"Plain {arg} is not valid as type argument")
TypeError: Plain typing.Final is not valid as type argument
```

Now comment the `get_type_hints` call, launch a Python interpreter, and enter these commands:

>>> import final
>>> from typing import get_type_hints
>>> get_type_hints(final)

And you'll get this traceback:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.9/typing.py", line 1449, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "/.../lib/python3.9/typing.py", line 283, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/.../lib/python3.9/typing.py", line 538, in _evaluate
    type_ =_type_check(
  File "/.../lib/python3.9/typing.py", line 149, in _type_check
    raise TypeError(f"{arg} is not valid as type argument")
TypeError: typing.Final[str] is not valid as type argument
```

I was able to replicate in 3.8, 3.9 and 3.10. The annotations future is not available in 3.7 or lower. Didn't try on 3.11.

----------
messages: 401590
nosy: pawamoy
priority: normal
severity: normal
status: open
title: get_type_hints + Final + future annotations = TypeError
type: behavior
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45166>
_______________________________________


More information about the New-bugs-announce mailing list