classification
Title: get_type_hints + Final + future annotations = TypeError
Type: behavior Stage: resolved
Components: Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: lukasz.langa, miss-islington, pawamoy, sobolevn
Priority: normal Keywords: patch

Created on 2021-09-10 16:19 by pawamoy, last changed 2021-09-25 09:32 by lukasz.langa. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28279 merged sobolevn, 2021-09-10 18:36
PR 28560 merged miss-islington, 2021-09-25 08:56
PR 28561 merged lukasz.langa, 2021-09-25 08:58
Messages (7)
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.
msg402610 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-25 08:56
New changeset 784905dbeff68cf788bbeefe0a675af1af04affc by Nikita Sobolev in branch 'main':
bpo-45166: fixes `get_type_hints` failure on `Final` (GH-28279)
https://github.com/python/cpython/commit/784905dbeff68cf788bbeefe0a675af1af04affc
msg402612 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-25 09:21
New changeset d312b8516e11027ce97897d39764e242f0f57087 by Miss Islington (bot) in branch '3.10':
bpo-45166: fixes `get_type_hints` failure on `Final` (GH-28279) (GH-28560)
https://github.com/python/cpython/commit/d312b8516e11027ce97897d39764e242f0f57087
msg402613 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-25 09:30
New changeset 1f08d16c90b6619607fe0656328062ab986cce29 by Łukasz Langa in branch '3.9':
[3.9] bpo-45166: fixes `get_type_hints` failure on `Final` (GH-28279) (GH-28561)
https://github.com/python/cpython/commit/1f08d16c90b6619607fe0656328062ab986cce29
msg402614 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-09-25 09:32
Thanks for the report Timothee, and Nikita for the fix! ✨ 🍰 ✨
History
Date User Action Args
2021-09-25 09:32:02lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg402614

stage: patch review -> resolved
2021-09-25 09:30:50lukasz.langasetmessages: + msg402613
2021-09-25 09:21:17lukasz.langasetmessages: + msg402612
2021-09-25 08:58:28lukasz.langasetpull_requests: + pull_request26944
2021-09-25 08:56:39lukasz.langasetnosy: + lukasz.langa
messages: + msg402610
2021-09-25 08:56:31miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26943
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