mirror of
https://github.com/python/cpython.git
synced 2025-11-01 02:38:53 +00:00
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`.
This commit is contained in:
parent
47dd2f9fd8
commit
f367242d10
4 changed files with 142 additions and 4 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue