8000 bpo-45166: fixes `get_type_hints` failure on `Final` by sobolevn · Pull Request #28279 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-45166: fixes get_type_hints failure on Final #28279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-45166: fixes get_type_hints failure on Final
  • Loading branch information
sobolevn committed Sep 10, 2021
commit 1d46e26aed40340a0f4a3442ec0cb9d431f139f5
8 changes: 8 additions & 0 deletions Lib/test/ann_module5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from typing import Final

name: Final[str] = "final"

class MyClass:
value: Final = 3000
10 changes: 9 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2975,7 +2975,7 @@ async def __aexit__(self, etype, eval, tb):

# Definitions needed for features introduced in Python 3.6

from test import ann_module, ann_module2, ann_module3
from test import ann_module, ann_module2, ann_module3, ann_module5
from typing import AsyncContextManager

class A:
Expand Down Expand Up @@ -3339,6 +3339,14 @@ class C(Generic[T]): pass
( 8000 Concatenate[int, P], int))
self.assertEqual(get_args(list | str), (list, str))

def test_forward_ref_and_final(self):
# https://bugs.python.org/issue45166
hints = get_type_hints(ann_module5)
self.assertEqual(hints, {'name': Final[str]})

hints = get_type_hints(ann_module5.MyClass)
self.assertEqual(hints, {'value': Final})


class CollectionsAbcTests(BaseTestCase):

Expand Down
5 changes: 3 additions & 2 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _type_check(arg, msg, is_argument=True, module=None):
if (isinstance(arg, _GenericAlias) and
arg.__origin__ in invalid_generic_forms):
raise TypeError(f"{arg} is not valid as type argument")
if arg in (Any, NoReturn):
if arg in (Any, NoReturn, Final):
return arg
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
raise TypeError(f"Plain {arg} is not valid as type argument")
Expand Down Expand Up @@ -1831,7 +1831,8 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
if value is None:
value = type(None)
if isinstance(value, str):
value = ForwardRef(value)
value = ForwardRef(
value, is_argument=not isinstance(obj, types.ModuleType))
value = _eval_type(value, globalns, localns)
if name in defaults and defaults[name] is None:
value = Optional[value]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes that ``Final`` wrapped in ``ForwardRef`` was failing during
``get_type_hints``.
0