diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-09-25 02:21:13 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-25 11:21:13 +0200 |
| commit | d312b8516e11027ce97897d39764e242f0f57087 (patch) | |
| tree | 9c7c81603df2bbfbe521d5ae1a639a3b5be22493 /Lib/test | |
| parent | 371ca3b54d355476cc735cb932e80f5cd16310da (diff) | |
| download | cpython-git-d312b8516e11027ce97897d39764e242f0f57087.tar.gz | |
bpo-45166: fixes `get_type_hints` failure on `Final` (GH-28279) (GH-28560)
Co-authored-by: Ćukasz Langa <lukasz@langa.pl>
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
(cherry picked from commit 784905dbeff68cf788bbeefe0a675af1af04affc)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Diffstat (limited to 'Lib/test')
| -rw-r--r-- | Lib/test/ann_module5.py | 10 | ||||
| -rw-r--r-- | Lib/test/ann_module6.py | 7 | ||||
| -rw-r--r-- | Lib/test/test_typing.py | 18 |
3 files changed, 34 insertions, 1 deletions
diff --git a/Lib/test/ann_module5.py b/Lib/test/ann_module5.py new file mode 100644 index 0000000000..837041e121 --- /dev/null +++ b/Lib/test/ann_module5.py @@ -0,0 +1,10 @@ +# Used by test_typing to verify that Final wrapped in ForwardRef works. + +from __future__ import annotations + +from typing import Final + +name: Final[str] = "final" + +class MyClass: + value: Final = 3000 diff --git a/Lib/test/ann_module6.py b/Lib/test/ann_module6.py new file mode 100644 index 0000000000..679175669b --- /dev/null +++ b/Lib/test/ann_module6.py @@ -0,0 +1,7 @@ +# Tests that top-level ClassVar is not allowed + +from __future__ import annotations + +from typing import ClassVar + +wrong: ClassVar[int] = 1 diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index c84ff0f0a2..4f250f2e10 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -2969,7 +2969,7 @@ else: # 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, ann_module6 from typing import AsyncContextManager class A: @@ -3333,6 +3333,22 @@ class GetUtilitiesTestCase(TestCase): (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}) + + def test_top_level_class_var(self): + # https://bugs.python.org/issue45166 + with self.assertRaisesRegex( + TypeError, + r'typing.ClassVar\[int\] is not valid as type argument', + ): + get_type_hints(ann_module6) + class CollectionsAbcTests(BaseTestCase): |
