diff options
| author | Ivan Levkivskyi <levkivskyi@gmail.com> | 2019-05-26 08:37:07 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-26 08:37:07 (GMT) |
| commit | f367242d10ef36db38133a39ab7627f63099cba4 (patch) | |
| tree | 9e6b8d67b14a69fda120cc5433013b6d1f7b6c35 /Lib/test/test_typing.py | |
| parent | 47dd2f9fd86c32a79e77fef1fbb1ce25dc929de6 (diff) | |
| download | cpython-f367242d10ef36db38133a39ab7627f63099cba4.zip cpython-f367242d10ef36db38133a39ab7627f63099cba4.tar.gz cpython-f367242d10ef36db38133a39ab7627f63099cba4.tar.bz2 | |
bpo-37045: PEP 591: Add final qualifiers to typing module (GH-13571)
The implementation is straightforward, it just mimics `ClassVar` (since the latter is also a name/access qualifier, not really a type). Also it is essentially copied from `typing_extensions`.
Diffstat (limited to 'Lib/test/test_typing.py')
| -rw-r--r-- | Lib/test/test_typing.py | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index c9bfd0c..3d93eb3 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -12,7 +12,7 @@ from typing import T, KT, VT # Not in __all__. from typing import Union, Optional from typing import Tuple, List, MutableMapping from typing import Callable -from typing import Generic, ClassVar +from typing import Generic, ClassVar, Final, final from typing import cast from typing import get_type_hints from typing import no_type_check, no_type_check_decorator @@ -1438,6 +1438,53 @@ class ClassVarTests(BaseTestCase): issubclass(int, ClassVar) +class FinalTests(BaseTestCase): + + def test_basics(self): + Final[int] # OK + with self.assertRaises(TypeError): + Final[1] + with self.assertRaises(TypeError): + Final[int, str] + with self.assertRaises(TypeError): + Final[int][str] + with self.assertRaises(TypeError): + Optional[Final[int]] + + def test_repr(self): + self.assertEqual(repr(Final), 'typing.Final') + cv = Final[int] + self.assertEqual(repr(cv), 'typing.Final[int]') + cv = Final[Employee] + self.assertEqual(repr(cv), 'typing.Final[%s.Employee]' % __name__) + + def test_cannot_subclass(self): + with self.assertRaises(TypeError): + class C(type(Final)): + pass + with self.assertRaises(TypeError): + class C(type(Final[int])): + pass + + def test_cannot_init(self): + with self.assertRaises(TypeError): + Final() + with self.assertRaises(TypeError): + type(Final)() + with self.assertRaises(TypeError): + type(Final[Optional[int]])() + + def test_no_isinstance(self): + with self.assertRaises(TypeError): + isinstance(1, Final[int]) + with self.assertRaises(TypeError): + issubclass(int, Final) + + def test_final_unmodified(self): + def func(x): ... + self.assertIs(func, final(func)) + + class CastTests(BaseTestCase): def test_basics(self): |
