summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_typing.py
diff options
context:
space:
mode:
authorIvan Levkivskyi <levkivskyi@gmail.com>2019-05-26 08:37:48 (GMT)
committerGitHub <noreply@github.com>2019-05-26 08:37:48 (GMT)
commitb891c465bb7d38a597c5c2ad547d7b19194f4dad (patch)
tree1b250227315edde01664d700c509baa3a9661ca9 /Lib/test/test_typing.py
parentf367242d10ef36db38133a39ab7627f63099cba4 (diff)
downloadcpython-b891c465bb7d38a597c5c2ad547d7b19194f4dad.zip
cpython-b891c465bb7d38a597c5c2ad547d7b19194f4dad.tar.gz
cpython-b891c465bb7d38a597c5c2ad547d7b19194f4dad.tar.bz2
bpo-37046: PEP 586: Add Literal to typing module (#13572)
The implementation is straightforward and essentially is just copied from `typing_extensions`.
Diffstat (limited to 'Lib/test/test_typing.py')
-rw-r--r--Lib/test/test_typing.py64
1 files changed, 63 insertions, 1 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 3d93eb3..eb61893 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -9,7 +9,7 @@ from copy import copy, deepcopy
from typing import Any, NoReturn
from typing import TypeVar, AnyStr
from typing import T, KT, VT # Not in __all__.
-from typing import Union, Optional
+from typing import Union, Optional, Literal
from typing import Tuple, List, MutableMapping
from typing import Callable
from typing import Generic, ClassVar, Final, final
@@ -489,6 +489,68 @@ class CallableTests(BaseTestCase):
typing.List[Callable[..., str]]
+class LiteralTests(BaseTestCase):
+ def test_basics(self):
+ # All of these are allowed.
+ Literal[1]
+ Literal[1, 2, 3]
+ Literal["x", "y", "z"]
+ Literal[None]
+ Literal[True]
+ Literal[1, "2", False]
+ Literal[Literal[1, 2], Literal[4, 5]]
+ Literal[b"foo", u"bar"]
+
+ def test_illegal_parameters_do_not_raise_runtime_errors(self):
+ # Type checkers should reject these types, but we do not
+ # raise errors at runtime to maintain maximium flexibility.
+ Literal[int]
+ Literal[3j + 2, ..., ()]
+ Literal[{"foo": 3, "bar": 4}]
+ Literal[T]
+
+ def test_literals_inside_other_types(self):
+ List[Literal[1, 2, 3]]
+ List[Literal[("foo", "bar", "baz")]]
+
+ def test_repr(self):
+ self.assertEqual(repr(Literal[1]), "typing.Literal[1]")
+ self.assertEqual(repr(Literal[1, True, "foo"]), "typing.Literal[1, True, 'foo']")
+ self.assertEqual(repr(Literal[int]), "typing.Literal[int]")
+ self.assertEqual(repr(Literal), "typing.Literal")
+ self.assertEqual(repr(Literal[None]), "typing.Literal[None]")
+
+ def test_cannot_init(self):
+ with self.assertRaises(TypeError):
+ Literal()
+ with self.assertRaises(TypeError):
+ Literal[1]()
+ with self.assertRaises(TypeError):
+ type(Literal)()
+ with self.assertRaises(TypeError):
+ type(Literal[1])()
+
+ def test_no_isinstance_or_issubclass(self):
+ with self.assertRaises(TypeError):
+ isinstance(1, Literal[1])
+ with self.assertRaises(TypeError):
+ isinstance(int, Literal[1])
+ with self.assertRaises(TypeError):
+ issubclass(1, Literal[1])
+ with self.assertRaises(TypeError):
+ issubclass(int, Literal[1])
+
+ def test_no_subclassing(self):
+ with self.assertRaises(TypeError):
+ class Foo(Literal[1]): pass
+ with self.assertRaises(TypeError):
+ class Bar(Literal): pass
+
+ def test_no_multiple_subscripts(self):
+ with self.assertRaises(TypeError):
+ Literal[1][1]
+
+
XK = TypeVar('XK', str, bytes)
XV = TypeVar('XV')