classification
Title: get_type_hints + Final + future annotations = TypeError
Type: behavior Stage: patch review
Components: Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: pawamoy, sobolevn
Priority: normal Keywords: patch

Created on 2021-09-10 16:19 by pawamoy, last changed 2021-09-10 18:36 by sobolevn.

Pull Requests
URL Status Linked Edit
PR 28279 open sobolevn, 2021-09-10 18:36
Messages (3)
msg401590 - (view) Author: Timothee Mazzucotelli (pawamoy) Date: 2021-09-10 16:19
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.
msg401592 - (view) Author: Timothee Mazzucotelli (pawamoy) Date: 2021-09-10 16:26
Just tried on 3.11-dev, it's affected as well.

This issue seems related to https://bugs.python.org/issue41249.
msg401594 - (view) Author: Timothee Mazzucotelli (pawamoy) Date: 2021-09-10 16:28
Oh, I see now that there is a python/typing/ repository on GitHub. Let me know if I should open there instead.
History
Date User Action Args
2021-09-10 18:36:43sobolevnsetkeywords: + patch
nosy: + sobolevn

pull_requests: + pull_request26697
stage: patch review
2021-09-10 16:28:20pawamoysetmessages: + msg401594
2021-09-10 16:26:34pawamoysetmessages: + msg401592
versions: + Python 3.11
2021-09-10 16:19:49pawamoycreate