8000 get_type_hints + Final + future annotations = TypeError · Issue #890 · python/typing · GitHub
[go: up one dir, main page]

Skip to content
get_type_hints + Final + future annotations = TypeError #890
Closed
@pawamoy

Description

@pawamoy

Opening for visibility. Issue on bugs.python.org: https://bugs.python.org/issue45166.
Feel free to close here or there 😄


Originally reported and investigated here: mkdocstrings/mkdocstrings#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, 3.10 and 3.11. The annotations future is not available in 3.7 or lower.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0