|
22 | 22 | from typing_extensions import TypeAlias, ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs, TypeGuard
|
23 | 23 | from typing_extensions import Awaitable, AsyncIterator, AsyncContextManager, Required, NotRequired
|
24 | 24 | from typing_extensions import Protocol, runtime, runtime_checkable, Annotated, overload, final, is_typeddict
|
25 |
| -from typing_extensions import dataclass_transform, reveal_type, Never, assert_never |
| 25 | +from typing_extensions import dataclass_transform, reveal_type, Never, assert_never, LiteralString |
26 | 26 | try:
|
27 | 27 | from typing_extensions import get_type_hints
|
28 | 28 | except ImportError:
|
@@ -111,6 +111,11 @@ def test_cannot_instantiate(self):
|
111 | 111 | with self.assertRaises(TypeError):
|
112 | 112 | type(self.bottom_type)()
|
113 | 113 |
|
| 114 | + def test_pickle(self): |
| 115 | + for proto in range(pickle.HIGHEST_PROTOCOL): |
| 116 | + pickled = pickle.dumps(self.bottom_type, protocol=proto) |
| 117 | + self.assertIs(self.bottom_type, pickle.loads(pickled)) |
| 118 | + |
114 | 119 |
|
115 | 120 | class NoReturnTests(BottomTypeTestsMixin, BaseTestCase):
|
116 | 121 | bottom_type = NoReturn
|
@@ -1896,7 +1901,8 @@ def test_cannot_check_subclass(self):
|
1896 | 1901 | def test_pickle(self):
|
1897 | 1902 | samples = [typing.Any, typing.Union[int, str],
|
1898 | 1903 | typing.Optional[str], Tuple[int, ...],
|
1899 |
| - typing.Callable[[str], bytes]] |
| 1904 | + typing.Callable[[str], bytes], |
| 1905 | + Self, LiteralString, Never] |
1900 | 1906 |
|
1901 | 1907 | for t in samples:
|
1902 | 1908 | x = Annotated[t, "a"]
|
@@ -2290,6 +2296,67 @@ def test_no_isinstance(self):
|
2290 | 2296 | issubclass(int, TypeGuard)
|
2291 | 2297 |
|
2292 | 2298 |
|
| 2299 | +class LiteralStringTests(BaseTestCase): |
| 2300 | + def test_basics(self): |
| 2301 | + class Foo: |
| 2302 | + def bar(self) -> LiteralString: ... |
| 2303 | + def baz(self) -> "LiteralString": ... |
| 2304 | + |
| 2305 | + self.assertEqual(gth(Foo.bar), {'return': LiteralString}) |
| 2306 | + self.assertEqual(gth(Foo.baz), {'return': LiteralString}) |
| 2307 | + |
| 2308 | + @skipUnless(PEP_560, "Python 3.7+ required") |
| 2309 | + def test_get_origin(self): |
| 2310 | + from typing_extensions import get_origin |
| 2311 | + self.assertIsNone(get_origin(LiteralString)) |
| 2312 | + |
| 2313 | + def test_repr(self): |
| 2314 | + if hasattr(typing, 'LiteralString'): |
| 2315 | + mod_name = 'typing' |
| 2316 | + else: |
| 2317 | + mod_name = 'typing_extensions' |
| 2318 | + self.assertEqual(repr(LiteralString), '{}.LiteralString'.format(mod_name)) |
| 2319 | + |
| 2320 | + def test_cannot_subscript(self): |
| 2321 | + with self.assertRaises(TypeError): |
| 2322 | + LiteralString[int] |
| 2323 | + |
| 2324 | + def test_cannot_subclass(self): |
| 2325 | + with self.assertRaises(TypeError): |
| 2326 | + class C(type(LiteralString)): |
| 2327 | + pass |
| 2328 | + with self.assertRaises(TypeError): |
| 2329 | + class C(LiteralString): |
| 2330 | + pass |
| 2331 | + |
| 2332 | + def test_cannot_init(self): |
| 2333 | + with self.assertRaises(TypeError): |
| 2334 | + LiteralString() |
| 2335 | + with self.assertRaises(TypeError): |
| 2336 | + type(LiteralString)() |
| 2337 | + |
| 2338 | + def test_no_isinstance(self): |
| 2339 | + with self.assertRaises(TypeError): |
| 2340 | + isinstance(1, LiteralString) |
| 2341 | + with self.assertRaises(TypeError): |
| 2342 | + issubclass(int, LiteralString) |
| 2343 | + |
| 2344 | + def test_alias(self): |
| 2345 | + StringTuple = Tuple[LiteralString, LiteralString] |
| 2346 | + class Alias: |
| 2347 | + def return_tuple(self) -> StringTuple: |
| 2348 | + return ("foo", "pep" + "675") |
| 2349 | + |
| 2350 | + def test_typevar(self): |
| 2351 | + StrT = TypeVar("StrT", bound=LiteralString) |
| 2352 | + self.assertIs(StrT.__bound__, LiteralString) |
| 2353 | + |
| 2354 | + def test_pickle(self): |
| 2355 | + for proto in range(pickle.HIGHEST_PROTOCOL): |
| 2356 | + pickled = pickle.dumps(LiteralString, protocol=proto) |
| 2357 | + self.assertIs(LiteralString, pickle.loads(pickled)) |
| 2358 | + |
| 2359 | + |
2293 | 2360 | class SelfTests(BaseTestCase):
|
2294 | 2361 | def test_basics(self):
|
2295 | 2362 | class Foo:
|
@@ -2331,6 +2398,11 @@ class Alias:
|
2331 | 2398 | def return_tuple(self) -> TupleSelf:
|
2332 | 2399 | return (self, self)
|
2333 | 2400 |
|
| 2401 | + def test_pickle(self): |
| 2402 | + for proto in range(pickle.HIGHEST_PROTOCOL): |
| 2403 | + pickled = pickle.dumps(Self, protocol=proto) |
| 2404 | + self.assertIs(Self, pickle.loads(pickled)) |
| 2405 | + |
2334 | 2406 |
|
2335 | 2407 | class FinalDecoratorTests(BaseTestCase):
|
2336 | 2408 | def test_final_unmodified(self):
|
|
0 commit comments