Debian Codegraph @

Lib/test/test_typing.py

raw
1 import annotationlib
2 import contextlib
3 import collections
4 import collections.abc
5 from collections import defaultdict
6 from functools import lru_cache, wraps, reduce
7 import gc
8 import inspect
9 import io
10 import itertools
11 import operator
12 import os
13 import pickle
14 import re
15 import sys
16 from unittest import TestCase, main
17 from unittest.mock import patch
18 from copy import copy, deepcopy
19
20 from typing import Any, NoReturn, Never, assert_never
21 from typing import overload, get_overloads, clear_overloads
22 from typing import TypeVar, TypeVarTuple, Unpack, AnyStr
23 from typing import T, KT, VT # Not in __all__.
24 from typing import Union, Optional, Literal
25 from typing import Tuple, List, Dict, MutableMapping
26 from typing import Callable
27 from typing import Generic, ClassVar, Final, final, Protocol
28 from typing import assert_type, cast, runtime_checkable
29 from typing import get_type_hints
30 from typing import get_origin, get_args, get_protocol_members
31 from typing import override
32 from typing import is_typeddict, is_protocol
33 from typing import reveal_type
34 from typing import dataclass_transform
35 from typing import no_type_check, no_type_check_decorator
36 from typing import Type
37 from typing import NamedTuple, NotRequired, Required, ReadOnly, TypedDict
38 from typing import IO, TextIO, BinaryIO
39 from typing import Pattern, Match
40 from typing import Annotated, ForwardRef
41 from typing import Self, LiteralString
42 from typing import TypeAlias
43 from typing import ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs
44 from typing import TypeGuard, TypeIs, NoDefault
45 import abc
46 import textwrap
47 import typing
48 import weakref
49 import warnings
50 import types
51
52 from test.support import (
53 captured_stderr, cpython_only, infinite_recursion, requires_docstrings, import_helper, run_code,
54 EqualToForwardRef,
55 )
56 from test.typinganndata import (
57 ann_module695, mod_generics_cache, _typed_dict_helper,
58 ann_module, ann_module2, ann_module3, ann_module5, ann_module6, ann_module8
59 )
60
61
62 CANNOT_SUBCLASS_TYPE = 'Cannot subclass special typing classes'
63 NOT_A_BASE_TYPE = "type 'typing.%s' is not an acceptable base type"
64 CANNOT_SUBCLASS_INSTANCE = 'Cannot subclass an instance of %s'
65
66
67 class BaseTestCase(TestCase):
68
69 def clear_caches(self):
70 for f in typing._cleanups:
71 f()
72
73
74 def all_pickle_protocols(test_func):
75 """Runs `test_func` with various values for `proto` argument."""
76
77 @wraps(test_func)
78 def wrapper(self):
79 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
80 with self.subTest(pickle_proto=proto):
81 test_func(self, proto=proto)
82
83 return wrapper
84
85
86 class Employee:
87 pass
88
89
90 class Manager(Employee):
91 pass
92
93
94 class Founder(Employee):
95 pass
96
97
98 class ManagingFounder(Manager, Founder):
99 pass
100
101
102 class AnyTests(BaseTestCase):
103
104 def test_any_instance_type_error(self):
105 with self.assertRaises(TypeError):
106 isinstance(42, Any)
107
108 def test_repr(self):
109 self.assertEqual(repr(Any), 'typing.Any')
110
111 class Sub(Any): pass
112 self.assertEqual(
113 repr(Sub),
114 f"<class '{__name__}.AnyTests.test_repr.<locals>.Sub'>",
115 )
116
117 def test_errors(self):
118 with self.assertRaises(TypeError):
119 isinstance(42, Any)
120 with self.assertRaises(TypeError):
121 Any[int] # Any is not a generic type.
122
123 def test_can_subclass(self):
124 class Mock(Any): pass
125 self.assertIsSubclass(Mock, Any)
126 self.assertIsInstance(Mock(), Mock)
127
128 class Something: pass
129 self.assertNotIsSubclass(Something, Any)
130 self.assertNotIsInstance(Something(), Mock)
131
132 class MockSomething(Something, Mock): pass
133 self.assertIsSubclass(MockSomething, Any)
134 self.assertIsSubclass(MockSomething, MockSomething)
135 self.assertIsSubclass(MockSomething, Something)
136 self.assertIsSubclass(MockSomething, Mock)
137 ms = MockSomething()
138 self.assertIsInstance(ms, MockSomething)
139 self.assertIsInstance(ms, Something)
140 self.assertIsInstance(ms, Mock)
141
142 def test_subclassing_with_custom_constructor(self):
143 class Sub(Any):
144 def __init__(self, *args, **kwargs): pass
145 # The instantiation must not fail.
146 Sub(0, s="")
147
148 def test_multiple_inheritance_with_custom_constructors(self):
149 class Foo:
150 def __init__(self, x):
151 self.x = x
152
153 class Bar(Any, Foo):
154 def __init__(self, x, y):
155 self.y = y
156 super().__init__(x)
157
158 b = Bar(1, 2)
159 self.assertEqual(b.x, 1)
160 self.assertEqual(b.y, 2)
161
162 def test_cannot_instantiate(self):
163 with self.assertRaises(TypeError):
164 Any()
165 with self.assertRaises(TypeError):
166 type(Any)()
167
168 def test_any_works_with_alias(self):
169 # These expressions must simply not fail.
170 typing.Match[Any]
171 typing.Pattern[Any]
172 typing.IO[Any]
173
174
175 class BottomTypeTestsMixin:
176 bottom_type: ClassVar[Any]
177
178 def test_equality(self):
179 self.assertEqual(self.bottom_type, self.bottom_type)
180 self.assertIs(self.bottom_type, self.bottom_type)
181 self.assertNotEqual(self.bottom_type, None)
182
183 def test_get_origin(self):
184 self.assertIs(get_origin(self.bottom_type), None)
185
186 def test_instance_type_error(self):
187 with self.assertRaises(TypeError):
188 isinstance(42, self.bottom_type)
189
190 def test_subclass_type_error(self):
191 with self.assertRaises(TypeError):
192 issubclass(Employee, self.bottom_type)
193 with self.assertRaises(TypeError):
194 issubclass(NoReturn, self.bottom_type)
195
196 def test_not_generic(self):
197 with self.assertRaises(TypeError):
198 self.bottom_type[int]
199
200 def test_cannot_subclass(self):
201 with self.assertRaisesRegex(TypeError,
202 'Cannot subclass ' + re.escape(str(self.bottom_type))):
203 class A(self.bottom_type):
204 pass
205 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
206 class B(type(self.bottom_type)):
207 pass
208
209 def test_cannot_instantiate(self):
210 with self.assertRaises(TypeError):
211 self.bottom_type()
212 with self.assertRaises(TypeError):
213 type(self.bottom_type)()
214
215
216 class NoReturnTests(BottomTypeTestsMixin, BaseTestCase):
217 bottom_type = NoReturn
218
219 def test_repr(self):
220 self.assertEqual(repr(NoReturn), 'typing.NoReturn')
221
222 def test_get_type_hints(self):
223 def some(arg: NoReturn) -> NoReturn: ...
224 def some_str(arg: 'NoReturn') -> 'typing.NoReturn': ...
225
226 expected = {'arg': NoReturn, 'return': NoReturn}
227 for target in [some, some_str]:
228 with self.subTest(target=target):
229 self.assertEqual(gth(target), expected)
230
231 def test_not_equality(self):
232 self.assertNotEqual(NoReturn, Never)
233 self.assertNotEqual(Never, NoReturn)
234
235
236 class NeverTests(BottomTypeTestsMixin, BaseTestCase):
237 bottom_type = Never
238
239 def test_repr(self):
240 self.assertEqual(repr(Never), 'typing.Never')
241
242 def test_get_type_hints(self):
243 def some(arg: Never) -> Never: ...
244 def some_str(arg: 'Never') -> 'typing.Never': ...
245
246 expected = {'arg': Never, 'return': Never}
247 for target in [some, some_str]:
248 with self.subTest(target=target):
249 self.assertEqual(gth(target), expected)
250
251
252 class AssertNeverTests(BaseTestCase):
253 def test_exception(self):
254 with self.assertRaises(AssertionError):
255 assert_never(None)
256
257 value = "some value"
258 with self.assertRaisesRegex(AssertionError, value):
259 assert_never(value)
260
261 # Make sure a huge value doesn't get printed in its entirety
262 huge_value = "a" * 10000
263 with self.assertRaises(AssertionError) as cm:
264 assert_never(huge_value)
265 self.assertLess(
266 len(cm.exception.args[0]),
267 typing._ASSERT_NEVER_REPR_MAX_LENGTH * 2,
268 )
269
270
271 class SelfTests(BaseTestCase):
272 def test_equality(self):
273 self.assertEqual(Self, Self)
274 self.assertIs(Self, Self)
275 self.assertNotEqual(Self, None)
276
277 def test_basics(self):
278 class Foo:
279 def bar(self) -> Self: ...
280 class FooStr:
281 def bar(self) -> 'Self': ...
282 class FooStrTyping:
283 def bar(self) -> 'typing.Self': ...
284
285 for target in [Foo, FooStr, FooStrTyping]:
286 with self.subTest(target=target):
287 self.assertEqual(gth(target.bar), {'return': Self})
288 self.assertIs(get_origin(Self), None)
289
290 def test_repr(self):
291 self.assertEqual(repr(Self), 'typing.Self')
292
293 def test_cannot_subscript(self):
294 with self.assertRaises(TypeError):
295 Self[int]
296
297 def test_cannot_subclass(self):
298 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
299 class C(type(Self)):
300 pass
301 with self.assertRaisesRegex(TypeError,
302 r'Cannot subclass typing\.Self'):
303 class D(Self):
304 pass
305
306 def test_cannot_init(self):
307 with self.assertRaises(TypeError):
308 Self()
309 with self.assertRaises(TypeError):
310 type(Self)()
311
312 def test_no_isinstance(self):
313 with self.assertRaises(TypeError):
314 isinstance(1, Self)
315 with self.assertRaises(TypeError):
316 issubclass(int, Self)
317
318 def test_alias(self):
319 # TypeAliases are not actually part of the spec
320 alias_1 = Tuple[Self, Self]
321 alias_2 = List[Self]
322 alias_3 = ClassVar[Self]
323 self.assertEqual(get_args(alias_1), (Self, Self))
324 self.assertEqual(get_args(alias_2), (Self,))
325 self.assertEqual(get_args(alias_3), (Self,))
326
327
328 class LiteralStringTests(BaseTestCase):
329 def test_equality(self):
330 self.assertEqual(LiteralString, LiteralString)
331 self.assertIs(LiteralString, LiteralString)
332 self.assertNotEqual(LiteralString, None)
333
334 def test_basics(self):
335 class Foo:
336 def bar(self) -> LiteralString: ...
337 class FooStr:
338 def bar(self) -> 'LiteralString': ...
339 class FooStrTyping:
340 def bar(self) -> 'typing.LiteralString': ...
341
342 for target in [Foo, FooStr, FooStrTyping]:
343 with self.subTest(target=target):
344 self.assertEqual(gth(target.bar), {'return': LiteralString})
345 self.assertIs(get_origin(LiteralString), None)
346
347 def test_repr(self):
348 self.assertEqual(repr(LiteralString), 'typing.LiteralString')
349
350 def test_cannot_subscript(self):
351 with self.assertRaises(TypeError):
352 LiteralString[int]
353
354 def test_cannot_subclass(self):
355 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
356 class C(type(LiteralString)):
357 pass
358 with self.assertRaisesRegex(TypeError,
359 r'Cannot subclass typing\.LiteralString'):
360 class D(LiteralString):
361 pass
362
363 def test_cannot_init(self):
364 with self.assertRaises(TypeError):
365 LiteralString()
366 with self.assertRaises(TypeError):
367 type(LiteralString)()
368
369 def test_no_isinstance(self):
370 with self.assertRaises(TypeError):
371 isinstance(1, LiteralString)
372 with self.assertRaises(TypeError):
373 issubclass(int, LiteralString)
374
375 def test_alias(self):
376 alias_1 = Tuple[LiteralString, LiteralString]
377 alias_2 = List[LiteralString]
378 alias_3 = ClassVar[LiteralString]
379 self.assertEqual(get_args(alias_1), (LiteralString, LiteralString))
380 self.assertEqual(get_args(alias_2), (LiteralString,))
381 self.assertEqual(get_args(alias_3), (LiteralString,))
382
383
384 class TypeVarTests(BaseTestCase):
385 def test_basic_plain(self):
386 T = TypeVar('T')
387 # T equals itself.
388 self.assertEqual(T, T)
389 # T is an instance of TypeVar
390 self.assertIsInstance(T, TypeVar)
391 self.assertEqual(T.__name__, 'T')
392 self.assertEqual(T.__constraints__, ())
393 self.assertIs(T.__bound__, None)
394 self.assertIs(T.__covariant__, False)
395 self.assertIs(T.__contravariant__, False)
396 self.assertIs(T.__infer_variance__, False)
397 self.assertEqual(T.__module__, __name__)
398
399 def test_basic_with_exec(self):
400 ns = {}
401 exec('from typing import TypeVar; T = TypeVar("T", bound=float)', ns, ns)
402 T = ns['T']
403 self.assertIsInstance(T, TypeVar)
404 self.assertEqual(T.__name__, 'T')
405 self.assertEqual(T.__constraints__, ())
406 self.assertIs(T.__bound__, float)
407 self.assertIs(T.__covariant__, False)
408 self.assertIs(T.__contravariant__, False)
409 self.assertIs(T.__infer_variance__, False)
410 self.assertIs(T.__module__, None)
411
412 def test_attributes(self):
413 T_bound = TypeVar('T_bound', bound=int)
414 self.assertEqual(T_bound.__name__, 'T_bound')
415 self.assertEqual(T_bound.__constraints__, ())
416 self.assertIs(T_bound.__bound__, int)
417
418 T_constraints = TypeVar('T_constraints', int, str)
419 self.assertEqual(T_constraints.__name__, 'T_constraints')
420 self.assertEqual(T_constraints.__constraints__, (int, str))
421 self.assertIs(T_constraints.__bound__, None)
422
423 T_co = TypeVar('T_co', covariant=True)
424 self.assertEqual(T_co.__name__, 'T_co')
425 self.assertIs(T_co.__covariant__, True)
426 self.assertIs(T_co.__contravariant__, False)
427 self.assertIs(T_co.__infer_variance__, False)
428
429 T_contra = TypeVar('T_contra', contravariant=True)
430 self.assertEqual(T_contra.__name__, 'T_contra')
431 self.assertIs(T_contra.__covariant__, False)
432 self.assertIs(T_contra.__contravariant__, True)
433 self.assertIs(T_contra.__infer_variance__, False)
434
435 T_infer = TypeVar('T_infer', infer_variance=True)
436 self.assertEqual(T_infer.__name__, 'T_infer')
437 self.assertIs(T_infer.__covariant__, False)
438 self.assertIs(T_infer.__contravariant__, False)
439 self.assertIs(T_infer.__infer_variance__, True)
440
441 def test_typevar_instance_type_error(self):
442 T = TypeVar('T')
443 with self.assertRaises(TypeError):
444 isinstance(42, T)
445
446 def test_typevar_subclass_type_error(self):
447 T = TypeVar('T')
448 with self.assertRaises(TypeError):
449 issubclass(int, T)
450 with self.assertRaises(TypeError):
451 issubclass(T, int)
452
453 def test_constrained_error(self):
454 with self.assertRaises(TypeError):
455 X = TypeVar('X', int)
456 X
457
458 def test_union_unique(self):
459 X = TypeVar('X')
460 Y = TypeVar('Y')
461 self.assertNotEqual(X, Y)
462 self.assertEqual(Union[X], X)
463 self.assertNotEqual(Union[X], Union[X, Y])
464 self.assertEqual(Union[X, X], X)
465 self.assertNotEqual(Union[X, int], Union[X])
466 self.assertNotEqual(Union[X, int], Union[int])
467 self.assertEqual(Union[X, int].__args__, (X, int))
468 self.assertEqual(Union[X, int].__parameters__, (X,))
469 self.assertIs(Union[X, int].__origin__, Union)
470
471 def test_or(self):
472 X = TypeVar('X')
473 # use a string because str doesn't implement
474 # __or__/__ror__ itself
475 self.assertEqual(X | "x", Union[X, "x"])
476 self.assertEqual("x" | X, Union["x", X])
477 # make sure the order is correct
478 self.assertEqual(get_args(X | "x"), (X, EqualToForwardRef("x")))
479 self.assertEqual(get_args("x" | X), (EqualToForwardRef("x"), X))
480
481 def test_union_constrained(self):
482 A = TypeVar('A', str, bytes)
483 self.assertNotEqual(Union[A, str], Union[A])
484
485 def test_repr(self):
486 self.assertEqual(repr(T), '~T')
487 self.assertEqual(repr(KT), '~KT')
488 self.assertEqual(repr(VT), '~VT')
489 self.assertEqual(repr(AnyStr), '~AnyStr')
490 T_co = TypeVar('T_co', covariant=True)
491 self.assertEqual(repr(T_co), '+T_co')
492 T_contra = TypeVar('T_contra', contravariant=True)
493 self.assertEqual(repr(T_contra), '-T_contra')
494
495 def test_no_redefinition(self):
496 self.assertNotEqual(TypeVar('T'), TypeVar('T'))
497 self.assertNotEqual(TypeVar('T', int, str), TypeVar('T', int, str))
498
499 def test_cannot_subclass(self):
500 with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'TypeVar'):
501 class V(TypeVar): pass
502 T = TypeVar("T")
503 with self.assertRaisesRegex(TypeError,
504 CANNOT_SUBCLASS_INSTANCE % 'TypeVar'):
505 class W(T): pass
506
507 def test_cannot_instantiate_vars(self):
508 with self.assertRaises(TypeError):
509 TypeVar('A')()
510
511 def test_bound_errors(self):
512 with self.assertRaises(TypeError):
513 TypeVar('X', bound=Optional)
514 with self.assertRaises(TypeError):
515 TypeVar('X', str, float, bound=Employee)
516 with self.assertRaisesRegex(TypeError,
517 r"Bound must be a type\. Got \(1, 2\)\."):
518 TypeVar('X', bound=(1, 2))
519
520 def test_missing__name__(self):
521 # See bpo-39942
522 code = ("import typing\n"
523 "T = typing.TypeVar('T')\n"
524 )
525 exec(code, {})
526
527 def test_no_bivariant(self):
528 with self.assertRaises(ValueError):
529 TypeVar('T', covariant=True, contravariant=True)
530
531 def test_cannot_combine_explicit_and_infer(self):
532 with self.assertRaises(ValueError):
533 TypeVar('T', covariant=True, infer_variance=True)
534 with self.assertRaises(ValueError):
535 TypeVar('T', contravariant=True, infer_variance=True)
536
537 def test_var_substitution(self):
538 T = TypeVar('T')
539 subst = T.__typing_subst__
540 self.assertIs(subst(int), int)
541 self.assertEqual(subst(list[int]), list[int])
542 self.assertEqual(subst(List[int]), List[int])
543 self.assertEqual(subst(List), List)
544 self.assertIs(subst(Any), Any)
545 self.assertIs(subst(None), type(None))
546 self.assertIs(subst(T), T)
547 self.assertEqual(subst(int|str), int|str)
548 self.assertEqual(subst(Union[int, str]), Union[int, str])
549
550 def test_bad_var_substitution(self):
551 T = TypeVar('T')
552 bad_args = (
553 (), (int, str), Optional,
554 Generic, Generic[T], Protocol, Protocol[T],
555 Final, Final[int], ClassVar, ClassVar[int],
556 )
557 for arg in bad_args:
558 with self.subTest(arg=arg):
559 with self.assertRaises(TypeError):
560 T.__typing_subst__(arg)
561 with self.assertRaises(TypeError):
562 List[T][arg]
563 with self.assertRaises(TypeError):
564 list[T][arg]
565
566 def test_many_weakrefs(self):
567 # gh-108295: this used to segfault
568 for cls in (ParamSpec, TypeVarTuple, TypeVar):
569 with self.subTest(cls=cls):
570 vals = weakref.WeakValueDictionary()
571
572 for x in range(10):
573 vals[x] = cls(str(x))
574 del vals
575
576 def test_constructor(self):
577 T = TypeVar(name="T")
578 self.assertEqual(T.__name__, "T")
579 self.assertEqual(T.__constraints__, ())
580 self.assertIs(T.__bound__, None)
581 self.assertIs(T.__default__, typing.NoDefault)
582 self.assertIs(T.__covariant__, False)
583 self.assertIs(T.__contravariant__, False)
584 self.assertIs(T.__infer_variance__, False)
585
586 T = TypeVar(name="T", bound=type)
587 self.assertEqual(T.__name__, "T")
588 self.assertEqual(T.__constraints__, ())
589 self.assertIs(T.__bound__, type)
590 self.assertIs(T.__default__, typing.NoDefault)
591 self.assertIs(T.__covariant__, False)
592 self.assertIs(T.__contravariant__, False)
593 self.assertIs(T.__infer_variance__, False)
594
595 T = TypeVar(name="T", default=())
596 self.assertEqual(T.__name__, "T")
597 self.assertEqual(T.__constraints__, ())
598 self.assertIs(T.__bound__, None)
599 self.assertIs(T.__default__, ())
600 self.assertIs(T.__covariant__, False)
601 self.assertIs(T.__contravariant__, False)
602 self.assertIs(T.__infer_variance__, False)
603
604 T = TypeVar(name="T", covariant=True)
605 self.assertEqual(T.__name__, "T")
606 self.assertEqual(T.__constraints__, ())
607 self.assertIs(T.__bound__, None)
608 self.assertIs(T.__default__, typing.NoDefault)
609 self.assertIs(T.__covariant__, True)
610 self.assertIs(T.__contravariant__, False)
611 self.assertIs(T.__infer_variance__, False)
612
613 T = TypeVar(name="T", contravariant=True)
614 self.assertEqual(T.__name__, "T")
615 self.assertEqual(T.__constraints__, ())
616 self.assertIs(T.__bound__, None)
617 self.assertIs(T.__default__, typing.NoDefault)
618 self.assertIs(T.__covariant__, False)
619 self.assertIs(T.__contravariant__, True)
620 self.assertIs(T.__infer_variance__, False)
621
622 T = TypeVar(name="T", infer_variance=True)
623 self.assertEqual(T.__name__, "T")
624 self.assertEqual(T.__constraints__, ())
625 self.assertIs(T.__bound__, None)
626 self.assertIs(T.__default__, typing.NoDefault)
627 self.assertIs(T.__covariant__, False)
628 self.assertIs(T.__contravariant__, False)
629 self.assertIs(T.__infer_variance__, True)
630
631
632 class TypeParameterDefaultsTests(BaseTestCase):
633 def test_typevar(self):
634 T = TypeVar('T', default=int)
635 self.assertEqual(T.__default__, int)
636 self.assertIs(T.has_default(), True)
637 self.assertIsInstance(T, TypeVar)
638
639 class A(Generic[T]): ...
640 Alias = Optional[T]
641
642 def test_typevar_none(self):
643 U = TypeVar('U')
644 U_None = TypeVar('U_None', default=None)
645 self.assertIs(U.__default__, NoDefault)
646 self.assertIs(U.has_default(), False)
647 self.assertIs(U_None.__default__, None)
648 self.assertIs(U_None.has_default(), True)
649
650 class X[T]: ...
651 T, = X.__type_params__
652 self.assertIs(T.__default__, NoDefault)
653 self.assertIs(T.has_default(), False)
654
655 def test_paramspec(self):
656 P = ParamSpec('P', default=(str, int))
657 self.assertEqual(P.__default__, (str, int))
658 self.assertIs(P.has_default(), True)
659 self.assertIsInstance(P, ParamSpec)
660
661 class A(Generic[P]): ...
662 Alias = typing.Callable[P, None]
663
664 P_default = ParamSpec('P_default', default=...)
665 self.assertIs(P_default.__default__, ...)
666
667 def test_paramspec_none(self):
668 U = ParamSpec('U')
669 U_None = ParamSpec('U_None', default=None)
670 self.assertIs(U.__default__, NoDefault)
671 self.assertIs(U.has_default(), False)
672 self.assertIs(U_None.__default__, None)
673 self.assertIs(U_None.has_default(), True)
674
675 class X[**P]: ...
676 P, = X.__type_params__
677 self.assertIs(P.__default__, NoDefault)
678 self.assertIs(P.has_default(), False)
679
680 def test_typevartuple(self):
681 Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
682 self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
683 self.assertIs(Ts.has_default(), True)
684 self.assertIsInstance(Ts, TypeVarTuple)
685
686 class A(Generic[Unpack[Ts]]): ...
687 Alias = Optional[Unpack[Ts]]
688
689 def test_typevartuple_specialization(self):
690 T = TypeVar("T")
691 Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
692 self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
693 class A(Generic[T, Unpack[Ts]]): ...
694 self.assertEqual(A[float].__args__, (float, str, int))
695 self.assertEqual(A[float, range].__args__, (float, range))
696 self.assertEqual(A[float, *tuple[int, ...]].__args__, (float, *tuple[int, ...]))
697
698 def test_typevar_and_typevartuple_specialization(self):
699 T = TypeVar("T")
700 U = TypeVar("U", default=float)
701 Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
702 self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
703 class A(Generic[T, U, Unpack[Ts]]): ...
704 self.assertEqual(A[int].__args__, (int, float, str, int))
705 self.assertEqual(A[int, str].__args__, (int, str, str, int))
706 self.assertEqual(A[int, str, range].__args__, (int, str, range))
707 self.assertEqual(A[int, str, *tuple[int, ...]].__args__, (int, str, *tuple[int, ...]))
708
709 def test_no_default_after_typevar_tuple(self):
710 T = TypeVar("T", default=int)
711 Ts = TypeVarTuple("Ts")
712 Ts_default = TypeVarTuple("Ts_default", default=Unpack[Tuple[str, int]])
713
714 with self.assertRaises(TypeError):
715 class X(Generic[*Ts, T]): ...
716
717 with self.assertRaises(TypeError):
718 class Y(Generic[*Ts_default, T]): ...
719
720 def test_allow_default_after_non_default_in_alias(self):
721 T_default = TypeVar('T_default', default=int)
722 T = TypeVar('T')
723 Ts = TypeVarTuple('Ts')
724
725 a1 = Callable[[T_default], T]
726 self.assertEqual(a1.__args__, (T_default, T))
727
728 a2 = dict[T_default, T]
729 self.assertEqual(a2.__args__, (T_default, T))
730
731 a3 = typing.Dict[T_default, T]
732 self.assertEqual(a3.__args__, (T_default, T))
733
734 a4 = Callable[*Ts, T]
735 self.assertEqual(a4.__args__, (*Ts, T))
736
737 def test_paramspec_specialization(self):
738 T = TypeVar("T")
739 P = ParamSpec('P', default=[str, int])
740 self.assertEqual(P.__default__, [str, int])
741 class A(Generic[T, P]): ...
742 self.assertEqual(A[float].__args__, (float, (str, int)))
743 self.assertEqual(A[float, [range]].__args__, (float, (range,)))
744
745 def test_typevar_and_paramspec_specialization(self):
746 T = TypeVar("T")
747 U = TypeVar("U", default=float)
748 P = ParamSpec('P', default=[str, int])
749 self.assertEqual(P.__default__, [str, int])
750 class A(Generic[T, U, P]): ...
751 self.assertEqual(A[float].__args__, (float, float, (str, int)))
752 self.assertEqual(A[float, int].__args__, (float, int, (str, int)))
753 self.assertEqual(A[float, int, [range]].__args__, (float, int, (range,)))
754
755 def test_paramspec_and_typevar_specialization(self):
756 T = TypeVar("T")
757 P = ParamSpec('P', default=[str, int])
758 U = TypeVar("U", default=float)
759 self.assertEqual(P.__default__, [str, int])
760 class A(Generic[T, P, U]): ...
761 self.assertEqual(A[float].__args__, (float, (str, int), float))
762 self.assertEqual(A[float, [range]].__args__, (float, (range,), float))
763 self.assertEqual(A[float, [range], int].__args__, (float, (range,), int))
764
765 def test_paramspec_and_typevar_specialization_2(self):
766 T = TypeVar("T")
767 P = ParamSpec('P', default=...)
768 U = TypeVar("U", default=float)
769 self.assertEqual(P.__default__, ...)
770 class A(Generic[T, P, U]): ...
771 self.assertEqual(A[float].__args__, (float, ..., float))
772 self.assertEqual(A[float, [range]].__args__, (float, (range,), float))
773 self.assertEqual(A[float, [range], int].__args__, (float, (range,), int))
774
775 def test_typevartuple_none(self):
776 U = TypeVarTuple('U')
777 U_None = TypeVarTuple('U_None', default=None)
778 self.assertIs(U.__default__, NoDefault)
779 self.assertIs(U.has_default(), False)
780 self.assertIs(U_None.__default__, None)
781 self.assertIs(U_None.has_default(), True)
782
783 class X[**Ts]: ...
784 Ts, = X.__type_params__
785 self.assertIs(Ts.__default__, NoDefault)
786 self.assertIs(Ts.has_default(), False)
787
788 def test_no_default_after_non_default(self):
789 DefaultStrT = TypeVar('DefaultStrT', default=str)
790 T = TypeVar('T')
791
792 with self.assertRaisesRegex(
793 TypeError, r"Type parameter ~T without a default follows type parameter with a default"
794 ):
795 Test = Generic[DefaultStrT, T]
796
797 def test_need_more_params(self):
798 DefaultStrT = TypeVar('DefaultStrT', default=str)
799 T = TypeVar('T')
800 U = TypeVar('U')
801
802 class A(Generic[T, U, DefaultStrT]): ...
803 A[int, bool]
804 A[int, bool, str]
805
806 with self.assertRaisesRegex(
807 TypeError, r"Too few arguments for .+; actual 1, expected at least 2"
808 ):
809 Test = A[int]
810
811 def test_pickle(self):
812 global U, U_co, U_contra, U_default # pickle wants to reference the class by name
813 U = TypeVar('U')
814 U_co = TypeVar('U_co', covariant=True)
815 U_contra = TypeVar('U_contra', contravariant=True)
816 U_default = TypeVar('U_default', default=int)
817 for proto in range(pickle.HIGHEST_PROTOCOL):
818 for typevar in (U, U_co, U_contra, U_default):
819 z = pickle.loads(pickle.dumps(typevar, proto))
820 self.assertEqual(z.__name__, typevar.__name__)
821 self.assertEqual(z.__covariant__, typevar.__covariant__)
822 self.assertEqual(z.__contravariant__, typevar.__contravariant__)
823 self.assertEqual(z.__bound__, typevar.__bound__)
824 self.assertEqual(z.__default__, typevar.__default__)
825
826
827 def template_replace(templates: list[str], replacements: dict[str, list[str]]) -> list[tuple[str]]:
828 """Renders templates with possible combinations of replacements.
829
830 Example 1: Suppose that:
831 templates = ["dog_breed are awesome", "dog_breed are cool"]
832 replacements = {"dog_breed": ["Huskies", "Beagles"]}
833 Then we would return:
834 [
835 ("Huskies are awesome", "Huskies are cool"),
836 ("Beagles are awesome", "Beagles are cool")
837 ]
838
839 Example 2: Suppose that:
840 templates = ["Huskies are word1 but also word2"]
841 replacements = {"word1": ["playful", "cute"],
842 "word2": ["feisty", "tiring"]}
843 Then we would return:
844 [
845 ("Huskies are playful but also feisty"),
846 ("Huskies are playful but also tiring"),
847 ("Huskies are cute but also feisty"),
848 ("Huskies are cute but also tiring")
849 ]
850
851 Note that if any of the replacements do not occur in any template:
852 templates = ["Huskies are word1", "Beagles!"]
853 replacements = {"word1": ["playful", "cute"],
854 "word2": ["feisty", "tiring"]}
855 Then we do not generate duplicates, returning:
856 [
857 ("Huskies are playful", "Beagles!"),
858 ("Huskies are cute", "Beagles!")
859 ]
860 """
861 # First, build a structure like:
862 # [
863 # [("word1", "playful"), ("word1", "cute")],
864 # [("word2", "feisty"), ("word2", "tiring")]
865 # ]
866 replacement_combos = []
867 for original, possible_replacements in replacements.items():
868 original_replacement_tuples = []
869 for replacement in possible_replacements:
870 original_replacement_tuples.append((original, replacement))
871 replacement_combos.append(original_replacement_tuples)
872
873 # Second, generate rendered templates, including possible duplicates.
874 rendered_templates = []
875 for replacement_combo in itertools.product(*replacement_combos):
876 # replacement_combo would be e.g.
877 # [("word1", "playful"), ("word2", "feisty")]
878 templates_with_replacements = []
879 for template in templates:
880 for original, replacement in replacement_combo:
881 template = template.replace(original, replacement)
882 templates_with_replacements.append(template)
883 rendered_templates.append(tuple(templates_with_replacements))
884
885 # Finally, remove the duplicates (but keep the order).
886 rendered_templates_no_duplicates = []
887 for x in rendered_templates:
888 # Inefficient, but should be fine for our purposes.
889 if x not in rendered_templates_no_duplicates:
890 rendered_templates_no_duplicates.append(x)
891
892 return rendered_templates_no_duplicates
893
894
895 class TemplateReplacementTests(BaseTestCase):
896
897 def test_two_templates_two_replacements_yields_correct_renders(self):
898 actual = template_replace(
899 templates=["Cats are word1", "Dogs are word2"],
900 replacements={
901 "word1": ["small", "cute"],
902 "word2": ["big", "fluffy"],
903 },
904 )
905 expected = [
906 ("Cats are small", "Dogs are big"),
907 ("Cats are small", "Dogs are fluffy"),
908 ("Cats are cute", "Dogs are big"),
909 ("Cats are cute", "Dogs are fluffy"),
910 ]
911 self.assertEqual(actual, expected)
912
913 def test_no_duplicates_if_replacement_not_in_templates(self):
914 actual = template_replace(
915 templates=["Cats are word1", "Dogs!"],
916 replacements={
917 "word1": ["small", "cute"],
918 "word2": ["big", "fluffy"],
919 },
920 )
921 expected = [
922 ("Cats are small", "Dogs!"),
923 ("Cats are cute", "Dogs!"),
924 ]
925 self.assertEqual(actual, expected)
926
927
928 class GenericAliasSubstitutionTests(BaseTestCase):
929 """Tests for type variable substitution in generic aliases.
930
931 For variadic cases, these tests should be regarded as the source of truth,
932 since we hadn't realised the full complexity of variadic substitution
933 at the time of finalizing PEP 646. For full discussion, see
934 https://github.com/python/cpython/issues/91162.
935 """
936
937 def test_one_parameter(self):
938 T = TypeVar('T')
939 Ts = TypeVarTuple('Ts')
940 Ts2 = TypeVarTuple('Ts2')
941
942 class C(Generic[T]): pass
943
944 generics = ['C', 'list', 'List']
945 tuple_types = ['tuple', 'Tuple']
946
947 tests = [
948 # Alias # Args # Expected result
949 ('generic[T]', '[()]', 'TypeError'),
950 ('generic[T]', '[int]', 'generic[int]'),
951 ('generic[T]', '[int, str]', 'TypeError'),
952 ('generic[T]', '[tuple_type[int, ...]]', 'generic[tuple_type[int, ...]]'),
953 ('generic[T]', '[*tuple_type[int]]', 'generic[int]'),
954 ('generic[T]', '[*tuple_type[()]]', 'TypeError'),
955 ('generic[T]', '[*tuple_type[int, str]]', 'TypeError'),
956 ('generic[T]', '[*tuple_type[int, ...]]', 'TypeError'),
957 ('generic[T]', '[*Ts]', 'TypeError'),
958 ('generic[T]', '[T, *Ts]', 'TypeError'),
959 ('generic[T]', '[*Ts, T]', 'TypeError'),
960 # Raises TypeError because C is not variadic.
961 # (If C _were_ variadic, it'd be fine.)
962 ('C[T, *tuple_type[int, ...]]', '[int]', 'TypeError'),
963 # Should definitely raise TypeError: list only takes one argument.
964 ('list[T, *tuple_type[int, ...]]', '[int]', 'list[int, *tuple_type[int, ...]]'),
965 ('List[T, *tuple_type[int, ...]]', '[int]', 'TypeError'),
966 # Should raise, because more than one `TypeVarTuple` is not supported.
967 ('generic[*Ts, *Ts2]', '[int]', 'TypeError'),
968 ]
969
970 for alias_template, args_template, expected_template in tests:
971 rendered_templates = template_replace(
972 templates=[alias_template, args_template, expected_template],
973 replacements={'generic': generics, 'tuple_type': tuple_types}
974 )
975 for alias_str, args_str, expected_str in rendered_templates:
976 with self.subTest(alias=alias_str, args=args_str, expected=expected_str):
977 if expected_str == 'TypeError':
978 with self.assertRaises(TypeError):
979 eval(alias_str + args_str)
980 else:
981 self.assertEqual(
982 eval(alias_str + args_str),
983 eval(expected_str)
984 )
985
986
987 def test_two_parameters(self):
988 T1 = TypeVar('T1')
989 T2 = TypeVar('T2')
990 Ts = TypeVarTuple('Ts')
991
992 class C(Generic[T1, T2]): pass
993
994 generics = ['C', 'dict', 'Dict']
995 tuple_types = ['tuple', 'Tuple']
996
997 tests = [
998 # Alias # Args # Expected result
999 ('generic[T1, T2]', '[()]', 'TypeError'),
1000 ('generic[T1, T2]', '[int]', 'TypeError'),
1001 ('generic[T1, T2]', '[int, str]', 'generic[int, str]'),
1002 ('generic[T1, T2]', '[int, str, bool]', 'TypeError'),
1003 ('generic[T1, T2]', '[*tuple_type[int]]', 'TypeError'),
1004 ('generic[T1, T2]', '[*tuple_type[int, str]]', 'generic[int, str]'),
1005 ('generic[T1, T2]', '[*tuple_type[int, str, bool]]', 'TypeError'),
1006
1007 ('generic[T1, T2]', '[int, *tuple_type[str]]', 'generic[int, str]'),
1008 ('generic[T1, T2]', '[*tuple_type[int], str]', 'generic[int, str]'),
1009 ('generic[T1, T2]', '[*tuple_type[int], *tuple_type[str]]', 'generic[int, str]'),
1010 ('generic[T1, T2]', '[*tuple_type[int, str], *tuple_type[()]]', 'generic[int, str]'),
1011 ('generic[T1, T2]', '[*tuple_type[()], *tuple_type[int, str]]', 'generic[int, str]'),
1012 ('generic[T1, T2]', '[*tuple_type[int], *tuple_type[()]]', 'TypeError'),
1013 ('generic[T1, T2]', '[*tuple_type[()], *tuple_type[int]]', 'TypeError'),
1014 ('generic[T1, T2]', '[*tuple_type[int, str], *tuple_type[float]]', 'TypeError'),
1015 ('generic[T1, T2]', '[*tuple_type[int], *tuple_type[str, float]]', 'TypeError'),
1016 ('generic[T1, T2]', '[*tuple_type[int, str], *tuple_type[float, bool]]', 'TypeError'),
1017
1018 ('generic[T1, T2]', '[tuple_type[int, ...]]', 'TypeError'),
1019 ('generic[T1, T2]', '[tuple_type[int, ...], tuple_type[str, ...]]', 'generic[tuple_type[int, ...], tuple_type[str, ...]]'),
1020 ('generic[T1, T2]', '[*tuple_type[int, ...]]', 'TypeError'),
1021 ('generic[T1, T2]', '[int, *tuple_type[str, ...]]', 'TypeError'),
1022 ('generic[T1, T2]', '[*tuple_type[int, ...], str]', 'TypeError'),
1023 ('generic[T1, T2]', '[*tuple_type[int, ...], *tuple_type[str, ...]]', 'TypeError'),
1024 ('generic[T1, T2]', '[*Ts]', 'TypeError'),
1025 ('generic[T1, T2]', '[T, *Ts]', 'TypeError'),
1026 ('generic[T1, T2]', '[*Ts, T]', 'TypeError'),
1027 # This one isn't technically valid - none of the things that
1028 # `generic` can be (defined in `generics` above) are variadic, so we
1029 # shouldn't really be able to do `generic[T1, *tuple_type[int, ...]]`.
1030 # So even if type checkers shouldn't allow it, we allow it at
1031 # runtime, in accordance with a general philosophy of "Keep the
1032 # runtime lenient so people can experiment with typing constructs".
1033 ('generic[T1, *tuple_type[int, ...]]', '[str]', 'generic[str, *tuple_type[int, ...]]'),
1034 ]
1035
1036 for alias_template, args_template, expected_template in tests:
1037 rendered_templates = template_replace(
1038 templates=[alias_template, args_template, expected_template],
1039 replacements={'generic': generics, 'tuple_type': tuple_types}
1040 )
1041 for alias_str, args_str, expected_str in rendered_templates:
1042 with self.subTest(alias=alias_str, args=args_str, expected=expected_str):
1043 if expected_str == 'TypeError':
1044 with self.assertRaises(TypeError):
1045 eval(alias_str + args_str)
1046 else:
1047 self.assertEqual(
1048 eval(alias_str + args_str),
1049 eval(expected_str)
1050 )
1051
1052 def test_three_parameters(self):
1053 T1 = TypeVar('T1')
1054 T2 = TypeVar('T2')
1055 T3 = TypeVar('T3')
1056
1057 class C(Generic[T1, T2, T3]): pass
1058
1059 generics = ['C']
1060 tuple_types = ['tuple', 'Tuple']
1061
1062 tests = [
1063 # Alias # Args # Expected result
1064 ('generic[T1, bool, T2]', '[int, str]', 'generic[int, bool, str]'),
1065 ('generic[T1, bool, T2]', '[*tuple_type[int, str]]', 'generic[int, bool, str]'),
1066 ]
1067
1068 for alias_template, args_template, expected_template in tests:
1069 rendered_templates = template_replace(
1070 templates=[alias_template, args_template, expected_template],
1071 replacements={'generic': generics, 'tuple_type': tuple_types}
1072 )
1073 for alias_str, args_str, expected_str in rendered_templates:
1074 with self.subTest(alias=alias_str, args=args_str, expected=expected_str):
1075 if expected_str == 'TypeError':
1076 with self.assertRaises(TypeError):
1077 eval(alias_str + args_str)
1078 else:
1079 self.assertEqual(
1080 eval(alias_str + args_str),
1081 eval(expected_str)
1082 )
1083
1084 def test_variadic_parameters(self):
1085 T1 = TypeVar('T1')
1086 T2 = TypeVar('T2')
1087 Ts = TypeVarTuple('Ts')
1088
1089 class C(Generic[*Ts]): pass
1090
1091 generics = ['C', 'tuple', 'Tuple']
1092 tuple_types = ['tuple', 'Tuple']
1093
1094 tests = [
1095 # Alias # Args # Expected result
1096 ('generic[*Ts]', '[()]', 'generic[()]'),
1097 ('generic[*Ts]', '[int]', 'generic[int]'),
1098 ('generic[*Ts]', '[int, str]', 'generic[int, str]'),
1099 ('generic[*Ts]', '[*tuple_type[int]]', 'generic[int]'),
1100 ('generic[*Ts]', '[*tuple_type[*Ts]]', 'generic[*Ts]'),
1101 ('generic[*Ts]', '[*tuple_type[int, str]]', 'generic[int, str]'),
1102 ('generic[*Ts]', '[str, *tuple_type[int, ...], bool]', 'generic[str, *tuple_type[int, ...], bool]'),
1103 ('generic[*Ts]', '[tuple_type[int, ...]]', 'generic[tuple_type[int, ...]]'),
1104 ('generic[*Ts]', '[tuple_type[int, ...], tuple_type[str, ...]]', 'generic[tuple_type[int, ...], tuple_type[str, ...]]'),
1105 ('generic[*Ts]', '[*tuple_type[int, ...]]', 'generic[*tuple_type[int, ...]]'),
1106 ('generic[*Ts]', '[*tuple_type[int, ...], *tuple_type[str, ...]]', 'TypeError'),
1107
1108 ('generic[*Ts]', '[*Ts]', 'generic[*Ts]'),
1109 ('generic[*Ts]', '[T, *Ts]', 'generic[T, *Ts]'),
1110 ('generic[*Ts]', '[*Ts, T]', 'generic[*Ts, T]'),
1111 ('generic[T, *Ts]', '[()]', 'TypeError'),
1112 ('generic[T, *Ts]', '[int]', 'generic[int]'),
1113 ('generic[T, *Ts]', '[int, str]', 'generic[int, str]'),
1114 ('generic[T, *Ts]', '[int, str, bool]', 'generic[int, str, bool]'),
1115 ('generic[list[T], *Ts]', '[()]', 'TypeError'),
1116 ('generic[list[T], *Ts]', '[int]', 'generic[list[int]]'),
1117 ('generic[list[T], *Ts]', '[int, str]', 'generic[list[int], str]'),
1118 ('generic[list[T], *Ts]', '[int, str, bool]', 'generic[list[int], str, bool]'),
1119
1120 ('generic[*Ts, T]', '[()]', 'TypeError'),
1121 ('generic[*Ts, T]', '[int]', 'generic[int]'),
1122 ('generic[*Ts, T]', '[int, str]', 'generic[int, str]'),
1123 ('generic[*Ts, T]', '[int, str, bool]', 'generic[int, str, bool]'),
1124 ('generic[*Ts, list[T]]', '[()]', 'TypeError'),
1125 ('generic[*Ts, list[T]]', '[int]', 'generic[list[int]]'),
1126 ('generic[*Ts, list[T]]', '[int, str]', 'generic[int, list[str]]'),
1127 ('generic[*Ts, list[T]]', '[int, str, bool]', 'generic[int, str, list[bool]]'),
1128
1129 ('generic[T1, T2, *Ts]', '[()]', 'TypeError'),
1130 ('generic[T1, T2, *Ts]', '[int]', 'TypeError'),
1131 ('generic[T1, T2, *Ts]', '[int, str]', 'generic[int, str]'),
1132 ('generic[T1, T2, *Ts]', '[int, str, bool]', 'generic[int, str, bool]'),
1133 ('generic[T1, T2, *Ts]', '[int, str, bool, bytes]', 'generic[int, str, bool, bytes]'),
1134
1135 ('generic[*Ts, T1, T2]', '[()]', 'TypeError'),
1136 ('generic[*Ts, T1, T2]', '[int]', 'TypeError'),
1137 ('generic[*Ts, T1, T2]', '[int, str]', 'generic[int, str]'),
1138 ('generic[*Ts, T1, T2]', '[int, str, bool]', 'generic[int, str, bool]'),
1139 ('generic[*Ts, T1, T2]', '[int, str, bool, bytes]', 'generic[int, str, bool, bytes]'),
1140
1141 ('generic[T1, *Ts, T2]', '[()]', 'TypeError'),
1142 ('generic[T1, *Ts, T2]', '[int]', 'TypeError'),
1143 ('generic[T1, *Ts, T2]', '[int, str]', 'generic[int, str]'),
1144 ('generic[T1, *Ts, T2]', '[int, str, bool]', 'generic[int, str, bool]'),
1145 ('generic[T1, *Ts, T2]', '[int, str, bool, bytes]', 'generic[int, str, bool, bytes]'),
1146
1147 ('generic[T, *Ts]', '[*tuple_type[int, ...]]', 'generic[int, *tuple_type[int, ...]]'),
1148 ('generic[T, *Ts]', '[str, *tuple_type[int, ...]]', 'generic[str, *tuple_type[int, ...]]'),
1149 ('generic[T, *Ts]', '[*tuple_type[int, ...], str]', 'generic[int, *tuple_type[int, ...], str]'),
1150 ('generic[*Ts, T]', '[*tuple_type[int, ...]]', 'generic[*tuple_type[int, ...], int]'),
1151 ('generic[*Ts, T]', '[str, *tuple_type[int, ...]]', 'generic[str, *tuple_type[int, ...], int]'),
1152 ('generic[*Ts, T]', '[*tuple_type[int, ...], str]', 'generic[*tuple_type[int, ...], str]'),
1153 ('generic[T1, *Ts, T2]', '[*tuple_type[int, ...]]', 'generic[int, *tuple_type[int, ...], int]'),
1154 ('generic[T, str, *Ts]', '[*tuple_type[int, ...]]', 'generic[int, str, *tuple_type[int, ...]]'),
1155 ('generic[*Ts, str, T]', '[*tuple_type[int, ...]]', 'generic[*tuple_type[int, ...], str, int]'),
1156 ('generic[list[T], *Ts]', '[*tuple_type[int, ...]]', 'generic[list[int], *tuple_type[int, ...]]'),
1157 ('generic[*Ts, list[T]]', '[*tuple_type[int, ...]]', 'generic[*tuple_type[int, ...], list[int]]'),
1158
1159 ('generic[T, *tuple_type[int, ...]]', '[str]', 'generic[str, *tuple_type[int, ...]]'),
1160 ('generic[T1, T2, *tuple_type[int, ...]]', '[str, bool]', 'generic[str, bool, *tuple_type[int, ...]]'),
1161 ('generic[T1, *tuple_type[int, ...], T2]', '[str, bool]', 'generic[str, *tuple_type[int, ...], bool]'),
1162 ('generic[T1, *tuple_type[int, ...], T2]', '[str, bool, float]', 'TypeError'),
1163
1164 ('generic[T1, *tuple_type[T2, ...]]', '[int, str]', 'generic[int, *tuple_type[str, ...]]'),
1165 ('generic[*tuple_type[T1, ...], T2]', '[int, str]', 'generic[*tuple_type[int, ...], str]'),
1166 ('generic[T1, *tuple_type[generic[*Ts], ...]]', '[int, str, bool]', 'generic[int, *tuple_type[generic[str, bool], ...]]'),
1167 ('generic[*tuple_type[generic[*Ts], ...], T1]', '[int, str, bool]', 'generic[*tuple_type[generic[int, str], ...], bool]'),
1168 ]
1169
1170 for alias_template, args_template, expected_template in tests:
1171 rendered_templates = template_replace(
1172 templates=[alias_template, args_template, expected_template],
1173 replacements={'generic': generics, 'tuple_type': tuple_types}
1174 )
1175 for alias_str, args_str, expected_str in rendered_templates:
1176 with self.subTest(alias=alias_str, args=args_str, expected=expected_str):
1177 if expected_str == 'TypeError':
1178 with self.assertRaises(TypeError):
1179 eval(alias_str + args_str)
1180 else:
1181 self.assertEqual(
1182 eval(alias_str + args_str),
1183 eval(expected_str)
1184 )
1185
1186
1187 class UnpackTests(BaseTestCase):
1188
1189 def test_accepts_single_type(self):
1190 (*tuple[int],)
1191 Unpack[Tuple[int]]
1192
1193 def test_dir(self):
1194 dir_items = set(dir(Unpack[Tuple[int]]))
1195 for required_item in [
1196 '__args__', '__parameters__', '__origin__',
1197 ]:
1198 with self.subTest(required_item=required_item):
1199 self.assertIn(required_item, dir_items)
1200
1201 def test_rejects_multiple_types(self):
1202 with self.assertRaises(TypeError):
1203 Unpack[Tuple[int], Tuple[str]]
1204 # We can't do the equivalent for `*` here -
1205 # *(Tuple[int], Tuple[str]) is just plain tuple unpacking,
1206 # which is valid.
1207
1208 def test_rejects_multiple_parameterization(self):
1209 with self.assertRaises(TypeError):
1210 (*tuple[int],)[0][tuple[int]]
1211 with self.assertRaises(TypeError):
1212 Unpack[Tuple[int]][Tuple[int]]
1213
1214 def test_cannot_be_called(self):
1215 with self.assertRaises(TypeError):
1216 Unpack()
1217
1218 def test_usage_with_kwargs(self):
1219 Movie = TypedDict('Movie', {'name': str, 'year': int})
1220 def foo(**kwargs: Unpack[Movie]): ...
1221 self.assertEqual(repr(foo.__annotations__['kwargs']),
1222 f"typing.Unpack[{__name__}.Movie]")
1223
1224 def test_builtin_tuple(self):
1225 Ts = TypeVarTuple("Ts")
1226
1227 class Old(Generic[*Ts]): ...
1228 class New[*Ts]: ...
1229
1230 PartOld = Old[int, *Ts]
1231 self.assertEqual(PartOld[str].__args__, (int, str))
1232 self.assertEqual(PartOld[*tuple[str]].__args__, (int, str))
1233 self.assertEqual(PartOld[*Tuple[str]].__args__, (int, str))
1234 self.assertEqual(PartOld[Unpack[tuple[str]]].__args__, (int, str))
1235 self.assertEqual(PartOld[Unpack[Tuple[str]]].__args__, (int, str))
1236
1237 PartNew = New[int, *Ts]
1238 self.assertEqual(PartNew[str].__args__, (int, str))
1239 self.assertEqual(PartNew[*tuple[str]].__args__, (int, str))
1240 self.assertEqual(PartNew[*Tuple[str]].__args__, (int, str))
1241 self.assertEqual(PartNew[Unpack[tuple[str]]].__args__, (int, str))
1242 self.assertEqual(PartNew[Unpack[Tuple[str]]].__args__, (int, str))
1243
1244 def test_unpack_wrong_type(self):
1245 Ts = TypeVarTuple("Ts")
1246 class Gen[*Ts]: ...
1247 PartGen = Gen[int, *Ts]
1248
1249 bad_unpack_param = re.escape("Unpack[...] must be used with a tuple type")
1250 with self.assertRaisesRegex(TypeError, bad_unpack_param):
1251 PartGen[Unpack[list[int]]]
1252 with self.assertRaisesRegex(TypeError, bad_unpack_param):
1253 PartGen[Unpack[List[int]]]
1254
1255
1256 class TypeVarTupleTests(BaseTestCase):
1257
1258 def test_name(self):
1259 Ts = TypeVarTuple('Ts')
1260 self.assertEqual(Ts.__name__, 'Ts')
1261 Ts2 = TypeVarTuple('Ts2')
1262 self.assertEqual(Ts2.__name__, 'Ts2')
1263
1264 def test_module(self):
1265 Ts = TypeVarTuple('Ts')
1266 self.assertEqual(Ts.__module__, __name__)
1267
1268 def test_exec(self):
1269 ns = {}
1270 exec('from typing import TypeVarTuple; Ts = TypeVarTuple("Ts")', ns)
1271 Ts = ns['Ts']
1272 self.assertEqual(Ts.__name__, 'Ts')
1273 self.assertIs(Ts.__module__, None)
1274
1275 def test_instance_is_equal_to_itself(self):
1276 Ts = TypeVarTuple('Ts')
1277 self.assertEqual(Ts, Ts)
1278
1279 def test_different_instances_are_different(self):
1280 self.assertNotEqual(TypeVarTuple('Ts'), TypeVarTuple('Ts'))
1281
1282 def test_instance_isinstance_of_typevartuple(self):
1283 Ts = TypeVarTuple('Ts')
1284 self.assertIsInstance(Ts, TypeVarTuple)
1285
1286 def test_cannot_call_instance(self):
1287 Ts = TypeVarTuple('Ts')
1288 with self.assertRaises(TypeError):
1289 Ts()
1290
1291 def test_unpacked_typevartuple_is_equal_to_itself(self):
1292 Ts = TypeVarTuple('Ts')
1293 self.assertEqual((*Ts,)[0], (*Ts,)[0])
1294 self.assertEqual(Unpack[Ts], Unpack[Ts])
1295
1296 def test_parameterised_tuple_is_equal_to_itself(self):
1297 Ts = TypeVarTuple('Ts')
1298 self.assertEqual(tuple[*Ts], tuple[*Ts])
1299 self.assertEqual(Tuple[Unpack[Ts]], Tuple[Unpack[Ts]])
1300
1301 def tests_tuple_arg_ordering_matters(self):
1302 Ts1 = TypeVarTuple('Ts1')
1303 Ts2 = TypeVarTuple('Ts2')
1304 self.assertNotEqual(
1305 tuple[*Ts1, *Ts2],
1306 tuple[*Ts2, *Ts1],
1307 )
1308 self.assertNotEqual(
1309 Tuple[Unpack[Ts1], Unpack[Ts2]],
1310 Tuple[Unpack[Ts2], Unpack[Ts1]],
1311 )
1312
1313 def test_tuple_args_and_parameters_are_correct(self):
1314 Ts = TypeVarTuple('Ts')
1315 t1 = tuple[*Ts]
1316 self.assertEqual(t1.__args__, (*Ts,))
1317 self.assertEqual(t1.__parameters__, (Ts,))
1318 t2 = Tuple[Unpack[Ts]]
1319 self.assertEqual(t2.__args__, (Unpack[Ts],))
1320 self.assertEqual(t2.__parameters__, (Ts,))
1321
1322 def test_var_substitution(self):
1323 Ts = TypeVarTuple('Ts')
1324 T = TypeVar('T')
1325 T2 = TypeVar('T2')
1326 class G1(Generic[*Ts]): pass
1327 class G2(Generic[Unpack[Ts]]): pass
1328
1329 for A in G1, G2, Tuple, tuple:
1330 B = A[*Ts]
1331 self.assertEqual(B[()], A[()])
1332 self.assertEqual(B[float], A[float])
1333 self.assertEqual(B[float, str], A[float, str])
1334
1335 C = A[Unpack[Ts]]
1336 self.assertEqual(C[()], A[()])
1337 self.assertEqual(C[float], A[float])
1338 self.assertEqual(C[float, str], A[float, str])
1339
1340 D = list[A[*Ts]]
1341 self.assertEqual(D[()], list[A[()]])
1342 self.assertEqual(D[float], list[A[float]])
1343 self.assertEqual(D[float, str], list[A[float, str]])
1344
1345 E = List[A[Unpack[Ts]]]
1346 self.assertEqual(E[()], List[A[()]])
1347 self.assertEqual(E[float], List[A[float]])
1348 self.assertEqual(E[float, str], List[A[float, str]])
1349
1350 F = A[T, *Ts, T2]
1351 with self.assertRaises(TypeError):
1352 F[()]
1353 with self.assertRaises(TypeError):
1354 F[float]
1355 self.assertEqual(F[float, str], A[float, str])
1356 self.assertEqual(F[float, str, int], A[float, str, int])
1357 self.assertEqual(F[float, str, int, bytes], A[float, str, int, bytes])
1358
1359 G = A[T, Unpack[Ts], T2]
1360 with self.assertRaises(TypeError):
1361 G[()]
1362 with self.assertRaises(TypeError):
1363 G[float]
1364 self.assertEqual(G[float, str], A[float, str])
1365 self.assertEqual(G[float, str, int], A[float, str, int])
1366 self.assertEqual(G[float, str, int, bytes], A[float, str, int, bytes])
1367
1368 H = tuple[list[T], A[*Ts], list[T2]]
1369 with self.assertRaises(TypeError):
1370 H[()]
1371 with self.assertRaises(TypeError):
1372 H[float]
1373 if A != Tuple:
1374 self.assertEqual(H[float, str],
1375 tuple[list[float], A[()], list[str]])
1376 self.assertEqual(H[float, str, int],
1377 tuple[list[float], A[str], list[int]])
1378 self.assertEqual(H[float, str, int, bytes],
1379 tuple[list[float], A[str, int], list[bytes]])
1380
1381 I = Tuple[List[T], A[Unpack[Ts]], List[T2]]
1382 with self.assertRaises(TypeError):
1383 I[()]
1384 with self.assertRaises(TypeError):
1385 I[float]
1386 if A != Tuple:
1387 self.assertEqual(I[float, str],
1388 Tuple[List[float], A[()], List[str]])
1389 self.assertEqual(I[float, str, int],
1390 Tuple[List[float], A[str], List[int]])
1391 self.assertEqual(I[float, str, int, bytes],
1392 Tuple[List[float], A[str, int], List[bytes]])
1393
1394 def test_bad_var_substitution(self):
1395 Ts = TypeVarTuple('Ts')
1396 T = TypeVar('T')
1397 T2 = TypeVar('T2')
1398 class G1(Generic[*Ts]): pass
1399 class G2(Generic[Unpack[Ts]]): pass
1400
1401 for A in G1, G2, Tuple, tuple:
1402 B = A[Ts]
1403 with self.assertRaises(TypeError):
1404 B[int, str]
1405
1406 C = A[T, T2]
1407 with self.assertRaises(TypeError):
1408 C[*Ts]
1409 with self.assertRaises(TypeError):
1410 C[Unpack[Ts]]
1411
1412 B = A[T, *Ts, str, T2]
1413 with self.assertRaises(TypeError):
1414 B[int, *Ts]
1415 with self.assertRaises(TypeError):
1416 B[int, *Ts, *Ts]
1417
1418 C = A[T, Unpack[Ts], str, T2]
1419 with self.assertRaises(TypeError):
1420 C[int, Unpack[Ts]]
1421 with self.assertRaises(TypeError):
1422 C[int, Unpack[Ts], Unpack[Ts]]
1423
1424 def test_repr_is_correct(self):
1425 Ts = TypeVarTuple('Ts')
1426
1427 class G1(Generic[*Ts]): pass
1428 class G2(Generic[Unpack[Ts]]): pass
1429
1430 self.assertEqual(repr(Ts), 'Ts')
1431
1432 self.assertEqual(repr((*Ts,)[0]), 'typing.Unpack[Ts]')
1433 self.assertEqual(repr(Unpack[Ts]), 'typing.Unpack[Ts]')
1434
1435 self.assertEqual(repr(tuple[*Ts]), 'tuple[typing.Unpack[Ts]]')
1436 self.assertEqual(repr(Tuple[Unpack[Ts]]), 'typing.Tuple[typing.Unpack[Ts]]')
1437
1438 self.assertEqual(repr(*tuple[*Ts]), '*tuple[typing.Unpack[Ts]]')
1439 self.assertEqual(repr(Unpack[Tuple[Unpack[Ts]]]), 'typing.Unpack[typing.Tuple[typing.Unpack[Ts]]]')
1440
1441 def test_variadic_class_repr_is_correct(self):
1442 Ts = TypeVarTuple('Ts')
1443 class A(Generic[*Ts]): pass
1444 class B(Generic[Unpack[Ts]]): pass
1445
1446 self.assertEndsWith(repr(A[()]), 'A[()]')
1447 self.assertEndsWith(repr(B[()]), 'B[()]')
1448 self.assertEndsWith(repr(A[float]), 'A[float]')
1449 self.assertEndsWith(repr(B[float]), 'B[float]')
1450 self.assertEndsWith(repr(A[float, str]), 'A[float, str]')
1451 self.assertEndsWith(repr(B[float, str]), 'B[float, str]')
1452
1453 self.assertEndsWith(repr(A[*tuple[int, ...]]),
1454 'A[*tuple[int, ...]]')
1455 self.assertEndsWith(repr(B[Unpack[Tuple[int, ...]]]),
1456 'B[typing.Unpack[typing.Tuple[int, ...]]]')
1457
1458 self.assertEndsWith(repr(A[float, *tuple[int, ...]]),
1459 'A[float, *tuple[int, ...]]')
1460 self.assertEndsWith(repr(A[float, Unpack[Tuple[int, ...]]]),
1461 'A[float, typing.Unpack[typing.Tuple[int, ...]]]')
1462
1463 self.assertEndsWith(repr(A[*tuple[int, ...], str]),
1464 'A[*tuple[int, ...], str]')
1465 self.assertEndsWith(repr(B[Unpack[Tuple[int, ...]], str]),
1466 'B[typing.Unpack[typing.Tuple[int, ...]], str]')
1467
1468 self.assertEndsWith(repr(A[float, *tuple[int, ...], str]),
1469 'A[float, *tuple[int, ...], str]')
1470 self.assertEndsWith(repr(B[float, Unpack[Tuple[int, ...]], str]),
1471 'B[float, typing.Unpack[typing.Tuple[int, ...]], str]')
1472
1473 def test_variadic_class_alias_repr_is_correct(self):
1474 Ts = TypeVarTuple('Ts')
1475 class A(Generic[Unpack[Ts]]): pass
1476
1477 B = A[*Ts]
1478 self.assertEndsWith(repr(B), 'A[typing.Unpack[Ts]]')
1479 self.assertEndsWith(repr(B[()]), 'A[()]')
1480 self.assertEndsWith(repr(B[float]), 'A[float]')
1481 self.assertEndsWith(repr(B[float, str]), 'A[float, str]')
1482
1483 C = A[Unpack[Ts]]
1484 self.assertEndsWith(repr(C), 'A[typing.Unpack[Ts]]')
1485 self.assertEndsWith(repr(C[()]), 'A[()]')
1486 self.assertEndsWith(repr(C[float]), 'A[float]')
1487 self.assertEndsWith(repr(C[float, str]), 'A[float, str]')
1488
1489 D = A[*Ts, int]
1490 self.assertEndsWith(repr(D), 'A[typing.Unpack[Ts], int]')
1491 self.assertEndsWith(repr(D[()]), 'A[int]')
1492 self.assertEndsWith(repr(D[float]), 'A[float, int]')
1493 self.assertEndsWith(repr(D[float, str]), 'A[float, str, int]')
1494
1495 E = A[Unpack[Ts], int]
1496 self.assertEndsWith(repr(E), 'A[typing.Unpack[Ts], int]')
1497 self.assertEndsWith(repr(E[()]), 'A[int]')
1498 self.assertEndsWith(repr(E[float]), 'A[float, int]')
1499 self.assertEndsWith(repr(E[float, str]), 'A[float, str, int]')
1500
1501 F = A[int, *Ts]
1502 self.assertEndsWith(repr(F), 'A[int, typing.Unpack[Ts]]')
1503 self.assertEndsWith(repr(F[()]), 'A[int]')
1504 self.assertEndsWith(repr(F[float]), 'A[int, float]')
1505 self.assertEndsWith(repr(F[float, str]), 'A[int, float, str]')
1506
1507 G = A[int, Unpack[Ts]]
1508 self.assertEndsWith(repr(G), 'A[int, typing.Unpack[Ts]]')
1509 self.assertEndsWith(repr(G[()]), 'A[int]')
1510 self.assertEndsWith(repr(G[float]), 'A[int, float]')
1511 self.assertEndsWith(repr(G[float, str]), 'A[int, float, str]')
1512
1513 H = A[int, *Ts, str]
1514 self.assertEndsWith(repr(H), 'A[int, typing.Unpack[Ts], str]')
1515 self.assertEndsWith(repr(H[()]), 'A[int, str]')
1516 self.assertEndsWith(repr(H[float]), 'A[int, float, str]')
1517 self.assertEndsWith(repr(H[float, str]), 'A[int, float, str, str]')
1518
1519 I = A[int, Unpack[Ts], str]
1520 self.assertEndsWith(repr(I), 'A[int, typing.Unpack[Ts], str]')
1521 self.assertEndsWith(repr(I[()]), 'A[int, str]')
1522 self.assertEndsWith(repr(I[float]), 'A[int, float, str]')
1523 self.assertEndsWith(repr(I[float, str]), 'A[int, float, str, str]')
1524
1525 J = A[*Ts, *tuple[str, ...]]
1526 self.assertEndsWith(repr(J), 'A[typing.Unpack[Ts], *tuple[str, ...]]')
1527 self.assertEndsWith(repr(J[()]), 'A[*tuple[str, ...]]')
1528 self.assertEndsWith(repr(J[float]), 'A[float, *tuple[str, ...]]')
1529 self.assertEndsWith(repr(J[float, str]), 'A[float, str, *tuple[str, ...]]')
1530
1531 K = A[Unpack[Ts], Unpack[Tuple[str, ...]]]
1532 self.assertEndsWith(repr(K), 'A[typing.Unpack[Ts], typing.Unpack[typing.Tuple[str, ...]]]')
1533 self.assertEndsWith(repr(K[()]), 'A[typing.Unpack[typing.Tuple[str, ...]]]')
1534 self.assertEndsWith(repr(K[float]), 'A[float, typing.Unpack[typing.Tuple[str, ...]]]')
1535 self.assertEndsWith(repr(K[float, str]), 'A[float, str, typing.Unpack[typing.Tuple[str, ...]]]')
1536
1537 def test_cannot_subclass(self):
1538 with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'TypeVarTuple'):
1539 class C(TypeVarTuple): pass
1540 Ts = TypeVarTuple('Ts')
1541 with self.assertRaisesRegex(TypeError,
1542 CANNOT_SUBCLASS_INSTANCE % 'TypeVarTuple'):
1543 class D(Ts): pass
1544 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
1545 class E(type(Unpack)): pass
1546 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
1547 class F(type(*Ts)): pass
1548 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
1549 class G(type(Unpack[Ts])): pass
1550 with self.assertRaisesRegex(TypeError,
1551 r'Cannot subclass typing\.Unpack'):
1552 class H(Unpack): pass
1553 with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'):
1554 class I(*Ts): pass
1555 with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'):
1556 class J(Unpack[Ts]): pass
1557
1558 def test_variadic_class_args_are_correct(self):
1559 T = TypeVar('T')
1560 Ts = TypeVarTuple('Ts')
1561 class A(Generic[*Ts]): pass
1562 class B(Generic[Unpack[Ts]]): pass
1563
1564 C = A[()]
1565 D = B[()]
1566 self.assertEqual(C.__args__, ())
1567 self.assertEqual(D.__args__, ())
1568
1569 E = A[int]
1570 F = B[int]
1571 self.assertEqual(E.__args__, (int,))
1572 self.assertEqual(F.__args__, (int,))
1573
1574 G = A[int, str]
1575 H = B[int, str]
1576 self.assertEqual(G.__args__, (int, str))
1577 self.assertEqual(H.__args__, (int, str))
1578
1579 I = A[T]
1580 J = B[T]
1581 self.assertEqual(I.__args__, (T,))
1582 self.assertEqual(J.__args__, (T,))
1583
1584 K = A[*Ts]
1585 L = B[Unpack[Ts]]
1586 self.assertEqual(K.__args__, (*Ts,))
1587 self.assertEqual(L.__args__, (Unpack[Ts],))
1588
1589 M = A[T, *Ts]
1590 N = B[T, Unpack[Ts]]
1591 self.assertEqual(M.__args__, (T, *Ts))
1592 self.assertEqual(N.__args__, (T, Unpack[Ts]))
1593
1594 O = A[*Ts, T]
1595 P = B[Unpack[Ts], T]
1596 self.assertEqual(O.__args__, (*Ts, T))
1597 self.assertEqual(P.__args__, (Unpack[Ts], T))
1598
1599 def test_variadic_class_origin_is_correct(self):
1600 Ts = TypeVarTuple('Ts')
1601
1602 class C(Generic[*Ts]): pass
1603 self.assertIs(C[int].__origin__, C)
1604 self.assertIs(C[T].__origin__, C)
1605 self.assertIs(C[Unpack[Ts]].__origin__, C)
1606
1607 class D(Generic[Unpack[Ts]]): pass
1608 self.assertIs(D[int].__origin__, D)
1609 self.assertIs(D[T].__origin__, D)
1610 self.assertIs(D[Unpack[Ts]].__origin__, D)
1611
1612 def test_get_type_hints_on_unpack_args(self):
1613 Ts = TypeVarTuple('Ts')
1614
1615 def func1(*args: *Ts): pass
1616 self.assertEqual(gth(func1), {'args': Unpack[Ts]})
1617
1618 def func2(*args: *tuple[int, str]): pass
1619 self.assertEqual(gth(func2), {'args': Unpack[tuple[int, str]]})
1620
1621 class CustomVariadic(Generic[*Ts]): pass
1622
1623 def func3(*args: *CustomVariadic[int, str]): pass
1624 self.assertEqual(gth(func3), {'args': Unpack[CustomVariadic[int, str]]})
1625
1626 def test_get_type_hints_on_unpack_args_string(self):
1627 Ts = TypeVarTuple('Ts')
1628
1629 def func1(*args: '*Ts'): pass
1630 self.assertEqual(gth(func1, localns={'Ts': Ts}),
1631 {'args': Unpack[Ts]})
1632
1633 def func2(*args: '*tuple[int, str]'): pass
1634 self.assertEqual(gth(func2), {'args': Unpack[tuple[int, str]]})
1635
1636 class CustomVariadic(Generic[*Ts]): pass
1637
1638 def func3(*args: '*CustomVariadic[int, str]'): pass
1639 self.assertEqual(gth(func3, localns={'CustomVariadic': CustomVariadic}),
1640 {'args': Unpack[CustomVariadic[int, str]]})
1641
1642 def test_tuple_args_are_correct(self):
1643 Ts = TypeVarTuple('Ts')
1644
1645 self.assertEqual(tuple[*Ts].__args__, (*Ts,))
1646 self.assertEqual(Tuple[Unpack[Ts]].__args__, (Unpack[Ts],))
1647
1648 self.assertEqual(tuple[*Ts, int].__args__, (*Ts, int))
1649 self.assertEqual(Tuple[Unpack[Ts], int].__args__, (Unpack[Ts], int))
1650
1651 self.assertEqual(tuple[int, *Ts].__args__, (int, *Ts))
1652 self.assertEqual(Tuple[int, Unpack[Ts]].__args__, (int, Unpack[Ts]))
1653
1654 self.assertEqual(tuple[int, *Ts, str].__args__,
1655 (int, *Ts, str))
1656 self.assertEqual(Tuple[int, Unpack[Ts], str].__args__,
1657 (int, Unpack[Ts], str))
1658
1659 self.assertEqual(tuple[*Ts, int].__args__, (*Ts, int))
1660 self.assertEqual(Tuple[Unpack[Ts]].__args__, (Unpack[Ts],))
1661
1662 def test_callable_args_are_correct(self):
1663 Ts = TypeVarTuple('Ts')
1664 Ts1 = TypeVarTuple('Ts1')
1665 Ts2 = TypeVarTuple('Ts2')
1666
1667 # TypeVarTuple in the arguments
1668
1669 a = Callable[[*Ts], None]
1670 b = Callable[[Unpack[Ts]], None]
1671 self.assertEqual(a.__args__, (*Ts, type(None)))
1672 self.assertEqual(b.__args__, (Unpack[Ts], type(None)))
1673
1674 c = Callable[[int, *Ts], None]
1675 d = Callable[[int, Unpack[Ts]], None]
1676 self.assertEqual(c.__args__, (int, *Ts, type(None)))
1677 self.assertEqual(d.__args__, (int, Unpack[Ts], type(None)))
1678
1679 e = Callable[[*Ts, int], None]
1680 f = Callable[[Unpack[Ts], int], None]
1681 self.assertEqual(e.__args__, (*Ts, int, type(None)))
1682 self.assertEqual(f.__args__, (Unpack[Ts], int, type(None)))
1683
1684 g = Callable[[str, *Ts, int], None]
1685 h = Callable[[str, Unpack[Ts], int], None]
1686 self.assertEqual(g.__args__, (str, *Ts, int, type(None)))
1687 self.assertEqual(h.__args__, (str, Unpack[Ts], int, type(None)))
1688
1689 # TypeVarTuple as the return
1690
1691 i = Callable[[None], *Ts]
1692 j = Callable[[None], Unpack[Ts]]
1693 self.assertEqual(i.__args__, (type(None), *Ts))
1694 self.assertEqual(j.__args__, (type(None), Unpack[Ts]))
1695
1696 k = Callable[[None], tuple[int, *Ts]]
1697 l = Callable[[None], Tuple[int, Unpack[Ts]]]
1698 self.assertEqual(k.__args__, (type(None), tuple[int, *Ts]))
1699 self.assertEqual(l.__args__, (type(None), Tuple[int, Unpack[Ts]]))
1700
1701 m = Callable[[None], tuple[*Ts, int]]
1702 n = Callable[[None], Tuple[Unpack[Ts], int]]
1703 self.assertEqual(m.__args__, (type(None), tuple[*Ts, int]))
1704 self.assertEqual(n.__args__, (type(None), Tuple[Unpack[Ts], int]))
1705
1706 o = Callable[[None], tuple[str, *Ts, int]]
1707 p = Callable[[None], Tuple[str, Unpack[Ts], int]]
1708 self.assertEqual(o.__args__, (type(None), tuple[str, *Ts, int]))
1709 self.assertEqual(p.__args__, (type(None), Tuple[str, Unpack[Ts], int]))
1710
1711 # TypeVarTuple in both
1712
1713 q = Callable[[*Ts], *Ts]
1714 r = Callable[[Unpack[Ts]], Unpack[Ts]]
1715 self.assertEqual(q.__args__, (*Ts, *Ts))
1716 self.assertEqual(r.__args__, (Unpack[Ts], Unpack[Ts]))
1717
1718 s = Callable[[*Ts1], *Ts2]
1719 u = Callable[[Unpack[Ts1]], Unpack[Ts2]]
1720 self.assertEqual(s.__args__, (*Ts1, *Ts2))
1721 self.assertEqual(u.__args__, (Unpack[Ts1], Unpack[Ts2]))
1722
1723 def test_variadic_class_with_duplicate_typevartuples_fails(self):
1724 Ts1 = TypeVarTuple('Ts1')
1725 Ts2 = TypeVarTuple('Ts2')
1726
1727 with self.assertRaises(TypeError):
1728 class C(Generic[*Ts1, *Ts1]): pass
1729 with self.assertRaises(TypeError):
1730 class D(Generic[Unpack[Ts1], Unpack[Ts1]]): pass
1731
1732 with self.assertRaises(TypeError):
1733 class E(Generic[*Ts1, *Ts2, *Ts1]): pass
1734 with self.assertRaises(TypeError):
1735 class F(Generic[Unpack[Ts1], Unpack[Ts2], Unpack[Ts1]]): pass
1736
1737 def test_type_concatenation_in_variadic_class_argument_list_succeeds(self):
1738 Ts = TypeVarTuple('Ts')
1739 class C(Generic[Unpack[Ts]]): pass
1740
1741 C[int, *Ts]
1742 C[int, Unpack[Ts]]
1743
1744 C[*Ts, int]
1745 C[Unpack[Ts], int]
1746
1747 C[int, *Ts, str]
1748 C[int, Unpack[Ts], str]
1749
1750 C[int, bool, *Ts, float, str]
1751 C[int, bool, Unpack[Ts], float, str]
1752
1753 def test_type_concatenation_in_tuple_argument_list_succeeds(self):
1754 Ts = TypeVarTuple('Ts')
1755
1756 tuple[int, *Ts]
1757 tuple[*Ts, int]
1758 tuple[int, *Ts, str]
1759 tuple[int, bool, *Ts, float, str]
1760
1761 Tuple[int, Unpack[Ts]]
1762 Tuple[Unpack[Ts], int]
1763 Tuple[int, Unpack[Ts], str]
1764 Tuple[int, bool, Unpack[Ts], float, str]
1765
1766 def test_variadic_class_definition_using_packed_typevartuple_fails(self):
1767 Ts = TypeVarTuple('Ts')
1768 with self.assertRaises(TypeError):
1769 class C(Generic[Ts]): pass
1770
1771 def test_variadic_class_definition_using_concrete_types_fails(self):
1772 Ts = TypeVarTuple('Ts')
1773 with self.assertRaises(TypeError):
1774 class F(Generic[*Ts, int]): pass
1775 with self.assertRaises(TypeError):
1776 class E(Generic[Unpack[Ts], int]): pass
1777
1778 def test_variadic_class_with_2_typevars_accepts_2_or_more_args(self):
1779 Ts = TypeVarTuple('Ts')
1780 T1 = TypeVar('T1')
1781 T2 = TypeVar('T2')
1782
1783 class A(Generic[T1, T2, *Ts]): pass
1784 A[int, str]
1785 A[int, str, float]
1786 A[int, str, float, bool]
1787
1788 class B(Generic[T1, T2, Unpack[Ts]]): pass
1789 B[int, str]
1790 B[int, str, float]
1791 B[int, str, float, bool]
1792
1793 class C(Generic[T1, *Ts, T2]): pass
1794 C[int, str]
1795 C[int, str, float]
1796 C[int, str, float, bool]
1797
1798 class D(Generic[T1, Unpack[Ts], T2]): pass
1799 D[int, str]
1800 D[int, str, float]
1801 D[int, str, float, bool]
1802
1803 class E(Generic[*Ts, T1, T2]): pass
1804 E[int, str]
1805 E[int, str, float]
1806 E[int, str, float, bool]
1807
1808 class F(Generic[Unpack[Ts], T1, T2]): pass
1809 F[int, str]
1810 F[int, str, float]
1811 F[int, str, float, bool]
1812
1813 def test_variadic_args_annotations_are_correct(self):
1814 Ts = TypeVarTuple('Ts')
1815
1816 def f(*args: Unpack[Ts]): pass
1817 def g(*args: *Ts): pass
1818 self.assertEqual(f.__annotations__, {'args': Unpack[Ts]})
1819 self.assertEqual(g.__annotations__, {'args': (*Ts,)[0]})
1820
1821 def test_variadic_args_with_ellipsis_annotations_are_correct(self):
1822 def a(*args: *tuple[int, ...]): pass
1823 self.assertEqual(a.__annotations__,
1824 {'args': (*tuple[int, ...],)[0]})
1825
1826 def b(*args: Unpack[Tuple[int, ...]]): pass
1827 self.assertEqual(b.__annotations__,
1828 {'args': Unpack[Tuple[int, ...]]})
1829
1830 def test_concatenation_in_variadic_args_annotations_are_correct(self):
1831 Ts = TypeVarTuple('Ts')
1832
1833 # Unpacking using `*`, native `tuple` type
1834
1835 def a(*args: *tuple[int, *Ts]): pass
1836 self.assertEqual(
1837 a.__annotations__,
1838 {'args': (*tuple[int, *Ts],)[0]},
1839 )
1840
1841 def b(*args: *tuple[*Ts, int]): pass
1842 self.assertEqual(
1843 b.__annotations__,
1844 {'args': (*tuple[*Ts, int],)[0]},
1845 )
1846
1847 def c(*args: *tuple[str, *Ts, int]): pass
1848 self.assertEqual(
1849 c.__annotations__,
1850 {'args': (*tuple[str, *Ts, int],)[0]},
1851 )
1852
1853 def d(*args: *tuple[int, bool, *Ts, float, str]): pass
1854 self.assertEqual(
1855 d.__annotations__,
1856 {'args': (*tuple[int, bool, *Ts, float, str],)[0]},
1857 )
1858
1859 # Unpacking using `Unpack`, `Tuple` type from typing.py
1860
1861 def e(*args: Unpack[Tuple[int, Unpack[Ts]]]): pass
1862 self.assertEqual(
1863 e.__annotations__,
1864 {'args': Unpack[Tuple[int, Unpack[Ts]]]},
1865 )
1866
1867 def f(*args: Unpack[Tuple[Unpack[Ts], int]]): pass
1868 self.assertEqual(
1869 f.__annotations__,
1870 {'args': Unpack[Tuple[Unpack[Ts], int]]},
1871 )
1872
1873 def g(*args: Unpack[Tuple[str, Unpack[Ts], int]]): pass
1874 self.assertEqual(
1875 g.__annotations__,
1876 {'args': Unpack[Tuple[str, Unpack[Ts], int]]},
1877 )
1878
1879 def h(*args: Unpack[Tuple[int, bool, Unpack[Ts], float, str]]): pass
1880 self.assertEqual(
1881 h.__annotations__,
1882 {'args': Unpack[Tuple[int, bool, Unpack[Ts], float, str]]},
1883 )
1884
1885 def test_variadic_class_same_args_results_in_equalty(self):
1886 Ts = TypeVarTuple('Ts')
1887 class C(Generic[*Ts]): pass
1888 class D(Generic[Unpack[Ts]]): pass
1889
1890 self.assertEqual(C[int], C[int])
1891 self.assertEqual(D[int], D[int])
1892
1893 Ts1 = TypeVarTuple('Ts1')
1894 Ts2 = TypeVarTuple('Ts2')
1895
1896 self.assertEqual(
1897 C[*Ts1],
1898 C[*Ts1],
1899 )
1900 self.assertEqual(
1901 D[Unpack[Ts1]],
1902 D[Unpack[Ts1]],
1903 )
1904
1905 self.assertEqual(
1906 C[*Ts1, *Ts2],
1907 C[*Ts1, *Ts2],
1908 )
1909 self.assertEqual(
1910 D[Unpack[Ts1], Unpack[Ts2]],
1911 D[Unpack[Ts1], Unpack[Ts2]],
1912 )
1913
1914 self.assertEqual(
1915 C[int, *Ts1, *Ts2],
1916 C[int, *Ts1, *Ts2],
1917 )
1918 self.assertEqual(
1919 D[int, Unpack[Ts1], Unpack[Ts2]],
1920 D[int, Unpack[Ts1], Unpack[Ts2]],
1921 )
1922
1923 def test_variadic_class_arg_ordering_matters(self):
1924 Ts = TypeVarTuple('Ts')
1925 class C(Generic[*Ts]): pass
1926 class D(Generic[Unpack[Ts]]): pass
1927
1928 self.assertNotEqual(
1929 C[int, str],
1930 C[str, int],
1931 )
1932 self.assertNotEqual(
1933 D[int, str],
1934 D[str, int],
1935 )
1936
1937 Ts1 = TypeVarTuple('Ts1')
1938 Ts2 = TypeVarTuple('Ts2')
1939
1940 self.assertNotEqual(
1941 C[*Ts1, *Ts2],
1942 C[*Ts2, *Ts1],
1943 )
1944 self.assertNotEqual(
1945 D[Unpack[Ts1], Unpack[Ts2]],
1946 D[Unpack[Ts2], Unpack[Ts1]],
1947 )
1948
1949 def test_variadic_class_arg_typevartuple_identity_matters(self):
1950 Ts = TypeVarTuple('Ts')
1951 Ts1 = TypeVarTuple('Ts1')
1952 Ts2 = TypeVarTuple('Ts2')
1953
1954 class C(Generic[*Ts]): pass
1955 class D(Generic[Unpack[Ts]]): pass
1956
1957 self.assertNotEqual(C[*Ts1], C[*Ts2])
1958 self.assertNotEqual(D[Unpack[Ts1]], D[Unpack[Ts2]])
1959
1960
1961 class TypeVarTuplePicklingTests(BaseTestCase):
1962 # These are slightly awkward tests to run, because TypeVarTuples are only
1963 # picklable if defined in the global scope. We therefore need to push
1964 # various things defined in these tests into the global scope with `global`
1965 # statements at the start of each test.
1966
1967 @all_pickle_protocols
1968 def test_pickling_then_unpickling_results_in_same_identity(self, proto):
1969 global global_Ts1 # See explanation at start of class.
1970 global_Ts1 = TypeVarTuple('global_Ts1')
1971 global_Ts2 = pickle.loads(pickle.dumps(global_Ts1, proto))
1972 self.assertIs(global_Ts1, global_Ts2)
1973
1974 @all_pickle_protocols
1975 def test_pickling_then_unpickling_unpacked_results_in_same_identity(self, proto):
1976 global global_Ts # See explanation at start of class.
1977 global_Ts = TypeVarTuple('global_Ts')
1978
1979 unpacked1 = (*global_Ts,)[0]
1980 unpacked2 = pickle.loads(pickle.dumps(unpacked1, proto))
1981 self.assertIs(unpacked1, unpacked2)
1982
1983 unpacked3 = Unpack[global_Ts]
1984 unpacked4 = pickle.loads(pickle.dumps(unpacked3, proto))
1985 self.assertIs(unpacked3, unpacked4)
1986
1987 @all_pickle_protocols
1988 def test_pickling_then_unpickling_tuple_with_typevartuple_equality(
1989 self, proto
1990 ):
1991 global global_T, global_Ts # See explanation at start of class.
1992 global_T = TypeVar('global_T')
1993 global_Ts = TypeVarTuple('global_Ts')
1994
1995 tuples = [
1996 tuple[*global_Ts],
1997 Tuple[Unpack[global_Ts]],
1998
1999 tuple[T, *global_Ts],
2000 Tuple[T, Unpack[global_Ts]],
2001
2002 tuple[int, *global_Ts],
2003 Tuple[int, Unpack[global_Ts]],
2004 ]
2005 for t in tuples:
2006 t2 = pickle.loads(pickle.dumps(t, proto))
2007 self.assertEqual(t, t2)
2008
2009
2010 class UnionTests(BaseTestCase):
2011
2012 def test_basics(self):
2013 u = Union[int, float]
2014 self.assertNotEqual(u, Union)
2015
2016 def test_union_isinstance(self):
2017 self.assertIsInstance(42, Union[int, str])
2018 self.assertIsInstance('abc', Union[int, str])
2019 self.assertNotIsInstance(3.14, Union[int, str])
2020 self.assertIsInstance(42, Union[int, list[int]])
2021 self.assertIsInstance(42, Union[int, Any])
2022
2023 def test_union_isinstance_type_error(self):
2024 with self.assertRaises(TypeError):
2025 isinstance(42, Union[str, list[int]])
2026 with self.assertRaises(TypeError):
2027 isinstance(42, Union[list[int], int])
2028 with self.assertRaises(TypeError):
2029 isinstance(42, Union[list[int], str])
2030 with self.assertRaises(TypeError):
2031 isinstance(42, Union[str, Any])
2032 with self.assertRaises(TypeError):
2033 isinstance(42, Union[Any, int])
2034 with self.assertRaises(TypeError):
2035 isinstance(42, Union[Any, str])
2036
2037 def test_optional_isinstance(self):
2038 self.assertIsInstance(42, Optional[int])
2039 self.assertIsInstance(None, Optional[int])
2040 self.assertNotIsInstance('abc', Optional[int])
2041
2042 def test_optional_isinstance_type_error(self):
2043 with self.assertRaises(TypeError):
2044 isinstance(42, Optional[list[int]])
2045 with self.assertRaises(TypeError):
2046 isinstance(None, Optional[list[int]])
2047 with self.assertRaises(TypeError):
2048 isinstance(42, Optional[Any])
2049 with self.assertRaises(TypeError):
2050 isinstance(None, Optional[Any])
2051
2052 def test_union_issubclass(self):
2053 self.assertIsSubclass(int, Union[int, str])
2054 self.assertIsSubclass(str, Union[int, str])
2055 self.assertNotIsSubclass(float, Union[int, str])
2056 self.assertIsSubclass(int, Union[int, list[int]])
2057 self.assertIsSubclass(int, Union[int, Any])
2058 self.assertNotIsSubclass(int, Union[str, Any])
2059 self.assertIsSubclass(int, Union[Any, int])
2060 self.assertNotIsSubclass(int, Union[Any, str])
2061
2062 def test_union_issubclass_type_error(self):
2063 with self.assertRaises(TypeError):
2064 issubclass(Union[int, str], int)
2065 with self.assertRaises(TypeError):
2066 issubclass(int, Union[str, list[int]])
2067 with self.assertRaises(TypeError):
2068 issubclass(int, Union[list[int], int])
2069 with self.assertRaises(TypeError):
2070 issubclass(int, Union[list[int], str])
2071
2072 def test_optional_issubclass(self):
2073 self.assertIsSubclass(int, Optional[int])
2074 self.assertIsSubclass(type(None), Optional[int])
2075 self.assertNotIsSubclass(str, Optional[int])
2076 self.assertIsSubclass(Any, Optional[Any])
2077 self.assertIsSubclass(type(None), Optional[Any])
2078 self.assertNotIsSubclass(int, Optional[Any])
2079
2080 def test_optional_issubclass_type_error(self):
2081 with self.assertRaises(TypeError):
2082 issubclass(list[int], Optional[list[int]])
2083 with self.assertRaises(TypeError):
2084 issubclass(type(None), Optional[list[int]])
2085 with self.assertRaises(TypeError):
2086 issubclass(int, Optional[list[int]])
2087
2088 def test_union_any(self):
2089 u = Union[Any]
2090 self.assertEqual(u, Any)
2091 u1 = Union[int, Any]
2092 u2 = Union[Any, int]
2093 u3 = Union[Any, object]
2094 self.assertEqual(u1, u2)
2095 self.assertNotEqual(u1, Any)
2096 self.assertNotEqual(u2, Any)
2097 self.assertNotEqual(u3, Any)
2098
2099 def test_union_object(self):
2100 u = Union[object]
2101 self.assertEqual(u, object)
2102 u1 = Union[int, object]
2103 u2 = Union[object, int]
2104 self.assertEqual(u1, u2)
2105 self.assertNotEqual(u1, object)
2106 self.assertNotEqual(u2, object)
2107
2108 def test_unordered(self):
2109 u1 = Union[int, float]
2110 u2 = Union[float, int]
2111 self.assertEqual(u1, u2)
2112
2113 def test_single_class_disappears(self):
2114 t = Union[Employee]
2115 self.assertIs(t, Employee)
2116
2117 def test_base_class_kept(self):
2118 u = Union[Employee, Manager]
2119 self.assertNotEqual(u, Employee)
2120 self.assertIn(Employee, u.__args__)
2121 self.assertIn(Manager, u.__args__)
2122
2123 def test_union_union(self):
2124 u = Union[int, float]
2125 v = Union[u, Employee]
2126 self.assertEqual(v, Union[int, float, Employee])
2127
2128 def test_union_of_unhashable(self):
2129 class UnhashableMeta(type):
2130 __hash__ = None
2131
2132 class A(metaclass=UnhashableMeta): ...
2133 class B(metaclass=UnhashableMeta): ...
2134
2135 self.assertEqual(Union[A, B].__args__, (A, B))
2136 union1 = Union[A, B]
2137 with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"):
2138 hash(union1)
2139
2140 union2 = Union[int, B]
2141 with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"):
2142 hash(union2)
2143
2144 union3 = Union[A, int]
2145 with self.assertRaisesRegex(TypeError, "unhashable type: 'UnhashableMeta'"):
2146 hash(union3)
2147
2148 def test_repr(self):
2149 u = Union[Employee, int]
2150 self.assertEqual(repr(u), f'{__name__}.Employee | int')
2151 u = Union[int, Employee]
2152 self.assertEqual(repr(u), f'int | {__name__}.Employee')
2153 T = TypeVar('T')
2154 u = Union[T, int][int]
2155 self.assertEqual(repr(u), repr(int))
2156 u = Union[List[int], int]
2157 self.assertEqual(repr(u), 'typing.List[int] | int')
2158 u = Union[list[int], dict[str, float]]
2159 self.assertEqual(repr(u), 'list[int] | dict[str, float]')
2160 u = Union[int | float]
2161 self.assertEqual(repr(u), 'int | float')
2162
2163 u = Union[None, str]
2164 self.assertEqual(repr(u), 'None | str')
2165 u = Union[str, None]
2166 self.assertEqual(repr(u), 'str | None')
2167 u = Union[None, str, int]
2168 self.assertEqual(repr(u), 'None | str | int')
2169 u = Optional[str]
2170 self.assertEqual(repr(u), 'str | None')
2171
2172 def test_dir(self):
2173 dir_items = set(dir(Union[str, int]))
2174 for required_item in [
2175 '__args__', '__parameters__', '__origin__',
2176 ]:
2177 with self.subTest(required_item=required_item):
2178 self.assertIn(required_item, dir_items)
2179
2180 def test_cannot_subclass(self):
2181 with self.assertRaisesRegex(TypeError,
2182 r"type 'typing\.Union' is not an acceptable base type"):
2183 class C(Union):
2184 pass
2185 with self.assertRaisesRegex(TypeError,
2186 r'Cannot subclass int \| str'):
2187 class E(Union[int, str]):
2188 pass
2189
2190 def test_cannot_instantiate(self):
2191 with self.assertRaises(TypeError):
2192 Union()
2193 with self.assertRaises(TypeError):
2194 type(Union)()
2195 u = Union[int, float]
2196 with self.assertRaises(TypeError):
2197 u()
2198 with self.assertRaises(TypeError):
2199 type(u)()
2200
2201 def test_union_generalization(self):
2202 self.assertNotEqual(Union[str, typing.Iterable[int]], str)
2203 self.assertNotEqual(Union[str, typing.Iterable[int]], typing.Iterable[int])
2204 self.assertIn(str, Union[str, typing.Iterable[int]].__args__)
2205 self.assertIn(typing.Iterable[int], Union[str, typing.Iterable[int]].__args__)
2206
2207 def test_union_compare_other(self):
2208 self.assertNotEqual(Union, object)
2209 self.assertNotEqual(Union, Any)
2210 self.assertNotEqual(ClassVar, Union)
2211 self.assertNotEqual(Optional, Union)
2212 self.assertNotEqual([None], Optional)
2213 self.assertNotEqual(Optional, typing.Mapping)
2214 self.assertNotEqual(Optional[typing.MutableMapping], Union)
2215
2216 def test_optional(self):
2217 o = Optional[int]
2218 u = Union[int, None]
2219 self.assertEqual(o, u)
2220
2221 def test_empty(self):
2222 with self.assertRaises(TypeError):
2223 Union[()]
2224
2225 def test_no_eval_union(self):
2226 u = Union[int, str]
2227 def f(x: u): ...
2228 self.assertIs(get_type_hints(f)['x'], u)
2229
2230 def test_function_repr_union(self):
2231 def fun() -> int: ...
2232 self.assertEqual(repr(Union[fun, int]), f'{__name__}.{fun.__qualname__} | int')
2233
2234 def test_union_str_pattern(self):
2235 # Shouldn't crash; see http://bugs.python.org/issue25390
2236 A = Union[str, Pattern]
2237 A
2238
2239 def test_etree(self):
2240 # See https://github.com/python/typing/issues/229
2241 # (Only relevant for Python 2.)
2242 from xml.etree.ElementTree import Element
2243
2244 Union[Element, str] # Shouldn't crash
2245
2246 def Elem(*args):
2247 return Element(*args)
2248
2249 Union[Elem, str] # Nor should this
2250
2251 def test_union_of_literals(self):
2252 self.assertEqual(Union[Literal[1], Literal[2]].__args__,
2253 (Literal[1], Literal[2]))
2254 self.assertEqual(Union[Literal[1], Literal[1]],
2255 Literal[1])
2256
2257 self.assertEqual(Union[Literal[False], Literal[0]].__args__,
2258 (Literal[False], Literal[0]))
2259 self.assertEqual(Union[Literal[True], Literal[1]].__args__,
2260 (Literal[True], Literal[1]))
2261
2262 import enum
2263 class Ints(enum.IntEnum):
2264 A = 0
2265 B = 1
2266
2267 self.assertEqual(Union[Literal[Ints.A], Literal[Ints.A]],
2268 Literal[Ints.A])
2269 self.assertEqual(Union[Literal[Ints.B], Literal[Ints.B]],
2270 Literal[Ints.B])
2271
2272 self.assertEqual(Union[Literal[Ints.A], Literal[Ints.B]].__args__,
2273 (Literal[Ints.A], Literal[Ints.B]))
2274
2275 self.assertEqual(Union[Literal[0], Literal[Ints.A], Literal[False]].__args__,
2276 (Literal[0], Literal[Ints.A], Literal[False]))
2277 self.assertEqual(Union[Literal[1], Literal[Ints.B], Literal[True]].__args__,
2278 (Literal[1], Literal[Ints.B], Literal[True]))
2279
2280 def test_allow_non_types_in_or(self):
2281 # gh-140348: Test that using | with a Union object allows things that are
2282 # not allowed by is_unionable().
2283 U1 = Union[int, str]
2284 self.assertEqual(U1 | float, Union[int, str, float])
2285 self.assertEqual(U1 | "float", Union[int, str, "float"])
2286 self.assertEqual(float | U1, Union[float, int, str])
2287 self.assertEqual("float" | U1, Union["float", int, str])
2288
2289
2290 class TupleTests(BaseTestCase):
2291
2292 def test_basics(self):
2293 with self.assertRaises(TypeError):
2294 issubclass(Tuple, Tuple[int, str])
2295 with self.assertRaises(TypeError):
2296 issubclass(tuple, Tuple[int, str])
2297
2298 class TP(tuple): ...
2299 self.assertIsSubclass(tuple, Tuple)
2300 self.assertIsSubclass(TP, Tuple)
2301
2302 def test_equality(self):
2303 self.assertEqual(Tuple[int], Tuple[int])
2304 self.assertEqual(Tuple[int, ...], Tuple[int, ...])
2305 self.assertNotEqual(Tuple[int], Tuple[int, int])
2306 self.assertNotEqual(Tuple[int], Tuple[int, ...])
2307
2308 def test_tuple_subclass(self):
2309 class MyTuple(tuple):
2310 pass
2311 self.assertIsSubclass(MyTuple, Tuple)
2312 self.assertIsSubclass(Tuple, Tuple)
2313 self.assertIsSubclass(tuple, Tuple)
2314
2315 def test_tuple_instance_type_error(self):
2316 with self.assertRaises(TypeError):
2317 isinstance((0, 0), Tuple[int, int])
2318 self.assertIsInstance((0, 0), Tuple)
2319
2320 def test_repr(self):
2321 self.assertEqual(repr(Tuple), 'typing.Tuple')
2322 self.assertEqual(repr(Tuple[()]), 'typing.Tuple[()]')
2323 self.assertEqual(repr(Tuple[int, float]), 'typing.Tuple[int, float]')
2324 self.assertEqual(repr(Tuple[int, ...]), 'typing.Tuple[int, ...]')
2325 self.assertEqual(repr(Tuple[list[int]]), 'typing.Tuple[list[int]]')
2326
2327 def test_errors(self):
2328 with self.assertRaises(TypeError):
2329 issubclass(42, Tuple)
2330 with self.assertRaises(TypeError):
2331 issubclass(42, Tuple[int])
2332
2333
2334 class BaseCallableTests:
2335
2336 def test_self_subclass(self):
2337 Callable = self.Callable
2338 with self.assertRaises(TypeError):
2339 issubclass(types.FunctionType, Callable[[int], int])
2340 self.assertIsSubclass(types.FunctionType, Callable)
2341 self.assertIsSubclass(Callable, Callable)
2342
2343 def test_eq_hash(self):
2344 Callable = self.Callable
2345 C = Callable[[int], int]
2346 self.assertEqual(C, Callable[[int], int])
2347 self.assertEqual(len({C, Callable[[int], int]}), 1)
2348 self.assertNotEqual(C, Callable[[int], str])
2349 self.assertNotEqual(C, Callable[[str], int])
2350 self.assertNotEqual(C, Callable[[int, int], int])
2351 self.assertNotEqual(C, Callable[[], int])
2352 self.assertNotEqual(C, Callable[..., int])
2353 self.assertNotEqual(C, Callable)
2354
2355 def test_dir(self):
2356 Callable = self.Callable
2357 dir_items = set(dir(Callable[..., int]))
2358 for required_item in [
2359 '__args__', '__parameters__', '__origin__',
2360 ]:
2361 with self.subTest(required_item=required_item):
2362 self.assertIn(required_item, dir_items)
2363
2364 def test_cannot_instantiate(self):
2365 Callable = self.Callable
2366 with self.assertRaises(TypeError):
2367 Callable()
2368 with self.assertRaises(TypeError):
2369 type(Callable)()
2370 c = Callable[[int], str]
2371 with self.assertRaises(TypeError):
2372 c()
2373 with self.assertRaises(TypeError):
2374 type(c)()
2375
2376 def test_callable_wrong_forms(self):
2377 Callable = self.Callable
2378 with self.assertRaises(TypeError):
2379 Callable[int]
2380
2381 def test_callable_instance_works(self):
2382 Callable = self.Callable
2383 def f():
2384 pass
2385 self.assertIsInstance(f, Callable)
2386 self.assertNotIsInstance(None, Callable)
2387
2388 def test_callable_instance_type_error(self):
2389 Callable = self.Callable
2390 def f():
2391 pass
2392 with self.assertRaises(TypeError):
2393 isinstance(f, Callable[[], None])
2394 with self.assertRaises(TypeError):
2395 isinstance(f, Callable[[], Any])
2396 with self.assertRaises(TypeError):
2397 isinstance(None, Callable[[], None])
2398 with self.assertRaises(TypeError):
2399 isinstance(None, Callable[[], Any])
2400
2401 def test_repr(self):
2402 Callable = self.Callable
2403 fullname = f'{Callable.__module__}.Callable'
2404 ct0 = Callable[[], bool]
2405 self.assertEqual(repr(ct0), f'{fullname}[[], bool]')
2406 ct2 = Callable[[str, float], int]
2407 self.assertEqual(repr(ct2), f'{fullname}[[str, float], int]')
2408 ctv = Callable[..., str]
2409 self.assertEqual(repr(ctv), f'{fullname}[..., str]')
2410 ct3 = Callable[[str, float], list[int]]
2411 self.assertEqual(repr(ct3), f'{fullname}[[str, float], list[int]]')
2412
2413 def test_callable_with_ellipsis(self):
2414 Callable = self.Callable
2415 def foo(a: Callable[..., T]):
2416 pass
2417
2418 self.assertEqual(get_type_hints(foo, globals(), locals()),
2419 {'a': Callable[..., T]})
2420
2421 def test_ellipsis_in_generic(self):
2422 Callable = self.Callable
2423 # Shouldn't crash; see https://github.com/python/typing/issues/259
2424 typing.List[Callable[..., str]]
2425
2426 def test_or_and_ror(self):
2427 Callable = self.Callable
2428 self.assertEqual(Callable | Tuple, Union[Callable, Tuple])
2429 self.assertEqual(Tuple | Callable, Union[Tuple, Callable])
2430
2431 def test_basic(self):
2432 Callable = self.Callable
2433 alias = Callable[[int, str], float]
2434 if Callable is collections.abc.Callable:
2435 self.assertIsInstance(alias, types.GenericAlias)
2436 self.assertIs(alias.__origin__, collections.abc.Callable)
2437 self.assertEqual(alias.__args__, (int, str, float))
2438 self.assertEqual(alias.__parameters__, ())
2439
2440 def test_weakref(self):
2441 Callable = self.Callable
2442 alias = Callable[[int, str], float]
2443 self.assertEqual(weakref.ref(alias)(), alias)
2444
2445 def test_pickle(self):
2446 global T_pickle, P_pickle, TS_pickle # needed for pickling
2447 Callable = self.Callable
2448 T_pickle = TypeVar('T_pickle')
2449 P_pickle = ParamSpec('P_pickle')
2450 TS_pickle = TypeVarTuple('TS_pickle')
2451
2452 samples = [
2453 Callable[[int, str], float],
2454 Callable[P_pickle, int],
2455 Callable[P_pickle, T_pickle],
2456 Callable[Concatenate[int, P_pickle], int],
2457 Callable[Concatenate[*TS_pickle, P_pickle], int],
2458 ]
2459 for alias in samples:
2460 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2461 with self.subTest(alias=alias, proto=proto):
2462 s = pickle.dumps(alias, proto)
2463 loaded = pickle.loads(s)
2464 self.assertEqual(alias.__origin__, loaded.__origin__)
2465 self.assertEqual(alias.__args__, loaded.__args__)
2466 self.assertEqual(alias.__parameters__, loaded.__parameters__)
2467
2468 del T_pickle, P_pickle, TS_pickle # cleaning up global state
2469
2470 def test_var_substitution(self):
2471 Callable = self.Callable
2472 fullname = f"{Callable.__module__}.Callable"
2473 C1 = Callable[[int, T], T]
2474 C2 = Callable[[KT, T], VT]
2475 C3 = Callable[..., T]
2476 self.assertEqual(C1[str], Callable[[int, str], str])
2477 self.assertEqual(C1[None], Callable[[int, type(None)], type(None)])
2478 self.assertEqual(C2[int, float, str], Callable[[int, float], str])
2479 self.assertEqual(C3[int], Callable[..., int])
2480 self.assertEqual(C3[NoReturn], Callable[..., NoReturn])
2481
2482 # multi chaining
2483 C4 = C2[int, VT, str]
2484 self.assertEqual(repr(C4), f"{fullname}[[int, ~VT], str]")
2485 self.assertEqual(repr(C4[dict]), f"{fullname}[[int, dict], str]")
2486 self.assertEqual(C4[dict], Callable[[int, dict], str])
2487
2488 # substitute a nested GenericAlias (both typing and the builtin
2489 # version)
2490 C5 = Callable[[typing.List[T], tuple[KT, T], VT], int]
2491 self.assertEqual(C5[int, str, float],
2492 Callable[[typing.List[int], tuple[str, int], float], int])
2493
2494 def test_type_subst_error(self):
2495 Callable = self.Callable
2496 P = ParamSpec('P')
2497 T = TypeVar('T')
2498
2499 pat = "Expected a list of types, an ellipsis, ParamSpec, or Concatenate."
2500
2501 with self.assertRaisesRegex(TypeError, pat):
2502 Callable[P, T][0, int]
2503
2504 def test_type_erasure(self):
2505 Callable = self.Callable
2506 class C1(Callable):
2507 def __call__(self):
2508 return None
2509 a = C1[[int], T]
2510 self.assertIs(a().__class__, C1)
2511 self.assertEqual(a().__orig_class__, C1[[int], T])
2512
2513 def test_paramspec(self):
2514 Callable = self.Callable
2515 fullname = f"{Callable.__module__}.Callable"
2516 P = ParamSpec('P')
2517 P2 = ParamSpec('P2')
2518 C1 = Callable[P, T]
2519 # substitution
2520 self.assertEqual(C1[[int], str], Callable[[int], str])
2521 self.assertEqual(C1[[int, str], str], Callable[[int, str], str])
2522 self.assertEqual(C1[[], str], Callable[[], str])
2523 self.assertEqual(C1[..., str], Callable[..., str])
2524 self.assertEqual(C1[P2, str], Callable[P2, str])
2525 self.assertEqual(C1[Concatenate[int, P2], str],
2526 Callable[Concatenate[int, P2], str])
2527 self.assertEqual(repr(C1), f"{fullname}[~P, ~T]")
2528 self.assertEqual(repr(C1[[int, str], str]), f"{fullname}[[int, str], str]")
2529 with self.assertRaises(TypeError):
2530 C1[int, str]
2531
2532 C2 = Callable[P, int]
2533 self.assertEqual(C2[[int]], Callable[[int], int])
2534 self.assertEqual(C2[[int, str]], Callable[[int, str], int])
2535 self.assertEqual(C2[[]], Callable[[], int])
2536 self.assertEqual(C2[...], Callable[..., int])
2537 self.assertEqual(C2[P2], Callable[P2, int])
2538 self.assertEqual(C2[Concatenate[int, P2]],
2539 Callable[Concatenate[int, P2], int])
2540 # special case in PEP 612 where
2541 # X[int, str, float] == X[[int, str, float]]
2542 self.assertEqual(C2[int], Callable[[int], int])
2543 self.assertEqual(C2[int, str], Callable[[int, str], int])
2544 self.assertEqual(repr(C2), f"{fullname}[~P, int]")
2545 self.assertEqual(repr(C2[int, str]), f"{fullname}[[int, str], int]")
2546
2547 def test_concatenate(self):
2548 Callable = self.Callable
2549 fullname = f"{Callable.__module__}.Callable"
2550 T = TypeVar('T')
2551 P = ParamSpec('P')
2552 P2 = ParamSpec('P2')
2553 C = Callable[Concatenate[int, P], T]
2554 self.assertEqual(repr(C),
2555 f"{fullname}[typing.Concatenate[int, ~P], ~T]")
2556 self.assertEqual(C[P2, int], Callable[Concatenate[int, P2], int])
2557 self.assertEqual(C[[str, float], int], Callable[[int, str, float], int])
2558 self.assertEqual(C[[], int], Callable[[int], int])
2559 self.assertEqual(C[Concatenate[str, P2], int],
2560 Callable[Concatenate[int, str, P2], int])
2561 self.assertEqual(C[..., int], Callable[Concatenate[int, ...], int])
2562
2563 C = Callable[Concatenate[int, P], int]
2564 self.assertEqual(repr(C),
2565 f"{fullname}[typing.Concatenate[int, ~P], int]")
2566 self.assertEqual(C[P2], Callable[Concatenate[int, P2], int])
2567 self.assertEqual(C[[str, float]], Callable[[int, str, float], int])
2568 self.assertEqual(C[str, float], Callable[[int, str, float], int])
2569 self.assertEqual(C[[]], Callable[[int], int])
2570 self.assertEqual(C[Concatenate[str, P2]],
2571 Callable[Concatenate[int, str, P2], int])
2572 self.assertEqual(C[...], Callable[Concatenate[int, ...], int])
2573
2574 def test_nested_paramspec(self):
2575 # Since Callable has some special treatment, we want to be sure
2576 # that substitution works correctly, see gh-103054
2577 Callable = self.Callable
2578 P = ParamSpec('P')
2579 P2 = ParamSpec('P2')
2580 T = TypeVar('T')
2581 T2 = TypeVar('T2')
2582 Ts = TypeVarTuple('Ts')
2583 class My(Generic[P, T]):
2584 pass
2585
2586 self.assertEqual(My.__parameters__, (P, T))
2587
2588 C1 = My[[int, T2], Callable[P2, T2]]
2589 self.assertEqual(C1.__args__, ((int, T2), Callable[P2, T2]))
2590 self.assertEqual(C1.__parameters__, (T2, P2))
2591 self.assertEqual(C1[str, [list[int], bytes]],
2592 My[[int, str], Callable[[list[int], bytes], str]])
2593
2594 C2 = My[[Callable[[T2], int], list[T2]], str]
2595 self.assertEqual(C2.__args__, ((Callable[[T2], int], list[T2]), str))
2596 self.assertEqual(C2.__parameters__, (T2,))
2597 self.assertEqual(C2[list[str]],
2598 My[[Callable[[list[str]], int], list[list[str]]], str])
2599
2600 C3 = My[[Callable[P2, T2], T2], T2]
2601 self.assertEqual(C3.__args__, ((Callable[P2, T2], T2), T2))
2602 self.assertEqual(C3.__parameters__, (P2, T2))
2603 self.assertEqual(C3[[], int],
2604 My[[Callable[[], int], int], int])
2605 self.assertEqual(C3[[str, bool], int],
2606 My[[Callable[[str, bool], int], int], int])
2607 self.assertEqual(C3[[str, bool], T][int],
2608 My[[Callable[[str, bool], int], int], int])
2609
2610 C4 = My[[Callable[[int, *Ts, str], T2], T2], T2]
2611 self.assertEqual(C4.__args__, ((Callable[[int, *Ts, str], T2], T2), T2))
2612 self.assertEqual(C4.__parameters__, (Ts, T2))
2613 self.assertEqual(C4[bool, bytes, float],
2614 My[[Callable[[int, bool, bytes, str], float], float], float])
2615
2616 def test_errors(self):
2617 Callable = self.Callable
2618 alias = Callable[[int, str], float]
2619 with self.assertRaisesRegex(TypeError, "is not a generic class"):
2620 alias[int]
2621 P = ParamSpec('P')
2622 C1 = Callable[P, T]
2623 with self.assertRaisesRegex(TypeError, "many arguments for"):
2624 C1[int, str, str]
2625 with self.assertRaisesRegex(TypeError, "few arguments for"):
2626 C1[int]
2627
2628
2629 class TypingCallableTests(BaseCallableTests, BaseTestCase):
2630 Callable = typing.Callable
2631
2632 def test_consistency(self):
2633 # bpo-42195
2634 # Testing collections.abc.Callable's consistency with typing.Callable
2635 c1 = typing.Callable[[int, str], dict]
2636 c2 = collections.abc.Callable[[int, str], dict]
2637 self.assertEqual(c1.__args__, c2.__args__)
2638 self.assertEqual(hash(c1.__args__), hash(c2.__args__))
2639
2640
2641 class CollectionsCallableTests(BaseCallableTests, BaseTestCase):
2642 Callable = collections.abc.Callable
2643
2644
2645 class LiteralTests(BaseTestCase):
2646 def test_basics(self):
2647 # All of these are allowed.
2648 Literal[1]
2649 Literal[1, 2, 3]
2650 Literal["x", "y", "z"]
2651 Literal[None]
2652 Literal[True]
2653 Literal[1, "2", False]
2654 Literal[Literal[1, 2], Literal[4, 5]]
2655 Literal[b"foo", u"bar"]
2656
2657 def test_enum(self):
2658 import enum
2659 class My(enum.Enum):
2660 A = 'A'
2661
2662 self.assertEqual(Literal[My.A].__args__, (My.A,))
2663
2664 def test_illegal_parameters_do_not_raise_runtime_errors(self):
2665 # Type checkers should reject these types, but we do not
2666 # raise errors at runtime to maintain maximum flexibility.
2667 Literal[int]
2668 Literal[3j + 2, ..., ()]
2669 Literal[{"foo": 3, "bar": 4}]
2670 Literal[T]
2671
2672 def test_literals_inside_other_types(self):
2673 List[Literal[1, 2, 3]]
2674 List[Literal[("foo", "bar", "baz")]]
2675
2676 def test_repr(self):
2677 self.assertEqual(repr(Literal[1]), "typing.Literal[1]")
2678 self.assertEqual(repr(Literal[1, True, "foo"]), "typing.Literal[1, True, 'foo']")
2679 self.assertEqual(repr(Literal[int]), "typing.Literal[int]")
2680 self.assertEqual(repr(Literal), "typing.Literal")
2681 self.assertEqual(repr(Literal[None]), "typing.Literal[None]")
2682 self.assertEqual(repr(Literal[1, 2, 3, 3]), "typing.Literal[1, 2, 3]")
2683
2684 def test_dir(self):
2685 dir_items = set(dir(Literal[1, 2, 3]))
2686 for required_item in [
2687 '__args__', '__parameters__', '__origin__',
2688 ]:
2689 with self.subTest(required_item=required_item):
2690 self.assertIn(required_item, dir_items)
2691
2692 def test_cannot_init(self):
2693 with self.assertRaises(TypeError):
2694 Literal()
2695 with self.assertRaises(TypeError):
2696 Literal[1]()
2697 with self.assertRaises(TypeError):
2698 type(Literal)()
2699 with self.assertRaises(TypeError):
2700 type(Literal[1])()
2701
2702 def test_no_isinstance_or_issubclass(self):
2703 with self.assertRaises(TypeError):
2704 isinstance(1, Literal[1])
2705 with self.assertRaises(TypeError):
2706 isinstance(int, Literal[1])
2707 with self.assertRaises(TypeError):
2708 issubclass(1, Literal[1])
2709 with self.assertRaises(TypeError):
2710 issubclass(int, Literal[1])
2711
2712 def test_no_subclassing(self):
2713 with self.assertRaises(TypeError):
2714 class Foo(Literal[1]): pass
2715 with self.assertRaises(TypeError):
2716 class Bar(Literal): pass
2717
2718 def test_no_multiple_subscripts(self):
2719 with self.assertRaises(TypeError):
2720 Literal[1][1]
2721
2722 def test_equal(self):
2723 self.assertNotEqual(Literal[0], Literal[False])
2724 self.assertNotEqual(Literal[True], Literal[1])
2725 self.assertNotEqual(Literal[1], Literal[2])
2726 self.assertNotEqual(Literal[1, True], Literal[1])
2727 self.assertNotEqual(Literal[1, True], Literal[1, 1])
2728 self.assertNotEqual(Literal[1, 2], Literal[True, 2])
2729 self.assertEqual(Literal[1], Literal[1])
2730 self.assertEqual(Literal[1, 2], Literal[2, 1])
2731 self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])
2732
2733 def test_hash(self):
2734 self.assertEqual(hash(Literal[1]), hash(Literal[1]))
2735 self.assertEqual(hash(Literal[1, 2]), hash(Literal[2, 1]))
2736 self.assertEqual(hash(Literal[1, 2, 3]), hash(Literal[1, 2, 3, 3]))
2737
2738 def test_args(self):
2739 self.assertEqual(Literal[1, 2, 3].__args__, (1, 2, 3))
2740 self.assertEqual(Literal[1, 2, 3, 3].__args__, (1, 2, 3))
2741 self.assertEqual(Literal[1, Literal[2], Literal[3, 4]].__args__, (1, 2, 3, 4))
2742 # Mutable arguments will not be deduplicated
2743 self.assertEqual(Literal[[], []].__args__, ([], []))
2744
2745 def test_flatten(self):
2746 l1 = Literal[Literal[1], Literal[2], Literal[3]]
2747 l2 = Literal[Literal[1, 2], 3]
2748 l3 = Literal[Literal[1, 2, 3]]
2749 for l in l1, l2, l3:
2750 self.assertEqual(l, Literal[1, 2, 3])
2751 self.assertEqual(l.__args__, (1, 2, 3))
2752
2753 def test_does_not_flatten_enum(self):
2754 import enum
2755 class Ints(enum.IntEnum):
2756 A = 1
2757 B = 2
2758
2759 l = Literal[
2760 Literal[Ints.A],
2761 Literal[Ints.B],
2762 Literal[1],
2763 Literal[2],
2764 ]
2765 self.assertEqual(l.__args__, (Ints.A, Ints.B, 1, 2))
2766
2767
2768 XK = TypeVar('XK', str, bytes)
2769 XV = TypeVar('XV')
2770
2771
2772 class SimpleMapping(Generic[XK, XV]):
2773
2774 def __getitem__(self, key: XK) -> XV:
2775 ...
2776
2777 def __setitem__(self, key: XK, value: XV):
2778 ...
2779
2780 def get(self, key: XK, default: XV = None) -> XV:
2781 ...
2782
2783
2784 class MySimpleMapping(SimpleMapping[XK, XV]):
2785
2786 def __init__(self):
2787 self.store = {}
2788
2789 def __getitem__(self, key: str):
2790 return self.store[key]
2791
2792 def __setitem__(self, key: str, value):
2793 self.store[key] = value
2794
2795 def get(self, key: str, default=None):
2796 try:
2797 return self.store[key]
2798 except KeyError:
2799 return default
2800
2801
2802 class Coordinate(Protocol):
2803 x: int
2804 y: int
2805
2806
2807 @runtime_checkable
2808 class Point(Coordinate, Protocol):
2809 label: str
2810
2811 class MyPoint:
2812 x: int
2813 y: int
2814 label: str
2815
2816 class XAxis(Protocol):
2817 x: int
2818
2819 class YAxis(Protocol):
2820 y: int
2821
2822 @runtime_checkable
2823 class Position(XAxis, YAxis, Protocol):
2824 pass
2825
2826 @runtime_checkable
2827 class Proto(Protocol):
2828 attr: int
2829 def meth(self, arg: str) -> int:
2830 ...
2831
2832 class Concrete(Proto):
2833 pass
2834
2835 class Other:
2836 attr: int = 1
2837 def meth(self, arg: str) -> int:
2838 if arg == 'this':
2839 return 1
2840 return 0
2841
2842 class NT(NamedTuple):
2843 x: int
2844 y: int
2845
2846 @runtime_checkable
2847 class HasCallProtocol(Protocol):
2848 __call__: typing.Callable
2849
2850
2851 class ProtocolTests(BaseTestCase):
2852 def test_basic_protocol(self):
2853 @runtime_checkable
2854 class P(Protocol):
2855 def meth(self):
2856 pass
2857
2858 class C: pass
2859
2860 class D:
2861 def meth(self):
2862 pass
2863
2864 def f():
2865 pass
2866
2867 self.assertIsSubclass(D, P)
2868 self.assertIsInstance(D(), P)
2869 self.assertNotIsSubclass(C, P)
2870 self.assertNotIsInstance(C(), P)
2871 self.assertNotIsSubclass(types.FunctionType, P)
2872 self.assertNotIsInstance(f, P)
2873
2874 def test_runtime_checkable_generic_non_protocol(self):
2875 # Make sure this doesn't raise AttributeError
2876 with self.assertRaisesRegex(
2877 TypeError,
2878 "@runtime_checkable can be only applied to protocol classes",
2879 ):
2880 @runtime_checkable
2881 class Foo[T]: ...
2882
2883 def test_runtime_checkable_generic(self):
2884 @runtime_checkable
2885 class Foo[T](Protocol):
2886 def meth(self) -> T: ...
2887
2888 class Impl:
2889 def meth(self) -> int: ...
2890
2891 self.assertIsSubclass(Impl, Foo)
2892
2893 class NotImpl:
2894 def method(self) -> int: ...
2895
2896 self.assertNotIsSubclass(NotImpl, Foo)
2897
2898 def test_pep695_generics_can_be_runtime_checkable(self):
2899 @runtime_checkable
2900 class HasX(Protocol):
2901 x: int
2902
2903 class Bar[T]:
2904 x: T
2905 def __init__(self, x):
2906 self.x = x
2907
2908 class Capybara[T]:
2909 y: str
2910 def __init__(self, y):
2911 self.y = y
2912
2913 self.assertIsInstance(Bar(1), HasX)
2914 self.assertNotIsInstance(Capybara('a'), HasX)
2915
2916 def test_everything_implements_empty_protocol(self):
2917 @runtime_checkable
2918 class Empty(Protocol):
2919 pass
2920
2921 class C:
2922 pass
2923
2924 def f():
2925 pass
2926
2927 for thing in (object, type, tuple, C, types.FunctionType):
2928 self.assertIsSubclass(thing, Empty)
2929 for thing in (object(), 1, (), typing, f):
2930 self.assertIsInstance(thing, Empty)
2931
2932 def test_function_implements_protocol(self):
2933 def f():
2934 pass
2935
2936 self.assertIsInstance(f, HasCallProtocol)
2937
2938 def test_no_inheritance_from_nominal(self):
2939 class C: pass
2940
2941 class BP(Protocol): pass
2942
2943 with self.assertRaises(TypeError):
2944 class P(C, Protocol):
2945 pass
2946 with self.assertRaises(TypeError):
2947 class Q(Protocol, C):
2948 pass
2949 with self.assertRaises(TypeError):
2950 class R(BP, C, Protocol):
2951 pass
2952
2953 class D(BP, C): pass
2954
2955 class E(C, BP): pass
2956
2957 self.assertNotIsInstance(D(), E)
2958 self.assertNotIsInstance(E(), D)
2959
2960 def test_inheritance_from_object(self):
2961 # Inheritance from object is specifically allowed, unlike other nominal classes
2962 class P(Protocol, object):
2963 x: int
2964
2965 self.assertEqual(typing.get_protocol_members(P), {'x'})
2966
2967 class OldGeneric(Protocol, Generic[T], object):
2968 y: T
2969
2970 self.assertEqual(typing.get_protocol_members(OldGeneric), {'y'})
2971
2972 class NewGeneric[T](Protocol, object):
2973 z: T
2974
2975 self.assertEqual(typing.get_protocol_members(NewGeneric), {'z'})
2976
2977 def test_no_instantiation(self):
2978 class P(Protocol): pass
2979
2980 with self.assertRaises(TypeError):
2981 P()
2982
2983 class C(P): pass
2984
2985 self.assertIsInstance(C(), C)
2986 with self.assertRaises(TypeError):
2987 C(42)
2988
2989 T = TypeVar('T')
2990
2991 class PG(Protocol[T]): pass
2992
2993 with self.assertRaises(TypeError):
2994 PG()
2995 with self.assertRaises(TypeError):
2996 PG[int]()
2997 with self.assertRaises(TypeError):
2998 PG[T]()
2999
3000 class CG(PG[T]): pass
3001
3002 self.assertIsInstance(CG[int](), CG)
3003 with self.assertRaises(TypeError):
3004 CG[int](42)
3005
3006 def test_protocol_defining_init_does_not_get_overridden(self):
3007 # check that P.__init__ doesn't get clobbered
3008 # see https://bugs.python.org/issue44807
3009
3010 class P(Protocol):
3011 x: int
3012 def __init__(self, x: int) -> None:
3013 self.x = x
3014 class C: pass
3015
3016 c = C()
3017 P.__init__(c, 1)
3018 self.assertEqual(c.x, 1)
3019
3020 def test_concrete_class_inheriting_init_from_protocol(self):
3021 class P(Protocol):
3022 x: int
3023 def __init__(self, x: int) -> None:
3024 self.x = x
3025
3026 class C(P): pass
3027
3028 c = C(1)
3029 self.assertIsInstance(c, C)
3030 self.assertEqual(c.x, 1)
3031
3032 def test_cannot_instantiate_abstract(self):
3033 @runtime_checkable
3034 class P(Protocol):
3035 @abc.abstractmethod
3036 def ameth(self) -> int:
3037 raise NotImplementedError
3038
3039 class B(P):
3040 pass
3041
3042 class C(B):
3043 def ameth(self) -> int:
3044 return 26
3045
3046 with self.assertRaises(TypeError):
3047 B()
3048 self.assertIsInstance(C(), P)
3049
3050 def test_subprotocols_extending(self):
3051 class P1(Protocol):
3052 def meth1(self):
3053 pass
3054
3055 @runtime_checkable
3056 class P2(P1, Protocol):
3057 def meth2(self):
3058 pass
3059
3060 class C:
3061 def meth1(self):
3062 pass
3063
3064 def meth2(self):
3065 pass
3066
3067 class C1:
3068 def meth1(self):
3069 pass
3070
3071 class C2:
3072 def meth2(self):
3073 pass
3074
3075 self.assertNotIsInstance(C1(), P2)
3076 self.assertNotIsInstance(C2(), P2)
3077 self.assertNotIsSubclass(C1, P2)
3078 self.assertNotIsSubclass(C2, P2)
3079 self.assertIsInstance(C(), P2)
3080 self.assertIsSubclass(C, P2)
3081
3082 def test_subprotocols_merging(self):
3083 class P1(Protocol):
3084 def meth1(self):
3085 pass
3086
3087 class P2(Protocol):
3088 def meth2(self):
3089 pass
3090
3091 @runtime_checkable
3092 class P(P1, P2, Protocol):
3093 pass
3094
3095 class C:
3096 def meth1(self):
3097 pass
3098
3099 def meth2(self):
3100 pass
3101
3102 class C1:
3103 def meth1(self):
3104 pass
3105
3106 class C2:
3107 def meth2(self):
3108 pass
3109
3110 self.assertNotIsInstance(C1(), P)
3111 self.assertNotIsInstance(C2(), P)
3112 self.assertNotIsSubclass(C1, P)
3113 self.assertNotIsSubclass(C2, P)
3114 self.assertIsInstance(C(), P)
3115 self.assertIsSubclass(C, P)
3116
3117 def test_protocols_issubclass(self):
3118 T = TypeVar('T')
3119
3120 @runtime_checkable
3121 class P(Protocol):
3122 def x(self): ...
3123
3124 @runtime_checkable
3125 class PG(Protocol[T]):
3126 def x(self): ...
3127
3128 class BadP(Protocol):
3129 def x(self): ...
3130
3131 class BadPG(Protocol[T]):
3132 def x(self): ...
3133
3134 class C:
3135 def x(self): ...
3136
3137 self.assertIsSubclass(C, P)
3138 self.assertIsSubclass(C, PG)
3139 self.assertIsSubclass(BadP, PG)
3140
3141 no_subscripted_generics = (
3142 "Subscripted generics cannot be used with class and instance checks"
3143 )
3144
3145 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3146 issubclass(C, PG[T])
3147 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3148 issubclass(C, PG[C])
3149
3150 only_runtime_checkable_protocols = (
3151 "Instance and class checks can only be used with "
3152 "@runtime_checkable protocols"
3153 )
3154
3155 with self.assertRaisesRegex(TypeError, only_runtime_checkable_protocols):
3156 issubclass(C, BadP)
3157 with self.assertRaisesRegex(TypeError, only_runtime_checkable_protocols):
3158 issubclass(C, BadPG)
3159
3160 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3161 issubclass(P, PG[T])
3162 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3163 issubclass(PG, PG[int])
3164
3165 only_classes_allowed = r"issubclass\(\) arg 1 must be a class"
3166
3167 with self.assertRaisesRegex(TypeError, only_classes_allowed):
3168 issubclass(1, P)
3169 with self.assertRaisesRegex(TypeError, only_classes_allowed):
3170 issubclass(1, PG)
3171 with self.assertRaisesRegex(TypeError, only_classes_allowed):
3172 issubclass(1, BadP)
3173 with self.assertRaisesRegex(TypeError, only_classes_allowed):
3174 issubclass(1, BadPG)
3175
3176 def test_isinstance_against_superproto_doesnt_affect_subproto_instance(self):
3177 @runtime_checkable
3178 class Base(Protocol):
3179 x: int
3180
3181 @runtime_checkable
3182 class Child(Base, Protocol):
3183 y: str
3184
3185 class Capybara:
3186 x = 43
3187
3188 self.assertIsInstance(Capybara(), Base)
3189 self.assertNotIsInstance(Capybara(), Child)
3190
3191 def test_implicit_issubclass_between_two_protocols(self):
3192 @runtime_checkable
3193 class CallableMembersProto(Protocol):
3194 def meth(self): ...
3195
3196 # All the below protocols should be considered "subclasses"
3197 # of CallableMembersProto at runtime,
3198 # even though none of them explicitly subclass CallableMembersProto
3199
3200 class IdenticalProto(Protocol):
3201 def meth(self): ...
3202
3203 class SupersetProto(Protocol):
3204 def meth(self): ...
3205 def meth2(self): ...
3206
3207 class NonCallableMembersProto(Protocol):
3208 meth: Callable[[], None]
3209
3210 class NonCallableMembersSupersetProto(Protocol):
3211 meth: Callable[[], None]
3212 meth2: Callable[[str, int], bool]
3213
3214 class MixedMembersProto1(Protocol):
3215 meth: Callable[[], None]
3216 def meth2(self): ...
3217
3218 class MixedMembersProto2(Protocol):
3219 def meth(self): ...
3220 meth2: Callable[[str, int], bool]
3221
3222 for proto in (
3223 IdenticalProto, SupersetProto, NonCallableMembersProto,
3224 NonCallableMembersSupersetProto, MixedMembersProto1, MixedMembersProto2
3225 ):
3226 with self.subTest(proto=proto.__name__):
3227 self.assertIsSubclass(proto, CallableMembersProto)
3228
3229 # These two shouldn't be considered subclasses of CallableMembersProto, however,
3230 # since they don't have the `meth` protocol member
3231
3232 class EmptyProtocol(Protocol): ...
3233 class UnrelatedProtocol(Protocol):
3234 def wut(self): ...
3235
3236 self.assertNotIsSubclass(EmptyProtocol, CallableMembersProto)
3237 self.assertNotIsSubclass(UnrelatedProtocol, CallableMembersProto)
3238
3239 # These aren't protocols at all (despite having annotations),
3240 # so they should only be considered subclasses of CallableMembersProto
3241 # if they *actually have an attribute* matching the `meth` member
3242 # (just having an annotation is insufficient)
3243
3244 class AnnotatedButNotAProtocol:
3245 meth: Callable[[], None]
3246
3247 class NotAProtocolButAnImplicitSubclass:
3248 def meth(self): pass
3249
3250 class NotAProtocolButAnImplicitSubclass2:
3251 meth: Callable[[], None]
3252 def meth(self): pass
3253
3254 class NotAProtocolButAnImplicitSubclass3:
3255 meth: Callable[[], None]
3256 meth2: Callable[[int, str], bool]
3257 def meth(self): pass
3258 def meth2(self, x, y): return True
3259
3260 self.assertNotIsSubclass(AnnotatedButNotAProtocol, CallableMembersProto)
3261 self.assertIsSubclass(NotAProtocolButAnImplicitSubclass, CallableMembersProto)
3262 self.assertIsSubclass(NotAProtocolButAnImplicitSubclass2, CallableMembersProto)
3263 self.assertIsSubclass(NotAProtocolButAnImplicitSubclass3, CallableMembersProto)
3264
3265 def test_isinstance_checks_not_at_whim_of_gc(self):
3266 self.addCleanup(gc.enable)
3267 gc.disable()
3268
3269 with self.assertRaisesRegex(
3270 TypeError,
3271 "Protocols can only inherit from other protocols"
3272 ):
3273 class Foo(collections.abc.Mapping, Protocol):
3274 pass
3275
3276 self.assertNotIsInstance([], collections.abc.Mapping)
3277
3278 def test_issubclass_and_isinstance_on_Protocol_itself(self):
3279 class C:
3280 def x(self): pass
3281
3282 self.assertNotIsSubclass(object, Protocol)
3283 self.assertNotIsInstance(object(), Protocol)
3284
3285 self.assertNotIsSubclass(str, Protocol)
3286 self.assertNotIsInstance('foo', Protocol)
3287
3288 self.assertNotIsSubclass(C, Protocol)
3289 self.assertNotIsInstance(C(), Protocol)
3290
3291 only_classes_allowed = r"issubclass\(\) arg 1 must be a class"
3292
3293 with self.assertRaisesRegex(TypeError, only_classes_allowed):
3294 issubclass(1, Protocol)
3295 with self.assertRaisesRegex(TypeError, only_classes_allowed):
3296 issubclass('foo', Protocol)
3297 with self.assertRaisesRegex(TypeError, only_classes_allowed):
3298 issubclass(C(), Protocol)
3299
3300 T = TypeVar('T')
3301
3302 @runtime_checkable
3303 class EmptyProtocol(Protocol): pass
3304
3305 @runtime_checkable
3306 class SupportsStartsWith(Protocol):
3307 def startswith(self, x: str) -> bool: ...
3308
3309 @runtime_checkable
3310 class SupportsX(Protocol[T]):
3311 def x(self): ...
3312
3313 for proto in EmptyProtocol, SupportsStartsWith, SupportsX:
3314 with self.subTest(proto=proto.__name__):
3315 self.assertIsSubclass(proto, Protocol)
3316
3317 # gh-105237 / PR #105239:
3318 # check that the presence of Protocol subclasses
3319 # where `issubclass(X, <subclass>)` evaluates to True
3320 # doesn't influence the result of `issubclass(X, Protocol)`
3321
3322 self.assertIsSubclass(object, EmptyProtocol)
3323 self.assertIsInstance(object(), EmptyProtocol)
3324 self.assertNotIsSubclass(object, Protocol)
3325 self.assertNotIsInstance(object(), Protocol)
3326
3327 self.assertIsSubclass(str, SupportsStartsWith)
3328 self.assertIsInstance('foo', SupportsStartsWith)
3329 self.assertNotIsSubclass(str, Protocol)
3330 self.assertNotIsInstance('foo', Protocol)
3331
3332 self.assertIsSubclass(C, SupportsX)
3333 self.assertIsInstance(C(), SupportsX)
3334 self.assertNotIsSubclass(C, Protocol)
3335 self.assertNotIsInstance(C(), Protocol)
3336
3337 def test_protocols_issubclass_non_callable(self):
3338 class C:
3339 x = 1
3340
3341 @runtime_checkable
3342 class PNonCall(Protocol):
3343 x = 1
3344
3345 non_callable_members_illegal = (
3346 "Protocols with non-method members don't support issubclass()"
3347 )
3348
3349 with self.assertRaisesRegex(TypeError, non_callable_members_illegal):
3350 issubclass(C, PNonCall)
3351
3352 self.assertIsInstance(C(), PNonCall)
3353 PNonCall.register(C)
3354
3355 with self.assertRaisesRegex(TypeError, non_callable_members_illegal):
3356 issubclass(C, PNonCall)
3357
3358 self.assertIsInstance(C(), PNonCall)
3359
3360 # check that non-protocol subclasses are not affected
3361 class D(PNonCall): ...
3362
3363 self.assertNotIsSubclass(C, D)
3364 self.assertNotIsInstance(C(), D)
3365 D.register(C)
3366 self.assertIsSubclass(C, D)
3367 self.assertIsInstance(C(), D)
3368
3369 with self.assertRaisesRegex(TypeError, non_callable_members_illegal):
3370 issubclass(D, PNonCall)
3371
3372 def test_no_weird_caching_with_issubclass_after_isinstance(self):
3373 @runtime_checkable
3374 class Spam(Protocol):
3375 x: int
3376
3377 class Eggs:
3378 def __init__(self) -> None:
3379 self.x = 42
3380
3381 self.assertIsInstance(Eggs(), Spam)
3382
3383 # gh-104555: If we didn't override ABCMeta.__subclasscheck__ in _ProtocolMeta,
3384 # TypeError wouldn't be raised here,
3385 # as the cached result of the isinstance() check immediately above
3386 # would mean the issubclass() call would short-circuit
3387 # before we got to the "raise TypeError" line
3388 with self.assertRaisesRegex(
3389 TypeError,
3390 "Protocols with non-method members don't support issubclass()"
3391 ):
3392 issubclass(Eggs, Spam)
3393
3394 def test_no_weird_caching_with_issubclass_after_isinstance_2(self):
3395 @runtime_checkable
3396 class Spam(Protocol):
3397 x: int
3398
3399 class Eggs: ...
3400
3401 self.assertNotIsInstance(Eggs(), Spam)
3402
3403 # gh-104555: If we didn't override ABCMeta.__subclasscheck__ in _ProtocolMeta,
3404 # TypeError wouldn't be raised here,
3405 # as the cached result of the isinstance() check immediately above
3406 # would mean the issubclass() call would short-circuit
3407 # before we got to the "raise TypeError" line
3408 with self.assertRaisesRegex(
3409 TypeError,
3410 "Protocols with non-method members don't support issubclass()"
3411 ):
3412 issubclass(Eggs, Spam)
3413
3414 def test_no_weird_caching_with_issubclass_after_isinstance_3(self):
3415 @runtime_checkable
3416 class Spam(Protocol):
3417 x: int
3418
3419 class Eggs:
3420 def __getattr__(self, attr):
3421 if attr == "x":
3422 return 42
3423 raise AttributeError(attr)
3424
3425 self.assertNotIsInstance(Eggs(), Spam)
3426
3427 # gh-104555: If we didn't override ABCMeta.__subclasscheck__ in _ProtocolMeta,
3428 # TypeError wouldn't be raised here,
3429 # as the cached result of the isinstance() check immediately above
3430 # would mean the issubclass() call would short-circuit
3431 # before we got to the "raise TypeError" line
3432 with self.assertRaisesRegex(
3433 TypeError,
3434 "Protocols with non-method members don't support issubclass()"
3435 ):
3436 issubclass(Eggs, Spam)
3437
3438 def test_no_weird_caching_with_issubclass_after_isinstance_pep695(self):
3439 @runtime_checkable
3440 class Spam[T](Protocol):
3441 x: T
3442
3443 class Eggs[T]:
3444 def __init__(self, x: T) -> None:
3445 self.x = x
3446
3447 self.assertIsInstance(Eggs(42), Spam)
3448
3449 # gh-104555: If we didn't override ABCMeta.__subclasscheck__ in _ProtocolMeta,
3450 # TypeError wouldn't be raised here,
3451 # as the cached result of the isinstance() check immediately above
3452 # would mean the issubclass() call would short-circuit
3453 # before we got to the "raise TypeError" line
3454 with self.assertRaisesRegex(
3455 TypeError,
3456 "Protocols with non-method members don't support issubclass()"
3457 ):
3458 issubclass(Eggs, Spam)
3459
3460 def test_protocols_isinstance(self):
3461 T = TypeVar('T')
3462
3463 @runtime_checkable
3464 class P(Protocol):
3465 def meth(x): ...
3466
3467 @runtime_checkable
3468 class PG(Protocol[T]):
3469 def meth(x): ...
3470
3471 @runtime_checkable
3472 class WeirdProto(Protocol):
3473 meth = str.maketrans
3474
3475 @runtime_checkable
3476 class WeirdProto2(Protocol):
3477 meth = lambda *args, **kwargs: None
3478
3479 class CustomCallable:
3480 def __call__(self, *args, **kwargs):
3481 pass
3482
3483 @runtime_checkable
3484 class WeirderProto(Protocol):
3485 meth = CustomCallable()
3486
3487 class BadP(Protocol):
3488 def meth(x): ...
3489
3490 class BadPG(Protocol[T]):
3491 def meth(x): ...
3492
3493 class C:
3494 def meth(x): ...
3495
3496 class C2:
3497 def __init__(self):
3498 self.meth = lambda: None
3499
3500 for klass in C, C2:
3501 for proto in P, PG, WeirdProto, WeirdProto2, WeirderProto:
3502 with self.subTest(klass=klass.__name__, proto=proto.__name__):
3503 self.assertIsInstance(klass(), proto)
3504
3505 no_subscripted_generics = "Subscripted generics cannot be used with class and instance checks"
3506
3507 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3508 isinstance(C(), PG[T])
3509 with self.assertRaisesRegex(TypeError, no_subscripted_generics):
3510 isinstance(C(), PG[C])
3511
3512 only_runtime_checkable_msg = (
3513 "Instance and class checks can only be used "
3514 "with @runtime_checkable protocols"
3515 )
3516
3517 with self.assertRaisesRegex(TypeError, only_runtime_checkable_msg):
3518 isinstance(C(), BadP)
3519 with self.assertRaisesRegex(TypeError, only_runtime_checkable_msg):
3520 isinstance(C(), BadPG)
3521
3522 def test_protocols_isinstance_properties_and_descriptors(self):
3523 class C:
3524 @property
3525 def attr(self):
3526 return 42
3527
3528 class CustomDescriptor:
3529 def __get__(self, obj, objtype=None):
3530 return 42
3531
3532 class D:
3533 attr = CustomDescriptor()
3534
3535 # Check that properties set on superclasses
3536 # are still found by the isinstance() logic
3537 class E(C): ...
3538 class F(D): ...
3539
3540 class Empty: ...
3541
3542 T = TypeVar('T')
3543
3544 @runtime_checkable
3545 class P(Protocol):
3546 @property
3547 def attr(self): ...
3548
3549 @runtime_checkable
3550 class P1(Protocol):
3551 attr: int
3552
3553 @runtime_checkable
3554 class PG(Protocol[T]):
3555 @property
3556 def attr(self): ...
3557
3558 @runtime_checkable
3559 class PG1(Protocol[T]):
3560 attr: T
3561
3562 @runtime_checkable
3563 class MethodP(Protocol):
3564 def attr(self): ...
3565
3566 @runtime_checkable
3567 class MethodPG(Protocol[T]):
3568 def attr(self) -> T: ...
3569
3570 for protocol_class in P, P1, PG, PG1, MethodP, MethodPG:
3571 for klass in C, D, E, F:
3572 with self.subTest(
3573 klass=klass.__name__,
3574 protocol_class=protocol_class.__name__
3575 ):
3576 self.assertIsInstance(klass(), protocol_class)
3577
3578 with self.subTest(klass="Empty", protocol_class=protocol_class.__name__):
3579 self.assertNotIsInstance(Empty(), protocol_class)
3580
3581 class BadP(Protocol):
3582 @property
3583 def attr(self): ...
3584
3585 class BadP1(Protocol):
3586 attr: int
3587
3588 class BadPG(Protocol[T]):
3589 @property
3590 def attr(self): ...
3591
3592 class BadPG1(Protocol[T]):
3593 attr: T
3594
3595 cases = (
3596 PG[T], PG[C], PG1[T], PG1[C], MethodPG[T],
3597 MethodPG[C], BadP, BadP1, BadPG, BadPG1
3598 )
3599
3600 for obj in cases:
3601 for klass in C, D, E, F, Empty:
3602 with self.subTest(klass=klass.__name__, obj=obj):
3603 with self.assertRaises(TypeError):
3604 isinstance(klass(), obj)
3605
3606 def test_protocols_isinstance_not_fooled_by_custom_dir(self):
3607 @runtime_checkable
3608 class HasX(Protocol):
3609 x: int
3610
3611 class CustomDirWithX:
3612 x = 10
3613 def __dir__(self):
3614 return []
3615
3616 class CustomDirWithoutX:
3617 def __dir__(self):
3618 return ["x"]
3619
3620 self.assertIsInstance(CustomDirWithX(), HasX)
3621 self.assertNotIsInstance(CustomDirWithoutX(), HasX)
3622
3623 def test_protocols_isinstance_attribute_access_with_side_effects(self):
3624 class C:
3625 @property
3626 def attr(self):
3627 raise AttributeError('no')
3628
3629 class CustomDescriptor:
3630 def __get__(self, obj, objtype=None):
3631 raise RuntimeError("NO")
3632
3633 class D:
3634 attr = CustomDescriptor()
3635
3636 # Check that properties set on superclasses
3637 # are still found by the isinstance() logic
3638 class E(C): ...
3639 class F(D): ...
3640
3641 class WhyWouldYouDoThis:
3642 def __getattr__(self, name):
3643 raise RuntimeError("wut")
3644
3645 T = TypeVar('T')
3646
3647 @runtime_checkable
3648 class P(Protocol):
3649 @property
3650 def attr(self): ...
3651
3652 @runtime_checkable
3653 class P1(Protocol):
3654 attr: int
3655
3656 @runtime_checkable
3657 class PG(Protocol[T]):
3658 @property
3659 def attr(self): ...
3660
3661 @runtime_checkable
3662 class PG1(Protocol[T]):
3663 attr: T
3664
3665 @runtime_checkable
3666 class MethodP(Protocol):
3667 def attr(self): ...
3668
3669 @runtime_checkable
3670 class MethodPG(Protocol[T]):
3671 def attr(self) -> T: ...
3672
3673 for protocol_class in P, P1, PG, PG1, MethodP, MethodPG:
3674 for klass in C, D, E, F:
3675 with self.subTest(
3676 klass=klass.__name__,
3677 protocol_class=protocol_class.__name__
3678 ):
3679 self.assertIsInstance(klass(), protocol_class)
3680
3681 with self.subTest(
3682 klass="WhyWouldYouDoThis",
3683 protocol_class=protocol_class.__name__
3684 ):
3685 self.assertNotIsInstance(WhyWouldYouDoThis(), protocol_class)
3686
3687 def test_protocols_isinstance___slots__(self):
3688 # As per the consensus in https://github.com/python/typing/issues/1367,
3689 # this is desirable behaviour
3690 @runtime_checkable
3691 class HasX(Protocol):
3692 x: int
3693
3694 class HasNothingButSlots:
3695 __slots__ = ("x",)
3696
3697 self.assertIsInstance(HasNothingButSlots(), HasX)
3698
3699 def test_protocols_isinstance_py36(self):
3700 class APoint:
3701 def __init__(self, x, y, label):
3702 self.x = x
3703 self.y = y
3704 self.label = label
3705
3706 class BPoint:
3707 label = 'B'
3708
3709 def __init__(self, x, y):
3710 self.x = x
3711 self.y = y
3712
3713 class C:
3714 def __init__(self, attr):
3715 self.attr = attr
3716
3717 def meth(self, arg):
3718 return 0
3719
3720 class Bad: pass
3721
3722 self.assertIsInstance(APoint(1, 2, 'A'), Point)
3723 self.assertIsInstance(BPoint(1, 2), Point)
3724 self.assertNotIsInstance(MyPoint(), Point)
3725 self.assertIsInstance(BPoint(1, 2), Position)
3726 self.assertIsInstance(Other(), Proto)
3727 self.assertIsInstance(Concrete(), Proto)
3728 self.assertIsInstance(C(42), Proto)
3729 self.assertNotIsInstance(Bad(), Proto)
3730 self.assertNotIsInstance(Bad(), Point)
3731 self.assertNotIsInstance(Bad(), Position)
3732 self.assertNotIsInstance(Bad(), Concrete)
3733 self.assertNotIsInstance(Other(), Concrete)
3734 self.assertIsInstance(NT(1, 2), Position)
3735
3736 def test_protocols_isinstance_init(self):
3737 T = TypeVar('T')
3738
3739 @runtime_checkable
3740 class P(Protocol):
3741 x = 1
3742
3743 @runtime_checkable
3744 class PG(Protocol[T]):
3745 x = 1
3746
3747 class C:
3748 def __init__(self, x):
3749 self.x = x
3750
3751 self.assertIsInstance(C(1), P)
3752 self.assertIsInstance(C(1), PG)
3753
3754 def test_protocols_isinstance_monkeypatching(self):
3755 @runtime_checkable
3756 class HasX(Protocol):
3757 x: int
3758
3759 class Foo: ...
3760
3761 f = Foo()
3762 self.assertNotIsInstance(f, HasX)
3763 f.x = 42
3764 self.assertIsInstance(f, HasX)
3765 del f.x
3766 self.assertNotIsInstance(f, HasX)
3767
3768 def test_protocol_checks_after_subscript(self):
3769 class P(Protocol[T]): pass
3770 class C(P[T]): pass
3771 class Other1: pass
3772 class Other2: pass
3773 CA = C[Any]
3774
3775 self.assertNotIsInstance(Other1(), C)
3776 self.assertNotIsSubclass(Other2, C)
3777
3778 class D1(C[Any]): pass
3779 class D2(C[Any]): pass
3780 CI = C[int]
3781
3782 self.assertIsInstance(D1(), C)
3783 self.assertIsSubclass(D2, C)
3784
3785 def test_protocols_support_register(self):
3786 @runtime_checkable
3787 class P(Protocol):
3788 x = 1
3789
3790 class PM(Protocol):
3791 def meth(self): pass
3792
3793 class D(PM): pass
3794
3795 class C: pass
3796
3797 D.register(C)
3798 P.register(C)
3799 self.assertIsInstance(C(), P)
3800 self.assertIsInstance(C(), D)
3801
3802 def test_none_on_non_callable_doesnt_block_implementation(self):
3803 @runtime_checkable
3804 class P(Protocol):
3805 x = 1
3806
3807 class A:
3808 x = 1
3809
3810 class B(A):
3811 x = None
3812
3813 class C:
3814 def __init__(self):
3815 self.x = None
3816
3817 self.assertIsInstance(B(), P)
3818 self.assertIsInstance(C(), P)
3819
3820 def test_none_on_callable_blocks_implementation(self):
3821 @runtime_checkable
3822 class P(Protocol):
3823 def x(self): ...
3824
3825 class A:
3826 def x(self): ...
3827
3828 class B(A):
3829 x = None
3830
3831 class C:
3832 def __init__(self):
3833 self.x = None
3834
3835 self.assertNotIsInstance(B(), P)
3836 self.assertNotIsInstance(C(), P)
3837
3838 def test_non_protocol_subclasses(self):
3839 class P(Protocol):
3840 x = 1
3841
3842 @runtime_checkable
3843 class PR(Protocol):
3844 def meth(self): pass
3845
3846 class NonP(P):
3847 x = 1
3848
3849 class NonPR(PR): pass
3850
3851 class C(metaclass=abc.ABCMeta):
3852 x = 1
3853
3854 class D(metaclass=abc.ABCMeta):
3855 def meth(self): pass
3856
3857 self.assertNotIsInstance(C(), NonP)
3858 self.assertNotIsInstance(D(), NonPR)
3859 self.assertNotIsSubclass(C, NonP)
3860 self.assertNotIsSubclass(D, NonPR)
3861 self.assertIsInstance(NonPR(), PR)
3862 self.assertIsSubclass(NonPR, PR)
3863
3864 self.assertNotIn("__protocol_attrs__", vars(NonP))
3865 self.assertNotIn("__protocol_attrs__", vars(NonPR))
3866 self.assertNotIn("__non_callable_proto_members__", vars(NonP))
3867 self.assertNotIn("__non_callable_proto_members__", vars(NonPR))
3868
3869 self.assertEqual(get_protocol_members(P), {"x"})
3870 self.assertEqual(get_protocol_members(PR), {"meth"})
3871
3872 # the returned object should be immutable,
3873 # and should be a different object to the original attribute
3874 # to prevent users from (accidentally or deliberately)
3875 # mutating the attribute on the original class
3876 self.assertIsInstance(get_protocol_members(P), frozenset)
3877 self.assertIsNot(get_protocol_members(P), P.__protocol_attrs__)
3878 self.assertIsInstance(get_protocol_members(PR), frozenset)
3879 self.assertIsNot(get_protocol_members(PR), P.__protocol_attrs__)
3880
3881 acceptable_extra_attrs = {
3882 '_is_protocol', '_is_runtime_protocol', '__parameters__',
3883 '__init__', '__annotations__', '__subclasshook__', '__annotate__',
3884 '__annotations_cache__', '__annotate_func__',
3885 }
3886 self.assertLessEqual(vars(NonP).keys(), vars(C).keys() | acceptable_extra_attrs)
3887 self.assertLessEqual(
3888 vars(NonPR).keys(), vars(D).keys() | acceptable_extra_attrs
3889 )
3890
3891 def test_custom_subclasshook(self):
3892 class P(Protocol):
3893 x = 1
3894
3895 class OKClass: pass
3896
3897 class BadClass:
3898 x = 1
3899
3900 class C(P):
3901 @classmethod
3902 def __subclasshook__(cls, other):
3903 return other.__name__.startswith("OK")
3904
3905 self.assertIsInstance(OKClass(), C)
3906 self.assertNotIsInstance(BadClass(), C)
3907 self.assertIsSubclass(OKClass, C)
3908 self.assertNotIsSubclass(BadClass, C)
3909
3910 def test_custom_subclasshook_2(self):
3911 @runtime_checkable
3912 class HasX(Protocol):
3913 # The presence of a non-callable member
3914 # would mean issubclass() checks would fail with TypeError
3915 # if it weren't for the custom `__subclasshook__` method
3916 x = 1
3917
3918 @classmethod
3919 def __subclasshook__(cls, other):
3920 return hasattr(other, 'x')
3921
3922 class Empty: pass
3923
3924 class ImplementsHasX:
3925 x = 1
3926
3927 self.assertIsInstance(ImplementsHasX(), HasX)
3928 self.assertNotIsInstance(Empty(), HasX)
3929 self.assertIsSubclass(ImplementsHasX, HasX)
3930 self.assertNotIsSubclass(Empty, HasX)
3931
3932 # isinstance() and issubclass() checks against this still raise TypeError,
3933 # despite the presence of the custom __subclasshook__ method,
3934 # as it's not decorated with @runtime_checkable
3935 class NotRuntimeCheckable(Protocol):
3936 @classmethod
3937 def __subclasshook__(cls, other):
3938 return hasattr(other, 'x')
3939
3940 must_be_runtime_checkable = (
3941 "Instance and class checks can only be used "
3942 "with @runtime_checkable protocols"
3943 )
3944
3945 with self.assertRaisesRegex(TypeError, must_be_runtime_checkable):
3946 issubclass(object, NotRuntimeCheckable)
3947 with self.assertRaisesRegex(TypeError, must_be_runtime_checkable):
3948 isinstance(object(), NotRuntimeCheckable)
3949
3950 def test_issubclass_fails_correctly(self):
3951 @runtime_checkable
3952 class NonCallableMembers(Protocol):
3953 x = 1
3954
3955 class NotRuntimeCheckable(Protocol):
3956 def callable_member(self) -> int: ...
3957
3958 @runtime_checkable
3959 class RuntimeCheckable(Protocol):
3960 def callable_member(self) -> int: ...
3961
3962 class C: pass
3963
3964 # These three all exercise different code paths,
3965 # but should result in the same error message:
3966 for protocol in NonCallableMembers, NotRuntimeCheckable, RuntimeCheckable:
3967 with self.subTest(proto_name=protocol.__name__):
3968 with self.assertRaisesRegex(
3969 TypeError, r"issubclass\(\) arg 1 must be a class"
3970 ):
3971 issubclass(C(), protocol)
3972
3973 def test_defining_generic_protocols(self):
3974 T = TypeVar('T')
3975 S = TypeVar('S')
3976
3977 @runtime_checkable
3978 class PR(Protocol[T, S]):
3979 def meth(self): pass
3980
3981 class P(PR[int, T], Protocol[T]):
3982 y = 1
3983
3984 with self.assertRaises(TypeError):
3985 PR[int]
3986 with self.assertRaises(TypeError):
3987 P[int, str]
3988
3989 class C(PR[int, T]): pass
3990
3991 self.assertIsInstance(C[str](), C)
3992
3993 def test_defining_generic_protocols_old_style(self):
3994 T = TypeVar('T')
3995 S = TypeVar('S')
3996
3997 @runtime_checkable
3998 class PR(Protocol, Generic[T, S]):
3999 def meth(self): pass
4000
4001 class P(PR[int, str], Protocol):
4002 y = 1
4003
4004 with self.assertRaises(TypeError):
4005 issubclass(PR[int, str], PR)
4006 self.assertIsSubclass(P, PR)
4007 with self.assertRaises(TypeError):
4008 PR[int]
4009
4010 class P1(Protocol, Generic[T]):
4011 def bar(self, x: T) -> str: ...
4012
4013 class P2(Generic[T], Protocol):
4014 def bar(self, x: T) -> str: ...
4015
4016 @runtime_checkable
4017 class PSub(P1[str], Protocol):
4018 x = 1
4019
4020 class Test:
4021 x = 1
4022
4023 def bar(self, x: str) -> str:
4024 return x
4025
4026 self.assertIsInstance(Test(), PSub)
4027
4028 def test_pep695_generic_protocol_callable_members(self):
4029 @runtime_checkable
4030 class Foo[T](Protocol):
4031 def meth(self, x: T) -> None: ...
4032
4033 class Bar[T]:
4034 def meth(self, x: T) -> None: ...
4035
4036 self.assertIsInstance(Bar(), Foo)
4037 self.assertIsSubclass(Bar, Foo)
4038
4039 @runtime_checkable
4040 class SupportsTrunc[T](Protocol):
4041 def __trunc__(self) -> T: ...
4042
4043 self.assertIsInstance(0.0, SupportsTrunc)
4044 self.assertIsSubclass(float, SupportsTrunc)
4045
4046 def test_init_called(self):
4047 T = TypeVar('T')
4048
4049 class P(Protocol[T]): pass
4050
4051 class C(P[T]):
4052 def __init__(self):
4053 self.test = 'OK'
4054
4055 self.assertEqual(C[int]().test, 'OK')
4056
4057 class B:
4058 def __init__(self):
4059 self.test = 'OK'
4060
4061 class D1(B, P[T]):
4062 pass
4063
4064 self.assertEqual(D1[int]().test, 'OK')
4065
4066 class D2(P[T], B):
4067 pass
4068
4069 self.assertEqual(D2[int]().test, 'OK')
4070
4071 def test_new_called(self):
4072 T = TypeVar('T')
4073
4074 class P(Protocol[T]): pass
4075
4076 class C(P[T]):
4077 def __new__(cls, *args):
4078 self = super().__new__(cls, *args)
4079 self.test = 'OK'
4080 return self
4081
4082 self.assertEqual(C[int]().test, 'OK')
4083 with self.assertRaises(TypeError):
4084 C[int](42)
4085 with self.assertRaises(TypeError):
4086 C[int](a=42)
4087
4088 def test_protocols_bad_subscripts(self):
4089 T = TypeVar('T')
4090 S = TypeVar('S')
4091 with self.assertRaises(TypeError):
4092 class P(Protocol[T, T]): pass
4093 with self.assertRaises(TypeError):
4094 class Q(Protocol[int]): pass
4095 with self.assertRaises(TypeError):
4096 class R(Protocol[T], Protocol[S]): pass
4097 with self.assertRaises(TypeError):
4098 class S(typing.Mapping[T, S], Protocol[T]): pass
4099
4100 def test_generic_protocols_repr(self):
4101 T = TypeVar('T')
4102 S = TypeVar('S')
4103
4104 class P(Protocol[T, S]): pass
4105
4106 self.assertEndsWith(repr(P[T, S]), 'P[~T, ~S]')
4107 self.assertEndsWith(repr(P[int, str]), 'P[int, str]')
4108
4109 def test_generic_protocols_eq(self):
4110 T = TypeVar('T')
4111 S = TypeVar('S')
4112
4113 class P(Protocol[T, S]): pass
4114
4115 self.assertEqual(P, P)
4116 self.assertEqual(P[int, T], P[int, T])
4117 self.assertEqual(P[T, T][Tuple[T, S]][int, str],
4118 P[Tuple[int, str], Tuple[int, str]])
4119
4120 def test_generic_protocols_special_from_generic(self):
4121 T = TypeVar('T')
4122
4123 class P(Protocol[T]): pass
4124
4125 self.assertEqual(P.__parameters__, (T,))
4126 self.assertEqual(P[int].__parameters__, ())
4127 self.assertEqual(P[int].__args__, (int,))
4128 self.assertIs(P[int].__origin__, P)
4129
4130 def test_generic_protocols_special_from_protocol(self):
4131 @runtime_checkable
4132 class PR(Protocol):
4133 x = 1
4134
4135 class P(Protocol):
4136 def meth(self):
4137 pass
4138
4139 T = TypeVar('T')
4140
4141 class PG(Protocol[T]):
4142 x = 1
4143
4144 def meth(self):
4145 pass
4146
4147 self.assertIs(P._is_protocol, True)
4148 self.assertIs(PR._is_protocol, True)
4149 self.assertIs(PG._is_protocol, True)
4150 self.assertIs(P._is_runtime_protocol, False)
4151 self.assertIs(PR._is_runtime_protocol, True)
4152 self.assertIs(PG[int]._is_protocol, True)
4153 self.assertEqual(typing._get_protocol_attrs(P), {'meth'})
4154 self.assertEqual(typing._get_protocol_attrs(PR), {'x'})
4155 self.assertEqual(frozenset(typing._get_protocol_attrs(PG)),
4156 frozenset({'x', 'meth'}))
4157
4158 def test_no_runtime_deco_on_nominal(self):
4159 with self.assertRaises(TypeError):
4160 @runtime_checkable
4161 class C: pass
4162
4163 class Proto(Protocol):
4164 x = 1
4165
4166 with self.assertRaises(TypeError):
4167 @runtime_checkable
4168 class Concrete(Proto):
4169 pass
4170
4171 def test_none_treated_correctly(self):
4172 @runtime_checkable
4173 class P(Protocol):
4174 x = None # type: int
4175
4176 class B(object): pass
4177
4178 self.assertNotIsInstance(B(), P)
4179
4180 class C:
4181 x = 1
4182
4183 class D:
4184 x = None
4185
4186 self.assertIsInstance(C(), P)
4187 self.assertIsInstance(D(), P)
4188
4189 class CI:
4190 def __init__(self):
4191 self.x = 1
4192
4193 class DI:
4194 def __init__(self):
4195 self.x = None
4196
4197 self.assertIsInstance(CI(), P)
4198 self.assertIsInstance(DI(), P)
4199
4200 def test_protocols_in_unions(self):
4201 class P(Protocol):
4202 x = None # type: int
4203
4204 Alias = typing.Union[typing.Iterable, P]
4205 Alias2 = typing.Union[P, typing.Iterable]
4206 self.assertEqual(Alias, Alias2)
4207
4208 def test_protocols_pickleable(self):
4209 global P, CP # pickle wants to reference the class by name
4210 T = TypeVar('T')
4211
4212 @runtime_checkable
4213 class P(Protocol[T]):
4214 x = 1
4215
4216 class CP(P[int]):
4217 pass
4218
4219 c = CP()
4220 c.foo = 42
4221 c.bar = 'abc'
4222 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
4223 z = pickle.dumps(c, proto)
4224 x = pickle.loads(z)
4225 self.assertEqual(x.foo, 42)
4226 self.assertEqual(x.bar, 'abc')
4227 self.assertEqual(x.x, 1)
4228 self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
4229 s = pickle.dumps(P, proto)
4230 D = pickle.loads(s)
4231
4232 class E:
4233 x = 1
4234
4235 self.assertIsInstance(E(), D)
4236
4237 def test_runtime_checkable_with_match_args(self):
4238 @runtime_checkable
4239 class P_regular(Protocol):
4240 x: int
4241 y: int
4242
4243 @runtime_checkable
4244 class P_match(Protocol):
4245 __match_args__ = ('x', 'y')
4246 x: int
4247 y: int
4248
4249 class Regular:
4250 def __init__(self, x: int, y: int):
4251 self.x = x
4252 self.y = y
4253
4254 class WithMatch:
4255 __match_args__ = ('x', 'y', 'z')
4256 def __init__(self, x: int, y: int, z: int):
4257 self.x = x
4258 self.y = y
4259 self.z = z
4260
4261 class Nope: ...
4262
4263 self.assertIsInstance(Regular(1, 2), P_regular)
4264 self.assertIsInstance(Regular(1, 2), P_match)
4265 self.assertIsInstance(WithMatch(1, 2, 3), P_regular)
4266 self.assertIsInstance(WithMatch(1, 2, 3), P_match)
4267 self.assertNotIsInstance(Nope(), P_regular)
4268 self.assertNotIsInstance(Nope(), P_match)
4269
4270 def test_supports_int(self):
4271 self.assertIsSubclass(int, typing.SupportsInt)
4272 self.assertNotIsSubclass(str, typing.SupportsInt)
4273
4274 def test_supports_float(self):
4275 self.assertIsSubclass(float, typing.SupportsFloat)
4276 self.assertNotIsSubclass(str, typing.SupportsFloat)
4277
4278 def test_supports_complex(self):
4279
4280 class C:
4281 def __complex__(self):
4282 return 0j
4283
4284 self.assertIsSubclass(complex, typing.SupportsComplex)
4285 self.assertIsSubclass(C, typing.SupportsComplex)
4286 self.assertNotIsSubclass(str, typing.SupportsComplex)
4287
4288 def test_supports_bytes(self):
4289
4290 class B:
4291 def __bytes__(self):
4292 return b''
4293
4294 self.assertIsSubclass(bytes, typing.SupportsBytes)
4295 self.assertIsSubclass(B, typing.SupportsBytes)
4296 self.assertNotIsSubclass(str, typing.SupportsBytes)
4297
4298 def test_supports_abs(self):
4299 self.assertIsSubclass(float, typing.SupportsAbs)
4300 self.assertIsSubclass(int, typing.SupportsAbs)
4301 self.assertNotIsSubclass(str, typing.SupportsAbs)
4302
4303 def test_supports_round(self):
4304 issubclass(float, typing.SupportsRound)
4305 self.assertIsSubclass(float, typing.SupportsRound)
4306 self.assertIsSubclass(int, typing.SupportsRound)
4307 self.assertNotIsSubclass(str, typing.SupportsRound)
4308
4309 def test_reversible(self):
4310 self.assertIsSubclass(list, typing.Reversible)
4311 self.assertNotIsSubclass(int, typing.Reversible)
4312
4313 def test_supports_index(self):
4314 self.assertIsSubclass(int, typing.SupportsIndex)
4315 self.assertNotIsSubclass(str, typing.SupportsIndex)
4316
4317 def test_bundled_protocol_instance_works(self):
4318 self.assertIsInstance(0, typing.SupportsAbs)
4319 class C1(typing.SupportsInt):
4320 def __int__(self) -> int:
4321 return 42
4322 class C2(C1):
4323 pass
4324 c = C2()
4325 self.assertIsInstance(c, C1)
4326
4327 def test_collections_protocols_allowed(self):
4328 @runtime_checkable
4329 class Custom(collections.abc.Iterable, Protocol):
4330 def close(self): ...
4331
4332 class A: pass
4333 class B:
4334 def __iter__(self):
4335 return []
4336 def close(self):
4337 return 0
4338
4339 self.assertIsSubclass(B, Custom)
4340 self.assertNotIsSubclass(A, Custom)
4341
4342 @runtime_checkable
4343 class ReleasableBuffer(collections.abc.Buffer, Protocol):
4344 def __release_buffer__(self, mv: memoryview) -> None: ...
4345
4346 class C: pass
4347 class D:
4348 def __buffer__(self, flags: int) -> memoryview:
4349 return memoryview(b'')
4350 def __release_buffer__(self, mv: memoryview) -> None:
4351 pass
4352
4353 self.assertIsSubclass(D, ReleasableBuffer)
4354 self.assertIsInstance(D(), ReleasableBuffer)
4355 self.assertNotIsSubclass(C, ReleasableBuffer)
4356 self.assertNotIsInstance(C(), ReleasableBuffer)
4357
4358 def test_io_reader_protocol_allowed(self):
4359 @runtime_checkable
4360 class CustomReader(io.Reader[bytes], Protocol):
4361 def close(self): ...
4362
4363 class A: pass
4364 class B:
4365 def read(self, sz=-1):
4366 return b""
4367 def close(self):
4368 pass
4369
4370 self.assertIsSubclass(B, CustomReader)
4371 self.assertIsInstance(B(), CustomReader)
4372 self.assertNotIsSubclass(A, CustomReader)
4373 self.assertNotIsInstance(A(), CustomReader)
4374
4375 def test_io_writer_protocol_allowed(self):
4376 @runtime_checkable
4377 class CustomWriter(io.Writer[bytes], Protocol):
4378 def close(self): ...
4379
4380 class A: pass
4381 class B:
4382 def write(self, b):
4383 pass
4384 def close(self):
4385 pass
4386
4387 self.assertIsSubclass(B, CustomWriter)
4388 self.assertIsInstance(B(), CustomWriter)
4389 self.assertNotIsSubclass(A, CustomWriter)
4390 self.assertNotIsInstance(A(), CustomWriter)
4391
4392 def test_builtin_protocol_allowlist(self):
4393 with self.assertRaises(TypeError):
4394 class CustomProtocol(TestCase, Protocol):
4395 pass
4396
4397 class CustomPathLikeProtocol(os.PathLike, Protocol):
4398 pass
4399
4400 class CustomContextManager(typing.ContextManager, Protocol):
4401 pass
4402
4403 class CustomAsyncIterator(typing.AsyncIterator, Protocol):
4404 pass
4405
4406 def test_non_runtime_protocol_isinstance_check(self):
4407 class P(Protocol):
4408 x: int
4409
4410 with self.assertRaisesRegex(TypeError, "@runtime_checkable"):
4411 isinstance(1, P)
4412
4413 def test_super_call_init(self):
4414 class P(Protocol):
4415 x: int
4416
4417 class Foo(P):
4418 def __init__(self):
4419 super().__init__()
4420
4421 Foo() # Previously triggered RecursionError
4422
4423 def test_get_protocol_members(self):
4424 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4425 get_protocol_members(object)
4426 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4427 get_protocol_members(object())
4428 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4429 get_protocol_members(Protocol)
4430 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4431 get_protocol_members(Generic)
4432
4433 class P(Protocol):
4434 a: int
4435 def b(self) -> str: ...
4436 @property
4437 def c(self) -> int: ...
4438
4439 self.assertEqual(get_protocol_members(P), {'a', 'b', 'c'})
4440 self.assertIsInstance(get_protocol_members(P), frozenset)
4441 self.assertIsNot(get_protocol_members(P), P.__protocol_attrs__)
4442
4443 class Concrete:
4444 a: int
4445 def b(self) -> str: return "capybara"
4446 @property
4447 def c(self) -> int: return 5
4448
4449 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4450 get_protocol_members(Concrete)
4451 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4452 get_protocol_members(Concrete())
4453
4454 class ConcreteInherit(P):
4455 a: int = 42
4456 def b(self) -> str: return "capybara"
4457 @property
4458 def c(self) -> int: return 5
4459
4460 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4461 get_protocol_members(ConcreteInherit)
4462 with self.assertRaisesRegex(TypeError, "not a Protocol"):
4463 get_protocol_members(ConcreteInherit())
4464
4465 def test_is_protocol(self):
4466 self.assertTrue(is_protocol(Proto))
4467 self.assertTrue(is_protocol(Point))
4468 self.assertFalse(is_protocol(Concrete))
4469 self.assertFalse(is_protocol(Concrete()))
4470 self.assertFalse(is_protocol(Generic))
4471 self.assertFalse(is_protocol(object))
4472
4473 # Protocol is not itself a protocol
4474 self.assertFalse(is_protocol(Protocol))
4475
4476 def test_interaction_with_isinstance_checks_on_superclasses_with_ABCMeta(self):
4477 # Ensure the cache is empty, or this test won't work correctly
4478 collections.abc.Sized._abc_registry_clear()
4479
4480 class Foo(collections.abc.Sized, Protocol): pass
4481
4482 # gh-105144: this previously raised TypeError
4483 # if a Protocol subclass of Sized had been created
4484 # before any isinstance() checks against Sized
4485 self.assertNotIsInstance(1, collections.abc.Sized)
4486
4487 def test_interaction_with_isinstance_checks_on_superclasses_with_ABCMeta_2(self):
4488 # Ensure the cache is empty, or this test won't work correctly
4489 collections.abc.Sized._abc_registry_clear()
4490
4491 class Foo(typing.Sized, Protocol): pass
4492
4493 # gh-105144: this previously raised TypeError
4494 # if a Protocol subclass of Sized had been created
4495 # before any isinstance() checks against Sized
4496 self.assertNotIsInstance(1, typing.Sized)
4497
4498 def test_empty_protocol_decorated_with_final(self):
4499 @final
4500 @runtime_checkable
4501 class EmptyProtocol(Protocol): ...
4502
4503 self.assertIsSubclass(object, EmptyProtocol)
4504 self.assertIsInstance(object(), EmptyProtocol)
4505
4506 def test_protocol_decorated_with_final_callable_members(self):
4507 @final
4508 @runtime_checkable
4509 class ProtocolWithMethod(Protocol):
4510 def startswith(self, string: str) -> bool: ...
4511
4512 self.assertIsSubclass(str, ProtocolWithMethod)
4513 self.assertNotIsSubclass(int, ProtocolWithMethod)
4514 self.assertIsInstance('foo', ProtocolWithMethod)
4515 self.assertNotIsInstance(42, ProtocolWithMethod)
4516
4517 def test_protocol_decorated_with_final_noncallable_members(self):
4518 @final
4519 @runtime_checkable
4520 class ProtocolWithNonCallableMember(Protocol):
4521 x: int
4522
4523 class Foo:
4524 x = 42
4525
4526 only_callable_members_please = (
4527 r"Protocols with non-method members don't support issubclass()"
4528 )
4529
4530 with self.assertRaisesRegex(TypeError, only_callable_members_please):
4531 issubclass(Foo, ProtocolWithNonCallableMember)
4532
4533 with self.assertRaisesRegex(TypeError, only_callable_members_please):
4534 issubclass(int, ProtocolWithNonCallableMember)
4535
4536 self.assertIsInstance(Foo(), ProtocolWithNonCallableMember)
4537 self.assertNotIsInstance(42, ProtocolWithNonCallableMember)
4538
4539 def test_protocol_decorated_with_final_mixed_members(self):
4540 @final
4541 @runtime_checkable
4542 class ProtocolWithMixedMembers(Protocol):
4543 x: int
4544 def method(self) -> None: ...
4545
4546 class Foo:
4547 x = 42
4548 def method(self) -> None: ...
4549
4550 only_callable_members_please = (
4551 r"Protocols with non-method members don't support issubclass()"
4552 )
4553
4554 with self.assertRaisesRegex(TypeError, only_callable_members_please):
4555 issubclass(Foo, ProtocolWithMixedMembers)
4556
4557 with self.assertRaisesRegex(TypeError, only_callable_members_please):
4558 issubclass(int, ProtocolWithMixedMembers)
4559
4560 self.assertIsInstance(Foo(), ProtocolWithMixedMembers)
4561 self.assertNotIsInstance(42, ProtocolWithMixedMembers)
4562
4563 def test_protocol_issubclass_error_message(self):
4564 @runtime_checkable
4565 class Vec2D(Protocol):
4566 x: float
4567 y: float
4568
4569 def square_norm(self) -> float:
4570 return self.x ** 2 + self.y ** 2
4571
4572 self.assertEqual(Vec2D.__protocol_attrs__, {'x', 'y', 'square_norm'})
4573 expected_error_message = (
4574 "Protocols with non-method members don't support issubclass()."
4575 " Non-method members: 'x', 'y'."
4576 )
4577 with self.assertRaisesRegex(TypeError, re.escape(expected_error_message)):
4578 issubclass(int, Vec2D)
4579
4580 def test_nonruntime_protocol_interaction_with_evil_classproperty(self):
4581 class classproperty:
4582 def __get__(self, instance, type):
4583 raise RuntimeError("NO")
4584
4585 class Commentable(Protocol):
4586 evil = classproperty()
4587
4588 # recognised as a protocol attr,
4589 # but not actually accessed by the protocol metaclass
4590 # (which would raise RuntimeError) for non-runtime protocols.
4591 # See gh-113320
4592 self.assertEqual(get_protocol_members(Commentable), {"evil"})
4593
4594 def test_runtime_protocol_interaction_with_evil_classproperty(self):
4595 class CustomError(Exception): pass
4596
4597 class classproperty:
4598 def __get__(self, instance, type):
4599 raise CustomError
4600
4601 with self.assertRaises(TypeError) as cm:
4602 @runtime_checkable
4603 class Commentable(Protocol):
4604 evil = classproperty()
4605
4606 exc = cm.exception
4607 self.assertEqual(
4608 exc.args[0],
4609 "Failed to determine whether protocol member 'evil' is a method member"
4610 )
4611 self.assertIs(type(exc.__cause__), CustomError)
4612
4613 def test_isinstance_with_deferred_evaluation_of_annotations(self):
4614 @runtime_checkable
4615 class P(Protocol):
4616 def meth(self):
4617 ...
4618
4619 class DeferredClass:
4620 x: undefined
4621
4622 class DeferredClassImplementingP:
4623 x: undefined | int
4624
4625 def __init__(self):
4626 self.x = 0
4627
4628 def meth(self):
4629 ...
4630
4631 # override meth with a non-method attribute to make it part of __annotations__ instead of __dict__
4632 class SubProtocol(P, Protocol):
4633 meth: undefined
4634
4635
4636 self.assertIsSubclass(SubProtocol, P)
4637 self.assertNotIsInstance(DeferredClass(), P)
4638 self.assertIsInstance(DeferredClassImplementingP(), P)
4639
4640 def test_deferred_evaluation_of_annotations(self):
4641 class DeferredProto(Protocol):
4642 x: DoesNotExist
4643 self.assertEqual(get_protocol_members(DeferredProto), {"x"})
4644 self.assertEqual(
4645 annotationlib.get_annotations(DeferredProto, format=annotationlib.Format.STRING),
4646 {'x': 'DoesNotExist'}
4647 )
4648
4649
4650 class GenericTests(BaseTestCase):
4651
4652 def test_basics(self):
4653 X = SimpleMapping[str, Any]
4654 self.assertEqual(X.__parameters__, ())
4655 with self.assertRaises(TypeError):
4656 X[str]
4657 with self.assertRaises(TypeError):
4658 X[str, str]
4659 Y = SimpleMapping[XK, str]
4660 self.assertEqual(Y.__parameters__, (XK,))
4661 Y[str]
4662 with self.assertRaises(TypeError):
4663 Y[str, str]
4664 SM1 = SimpleMapping[str, int]
4665 with self.assertRaises(TypeError):
4666 issubclass(SM1, SimpleMapping)
4667 self.assertIsInstance(SM1(), SimpleMapping)
4668 T = TypeVar("T")
4669 self.assertEqual(List[list[T] | float].__parameters__, (T,))
4670
4671 def test_generic_errors(self):
4672 T = TypeVar('T')
4673 S = TypeVar('S')
4674 with self.assertRaises(TypeError):
4675 Generic[T][T]
4676 with self.assertRaises(TypeError):
4677 Generic[T][S]
4678 with self.assertRaises(TypeError):
4679 class C(Generic[T], Generic[T]): ...
4680 with self.assertRaises(TypeError):
4681 isinstance([], List[int])
4682 with self.assertRaises(TypeError):
4683 issubclass(list, List[int])
4684 with self.assertRaises(TypeError):
4685 class NewGeneric(Generic): ...
4686 with self.assertRaises(TypeError):
4687 class MyGeneric(Generic[T], Generic[S]): ...
4688 with self.assertRaises(TypeError):
4689 class MyGeneric2(List[T], Generic[S]): ...
4690 with self.assertRaises(TypeError):
4691 Generic[()]
4692 class D(Generic[T]): pass
4693 with self.assertRaises(TypeError):
4694 D[()]
4695
4696 def test_generic_init_subclass_not_called_error(self):
4697 notes = ["Note: this exception may have been caused by "
4698 r"'GenericTests.test_generic_init_subclass_not_called_error.<locals>.Base.__init_subclass__' "
4699 "(or the '__init_subclass__' method on a superclass) not calling 'super().__init_subclass__()'"]
4700
4701 class Base:
4702 def __init_subclass__(cls) -> None:
4703 # Oops, I forgot super().__init_subclass__()!
4704 pass
4705
4706 with self.subTest():
4707 class Sub(Base, Generic[T]):
4708 pass
4709
4710 with self.assertRaises(AttributeError) as cm:
4711 Sub[int]
4712
4713 self.assertEqual(cm.exception.__notes__, notes)
4714
4715 with self.subTest():
4716 class Sub[U](Base):
4717 pass
4718
4719 with self.assertRaises(AttributeError) as cm:
4720 Sub[int]
4721
4722 self.assertEqual(cm.exception.__notes__, notes)
4723
4724 def test_generic_subclass_checks(self):
4725 for typ in [list[int], List[int],
4726 tuple[int, str], Tuple[int, str],
4727 typing.Callable[..., None],
4728 collections.abc.Callable[..., None]]:
4729 with self.subTest(typ=typ):
4730 self.assertRaises(TypeError, issubclass, typ, object)
4731 self.assertRaises(TypeError, issubclass, typ, type)
4732 self.assertRaises(TypeError, issubclass, typ, typ)
4733 self.assertRaises(TypeError, issubclass, object, typ)
4734
4735 # isinstance is fine:
4736 self.assertTrue(isinstance(typ, object))
4737 # but, not when the right arg is also a generic:
4738 self.assertRaises(TypeError, isinstance, typ, typ)
4739
4740 def test_init(self):
4741 T = TypeVar('T')
4742 S = TypeVar('S')
4743 with self.assertRaises(TypeError):
4744 Generic[T, T]
4745 with self.assertRaises(TypeError):
4746 Generic[T, S, T]
4747
4748 def test_init_subclass(self):
4749 class X(typing.Generic[T]):
4750 def __init_subclass__(cls, **kwargs):
4751 super().__init_subclass__(**kwargs)
4752 cls.attr = 42
4753 class Y(X):
4754 pass
4755 self.assertEqual(Y.attr, 42)
4756 with self.assertRaises(AttributeError):
4757 X.attr
4758 X.attr = 1
4759 Y.attr = 2
4760 class Z(Y):
4761 pass
4762 class W(X[int]):
4763 pass
4764 self.assertEqual(Y.attr, 2)
4765 self.assertEqual(Z.attr, 42)
4766 self.assertEqual(W.attr, 42)
4767
4768 def test_repr(self):
4769 self.assertEqual(repr(SimpleMapping),
4770 f"<class '{__name__}.SimpleMapping'>")
4771 self.assertEqual(repr(MySimpleMapping),
4772 f"<class '{__name__}.MySimpleMapping'>")
4773
4774 def test_chain_repr(self):
4775 T = TypeVar('T')
4776 S = TypeVar('S')
4777
4778 class C(Generic[T]):
4779 pass
4780
4781 X = C[Tuple[S, T]]
4782 self.assertEqual(X, C[Tuple[S, T]])
4783 self.assertNotEqual(X, C[Tuple[T, S]])
4784
4785 Y = X[T, int]
4786 self.assertEqual(Y, X[T, int])
4787 self.assertNotEqual(Y, X[S, int])
4788 self.assertNotEqual(Y, X[T, str])
4789
4790 Z = Y[str]
4791 self.assertEqual(Z, Y[str])
4792 self.assertNotEqual(Z, Y[int])
4793 self.assertNotEqual(Z, Y[T])
4794
4795 self.assertEndsWith(str(Z), '.C[typing.Tuple[str, int]]')
4796
4797 def test_new_repr(self):
4798 T = TypeVar('T')
4799 U = TypeVar('U', covariant=True)
4800 S = TypeVar('S')
4801
4802 self.assertEqual(repr(List), 'typing.List')
4803 self.assertEqual(repr(List[T]), 'typing.List[~T]')
4804 self.assertEqual(repr(List[U]), 'typing.List[+U]')
4805 self.assertEqual(repr(List[S][T][int]), 'typing.List[int]')
4806 self.assertEqual(repr(List[int]), 'typing.List[int]')
4807
4808 def test_new_repr_complex(self):
4809 T = TypeVar('T')
4810 TS = TypeVar('TS')
4811
4812 self.assertEqual(repr(typing.Mapping[T, TS][TS, T]), 'typing.Mapping[~TS, ~T]')
4813 self.assertEqual(repr(List[Tuple[T, TS]][int, T]),
4814 'typing.List[typing.Tuple[int, ~T]]')
4815 self.assertEqual(
4816 repr(List[Tuple[T, T]][List[int]]),
4817 'typing.List[typing.Tuple[typing.List[int], typing.List[int]]]'
4818 )
4819
4820 def test_new_repr_bare(self):
4821 T = TypeVar('T')
4822 self.assertEqual(repr(Generic[T]), 'typing.Generic[~T]')
4823 self.assertEqual(repr(typing.Protocol[T]), 'typing.Protocol[~T]')
4824 class C(typing.Dict[Any, Any]): ...
4825 # this line should just work
4826 repr(C.__mro__)
4827
4828 def test_dict(self):
4829 T = TypeVar('T')
4830
4831 class B(Generic[T]):
4832 pass
4833
4834 b = B()
4835 b.foo = 42
4836 self.assertEqual(b.__dict__, {'foo': 42})
4837
4838 class C(B[int]):
4839 pass
4840
4841 c = C()
4842 c.bar = 'abc'
4843 self.assertEqual(c.__dict__, {'bar': 'abc'})
4844
4845 def test_setattr_exceptions(self):
4846 class Immutable[T]:
4847 def __setattr__(self, key, value):
4848 raise RuntimeError("immutable")
4849
4850 # gh-115165: This used to cause RuntimeError to be raised
4851 # when we tried to set `__orig_class__` on the `Immutable` instance
4852 # returned by the `Immutable[int]()` call
4853 self.assertIsInstance(Immutable[int](), Immutable)
4854
4855 def test_subscripted_generics_as_proxies(self):
4856 T = TypeVar('T')
4857 class C(Generic[T]):
4858 x = 'def'
4859 self.assertEqual(C[int].x, 'def')
4860 self.assertEqual(C[C[int]].x, 'def')
4861 C[C[int]].x = 'changed'
4862 self.assertEqual(C.x, 'changed')
4863 self.assertEqual(C[str].x, 'changed')
4864 C[List[str]].z = 'new'
4865 self.assertEqual(C.z, 'new')
4866 self.assertEqual(C[Tuple[int]].z, 'new')
4867
4868 self.assertEqual(C().x, 'changed')
4869 self.assertEqual(C[Tuple[str]]().z, 'new')
4870
4871 class D(C[T]):
4872 pass
4873 self.assertEqual(D[int].x, 'changed')
4874 self.assertEqual(D.z, 'new')
4875 D.z = 'from derived z'
4876 D[int].x = 'from derived x'
4877 self.assertEqual(C.x, 'changed')
4878 self.assertEqual(C[int].z, 'new')
4879 self.assertEqual(D.x, 'from derived x')
4880 self.assertEqual(D[str].z, 'from derived z')
4881
4882 def test_abc_registry_kept(self):
4883 T = TypeVar('T')
4884 class C(collections.abc.Mapping, Generic[T]): ...
4885 C.register(int)
4886 self.assertIsInstance(1, C)
4887 C[int]
4888 self.assertIsInstance(1, C)
4889 C._abc_registry_clear()
4890 C._abc_caches_clear() # To keep refleak hunting mode clean
4891
4892 def test_false_subclasses(self):
4893 class MyMapping(MutableMapping[str, str]): pass
4894 self.assertNotIsInstance({}, MyMapping)
4895 self.assertNotIsSubclass(dict, MyMapping)
4896
4897 def test_abc_bases(self):
4898 class MM(MutableMapping[str, str]):
4899 def __getitem__(self, k):
4900 return None
4901 def __setitem__(self, k, v):
4902 pass
4903 def __delitem__(self, k):
4904 pass
4905 def __iter__(self):
4906 return iter(())
4907 def __len__(self):
4908 return 0
4909 # this should just work
4910 MM().update()
4911 self.assertIsInstance(MM(), collections.abc.MutableMapping)
4912 self.assertIsInstance(MM(), MutableMapping)
4913 self.assertNotIsInstance(MM(), List)
4914 self.assertNotIsInstance({}, MM)
4915
4916 def test_multiple_bases(self):
4917 class MM1(MutableMapping[str, str], collections.abc.MutableMapping):
4918 pass
4919 class MM2(collections.abc.MutableMapping, MutableMapping[str, str]):
4920 pass
4921 self.assertEqual(MM2.__bases__, (collections.abc.MutableMapping, Generic))
4922
4923 def test_orig_bases(self):
4924 T = TypeVar('T')
4925 class C(typing.Dict[str, T]): ...
4926 self.assertEqual(C.__orig_bases__, (typing.Dict[str, T],))
4927
4928 def test_naive_runtime_checks(self):
4929 def naive_dict_check(obj, tp):
4930 # Check if a dictionary conforms to Dict type
4931 if len(tp.__parameters__) > 0:
4932 raise NotImplementedError
4933 if tp.__args__:
4934 KT, VT = tp.__args__
4935 return all(
4936 isinstance(k, KT) and isinstance(v, VT)
4937 for k, v in obj.items()
4938 )
4939 self.assertTrue(naive_dict_check({'x': 1}, typing.Dict[str, int]))
4940 self.assertFalse(naive_dict_check({1: 'x'}, typing.Dict[str, int]))
4941 with self.assertRaises(NotImplementedError):
4942 naive_dict_check({1: 'x'}, typing.Dict[str, T])
4943
4944 def naive_generic_check(obj, tp):
4945 # Check if an instance conforms to the generic class
4946 if not hasattr(obj, '__orig_class__'):
4947 raise NotImplementedError
4948 return obj.__orig_class__ == tp
4949 class Node(Generic[T]): ...
4950 self.assertTrue(naive_generic_check(Node[int](), Node[int]))
4951 self.assertFalse(naive_generic_check(Node[str](), Node[int]))
4952 self.assertFalse(naive_generic_check(Node[str](), List))
4953 with self.assertRaises(NotImplementedError):
4954 naive_generic_check([1, 2, 3], Node[int])
4955
4956 def naive_list_base_check(obj, tp):
4957 # Check if list conforms to a List subclass
4958 return all(isinstance(x, tp.__orig_bases__[0].__args__[0])
4959 for x in obj)
4960 class C(List[int]): ...
4961 self.assertTrue(naive_list_base_check([1, 2, 3], C))
4962 self.assertFalse(naive_list_base_check(['a', 'b'], C))
4963
4964 def test_multi_subscr_base(self):
4965 T = TypeVar('T')
4966 U = TypeVar('U')
4967 V = TypeVar('V')
4968 class C(List[T][U][V]): ...
4969 class D(C, List[T][U][V]): ...
4970 self.assertEqual(C.__parameters__, (V,))
4971 self.assertEqual(D.__parameters__, (V,))
4972 self.assertEqual(C[int].__parameters__, ())
4973 self.assertEqual(D[int].__parameters__, ())
4974 self.assertEqual(C[int].__args__, (int,))
4975 self.assertEqual(D[int].__args__, (int,))
4976 self.assertEqual(C.__bases__, (list, Generic))
4977 self.assertEqual(D.__bases__, (C, list, Generic))
4978 self.assertEqual(C.__orig_bases__, (List[T][U][V],))
4979 self.assertEqual(D.__orig_bases__, (C, List[T][U][V]))
4980
4981 def test_subscript_meta(self):
4982 T = TypeVar('T')
4983 class Meta(type): ...
4984 self.assertEqual(Type[Meta], Type[Meta])
4985 self.assertEqual(Union[T, int][Meta], Union[Meta, int])
4986 self.assertEqual(Callable[..., Meta].__args__, (Ellipsis, Meta))
4987
4988 def test_generic_hashes(self):
4989 class A(Generic[T]):
4990 ...
4991
4992 class B(Generic[T]):
4993 class A(Generic[T]):
4994 ...
4995
4996 self.assertEqual(A, A)
4997 self.assertEqual(mod_generics_cache.A[str], mod_generics_cache.A[str])
4998 self.assertEqual(B.A, B.A)
4999 self.assertEqual(mod_generics_cache.B.A[B.A[str]],
5000 mod_generics_cache.B.A[B.A[str]])
5001
5002 self.assertNotEqual(A, B.A)
5003 self.assertNotEqual(A, mod_generics_cache.A)
5004 self.assertNotEqual(A, mod_generics_cache.B.A)
5005 self.assertNotEqual(B.A, mod_generics_cache.A)
5006 self.assertNotEqual(B.A, mod_generics_cache.B.A)
5007
5008 self.assertNotEqual(A[str], B.A[str])
5009 self.assertNotEqual(A[List[Any]], B.A[List[Any]])
5010 self.assertNotEqual(A[str], mod_generics_cache.A[str])
5011 self.assertNotEqual(A[str], mod_generics_cache.B.A[str])
5012 self.assertNotEqual(B.A[int], mod_generics_cache.A[int])
5013 self.assertNotEqual(B.A[List[Any]], mod_generics_cache.B.A[List[Any]])
5014
5015 self.assertNotEqual(Tuple[A[str]], Tuple[B.A[str]])
5016 self.assertNotEqual(Tuple[A[List[Any]]], Tuple[B.A[List[Any]]])
5017 self.assertNotEqual(Union[str, A[str]], Union[str, mod_generics_cache.A[str]])
5018 self.assertNotEqual(Union[A[str], A[str]],
5019 Union[A[str], mod_generics_cache.A[str]])
5020 self.assertNotEqual(typing.FrozenSet[A[str]],
5021 typing.FrozenSet[mod_generics_cache.B.A[str]])
5022
5023 self.assertEndsWith(repr(Tuple[A[str]]), '<locals>.A[str]]')
5024 self.assertEndsWith(repr(Tuple[B.A[str]]), '<locals>.B.A[str]]')
5025 self.assertEndsWith(repr(Tuple[mod_generics_cache.A[str]]),
5026 'mod_generics_cache.A[str]]')
5027 self.assertEndsWith(repr(Tuple[mod_generics_cache.B.A[str]]),
5028 'mod_generics_cache.B.A[str]]')
5029
5030 def test_extended_generic_rules_eq(self):
5031 T = TypeVar('T')
5032 U = TypeVar('U')
5033 self.assertEqual(Tuple[T, T][int], Tuple[int, int])
5034 self.assertEqual(typing.Iterable[Tuple[T, T]][T], typing.Iterable[Tuple[T, T]])
5035 with self.assertRaises(TypeError):
5036 Tuple[T, int][()]
5037
5038 self.assertEqual(Union[T, int][int], int)
5039 self.assertEqual(Union[T, U][int, Union[int, str]], Union[int, str])
5040 class Base: ...
5041 class Derived(Base): ...
5042 self.assertEqual(Union[T, Base][Union[Base, Derived]], Union[Base, Derived])
5043 self.assertEqual(Callable[[T], T][KT], Callable[[KT], KT])
5044 self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])
5045
5046 def test_extended_generic_rules_repr(self):
5047 T = TypeVar('T')
5048 self.assertEqual(repr(Union[Tuple, Callable]).replace('typing.', ''),
5049 'Tuple | Callable')
5050 self.assertEqual(repr(Union[Tuple, Tuple[int]]).replace('typing.', ''),
5051 'Tuple | Tuple[int]')
5052 self.assertEqual(repr(Callable[..., Optional[T]][int]).replace('typing.', ''),
5053 'Callable[..., int | None]')
5054 self.assertEqual(repr(Callable[[], List[T]][int]).replace('typing.', ''),
5055 'Callable[[], List[int]]')
5056
5057 def test_generic_forward_ref(self):
5058 def foobar(x: List[List['CC']]): ...
5059 def foobar2(x: list[list[ForwardRef('CC')]]): ...
5060 def foobar3(x: list[ForwardRef('CC | int')] | int): ...
5061 class CC: ...
5062 self.assertEqual(
5063 get_type_hints(foobar, globals(), locals()),
5064 {'x': List[List[CC]]}
5065 )
5066 self.assertEqual(
5067 get_type_hints(foobar2, globals(), locals()),
5068 {'x': list[list[CC]]}
5069 )
5070 self.assertEqual(
5071 get_type_hints(foobar3, globals(), locals()),
5072 {'x': list[CC | int] | int}
5073 )
5074
5075 T = TypeVar('T')
5076 AT = Tuple[T, ...]
5077 def barfoo(x: AT): ...
5078 self.assertIs(get_type_hints(barfoo, globals(), locals())['x'], AT)
5079 CT = Callable[..., List[T]]
5080 def barfoo2(x: CT): ...
5081 self.assertIs(get_type_hints(barfoo2, globals(), locals())['x'], CT)
5082
5083 def test_generic_pep585_forward_ref(self):
5084 # See https://bugs.python.org/issue41370
5085
5086 class C1:
5087 a: list['C1']
5088 self.assertEqual(
5089 get_type_hints(C1, globals(), locals()),
5090 {'a': list[C1]}
5091 )
5092
5093 class C2:
5094 a: dict['C1', list[List[list['C2']]]]
5095 self.assertEqual(
5096 get_type_hints(C2, globals(), locals()),
5097 {'a': dict[C1, list[List[list[C2]]]]}
5098 )
5099
5100 # Test stringified annotations
5101 scope = {}
5102 exec(textwrap.dedent('''
5103 from __future__ import annotations
5104 class C3:
5105 a: List[list["C2"]]
5106 '''), scope)
5107 C3 = scope['C3']
5108 self.assertEqual(C3.__annotations__['a'], "List[list['C2']]")
5109 self.assertEqual(
5110 get_type_hints(C3, globals(), locals()),
5111 {'a': List[list[C2]]}
5112 )
5113
5114 # Test recursive types
5115 X = list["X"]
5116 def f(x: X): ...
5117 self.assertEqual(
5118 get_type_hints(f, globals(), locals()),
5119 {'x': list[list[EqualToForwardRef('X')]]}
5120 )
5121
5122 def test_pep695_generic_class_with_future_annotations(self):
5123 original_globals = dict(ann_module695.__dict__)
5124
5125 hints_for_A = get_type_hints(ann_module695.A)
5126 A_type_params = ann_module695.A.__type_params__
5127 self.assertIs(hints_for_A["x"], A_type_params[0])
5128 self.assertEqual(hints_for_A["y"].__args__[0], Unpack[A_type_params[1]])
5129 self.assertIs(hints_for_A["z"].__args__[0], A_type_params[2])
5130
5131 # should not have changed as a result of the get_type_hints() calls!
5132 self.assertEqual(ann_module695.__dict__, original_globals)
5133
5134 def test_pep695_generic_class_with_future_annotations_and_local_shadowing(self):
5135 hints_for_B = get_type_hints(ann_module695.B)
5136 self.assertEqual(hints_for_B, {"x": int, "y": str, "z": bytes})
5137
5138 def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(self):
5139 hints_for_C = get_type_hints(ann_module695.C)
5140 self.assertEqual(
5141 set(hints_for_C.values()),
5142 set(ann_module695.C.__type_params__)
5143 )
5144
5145 def test_pep_695_generic_function_with_future_annotations(self):
5146 hints_for_generic_function = get_type_hints(ann_module695.generic_function)
5147 func_t_params = ann_module695.generic_function.__type_params__
5148 self.assertEqual(
5149 hints_for_generic_function.keys(), {"x", "y", "z", "zz", "return"}
5150 )
5151 self.assertIs(hints_for_generic_function["x"], func_t_params[0])
5152 self.assertEqual(hints_for_generic_function["y"], Unpack[func_t_params[1]])
5153 self.assertIs(hints_for_generic_function["z"].__origin__, func_t_params[2])
5154 self.assertIs(hints_for_generic_function["zz"].__origin__, func_t_params[2])
5155
5156 def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(self):
5157 self.assertEqual(
5158 set(get_type_hints(ann_module695.generic_function_2).values()),
5159 set(ann_module695.generic_function_2.__type_params__)
5160 )
5161
5162 def test_pep_695_generic_method_with_future_annotations(self):
5163 hints_for_generic_method = get_type_hints(ann_module695.D.generic_method)
5164 params = {
5165 param.__name__: param
5166 for param in ann_module695.D.generic_method.__type_params__
5167 }
5168 self.assertEqual(
5169 hints_for_generic_method,
5170 {"x": params["Foo"], "y": params["Bar"], "return": types.NoneType}
5171 )
5172
5173 def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(self):
5174 self.assertEqual(
5175 set(get_type_hints(ann_module695.D.generic_method_2).values()),
5176 set(ann_module695.D.generic_method_2.__type_params__)
5177 )
5178
5179 def test_pep_695_generics_with_future_annotations_nested_in_function(self):
5180 results = ann_module695.nested()
5181
5182 self.assertEqual(
5183 set(results.hints_for_E.values()),
5184 set(results.E.__type_params__)
5185 )
5186 self.assertEqual(
5187 set(results.hints_for_E_meth.values()),
5188 set(results.E.generic_method.__type_params__)
5189 )
5190 self.assertNotEqual(
5191 set(results.hints_for_E_meth.values()),
5192 set(results.E.__type_params__)
5193 )
5194 self.assertEqual(
5195 set(results.hints_for_E_meth.values()).intersection(results.E.__type_params__),
5196 set()
5197 )
5198
5199 self.assertEqual(
5200 set(results.hints_for_generic_func.values()),
5201 set(results.generic_func.__type_params__)
5202 )
5203
5204 def test_extended_generic_rules_subclassing(self):
5205 class T1(Tuple[T, KT]): ...
5206 class T2(Tuple[T, ...]): ...
5207 class C1(typing.Container[T]):
5208 def __contains__(self, item):
5209 return False
5210
5211 self.assertEqual(T1.__parameters__, (T, KT))
5212 self.assertEqual(T1[int, str].__args__, (int, str))
5213 self.assertEqual(T1[int, T].__origin__, T1)
5214
5215 self.assertEqual(T2.__parameters__, (T,))
5216 # These don't work because of tuple.__class_item__
5217 ## with self.assertRaises(TypeError):
5218 ## T1[int]
5219 ## with self.assertRaises(TypeError):
5220 ## T2[int, str]
5221
5222 self.assertEqual(repr(C1[int]).split('.')[-1], 'C1[int]')
5223 self.assertEqual(C1.__parameters__, (T,))
5224 self.assertIsInstance(C1(), collections.abc.Container)
5225 self.assertIsSubclass(C1, collections.abc.Container)
5226 self.assertIsInstance(T1(), tuple)
5227 self.assertIsSubclass(T2, tuple)
5228 with self.assertRaises(TypeError):
5229 issubclass(Tuple[int, ...], typing.Sequence)
5230 with self.assertRaises(TypeError):
5231 issubclass(Tuple[int, ...], typing.Iterable)
5232
5233 def test_fail_with_special_forms(self):
5234 with self.assertRaises(TypeError):
5235 List[Final]
5236 with self.assertRaises(TypeError):
5237 Tuple[Optional]
5238 with self.assertRaises(TypeError):
5239 List[ClassVar[int]]
5240
5241 def test_fail_with_bare_generic(self):
5242 T = TypeVar('T')
5243 with self.assertRaises(TypeError):
5244 List[Generic]
5245 with self.assertRaises(TypeError):
5246 Tuple[Generic[T]]
5247 with self.assertRaises(TypeError):
5248 List[typing.Protocol]
5249
5250 def test_type_erasure_special(self):
5251 T = TypeVar('T')
5252 # this is the only test that checks type caching
5253 self.clear_caches()
5254 class MyTup(Tuple[T, T]): ...
5255 self.assertIs(MyTup[int]().__class__, MyTup)
5256 self.assertEqual(MyTup[int]().__orig_class__, MyTup[int])
5257 class MyDict(typing.Dict[T, T]): ...
5258 self.assertIs(MyDict[int]().__class__, MyDict)
5259 self.assertEqual(MyDict[int]().__orig_class__, MyDict[int])
5260 class MyDef(typing.DefaultDict[str, T]): ...
5261 self.assertIs(MyDef[int]().__class__, MyDef)
5262 self.assertEqual(MyDef[int]().__orig_class__, MyDef[int])
5263 class MyChain(typing.ChainMap[str, T]): ...
5264 self.assertIs(MyChain[int]().__class__, MyChain)
5265 self.assertEqual(MyChain[int]().__orig_class__, MyChain[int])
5266
5267 def test_all_repr_eq_any(self):
5268 objs = (getattr(typing, el) for el in typing.__all__)
5269 for obj in objs:
5270 self.assertNotEqual(repr(obj), '')
5271 self.assertEqual(obj, obj)
5272 if (getattr(obj, '__parameters__', None)
5273 and not isinstance(obj, typing.TypeVar)
5274 and isinstance(obj.__parameters__, tuple)
5275 and len(obj.__parameters__) == 1):
5276 self.assertEqual(obj[Any].__args__, (Any,))
5277 if isinstance(obj, type):
5278 for base in obj.__mro__:
5279 self.assertNotEqual(repr(base), '')
5280 self.assertEqual(base, base)
5281
5282 def test_pickle(self):
5283 global C # pickle wants to reference the class by name
5284 T = TypeVar('T')
5285
5286 class B(Generic[T]):
5287 pass
5288
5289 class C(B[int]):
5290 pass
5291
5292 c = C()
5293 c.foo = 42
5294 c.bar = 'abc'
5295 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
5296 z = pickle.dumps(c, proto)
5297 x = pickle.loads(z)
5298 self.assertEqual(x.foo, 42)
5299 self.assertEqual(x.bar, 'abc')
5300 self.assertEqual(x.__dict__, {'foo': 42, 'bar': 'abc'})
5301 samples = [Any, Union, Tuple, Callable, ClassVar,
5302 Union[int, str], ClassVar[List], Tuple[int, ...], Tuple[()],
5303 Callable[[str], bytes],
5304 typing.DefaultDict, typing.FrozenSet[int]]
5305 for s in samples:
5306 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
5307 z = pickle.dumps(s, proto)
5308 x = pickle.loads(z)
5309 self.assertEqual(s, x)
5310 more_samples = [List, typing.Iterable, typing.Type, List[int],
5311 typing.Type[typing.Mapping], typing.AbstractSet[Tuple[int, str]]]
5312 for s in more_samples:
5313 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
5314 z = pickle.dumps(s, proto)
5315 x = pickle.loads(z)
5316 self.assertEqual(s, x)
5317
5318 # Test ParamSpec args and kwargs
5319 global PP
5320 PP = ParamSpec('PP')
5321 for thing in [PP.args, PP.kwargs]:
5322 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
5323 with self.subTest(thing=thing, proto=proto):
5324 self.assertEqual(
5325 pickle.loads(pickle.dumps(thing, proto)),
5326 thing,
5327 )
5328 del PP
5329
5330 def test_copy_and_deepcopy(self):
5331 T = TypeVar('T')
5332 class Node(Generic[T]): ...
5333 things = [Union[T, int], Tuple[T, int], Tuple[()],
5334 Callable[..., T], Callable[[int], int],
5335 Tuple[Any, Any], Node[T], Node[int], Node[Any], typing.Iterable[T],
5336 typing.Iterable[Any], typing.Iterable[int], typing.Dict[int, str],
5337 typing.Dict[T, Any], ClassVar[int], ClassVar[List[T]], Tuple['T', 'T'],
5338 Union['T', int], List['T'], typing.Mapping['T', int],
5339 Union[b"x", b"y"], Any]
5340 for t in things:
5341 with self.subTest(thing=t):
5342 self.assertEqual(t, copy(t))
5343 self.assertEqual(t, deepcopy(t))
5344
5345 def test_immutability_by_copy_and_pickle(self):
5346 # Special forms like Union, Any, etc., generic aliases to containers like List,
5347 # Mapping, etc., and type variabcles are considered immutable by copy and pickle.
5348 global TP, TPB, TPV, PP # for pickle
5349 TP = TypeVar('TP')
5350 TPB = TypeVar('TPB', bound=int)
5351 TPV = TypeVar('TPV', bytes, str)
5352 PP = ParamSpec('PP')
5353 for X in [TP, TPB, TPV, PP,
5354 List, typing.Mapping, ClassVar, typing.Iterable,
5355 Union, Any, Tuple, Callable]:
5356 with self.subTest(thing=X):
5357 self.assertIs(copy(X), X)
5358 self.assertIs(deepcopy(X), X)
5359 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
5360 self.assertIs(pickle.loads(pickle.dumps(X, proto)), X)
5361 del TP, TPB, TPV, PP
5362
5363 # Check that local type variables are copyable.
5364 TL = TypeVar('TL')
5365 TLB = TypeVar('TLB', bound=int)
5366 TLV = TypeVar('TLV', bytes, str)
5367 PL = ParamSpec('PL')
5368 for X in [TL, TLB, TLV, PL]:
5369 with self.subTest(thing=X):
5370 self.assertIs(copy(X), X)
5371 self.assertIs(deepcopy(X), X)
5372
5373 def test_copy_generic_instances(self):
5374 T = TypeVar('T')
5375 class C(Generic[T]):
5376 def __init__(self, attr: T) -> None:
5377 self.attr = attr
5378
5379 c = C(42)
5380 self.assertEqual(copy(c).attr, 42)
5381 self.assertEqual(deepcopy(c).attr, 42)
5382 self.assertIsNot(copy(c), c)
5383 self.assertIsNot(deepcopy(c), c)
5384 c.attr = 1
5385 self.assertEqual(copy(c).attr, 1)
5386 self.assertEqual(deepcopy(c).attr, 1)
5387 ci = C[int](42)
5388 self.assertEqual(copy(ci).attr, 42)
5389 self.assertEqual(deepcopy(ci).attr, 42)
5390 self.assertIsNot(copy(ci), ci)
5391 self.assertIsNot(deepcopy(ci), ci)
5392 ci.attr = 1
5393 self.assertEqual(copy(ci).attr, 1)
5394 self.assertEqual(deepcopy(ci).attr, 1)
5395 self.assertEqual(ci.__orig_class__, C[int])
5396
5397 def test_weakref_all(self):
5398 T = TypeVar('T')
5399 things = [Any, Union[T, int], Callable[..., T], Tuple[Any, Any],
5400 Optional[List[int]], typing.Mapping[int, str],
5401 typing.Match[bytes], typing.Iterable['whatever']]
5402 for t in things:
5403 self.assertEqual(weakref.ref(t)(), t)
5404
5405 def test_parameterized_slots(self):
5406 T = TypeVar('T')
5407 class C(Generic[T]):
5408 __slots__ = ('potato',)
5409
5410 c = C()
5411 c_int = C[int]()
5412
5413 c.potato = 0
5414 c_int.potato = 0
5415 with self.assertRaises(AttributeError):
5416 c.tomato = 0
5417 with self.assertRaises(AttributeError):
5418 c_int.tomato = 0
5419
5420 def foo(x: C['C']): ...
5421 self.assertEqual(get_type_hints(foo, globals(), locals())['x'], C[C])
5422 self.assertEqual(copy(C[int]), deepcopy(C[int]))
5423
5424 def test_parameterized_slots_dict(self):
5425 T = TypeVar('T')
5426 class D(Generic[T]):
5427 __slots__ = {'banana': 42}
5428
5429 d = D()
5430 d_int = D[int]()
5431
5432 d.banana = 'yes'
5433 d_int.banana = 'yes'
5434 with self.assertRaises(AttributeError):
5435 d.foobar = 'no'
5436 with self.assertRaises(AttributeError):
5437 d_int.foobar = 'no'
5438
5439 def test_errors(self):
5440 with self.assertRaises(TypeError):
5441 B = SimpleMapping[XK, Any]
5442
5443 class C(Generic[B]):
5444 pass
5445
5446 def test_repr_2(self):
5447 class C(Generic[T]):
5448 pass
5449
5450 self.assertEqual(C.__module__, __name__)
5451 self.assertEqual(C.__qualname__,
5452 'GenericTests.test_repr_2.<locals>.C')
5453 X = C[int]
5454 self.assertEqual(X.__module__, __name__)
5455 self.assertEqual(repr(X).split('.')[-1], 'C[int]')
5456
5457 class Y(C[int]):
5458 pass
5459
5460 self.assertEqual(Y.__module__, __name__)
5461 self.assertEqual(Y.__qualname__,
5462 'GenericTests.test_repr_2.<locals>.Y')
5463
5464 def test_repr_3(self):
5465 T = TypeVar('T')
5466 T1 = TypeVar('T1')
5467 P = ParamSpec('P')
5468 P2 = ParamSpec('P2')
5469 Ts = TypeVarTuple('Ts')
5470
5471 class MyCallable(Generic[P, T]):
5472 pass
5473
5474 class DoubleSpec(Generic[P, P2, T]):
5475 pass
5476
5477 class TsP(Generic[*Ts, P]):
5478 pass
5479
5480 object_to_expected_repr = {
5481 MyCallable[P, T]: "MyCallable[~P, ~T]",
5482 MyCallable[Concatenate[T1, P], T]: "MyCallable[typing.Concatenate[~T1, ~P], ~T]",
5483 MyCallable[[], bool]: "MyCallable[[], bool]",
5484 MyCallable[[int], bool]: "MyCallable[[int], bool]",
5485 MyCallable[[int, str], bool]: "MyCallable[[int, str], bool]",
5486 MyCallable[[int, list[int]], bool]: "MyCallable[[int, list[int]], bool]",
5487 MyCallable[Concatenate[*Ts, P], T]: "MyCallable[typing.Concatenate[typing.Unpack[Ts], ~P], ~T]",
5488
5489 DoubleSpec[P2, P, T]: "DoubleSpec[~P2, ~P, ~T]",
5490 DoubleSpec[[int], [str], bool]: "DoubleSpec[[int], [str], bool]",
5491 DoubleSpec[[int, int], [str, str], bool]: "DoubleSpec[[int, int], [str, str], bool]",
5492
5493 TsP[*Ts, P]: "TsP[typing.Unpack[Ts], ~P]",
5494 TsP[int, str, list[int], []]: "TsP[int, str, list[int], []]",
5495 TsP[int, [str, list[int]]]: "TsP[int, [str, list[int]]]",
5496
5497 # These lines are just too long to fit:
5498 MyCallable[Concatenate[*Ts, P], int][int, str, [bool, float]]:
5499 "MyCallable[[int, str, bool, float], int]",
5500 }
5501
5502 for obj, expected_repr in object_to_expected_repr.items():
5503 with self.subTest(obj=obj, expected_repr=expected_repr):
5504 self.assertRegex(
5505 repr(obj),
5506 fr"^{re.escape(MyCallable.__module__)}.*\.{re.escape(expected_repr)}$",
5507 )
5508
5509 def test_eq_1(self):
5510 self.assertEqual(Generic, Generic)
5511 self.assertEqual(Generic[T], Generic[T])
5512 self.assertNotEqual(Generic[KT], Generic[VT])
5513
5514 def test_eq_2(self):
5515
5516 class A(Generic[T]):
5517 pass
5518
5519 class B(Generic[T]):
5520 pass
5521
5522 self.assertEqual(A, A)
5523 self.assertNotEqual(A, B)
5524 self.assertEqual(A[T], A[T])
5525 self.assertNotEqual(A[T], B[T])
5526
5527 def test_multiple_inheritance(self):
5528
5529 class A(Generic[T, VT]):
5530 pass
5531
5532 class B(Generic[KT, T]):
5533 pass
5534
5535 class C(A[T, VT], Generic[VT, T, KT], B[KT, T]):
5536 pass
5537
5538 self.assertEqual(C.__parameters__, (VT, T, KT))
5539
5540 def test_multiple_inheritance_special(self):
5541 S = TypeVar('S')
5542 class B(Generic[S]): ...
5543 class C(List[int], B): ...
5544 self.assertEqual(C.__mro__, (C, list, B, Generic, object))
5545
5546 def test_multiple_inheritance_non_type_with___mro_entries__(self):
5547 class GoodEntries:
5548 def __mro_entries__(self, bases):
5549 return (object,)
5550
5551 class A(List[int], GoodEntries()): ...
5552
5553 self.assertEqual(A.__mro__, (A, list, Generic, object))
5554
5555 def test_multiple_inheritance_non_type_without___mro_entries__(self):
5556 # Error should be from the type machinery, not from typing.py
5557 with self.assertRaisesRegex(TypeError, r"^bases must be types"):
5558 class A(List[int], object()): ...
5559
5560 def test_multiple_inheritance_non_type_bad___mro_entries__(self):
5561 class BadEntries:
5562 def __mro_entries__(self, bases):
5563 return None
5564
5565 # Error should be from the type machinery, not from typing.py
5566 with self.assertRaisesRegex(
5567 TypeError,
5568 r"^__mro_entries__ must return a tuple",
5569 ):
5570 class A(List[int], BadEntries()): ...
5571
5572 def test_multiple_inheritance___mro_entries___returns_non_type(self):
5573 class BadEntries:
5574 def __mro_entries__(self, bases):
5575 return (object(),)
5576
5577 # Error should be from the type machinery, not from typing.py
5578 with self.assertRaisesRegex(
5579 TypeError,
5580 r"^bases must be types",
5581 ):
5582 class A(List[int], BadEntries()): ...
5583
5584 def test_multiple_inheritance_with_genericalias(self):
5585 class A(typing.Sized, list[int]): ...
5586
5587 self.assertEqual(
5588 A.__mro__,
5589 (A, collections.abc.Sized, Generic, list, object),
5590 )
5591
5592 def test_multiple_inheritance_with_genericalias_2(self):
5593 T = TypeVar("T")
5594
5595 class BaseSeq(typing.Sequence[T]): ...
5596 class MySeq(List[T], BaseSeq[T]): ...
5597
5598 self.assertEqual(
5599 MySeq.__mro__,
5600 (
5601 MySeq,
5602 list,
5603 BaseSeq,
5604 collections.abc.Sequence,
5605 collections.abc.Reversible,
5606 collections.abc.Collection,
5607 collections.abc.Sized,
5608 collections.abc.Iterable,
5609 collections.abc.Container,
5610 Generic,
5611 object,
5612 ),
5613 )
5614
5615 def test_init_subclass_super_called(self):
5616 class FinalException(Exception):
5617 pass
5618
5619 class Final:
5620 def __init_subclass__(cls, **kwargs) -> None:
5621 for base in cls.__bases__:
5622 if base is not Final and issubclass(base, Final):
5623 raise FinalException(base)
5624 super().__init_subclass__(**kwargs)
5625 class Test(Generic[T], Final):
5626 pass
5627 with self.assertRaises(FinalException):
5628 class Subclass(Test):
5629 pass
5630 with self.assertRaises(FinalException):
5631 class Subclass2(Test[int]):
5632 pass
5633
5634 def test_nested(self):
5635
5636 G = Generic
5637
5638 class Visitor(G[T]):
5639
5640 a = None
5641
5642 def set(self, a: T):
5643 self.a = a
5644
5645 def get(self):
5646 return self.a
5647
5648 def visit(self) -> T:
5649 return self.a
5650
5651 V = Visitor[typing.List[int]]
5652
5653 class IntListVisitor(V):
5654
5655 def append(self, x: int):
5656 self.a.append(x)
5657
5658 a = IntListVisitor()
5659 a.set([])
5660 a.append(1)
5661 a.append(42)
5662 self.assertEqual(a.get(), [1, 42])
5663
5664 def test_type_erasure(self):
5665 T = TypeVar('T')
5666
5667 class Node(Generic[T]):
5668 def __init__(self, label: T,
5669 left: 'Node[T]' = None,
5670 right: 'Node[T]' = None):
5671 self.label = label # type: T
5672 self.left = left # type: Optional[Node[T]]
5673 self.right = right # type: Optional[Node[T]]
5674
5675 def foo(x: T):
5676 a = Node(x)
5677 b = Node[T](x)
5678 c = Node[Any](x)
5679 self.assertIs(type(a), Node)
5680 self.assertIs(type(b), Node)
5681 self.assertIs(type(c), Node)
5682 self.assertEqual(a.label, x)
5683 self.assertEqual(b.label, x)
5684 self.assertEqual(c.label, x)
5685
5686 foo(42)
5687
5688 def test_implicit_any(self):
5689 T = TypeVar('T')
5690
5691 class C(Generic[T]):
5692 pass
5693
5694 class D(C):
5695 pass
5696
5697 self.assertEqual(D.__parameters__, ())
5698
5699 with self.assertRaises(TypeError):
5700 D[int]
5701 with self.assertRaises(TypeError):
5702 D[Any]
5703 with self.assertRaises(TypeError):
5704 D[T]
5705
5706 def test_new_with_args(self):
5707
5708 class A(Generic[T]):
5709 pass
5710
5711 class B:
5712 def __new__(cls, arg):
5713 # call object
5714 obj = super().__new__(cls)
5715 obj.arg = arg
5716 return obj
5717
5718 # mro: C, A, Generic, B, object
5719 class C(A, B):
5720 pass
5721
5722 c = C('foo')
5723 self.assertEqual(c.arg, 'foo')
5724
5725 def test_new_with_args2(self):
5726
5727 class A:
5728 def __init__(self, arg):
5729 self.from_a = arg
5730 # call object
5731 super().__init__()
5732
5733 # mro: C, Generic, A, object
5734 class C(Generic[T], A):
5735 def __init__(self, arg):
5736 self.from_c = arg
5737 # call Generic
5738 super().__init__(arg)
5739
5740 c = C('foo')
5741 self.assertEqual(c.from_a, 'foo')
5742 self.assertEqual(c.from_c, 'foo')
5743
5744 def test_new_no_args(self):
5745
5746 class A(Generic[T]):
5747 pass
5748
5749 with self.assertRaises(TypeError):
5750 A('foo')
5751
5752 class B:
5753 def __new__(cls):
5754 # call object
5755 obj = super().__new__(cls)
5756 obj.from_b = 'b'
5757 return obj
5758
5759 # mro: C, A, Generic, B, object
5760 class C(A, B):
5761 def __init__(self, arg):
5762 self.arg = arg
5763
5764 def __new__(cls, arg):
5765 # call A
5766 obj = super().__new__(cls)
5767 obj.from_c = 'c'
5768 return obj
5769
5770 c = C('foo')
5771 self.assertEqual(c.arg, 'foo')
5772 self.assertEqual(c.from_b, 'b')
5773 self.assertEqual(c.from_c, 'c')
5774
5775 def test_subclass_special_form(self):
5776 for obj in (
5777 ClassVar[int],
5778 Final[int],
5779 Literal[1, 2],
5780 Concatenate[int, ParamSpec("P")],
5781 TypeGuard[int],
5782 TypeIs[range],
5783 ):
5784 with self.subTest(msg=obj):
5785 with self.assertRaisesRegex(
5786 TypeError, f'^{re.escape(f"Cannot subclass {obj!r}")}$'
5787 ):
5788 class Foo(obj):
5789 pass
5790
5791 def test_complex_subclasses(self):
5792 T_co = TypeVar("T_co", covariant=True)
5793
5794 class Base(Generic[T_co]):
5795 ...
5796
5797 T = TypeVar("T")
5798
5799 # see gh-94607: this fails in that bug
5800 class Sub(Base, Generic[T]):
5801 ...
5802
5803 def test_parameter_detection(self):
5804 self.assertEqual(List[T].__parameters__, (T,))
5805 self.assertEqual(List[List[T]].__parameters__, (T,))
5806 class A:
5807 __parameters__ = (T,)
5808 # Bare classes should be skipped
5809 for a in (List, list):
5810 for b in (A, int, TypeVar, TypeVarTuple, ParamSpec, types.GenericAlias, Union):
5811 with self.subTest(generic=a, sub=b):
5812 with self.assertRaisesRegex(TypeError, '.* is not a generic class'):
5813 a[b][str]
5814 # Duck-typing anything that looks like it has __parameters__.
5815 # These tests are optional and failure is okay.
5816 self.assertEqual(List[A()].__parameters__, (T,))
5817 # C version of GenericAlias
5818 self.assertEqual(list[A()].__parameters__, (T,))
5819
5820 def test_non_generic_subscript(self):
5821 T = TypeVar('T')
5822 class G(Generic[T]):
5823 pass
5824 class A:
5825 __parameters__ = (T,)
5826
5827 for s in (int, G, A, List, list,
5828 TypeVar, TypeVarTuple, ParamSpec,
5829 types.GenericAlias, Union):
5830
5831 for t in Tuple, tuple:
5832 with self.subTest(tuple=t, sub=s):
5833 self.assertEqual(t[s, T][int], t[s, int])
5834 self.assertEqual(t[T, s][int], t[int, s])
5835 a = t[s]
5836 with self.assertRaises(TypeError):
5837 a[int]
5838
5839 for c in Callable, collections.abc.Callable:
5840 with self.subTest(callable=c, sub=s):
5841 self.assertEqual(c[[s], T][int], c[[s], int])
5842 self.assertEqual(c[[T], s][int], c[[int], s])
5843 a = c[[s], s]
5844 with self.assertRaises(TypeError):
5845 a[int]
5846
5847 def test_return_non_tuple_while_unpacking(self):
5848 # GH-138497: GenericAlias objects didn't ensure that __typing_subst__ actually
5849 # returned a tuple
5850 class EvilTypeVar:
5851 __typing_is_unpacked_typevartuple__ = True
5852 def __typing_prepare_subst__(*_):
5853 return None # any value
5854 def __typing_subst__(*_):
5855 return 42 # not tuple
5856
5857 evil = EvilTypeVar()
5858 # Create a dummy TypeAlias that will be given the evil generic from
5859 # above.
5860 type type_alias[*_] = 0
5861 with self.assertRaisesRegex(TypeError, ".+__typing_subst__.+tuple.+int.*"):
5862 type_alias[evil][0]
5863
5864
5865 class ClassVarTests(BaseTestCase):
5866
5867 def test_basics(self):
5868 with self.assertRaises(TypeError):
5869 ClassVar[int, str]
5870 with self.assertRaises(TypeError):
5871 ClassVar[int][str]
5872
5873 def test_repr(self):
5874 self.assertEqual(repr(ClassVar), 'typing.ClassVar')
5875 cv = ClassVar[int]
5876 self.assertEqual(repr(cv), 'typing.ClassVar[int]')
5877 cv = ClassVar[Employee]
5878 self.assertEqual(repr(cv), 'typing.ClassVar[%s.Employee]' % __name__)
5879
5880 def test_cannot_subclass(self):
5881 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
5882 class C(type(ClassVar)):
5883 pass
5884 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
5885 class D(type(ClassVar[int])):
5886 pass
5887 with self.assertRaisesRegex(TypeError,
5888 r'Cannot subclass typing\.ClassVar'):
5889 class E(ClassVar):
5890 pass
5891 with self.assertRaisesRegex(TypeError,
5892 r'Cannot subclass typing\.ClassVar\[int\]'):
5893 class F(ClassVar[int]):
5894 pass
5895
5896 def test_cannot_init(self):
5897 with self.assertRaises(TypeError):
5898 ClassVar()
5899 with self.assertRaises(TypeError):
5900 type(ClassVar)()
5901 with self.assertRaises(TypeError):
5902 type(ClassVar[Optional[int]])()
5903
5904 def test_no_isinstance(self):
5905 with self.assertRaises(TypeError):
5906 isinstance(1, ClassVar[int])
5907 with self.assertRaises(TypeError):
5908 issubclass(int, ClassVar)
5909
5910
5911 class FinalTests(BaseTestCase):
5912
5913 def test_basics(self):
5914 Final[int] # OK
5915 with self.assertRaises(TypeError):
5916 Final[int, str]
5917 with self.assertRaises(TypeError):
5918 Final[int][str]
5919 with self.assertRaises(TypeError):
5920 Optional[Final[int]]
5921
5922 def test_repr(self):
5923 self.assertEqual(repr(Final), 'typing.Final')
5924 cv = Final[int]
5925 self.assertEqual(repr(cv), 'typing.Final[int]')
5926 cv = Final[Employee]
5927 self.assertEqual(repr(cv), 'typing.Final[%s.Employee]' % __name__)
5928 cv = Final[tuple[int]]
5929 self.assertEqual(repr(cv), 'typing.Final[tuple[int]]')
5930
5931 def test_cannot_subclass(self):
5932 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
5933 class C(type(Final)):
5934 pass
5935 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
5936 class D(type(Final[int])):
5937 pass
5938 with self.assertRaisesRegex(TypeError,
5939 r'Cannot subclass typing\.Final'):
5940 class E(Final):
5941 pass
5942 with self.assertRaisesRegex(TypeError,
5943 r'Cannot subclass typing\.Final\[int\]'):
5944 class F(Final[int]):
5945 pass
5946
5947 def test_cannot_init(self):
5948 with self.assertRaises(TypeError):
5949 Final()
5950 with self.assertRaises(TypeError):
5951 type(Final)()
5952 with self.assertRaises(TypeError):
5953 type(Final[Optional[int]])()
5954
5955 def test_no_isinstance(self):
5956 with self.assertRaises(TypeError):
5957 isinstance(1, Final[int])
5958 with self.assertRaises(TypeError):
5959 issubclass(int, Final)
5960
5961
5962 class FinalDecoratorTests(BaseTestCase):
5963 def test_final_unmodified(self):
5964 def func(x): ...
5965 self.assertIs(func, final(func))
5966
5967 def test_dunder_final(self):
5968 @final
5969 def func(): ...
5970 @final
5971 class Cls: ...
5972 self.assertIs(True, func.__final__)
5973 self.assertIs(True, Cls.__final__)
5974
5975 class Wrapper:
5976 __slots__ = ("func",)
5977 def __init__(self, func):
5978 self.func = func
5979 def __call__(self, *args, **kwargs):
5980 return self.func(*args, **kwargs)
5981
5982 # Check that no error is thrown if the attribute
5983 # is not writable.
5984 @final
5985 @Wrapper
5986 def wrapped(): ...
5987 self.assertIsInstance(wrapped, Wrapper)
5988 self.assertNotHasAttr(wrapped, "__final__")
5989
5990 class Meta(type):
5991 @property
5992 def __final__(self): return "can't set me"
5993 @final
5994 class WithMeta(metaclass=Meta): ...
5995 self.assertEqual(WithMeta.__final__, "can't set me")
5996
5997 # Builtin classes throw TypeError if you try to set an
5998 # attribute.
5999 final(int)
6000 self.assertNotHasAttr(int, "__final__")
6001
6002 # Make sure it works with common builtin decorators
6003 class Methods:
6004 @final
6005 @classmethod
6006 def clsmethod(cls): ...
6007
6008 @final
6009 @staticmethod
6010 def stmethod(): ...
6011
6012 # The other order doesn't work because property objects
6013 # don't allow attribute assignment.
6014 @property
6015 @final
6016 def prop(self): ...
6017
6018 @final
6019 @lru_cache()
6020 def cached(self): ...
6021
6022 # Use getattr_static because the descriptor returns the
6023 # underlying function, which doesn't have __final__.
6024 self.assertIs(
6025 True,
6026 inspect.getattr_static(Methods, "clsmethod").__final__
6027 )
6028 self.assertIs(
6029 True,
6030 inspect.getattr_static(Methods, "stmethod").__final__
6031 )
6032 self.assertIs(True, Methods.prop.fget.__final__)
6033 self.assertIs(True, Methods.cached.__final__)
6034
6035
6036 class OverrideDecoratorTests(BaseTestCase):
6037 def test_override(self):
6038 class Base:
6039 def normal_method(self): ...
6040 @classmethod
6041 def class_method_good_order(cls): ...
6042 @classmethod
6043 def class_method_bad_order(cls): ...
6044 @staticmethod
6045 def static_method_good_order(): ...
6046 @staticmethod
6047 def static_method_bad_order(): ...
6048
6049 class Derived(Base):
6050 @override
6051 def normal_method(self):
6052 return 42
6053
6054 @classmethod
6055 @override
6056 def class_method_good_order(cls):
6057 return 42
6058 @override
6059 @classmethod
6060 def class_method_bad_order(cls):
6061 return 42
6062
6063 @staticmethod
6064 @override
6065 def static_method_good_order():
6066 return 42
6067 @override
6068 @staticmethod
6069 def static_method_bad_order():
6070 return 42
6071
6072 self.assertIsSubclass(Derived, Base)
6073 instance = Derived()
6074 self.assertEqual(instance.normal_method(), 42)
6075 self.assertIs(True, Derived.normal_method.__override__)
6076 self.assertIs(True, instance.normal_method.__override__)
6077
6078 self.assertEqual(Derived.class_method_good_order(), 42)
6079 self.assertIs(True, Derived.class_method_good_order.__override__)
6080 self.assertEqual(Derived.class_method_bad_order(), 42)
6081 self.assertNotHasAttr(Derived.class_method_bad_order, "__override__")
6082
6083 self.assertEqual(Derived.static_method_good_order(), 42)
6084 self.assertIs(True, Derived.static_method_good_order.__override__)
6085 self.assertEqual(Derived.static_method_bad_order(), 42)
6086 self.assertNotHasAttr(Derived.static_method_bad_order, "__override__")
6087
6088 # Base object is not changed:
6089 self.assertNotHasAttr(Base.normal_method, "__override__")
6090 self.assertNotHasAttr(Base.class_method_good_order, "__override__")
6091 self.assertNotHasAttr(Base.class_method_bad_order, "__override__")
6092 self.assertNotHasAttr(Base.static_method_good_order, "__override__")
6093 self.assertNotHasAttr(Base.static_method_bad_order, "__override__")
6094
6095 def test_property(self):
6096 class Base:
6097 @property
6098 def correct(self) -> int:
6099 return 1
6100 @property
6101 def wrong(self) -> int:
6102 return 1
6103
6104 class Child(Base):
6105 @property
6106 @override
6107 def correct(self) -> int:
6108 return 2
6109 @override
6110 @property
6111 def wrong(self) -> int:
6112 return 2
6113
6114 instance = Child()
6115 self.assertEqual(instance.correct, 2)
6116 self.assertIs(Child.correct.fget.__override__, True)
6117 self.assertEqual(instance.wrong, 2)
6118 self.assertNotHasAttr(Child.wrong, "__override__")
6119 self.assertNotHasAttr(Child.wrong.fset, "__override__")
6120
6121 def test_silent_failure(self):
6122 class CustomProp:
6123 __slots__ = ('fget',)
6124 def __init__(self, fget):
6125 self.fget = fget
6126 def __get__(self, obj, objtype=None):
6127 return self.fget(obj)
6128
6129 class WithOverride:
6130 @override # must not fail on object with `__slots__`
6131 @CustomProp
6132 def some(self):
6133 return 1
6134
6135 self.assertEqual(WithOverride.some, 1)
6136 self.assertNotHasAttr(WithOverride.some, "__override__")
6137
6138 def test_multiple_decorators(self):
6139 def with_wraps(f): # similar to `lru_cache` definition
6140 @wraps(f)
6141 def wrapper(*args, **kwargs):
6142 return f(*args, **kwargs)
6143 return wrapper
6144
6145 class WithOverride:
6146 @override
6147 @with_wraps
6148 def on_top(self, a: int) -> int:
6149 return a + 1
6150 @with_wraps
6151 @override
6152 def on_bottom(self, a: int) -> int:
6153 return a + 2
6154
6155 instance = WithOverride()
6156 self.assertEqual(instance.on_top(1), 2)
6157 self.assertIs(instance.on_top.__override__, True)
6158 self.assertEqual(instance.on_bottom(1), 3)
6159 self.assertIs(instance.on_bottom.__override__, True)
6160
6161
6162 class CastTests(BaseTestCase):
6163
6164 def test_basics(self):
6165 self.assertEqual(cast(int, 42), 42)
6166 self.assertEqual(cast(float, 42), 42)
6167 self.assertIs(type(cast(float, 42)), int)
6168 self.assertEqual(cast(Any, 42), 42)
6169 self.assertEqual(cast(list, 42), 42)
6170 self.assertEqual(cast(Union[str, float], 42), 42)
6171 self.assertEqual(cast(AnyStr, 42), 42)
6172 self.assertEqual(cast(None, 42), 42)
6173
6174 def test_errors(self):
6175 # Bogus calls are not expected to fail.
6176 cast(42, 42)
6177 cast('hello', 42)
6178
6179
6180 class AssertTypeTests(BaseTestCase):
6181
6182 def test_basics(self):
6183 arg = 42
6184 self.assertIs(assert_type(arg, int), arg)
6185 self.assertIs(assert_type(arg, str | float), arg)
6186 self.assertIs(assert_type(arg, AnyStr), arg)
6187 self.assertIs(assert_type(arg, None), arg)
6188
6189 def test_errors(self):
6190 # Bogus calls are not expected to fail.
6191 arg = 42
6192 self.assertIs(assert_type(arg, 42), arg)
6193 self.assertIs(assert_type(arg, 'hello'), arg)
6194
6195
6196 # We need this to make sure that `@no_type_check` respects `__module__` attr:
6197 @no_type_check
6198 class NoTypeCheck_Outer:
6199 Inner = ann_module8.NoTypeCheck_Outer.Inner
6200
6201 @no_type_check
6202 class NoTypeCheck_WithFunction:
6203 NoTypeCheck_function = ann_module8.NoTypeCheck_function
6204
6205
6206 class NoTypeCheckTests(BaseTestCase):
6207 def test_no_type_check(self):
6208
6209 @no_type_check
6210 def foo(a: 'whatevers') -> {}:
6211 pass
6212
6213 th = get_type_hints(foo)
6214 self.assertEqual(th, {})
6215
6216 def test_no_type_check_class(self):
6217
6218 @no_type_check
6219 class C:
6220 def foo(a: 'whatevers') -> {}:
6221 pass
6222
6223 cth = get_type_hints(C.foo)
6224 self.assertEqual(cth, {})
6225 ith = get_type_hints(C().foo)
6226 self.assertEqual(ith, {})
6227
6228 def test_no_type_check_no_bases(self):
6229 class C:
6230 def meth(self, x: int): ...
6231 @no_type_check
6232 class D(C):
6233 c = C
6234
6235 # verify that @no_type_check never affects bases
6236 self.assertEqual(get_type_hints(C.meth), {'x': int})
6237
6238 # and never child classes:
6239 class Child(D):
6240 def foo(self, x: int): ...
6241
6242 self.assertEqual(get_type_hints(Child.foo), {'x': int})
6243
6244 def test_no_type_check_nested_types(self):
6245 # See https://bugs.python.org/issue46571
6246 class Other:
6247 o: int
6248 class B: # Has the same `__name__`` as `A.B` and different `__qualname__`
6249 o: int
6250 @no_type_check
6251 class A:
6252 a: int
6253 class B:
6254 b: int
6255 class C:
6256 c: int
6257 class D:
6258 d: int
6259
6260 Other = Other
6261
6262 for klass in [A, A.B, A.B.C, A.D]:
6263 with self.subTest(klass=klass):
6264 self.assertIs(klass.__no_type_check__, True)
6265 self.assertEqual(get_type_hints(klass), {})
6266
6267 for not_modified in [Other, B]:
6268 with self.subTest(not_modified=not_modified):
6269 with self.assertRaises(AttributeError):
6270 not_modified.__no_type_check__
6271 self.assertNotEqual(get_type_hints(not_modified), {})
6272
6273 def test_no_type_check_class_and_static_methods(self):
6274 @no_type_check
6275 class Some:
6276 @staticmethod
6277 def st(x: int) -> int: ...
6278 @classmethod
6279 def cl(cls, y: int) -> int: ...
6280
6281 self.assertIs(Some.st.__no_type_check__, True)
6282 self.assertEqual(get_type_hints(Some.st), {})
6283 self.assertIs(Some.cl.__no_type_check__, True)
6284 self.assertEqual(get_type_hints(Some.cl), {})
6285
6286 def test_no_type_check_other_module(self):
6287 self.assertIs(NoTypeCheck_Outer.__no_type_check__, True)
6288 with self.assertRaises(AttributeError):
6289 ann_module8.NoTypeCheck_Outer.__no_type_check__
6290 with self.assertRaises(AttributeError):
6291 ann_module8.NoTypeCheck_Outer.Inner.__no_type_check__
6292
6293 self.assertIs(NoTypeCheck_WithFunction.__no_type_check__, True)
6294 with self.assertRaises(AttributeError):
6295 ann_module8.NoTypeCheck_function.__no_type_check__
6296
6297 def test_no_type_check_foreign_functions(self):
6298 # We should not modify this function:
6299 def some(*args: int) -> int:
6300 ...
6301
6302 @no_type_check
6303 class A:
6304 some_alias = some
6305 some_class = classmethod(some)
6306 some_static = staticmethod(some)
6307
6308 with self.assertRaises(AttributeError):
6309 some.__no_type_check__
6310 self.assertEqual(get_type_hints(some), {'args': int, 'return': int})
6311
6312 def test_no_type_check_lambda(self):
6313 @no_type_check
6314 class A:
6315 # Corner case: `lambda` is both an assignment and a function:
6316 bar: Callable[[int], int] = lambda arg: arg
6317
6318 self.assertIs(A.bar.__no_type_check__, True)
6319 self.assertEqual(get_type_hints(A.bar), {})
6320
6321 def test_no_type_check_TypeError(self):
6322 # This simply should not fail with
6323 # `TypeError: can't set attributes of built-in/extension type 'dict'`
6324 no_type_check(dict)
6325
6326 def test_no_type_check_forward_ref_as_string(self):
6327 class C:
6328 foo: typing.ClassVar[int] = 7
6329 class D:
6330 foo: ClassVar[int] = 7
6331 class E:
6332 foo: 'typing.ClassVar[int]' = 7
6333 class F:
6334 foo: 'ClassVar[int]' = 7
6335
6336 expected_result = {'foo': typing.ClassVar[int]}
6337 for clazz in [C, D, E, F]:
6338 self.assertEqual(get_type_hints(clazz), expected_result)
6339
6340 def test_meta_no_type_check(self):
6341 depr_msg = (
6342 "'typing.no_type_check_decorator' is deprecated "
6343 "and slated for removal in Python 3.15"
6344 )
6345 with self.assertWarnsRegex(DeprecationWarning, depr_msg):
6346 @no_type_check_decorator
6347 def magic_decorator(func):
6348 return func
6349
6350 self.assertEqual(magic_decorator.__name__, 'magic_decorator')
6351
6352 @magic_decorator
6353 def foo(a: 'whatevers') -> {}:
6354 pass
6355
6356 @magic_decorator
6357 class C:
6358 def foo(a: 'whatevers') -> {}:
6359 pass
6360
6361 self.assertEqual(foo.__name__, 'foo')
6362 th = get_type_hints(foo)
6363 self.assertEqual(th, {})
6364 cth = get_type_hints(C.foo)
6365 self.assertEqual(cth, {})
6366 ith = get_type_hints(C().foo)
6367 self.assertEqual(ith, {})
6368
6369
6370 class InternalsTests(BaseTestCase):
6371 def test_deprecation_for_no_type_params_passed_to__evaluate(self):
6372 with self.assertWarnsRegex(
6373 DeprecationWarning,
6374 (
6375 "Failing to pass a value to the 'type_params' parameter "
6376 "of 'typing._eval_type' is deprecated"
6377 )
6378 ) as cm:
6379 self.assertEqual(typing._eval_type(list["int"], globals(), {}), list[int])
6380
6381 self.assertEqual(cm.filename, __file__)
6382
6383 f = ForwardRef("int")
6384
6385 with self.assertWarnsRegex(
6386 DeprecationWarning,
6387 (
6388 "Failing to pass a value to the 'type_params' parameter "
6389 "of 'typing.ForwardRef._evaluate' is deprecated"
6390 )
6391 ) as cm:
6392 self.assertIs(f._evaluate(globals(), {}, recursive_guard=frozenset()), int)
6393
6394 self.assertEqual(cm.filename, __file__)
6395
6396 def test_collect_parameters(self):
6397 typing = import_helper.import_fresh_module("typing")
6398 with self.assertWarnsRegex(
6399 DeprecationWarning,
6400 "The private _collect_parameters function is deprecated"
6401 ) as cm:
6402 typing._collect_parameters
6403 self.assertEqual(cm.filename, __file__)
6404
6405 @cpython_only
6406 def test_lazy_import(self):
6407 import_helper.ensure_lazy_imports("typing", {
6408 "warnings",
6409 "inspect",
6410 "re",
6411 "contextlib",
6412 "annotationlib",
6413 })
6414
6415
6416 @lru_cache()
6417 def cached_func(x, y):
6418 return 3 * x + y
6419
6420
6421 class MethodHolder:
6422 @classmethod
6423 def clsmethod(cls): ...
6424 @staticmethod
6425 def stmethod(): ...
6426 def method(self): ...
6427
6428
6429 class OverloadTests(BaseTestCase):
6430
6431 def test_overload_fails(self):
6432 with self.assertRaises(NotImplementedError):
6433
6434 @overload
6435 def blah():
6436 pass
6437
6438 blah()
6439
6440 def test_overload_succeeds(self):
6441 @overload
6442 def blah():
6443 pass
6444
6445 def blah():
6446 pass
6447
6448 blah()
6449
6450 @cpython_only # gh-98713
6451 def test_overload_on_compiled_functions(self):
6452 with patch("typing._overload_registry",
6453 defaultdict(lambda: defaultdict(dict))):
6454 # The registry starts out empty:
6455 self.assertEqual(typing._overload_registry, {})
6456
6457 # This should just not fail:
6458 overload(sum)
6459 overload(print)
6460
6461 # No overloads are recorded (but, it still has a side-effect):
6462 self.assertEqual(typing.get_overloads(sum), [])
6463 self.assertEqual(typing.get_overloads(print), [])
6464
6465 def set_up_overloads(self):
6466 def blah():
6467 pass
6468
6469 overload1 = blah
6470 overload(blah)
6471
6472 def blah():
6473 pass
6474
6475 overload2 = blah
6476 overload(blah)
6477
6478 def blah():
6479 pass
6480
6481 return blah, [overload1, overload2]
6482
6483 # Make sure we don't clear the global overload registry
6484 @patch("typing._overload_registry",
6485 defaultdict(lambda: defaultdict(dict)))
6486 def test_overload_registry(self):
6487 # The registry starts out empty
6488 self.assertEqual(typing._overload_registry, {})
6489
6490 impl, overloads = self.set_up_overloads()
6491 self.assertNotEqual(typing._overload_registry, {})
6492 self.assertEqual(list(get_overloads(impl)), overloads)
6493
6494 def some_other_func(): pass
6495 overload(some_other_func)
6496 other_overload = some_other_func
6497 def some_other_func(): pass
6498 self.assertEqual(list(get_overloads(some_other_func)), [other_overload])
6499 # Unrelated function still has no overloads:
6500 def not_overloaded(): pass
6501 self.assertEqual(list(get_overloads(not_overloaded)), [])
6502
6503 # Make sure that after we clear all overloads, the registry is
6504 # completely empty.
6505 clear_overloads()
6506 self.assertEqual(typing._overload_registry, {})
6507 self.assertEqual(get_overloads(impl), [])
6508
6509 # Querying a function with no overloads shouldn't change the registry.
6510 def the_only_one(): pass
6511 self.assertEqual(get_overloads(the_only_one), [])
6512 self.assertEqual(typing._overload_registry, {})
6513
6514 def test_overload_registry_repeated(self):
6515 for _ in range(2):
6516 impl, overloads = self.set_up_overloads()
6517
6518 self.assertEqual(list(get_overloads(impl)), overloads)
6519
6520
6521 T_a = TypeVar('T_a')
6522
6523 class AwaitableWrapper(typing.Awaitable[T_a]):
6524
6525 def __init__(self, value):
6526 self.value = value
6527
6528 def __await__(self) -> typing.Iterator[T_a]:
6529 yield
6530 return self.value
6531
6532 class AsyncIteratorWrapper(typing.AsyncIterator[T_a]):
6533
6534 def __init__(self, value: typing.Iterable[T_a]):
6535 self.value = value
6536
6537 def __aiter__(self) -> typing.AsyncIterator[T_a]:
6538 return self
6539
6540 async def __anext__(self) -> T_a:
6541 data = await self.value
6542 if data:
6543 return data
6544 else:
6545 raise StopAsyncIteration
6546
6547 class ACM:
6548 async def __aenter__(self) -> int:
6549 return 42
6550 async def __aexit__(self, etype, eval, tb):
6551 return None
6552
6553 class A:
6554 y: float
6555 class B(A):
6556 x: ClassVar[Optional['B']] = None
6557 y: int
6558 b: int
6559 class CSub(B):
6560 z: ClassVar['CSub'] = B()
6561 class G(Generic[T]):
6562 lst: ClassVar[List[T]] = []
6563
6564 class Loop:
6565 attr: Final['Loop']
6566
6567 class NoneAndForward:
6568 parent: 'NoneAndForward'
6569 meaning: None
6570
6571 class CoolEmployee(NamedTuple):
6572 name: str
6573 cool: int
6574
6575 class CoolEmployeeWithDefault(NamedTuple):
6576 name: str
6577 cool: int = 0
6578
6579 class XMeth(NamedTuple):
6580 x: int
6581 def double(self):
6582 return 2 * self.x
6583
6584 class XRepr(NamedTuple):
6585 x: int
6586 y: int = 1
6587 def __str__(self):
6588 return f'{self.x} -> {self.y}'
6589 def __add__(self, other):
6590 return 0
6591
6592 Label = TypedDict('Label', [('label', str)])
6593
6594 class Point2D(TypedDict):
6595 x: int
6596 y: int
6597
6598 class Point2DGeneric(Generic[T], TypedDict):
6599 a: T
6600 b: T
6601
6602 class Bar(_typed_dict_helper.Foo, total=False):
6603 b: int
6604
6605 class BarGeneric(_typed_dict_helper.FooGeneric[T], total=False):
6606 b: int
6607
6608 class LabelPoint2D(Point2D, Label): ...
6609
6610 class Options(TypedDict, total=False):
6611 log_level: int
6612 log_path: str
6613
6614 class TotalMovie(TypedDict):
6615 title: str
6616 year: NotRequired[int]
6617
6618 class NontotalMovie(TypedDict, total=False):
6619 title: Required[str]
6620 year: int
6621
6622 class ParentNontotalMovie(TypedDict, total=False):
6623 title: Required[str]
6624
6625 class ChildTotalMovie(ParentNontotalMovie):
6626 year: NotRequired[int]
6627
6628 class ParentDeeplyAnnotatedMovie(TypedDict):
6629 title: Annotated[Annotated[Required[str], "foobar"], "another level"]
6630
6631 class ChildDeeplyAnnotatedMovie(ParentDeeplyAnnotatedMovie):
6632 year: NotRequired[Annotated[int, 2000]]
6633
6634 class AnnotatedMovie(TypedDict):
6635 title: Annotated[Required[str], "foobar"]
6636 year: NotRequired[Annotated[int, 2000]]
6637
6638 class DeeplyAnnotatedMovie(TypedDict):
6639 title: Annotated[Annotated[Required[str], "foobar"], "another level"]
6640 year: NotRequired[Annotated[int, 2000]]
6641
6642 class WeirdlyQuotedMovie(TypedDict):
6643 title: Annotated['Annotated[Required[str], "foobar"]', "another level"]
6644 year: NotRequired['Annotated[int, 2000]']
6645
6646 class HasForeignBaseClass(mod_generics_cache.A):
6647 some_xrepr: 'XRepr'
6648 other_a: 'mod_generics_cache.A'
6649
6650 async def g_with(am: typing.AsyncContextManager[int]):
6651 x: int
6652 async with am as x:
6653 return x
6654
6655 try:
6656 g_with(ACM()).send(None)
6657 except StopIteration as e:
6658 assert e.args[0] == 42
6659
6660 gth = get_type_hints
6661
6662 class ForRefExample:
6663 @ann_module.dec
6664 def func(self: 'ForRefExample'):
6665 pass
6666
6667 @ann_module.dec
6668 @ann_module.dec
6669 def nested(self: 'ForRefExample'):
6670 pass
6671
6672
6673 class GetTypeHintsTests(BaseTestCase):
6674 def test_get_type_hints_from_various_objects(self):
6675 # For invalid objects should fail with TypeError (not AttributeError etc).
6676 with self.assertRaises(TypeError):
6677 gth(123)
6678 with self.assertRaises(TypeError):
6679 gth('abc')
6680 with self.assertRaises(TypeError):
6681 gth(None)
6682
6683 def test_get_type_hints_modules(self):
6684 ann_module_type_hints = {'f': Tuple[int, int], 'x': int, 'y': str, 'u': int | float}
6685 self.assertEqual(gth(ann_module), ann_module_type_hints)
6686 self.assertEqual(gth(ann_module2), {})
6687 self.assertEqual(gth(ann_module3), {})
6688
6689 def test_get_type_hints_modules_forwardref(self):
6690 mgc_hints = {'default_a': Optional[mod_generics_cache.A],
6691 'default_b': Optional[mod_generics_cache.B]}
6692 self.assertEqual(gth(mod_generics_cache), mgc_hints)
6693
6694 def test_get_type_hints_classes(self):
6695 self.assertEqual(gth(ann_module.C), # gth will find the right globalns
6696 {'y': Optional[ann_module.C]})
6697 self.assertIsInstance(gth(ann_module.j_class), dict)
6698 self.assertEqual(gth(ann_module.M), {'o': type})
6699 self.assertEqual(gth(ann_module.D),
6700 {'j': str, 'k': str, 'y': Optional[ann_module.C]})
6701 self.assertEqual(gth(ann_module.Y), {'z': int})
6702 self.assertEqual(gth(ann_module.h_class),
6703 {'y': Optional[ann_module.C]})
6704 self.assertEqual(gth(ann_module.S), {'x': str, 'y': str})
6705 self.assertEqual(gth(ann_module.foo), {'x': int})
6706 self.assertEqual(gth(NoneAndForward),
6707 {'parent': NoneAndForward, 'meaning': type(None)})
6708 self.assertEqual(gth(HasForeignBaseClass),
6709 {'some_xrepr': XRepr, 'other_a': mod_generics_cache.A,
6710 'some_b': mod_generics_cache.B})
6711 self.assertEqual(gth(XRepr.__new__),
6712 {'x': int, 'y': int})
6713 self.assertEqual(gth(mod_generics_cache.B),
6714 {'my_inner_a1': mod_generics_cache.B.A,
6715 'my_inner_a2': mod_generics_cache.B.A,
6716 'my_outer_a': mod_generics_cache.A})
6717
6718 def test_get_type_hints_classes_no_implicit_optional(self):
6719 class WithNoneDefault:
6720 field: int = None # most type-checkers won't be happy with it
6721
6722 self.assertEqual(gth(WithNoneDefault), {'field': int})
6723
6724 def test_respect_no_type_check(self):
6725 @no_type_check
6726 class NoTpCheck:
6727 class Inn:
6728 def __init__(self, x: 'not a type'): ...
6729 self.assertIs(NoTpCheck.__no_type_check__, True)
6730 self.assertIs(NoTpCheck.Inn.__init__.__no_type_check__, True)
6731 self.assertEqual(gth(ann_module2.NTC.meth), {})
6732 class ABase(Generic[T]):
6733 def meth(x: int): ...
6734 @no_type_check
6735 class Der(ABase): ...
6736 self.assertEqual(gth(ABase.meth), {'x': int})
6737
6738 def test_get_type_hints_for_builtins(self):
6739 # Should not fail for built-in classes and functions.
6740 self.assertEqual(gth(int), {})
6741 self.assertEqual(gth(type), {})
6742 self.assertEqual(gth(dir), {})
6743 self.assertEqual(gth(len), {})
6744 self.assertEqual(gth(object.__str__), {})
6745 self.assertEqual(gth(object().__str__), {})
6746 self.assertEqual(gth(str.join), {})
6747
6748 def test_previous_behavior(self):
6749 def testf(x, y): ...
6750 testf.__annotations__['x'] = 'int'
6751 self.assertEqual(gth(testf), {'x': int})
6752 def testg(x: None): ...
6753 self.assertEqual(gth(testg), {'x': type(None)})
6754
6755 def test_get_type_hints_for_object_with_annotations(self):
6756 class A: ...
6757 class B: ...
6758 b = B()
6759 b.__annotations__ = {'x': 'A'}
6760 self.assertEqual(gth(b, locals()), {'x': A})
6761
6762 def test_get_type_hints_ClassVar(self):
6763 self.assertEqual(gth(ann_module2.CV, ann_module2.__dict__),
6764 {'var': typing.ClassVar[ann_module2.CV]})
6765 self.assertEqual(gth(B, globals()),
6766 {'y': int, 'x': ClassVar[Optional[B]], 'b': int})
6767 self.assertEqual(gth(CSub, globals()),
6768 {'z': ClassVar[CSub], 'y': int, 'b': int,
6769 'x': ClassVar[Optional[B]]})
6770 self.assertEqual(gth(G), {'lst': ClassVar[List[T]]})
6771
6772 def test_get_type_hints_wrapped_decoratored_func(self):
6773 expects = {'self': ForRefExample}
6774 self.assertEqual(gth(ForRefExample.func), expects)
6775 self.assertEqual(gth(ForRefExample.nested), expects)
6776
6777 def test_get_type_hints_wrapped_cycle_self(self):
6778 # gh-146553: __wrapped__ self-reference must raise ValueError,
6779 # not loop forever.
6780 def f(x: int) -> str: ...
6781 f.__wrapped__ = f
6782 with self.assertRaisesRegex(ValueError, 'wrapper loop'):
6783 get_type_hints(f)
6784
6785 def test_get_type_hints_wrapped_cycle_mutual(self):
6786 # gh-146553: mutual __wrapped__ cycle (a -> b -> a) must raise
6787 # ValueError, not loop forever.
6788 def a(): ...
6789 def b(): ...
6790 a.__wrapped__ = b
6791 b.__wrapped__ = a
6792 with self.assertRaisesRegex(ValueError, 'wrapper loop'):
6793 get_type_hints(a)
6794
6795 def test_get_type_hints_annotated(self):
6796 def foobar(x: List['X']): ...
6797 X = Annotated[int, (1, 10)]
6798 self.assertEqual(
6799 get_type_hints(foobar, globals(), locals()),
6800 {'x': List[int]}
6801 )
6802 self.assertEqual(
6803 get_type_hints(foobar, globals(), locals(), include_extras=True),
6804 {'x': List[Annotated[int, (1, 10)]]}
6805 )
6806
6807 def foobar(x: list[ForwardRef('X')]): ...
6808 X = Annotated[int, (1, 10)]
6809 self.assertEqual(
6810 get_type_hints(foobar, globals(), locals()),
6811 {'x': list[int]}
6812 )
6813 self.assertEqual(
6814 get_type_hints(foobar, globals(), locals(), include_extras=True),
6815 {'x': list[Annotated[int, (1, 10)]]}
6816 )
6817
6818 BA = Tuple[Annotated[T, (1, 0)], ...]
6819 def barfoo(x: BA): ...
6820 self.assertEqual(get_type_hints(barfoo, globals(), locals())['x'], Tuple[T, ...])
6821 self.assertEqual(
6822 get_type_hints(barfoo, globals(), locals(), include_extras=True)['x'],
6823 BA
6824 )
6825
6826 BA = tuple[Annotated[T, (1, 0)], ...]
6827 def barfoo(x: BA): ...
6828 self.assertEqual(get_type_hints(barfoo, globals(), locals())['x'], tuple[T, ...])
6829 self.assertEqual(
6830 get_type_hints(barfoo, globals(), locals(), include_extras=True)['x'],
6831 BA
6832 )
6833
6834 def barfoo2(x: typing.Callable[..., Annotated[List[T], "const"]],
6835 y: typing.Union[int, Annotated[T, "mutable"]]): ...
6836 self.assertEqual(
6837 get_type_hints(barfoo2, globals(), locals()),
6838 {'x': typing.Callable[..., List[T]], 'y': typing.Union[int, T]}
6839 )
6840
6841 BA2 = typing.Callable[..., List[T]]
6842 def barfoo3(x: BA2): ...
6843 self.assertIs(
6844 get_type_hints(barfoo3, globals(), locals(), include_extras=True)["x"],
6845 BA2
6846 )
6847 BA3 = typing.Annotated[int | float, "const"]
6848 def barfoo4(x: BA3): ...
6849 self.assertEqual(
6850 get_type_hints(barfoo4, globals(), locals()),
6851 {"x": int | float}
6852 )
6853 self.assertEqual(
6854 get_type_hints(barfoo4, globals(), locals(), include_extras=True),
6855 {"x": typing.Annotated[int | float, "const"]}
6856 )
6857
6858 def test_get_type_hints_annotated_in_union(self): # bpo-46603
6859 def with_union(x: int | list[Annotated[str, 'meta']]): ...
6860
6861 self.assertEqual(get_type_hints(with_union), {'x': int | list[str]})
6862 self.assertEqual(
6863 get_type_hints(with_union, include_extras=True),
6864 {'x': int | list[Annotated[str, 'meta']]},
6865 )
6866
6867 def test_get_type_hints_annotated_refs(self):
6868
6869 Const = Annotated[T, "Const"]
6870
6871 class MySet(Generic[T]):
6872
6873 def __ior__(self, other: "Const[MySet[T]]") -> "MySet[T]":
6874 ...
6875
6876 def __iand__(self, other: Const["MySet[T]"]) -> "MySet[T]":
6877 ...
6878
6879 self.assertEqual(
6880 get_type_hints(MySet.__iand__, globals(), locals()),
6881 {'other': MySet[T], 'return': MySet[T]}
6882 )
6883
6884 self.assertEqual(
6885 get_type_hints(MySet.__iand__, globals(), locals(), include_extras=True),
6886 {'other': Const[MySet[T]], 'return': MySet[T]}
6887 )
6888
6889 self.assertEqual(
6890 get_type_hints(MySet.__ior__, globals(), locals()),
6891 {'other': MySet[T], 'return': MySet[T]}
6892 )
6893
6894 def test_get_type_hints_annotated_with_none_default(self):
6895 # See: https://bugs.python.org/issue46195
6896 def annotated_with_none_default(x: Annotated[int, 'data'] = None): ...
6897 self.assertEqual(
6898 get_type_hints(annotated_with_none_default),
6899 {'x': int},
6900 )
6901 self.assertEqual(
6902 get_type_hints(annotated_with_none_default, include_extras=True),
6903 {'x': Annotated[int, 'data']},
6904 )
6905
6906 def test_get_type_hints_classes_str_annotations(self):
6907 class Foo:
6908 y = str
6909 x: 'y'
6910 # This previously raised an error under PEP 563.
6911 self.assertEqual(get_type_hints(Foo), {'x': str})
6912
6913 def test_get_type_hints_bad_module(self):
6914 # bpo-41515
6915 class BadModule:
6916 pass
6917 BadModule.__module__ = 'bad' # Something not in sys.modules
6918 self.assertNotIn('bad', sys.modules)
6919 self.assertEqual(get_type_hints(BadModule), {})
6920
6921 def test_get_type_hints_annotated_bad_module(self):
6922 # See https://bugs.python.org/issue44468
6923 class BadBase:
6924 foo: tuple
6925 class BadType(BadBase):
6926 bar: list
6927 BadType.__module__ = BadBase.__module__ = 'bad'
6928 self.assertNotIn('bad', sys.modules)
6929 self.assertEqual(get_type_hints(BadType), {'foo': tuple, 'bar': list})
6930
6931 def test_forward_ref_and_final(self):
6932 # https://bugs.python.org/issue45166
6933 hints = get_type_hints(ann_module5)
6934 self.assertEqual(hints, {'name': Final[str]})
6935
6936 hints = get_type_hints(ann_module5.MyClass)
6937 self.assertEqual(hints, {'value': Final})
6938
6939 def test_top_level_class_var(self):
6940 # This is not meaningful but we don't raise for it.
6941 # https://github.com/python/cpython/issues/133959
6942 hints = get_type_hints(ann_module6)
6943 self.assertEqual(hints, {'wrong': ClassVar[int]})
6944
6945 def test_get_type_hints_typeddict(self):
6946 self.assertEqual(get_type_hints(TotalMovie), {'title': str, 'year': int})
6947 self.assertEqual(get_type_hints(TotalMovie, include_extras=True), {
6948 'title': str,
6949 'year': NotRequired[int],
6950 })
6951
6952 self.assertEqual(get_type_hints(AnnotatedMovie), {'title': str, 'year': int})
6953 self.assertEqual(get_type_hints(AnnotatedMovie, include_extras=True), {
6954 'title': Annotated[Required[str], "foobar"],
6955 'year': NotRequired[Annotated[int, 2000]],
6956 })
6957
6958 self.assertEqual(get_type_hints(DeeplyAnnotatedMovie), {'title': str, 'year': int})
6959 self.assertEqual(get_type_hints(DeeplyAnnotatedMovie, include_extras=True), {
6960 'title': Annotated[Required[str], "foobar", "another level"],
6961 'year': NotRequired[Annotated[int, 2000]],
6962 })
6963
6964 self.assertEqual(get_type_hints(WeirdlyQuotedMovie), {'title': str, 'year': int})
6965 self.assertEqual(get_type_hints(WeirdlyQuotedMovie, include_extras=True), {
6966 'title': Annotated[Required[str], "foobar", "another level"],
6967 'year': NotRequired[Annotated[int, 2000]],
6968 })
6969
6970 self.assertEqual(get_type_hints(_typed_dict_helper.VeryAnnotated), {'a': int})
6971 self.assertEqual(get_type_hints(_typed_dict_helper.VeryAnnotated, include_extras=True), {
6972 'a': Annotated[Required[int], "a", "b", "c"]
6973 })
6974
6975 self.assertEqual(get_type_hints(ChildTotalMovie), {"title": str, "year": int})
6976 self.assertEqual(get_type_hints(ChildTotalMovie, include_extras=True), {
6977 "title": Required[str], "year": NotRequired[int]
6978 })
6979
6980 self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie), {"title": str, "year": int})
6981 self.assertEqual(get_type_hints(ChildDeeplyAnnotatedMovie, include_extras=True), {
6982 "title": Annotated[Required[str], "foobar", "another level"],
6983 "year": NotRequired[Annotated[int, 2000]]
6984 })
6985
6986 def test_get_type_hints_collections_abc_callable(self):
6987 # https://github.com/python/cpython/issues/91621
6988 P = ParamSpec('P')
6989 def f(x: collections.abc.Callable[[int], int]): ...
6990 def g(x: collections.abc.Callable[..., int]): ...
6991 def h(x: collections.abc.Callable[P, int]): ...
6992
6993 self.assertEqual(get_type_hints(f), {'x': collections.abc.Callable[[int], int]})
6994 self.assertEqual(get_type_hints(g), {'x': collections.abc.Callable[..., int]})
6995 self.assertEqual(get_type_hints(h), {'x': collections.abc.Callable[P, int]})
6996
6997 def test_get_type_hints_format(self):
6998 class C:
6999 x: undefined
7000
7001 with self.assertRaises(NameError):
7002 get_type_hints(C)
7003
7004 with self.assertRaises(NameError):
7005 get_type_hints(C, format=annotationlib.Format.VALUE)
7006
7007 annos = get_type_hints(C, format=annotationlib.Format.FORWARDREF)
7008 self.assertIsInstance(annos, dict)
7009 self.assertEqual(list(annos), ['x'])
7010 self.assertIsInstance(annos['x'], annotationlib.ForwardRef)
7011 self.assertEqual(annos['x'].__arg__, 'undefined')
7012
7013 self.assertEqual(get_type_hints(C, format=annotationlib.Format.STRING),
7014 {'x': 'undefined'})
7015 # Make sure using an int as format also works:
7016 self.assertEqual(get_type_hints(C, format=4), {'x': 'undefined'})
7017
7018 def test_get_type_hints_format_function(self):
7019 def func(x: undefined) -> undefined: ...
7020
7021 # VALUE
7022 with self.assertRaises(NameError):
7023 get_type_hints(func)
7024 with self.assertRaises(NameError):
7025 get_type_hints(func, format=annotationlib.Format.VALUE)
7026
7027 # FORWARDREF
7028 self.assertEqual(
7029 get_type_hints(func, format=annotationlib.Format.FORWARDREF),
7030 {'x': EqualToForwardRef('undefined', owner=func),
7031 'return': EqualToForwardRef('undefined', owner=func)},
7032 )
7033
7034 # STRING
7035 self.assertEqual(get_type_hints(func, format=annotationlib.Format.STRING),
7036 {'x': 'undefined', 'return': 'undefined'})
7037
7038 def test_callable_with_ellipsis_forward(self):
7039
7040 def foo(a: 'Callable[..., T]'):
7041 pass
7042
7043 self.assertEqual(get_type_hints(foo, globals(), locals()),
7044 {'a': Callable[..., T]})
7045
7046 def test_special_forms_no_forward(self):
7047 def f(x: ClassVar[int]):
7048 pass
7049 self.assertEqual(get_type_hints(f), {'x': ClassVar[int]})
7050
7051 def test_special_forms_forward(self):
7052
7053 class C:
7054 a: Annotated['ClassVar[int]', (3, 5)] = 4
7055 b: Annotated['Final[int]', "const"] = 4
7056 x: 'ClassVar' = 4
7057 y: 'Final' = 4
7058
7059 class CF:
7060 b: List['Final[int]'] = 4
7061
7062 self.assertEqual(get_type_hints(C, globals())['a'], ClassVar[int])
7063 self.assertEqual(get_type_hints(C, globals())['b'], Final[int])
7064 self.assertEqual(get_type_hints(C, globals())['x'], ClassVar)
7065 self.assertEqual(get_type_hints(C, globals())['y'], Final)
7066 lfi = get_type_hints(CF, globals())['b']
7067 self.assertIs(get_origin(lfi), list)
7068 self.assertEqual(get_args(lfi), (Final[int],))
7069
7070 def test_union_forward_recursion(self):
7071 ValueList = List['Value']
7072 Value = Union[str, ValueList]
7073
7074 class C:
7075 foo: List[Value]
7076 class D:
7077 foo: Union[Value, ValueList]
7078 class E:
7079 foo: Union[List[Value], ValueList]
7080 class F:
7081 foo: Union[Value, List[Value], ValueList]
7082
7083 self.assertEqual(get_type_hints(C, globals(), locals()), get_type_hints(C, globals(), locals()))
7084 self.assertEqual(get_type_hints(C, globals(), locals()),
7085 {'foo': List[Union[str, List[Union[str, List['Value']]]]]})
7086 self.assertEqual(get_type_hints(D, globals(), locals()),
7087 {'foo': Union[str, List[Union[str, List['Value']]]]})
7088 self.assertEqual(get_type_hints(E, globals(), locals()),
7089 {'foo': Union[
7090 List[Union[str, List[Union[str, List['Value']]]]],
7091 List[Union[str, List['Value']]]
7092 ]
7093 })
7094 self.assertEqual(get_type_hints(F, globals(), locals()),
7095 {'foo': Union[
7096 str,
7097 List[Union[str, List['Value']]],
7098 List[Union[str, List[Union[str, List['Value']]]]]
7099 ]
7100 })
7101
7102 def test_tuple_forward(self):
7103
7104 def foo(a: Tuple['T']):
7105 pass
7106
7107 self.assertEqual(get_type_hints(foo, globals(), locals()),
7108 {'a': Tuple[T]})
7109
7110 def foo(a: tuple[ForwardRef('T')]):
7111 pass
7112
7113 self.assertEqual(get_type_hints(foo, globals(), locals()),
7114 {'a': tuple[T]})
7115
7116 def test_double_forward(self):
7117 def foo(a: 'List[\'int\']'):
7118 pass
7119 self.assertEqual(get_type_hints(foo, globals(), locals()),
7120 {'a': List[int]})
7121
7122 def test_union_forward(self):
7123
7124 def foo(a: Union['T']):
7125 pass
7126
7127 self.assertEqual(get_type_hints(foo, globals(), locals()),
7128 {'a': Union[T]})
7129
7130 def foo(a: tuple[ForwardRef('T')] | int):
7131 pass
7132
7133 self.assertEqual(get_type_hints(foo, globals(), locals()),
7134 {'a': tuple[T] | int})
7135
7136 def test_default_globals(self):
7137 code = ("class C:\n"
7138 " def foo(self, a: 'C') -> 'D': pass\n"
7139 "class D:\n"
7140 " def bar(self, b: 'D') -> C: pass\n"
7141 )
7142 ns = {}
7143 exec(code, ns)
7144 hints = get_type_hints(ns['C'].foo)
7145 self.assertEqual(hints, {'a': ns['C'], 'return': ns['D']})
7146
7147 def test_final_forward_ref(self):
7148 gth = get_type_hints
7149 self.assertEqual(gth(Loop, globals())['attr'], Final[Loop])
7150 self.assertNotEqual(gth(Loop, globals())['attr'], Final[int])
7151 self.assertNotEqual(gth(Loop, globals())['attr'], Final)
7152
7153 def test_name_error(self):
7154
7155 def foo(a: 'Noode[T]'):
7156 pass
7157
7158 with self.assertRaises(NameError):
7159 get_type_hints(foo, locals())
7160
7161 def test_basics(self):
7162
7163 class Node(Generic[T]):
7164
7165 def __init__(self, label: T):
7166 self.label = label
7167 self.left = self.right = None
7168
7169 def add_both(self,
7170 left: 'Optional[Node[T]]',
7171 right: 'Node[T]' = None,
7172 stuff: int = None,
7173 blah=None):
7174 self.left = left
7175 self.right = right
7176
7177 def add_left(self, node: Optional['Node[T]']):
7178 self.add_both(node, None)
7179
7180 def add_right(self, node: 'Node[T]' = None):
7181 self.add_both(None, node)
7182
7183 t = Node[int]
7184 both_hints = get_type_hints(t.add_both, globals(), locals())
7185 self.assertEqual(both_hints['left'], Optional[Node[T]])
7186 self.assertEqual(both_hints['right'], Node[T])
7187 self.assertEqual(both_hints['stuff'], int)
7188 self.assertNotIn('blah', both_hints)
7189
7190 left_hints = get_type_hints(t.add_left, globals(), locals())
7191 self.assertEqual(left_hints['node'], Optional[Node[T]])
7192
7193 right_hints = get_type_hints(t.add_right, globals(), locals())
7194 self.assertEqual(right_hints['node'], Node[T])
7195
7196 def test_stringified_typeddict(self):
7197 ns = run_code(
7198 """
7199 from __future__ import annotations
7200 from typing import TypedDict
7201 class TD[UniqueT](TypedDict):
7202 a: UniqueT
7203 """
7204 )
7205 TD = ns['TD']
7206 self.assertEqual(TD.__annotations__, {'a': EqualToForwardRef('UniqueT', owner=TD, module=TD.__module__)})
7207 self.assertEqual(get_type_hints(TD), {'a': TD.__type_params__[0]})
7208
7209
7210 class GetUtilitiesTestCase(TestCase):
7211 def test_get_origin(self):
7212 T = TypeVar('T')
7213 Ts = TypeVarTuple('Ts')
7214 P = ParamSpec('P')
7215 class C(Generic[T]): pass
7216 self.assertIs(get_origin(C[int]), C)
7217 self.assertIs(get_origin(C[T]), C)
7218 self.assertIs(get_origin(int), None)
7219 self.assertIs(get_origin(ClassVar[int]), ClassVar)
7220 self.assertIs(get_origin(Union[int, str]), Union)
7221 self.assertIs(get_origin(Literal[42, 43]), Literal)
7222 self.assertIs(get_origin(Final[List[int]]), Final)
7223 self.assertIs(get_origin(Generic), Generic)
7224 self.assertIs(get_origin(Generic[T]), Generic)
7225 self.assertIs(get_origin(List[Tuple[T, T]][int]), list)
7226 self.assertIs(get_origin(Annotated[T, 'thing']), Annotated)
7227 self.assertIs(get_origin(List), list)
7228 self.assertIs(get_origin(Tuple), tuple)
7229 self.assertIs(get_origin(Callable), collections.abc.Callable)
7230 self.assertIs(get_origin(list[int]), list)
7231 self.assertIs(get_origin(list), None)
7232 self.assertIs(get_origin(list | str), Union)
7233 self.assertIs(get_origin(P.args), P)
7234 self.assertIs(get_origin(P.kwargs), P)
7235 self.assertIs(get_origin(Required[int]), Required)
7236 self.assertIs(get_origin(NotRequired[int]), NotRequired)
7237 self.assertIs(get_origin((*Ts,)[0]), Unpack)
7238 self.assertIs(get_origin(Unpack[Ts]), Unpack)
7239 self.assertIs(get_origin((*tuple[*Ts],)[0]), tuple)
7240 self.assertIs(get_origin(Unpack[Tuple[Unpack[Ts]]]), Unpack)
7241
7242 def test_get_args(self):
7243 T = TypeVar('T')
7244 class C(Generic[T]): pass
7245 self.assertEqual(get_args(C[int]), (int,))
7246 self.assertEqual(get_args(C[T]), (T,))
7247 self.assertEqual(get_args(typing.SupportsAbs[int]), (int,)) # Protocol
7248 self.assertEqual(get_args(typing.SupportsAbs[T]), (T,))
7249 self.assertEqual(get_args(Point2DGeneric[int]), (int,)) # TypedDict
7250 self.assertEqual(get_args(Point2DGeneric[T]), (T,))
7251 self.assertEqual(get_args(T), ())
7252 self.assertEqual(get_args(int), ())
7253 self.assertEqual(get_args(Any), ())
7254 self.assertEqual(get_args(Self), ())
7255 self.assertEqual(get_args(LiteralString), ())
7256 self.assertEqual(get_args(ClassVar[int]), (int,))
7257 self.assertEqual(get_args(Union[int, str]), (int, str))
7258 self.assertEqual(get_args(Literal[42, 43]), (42, 43))
7259 self.assertEqual(get_args(Final[List[int]]), (List[int],))
7260 self.assertEqual(get_args(Optional[int]), (int, type(None)))
7261 self.assertEqual(get_args(Union[int, None]), (int, type(None)))
7262 self.assertEqual(get_args(Union[int, Tuple[T, int]][str]),
7263 (int, Tuple[str, int]))
7264 self.assertEqual(get_args(typing.Dict[int, Tuple[T, T]][Optional[int]]),
7265 (int, Tuple[Optional[int], Optional[int]]))
7266 self.assertEqual(get_args(Callable[[], T][int]), ([], int))
7267 self.assertEqual(get_args(Callable[..., int]), (..., int))
7268 self.assertEqual(get_args(Callable[[int], str]), ([int], str))
7269 self.assertEqual(get_args(Union[int, Callable[[Tuple[T, ...]], str]]),
7270 (int, Callable[[Tuple[T, ...]], str]))
7271 self.assertEqual(get_args(Tuple[int, ...]), (int, ...))
7272 self.assertEqual(get_args(Tuple[()]), ())
7273 self.assertEqual(get_args(Annotated[T, 'one', 2, ['three']]), (T, 'one', 2, ['three']))
7274 self.assertEqual(get_args(List), ())
7275 self.assertEqual(get_args(Tuple), ())
7276 self.assertEqual(get_args(Callable), ())
7277 self.assertEqual(get_args(list[int]), (int,))
7278 self.assertEqual(get_args(list), ())
7279 self.assertEqual(get_args(collections.abc.Callable[[int], str]), ([int], str))
7280 self.assertEqual(get_args(collections.abc.Callable[..., str]), (..., str))
7281 self.assertEqual(get_args(collections.abc.Callable[[], str]), ([], str))
7282 self.assertEqual(get_args(collections.abc.Callable[[int], str]),
7283 get_args(Callable[[int], str]))
7284 P = ParamSpec('P')
7285 self.assertEqual(get_args(P), ())
7286 self.assertEqual(get_args(P.args), ())
7287 self.assertEqual(get_args(P.kwargs), ())
7288 self.assertEqual(get_args(Callable[P, int]), (P, int))
7289 self.assertEqual(get_args(collections.abc.Callable[P, int]), (P, int))
7290 self.assertEqual(get_args(Callable[Concatenate[int, P], int]),
7291 (Concatenate[int, P], int))
7292 self.assertEqual(get_args(collections.abc.Callable[Concatenate[int, P], int]),
7293 (Concatenate[int, P], int))
7294 self.assertEqual(get_args(Concatenate[int, str, P]), (int, str, P))
7295 self.assertEqual(get_args(list | str), (list, str))
7296 self.assertEqual(get_args(Required[int]), (int,))
7297 self.assertEqual(get_args(NotRequired[int]), (int,))
7298 self.assertEqual(get_args(TypeAlias), ())
7299 self.assertEqual(get_args(TypeGuard[int]), (int,))
7300 self.assertEqual(get_args(TypeIs[range]), (range,))
7301 Ts = TypeVarTuple('Ts')
7302 self.assertEqual(get_args(Ts), ())
7303 self.assertEqual(get_args((*Ts,)[0]), (Ts,))
7304 self.assertEqual(get_args(Unpack[Ts]), (Ts,))
7305 self.assertEqual(get_args(tuple[*Ts]), (*Ts,))
7306 self.assertEqual(get_args(tuple[Unpack[Ts]]), (Unpack[Ts],))
7307 self.assertEqual(get_args((*tuple[*Ts],)[0]), (*Ts,))
7308 self.assertEqual(get_args(Unpack[tuple[Unpack[Ts]]]), (tuple[Unpack[Ts]],))
7309
7310
7311 class EvaluateForwardRefTests(BaseTestCase):
7312 def test_evaluate_forward_ref(self):
7313 int_ref = ForwardRef('int')
7314 self.assertIs(typing.evaluate_forward_ref(int_ref), int)
7315 self.assertIs(
7316 typing.evaluate_forward_ref(int_ref, type_params=()),
7317 int,
7318 )
7319 self.assertIs(
7320 typing.evaluate_forward_ref(int_ref, format=annotationlib.Format.VALUE),
7321 int,
7322 )
7323 self.assertIs(
7324 typing.evaluate_forward_ref(
7325 int_ref, format=annotationlib.Format.FORWARDREF,
7326 ),
7327 int,
7328 )
7329 self.assertEqual(
7330 typing.evaluate_forward_ref(
7331 int_ref, format=annotationlib.Format.STRING,
7332 ),
7333 'int',
7334 )
7335
7336 def test_evaluate_forward_ref_undefined(self):
7337 missing = ForwardRef('missing')
7338 with self.assertRaises(NameError):
7339 typing.evaluate_forward_ref(missing)
7340 self.assertIs(
7341 typing.evaluate_forward_ref(
7342 missing, format=annotationlib.Format.FORWARDREF,
7343 ),
7344 missing,
7345 )
7346 self.assertEqual(
7347 typing.evaluate_forward_ref(
7348 missing, format=annotationlib.Format.STRING,
7349 ),
7350 "missing",
7351 )
7352
7353 def test_evaluate_forward_ref_nested(self):
7354 ref = ForwardRef("int | list['str']")
7355 self.assertEqual(
7356 typing.evaluate_forward_ref(ref),
7357 int | list[str],
7358 )
7359 self.assertEqual(
7360 typing.evaluate_forward_ref(ref, format=annotationlib.Format.FORWARDREF),
7361 int | list[str],
7362 )
7363 self.assertEqual(
7364 typing.evaluate_forward_ref(ref, format=annotationlib.Format.STRING),
7365 "int | list['str']",
7366 )
7367
7368 why = ForwardRef('"\'str\'"')
7369 self.assertIs(typing.evaluate_forward_ref(why), str)
7370
7371 def test_evaluate_forward_ref_none(self):
7372 none_ref = ForwardRef('None')
7373 self.assertIs(typing.evaluate_forward_ref(none_ref), None)
7374
7375 def test_globals(self):
7376 A = "str"
7377 ref = ForwardRef('list[A]')
7378 with self.assertRaises(NameError):
7379 typing.evaluate_forward_ref(ref)
7380 self.assertEqual(
7381 typing.evaluate_forward_ref(ref, globals={'A': A}),
7382 list[str],
7383 )
7384
7385 def test_owner(self):
7386 ref = ForwardRef("A")
7387
7388 with self.assertRaises(NameError):
7389 typing.evaluate_forward_ref(ref)
7390
7391 # We default to the globals of `owner`,
7392 # so it no longer raises `NameError`
7393 self.assertIs(
7394 typing.evaluate_forward_ref(ref, owner=Loop), A
7395 )
7396
7397 def test_inherited_owner(self):
7398 # owner passed to evaluate_forward_ref
7399 ref = ForwardRef("list['A']")
7400 self.assertEqual(
7401 typing.evaluate_forward_ref(ref, owner=Loop),
7402 list[A],
7403 )
7404
7405 # owner set on the ForwardRef
7406 ref = ForwardRef("list['A']", owner=Loop)
7407 self.assertEqual(
7408 typing.evaluate_forward_ref(ref),
7409 list[A],
7410 )
7411
7412 def test_partial_evaluation(self):
7413 ref = ForwardRef("list[A]")
7414 with self.assertRaises(NameError):
7415 typing.evaluate_forward_ref(ref)
7416
7417 self.assertEqual(
7418 typing.evaluate_forward_ref(ref, format=annotationlib.Format.FORWARDREF),
7419 list[EqualToForwardRef('A')],
7420 )
7421
7422 def test_with_module(self):
7423 from test.typinganndata import fwdref_module
7424
7425 typing.evaluate_forward_ref(
7426 fwdref_module.fw,)
7427
7428
7429 class CollectionsAbcTests(BaseTestCase):
7430
7431 def test_hashable(self):
7432 self.assertIsInstance(42, typing.Hashable)
7433 self.assertNotIsInstance([], typing.Hashable)
7434
7435 def test_iterable(self):
7436 self.assertIsInstance([], typing.Iterable)
7437 # Due to ABC caching, the second time takes a separate code
7438 # path and could fail. So call this a few times.
7439 self.assertIsInstance([], typing.Iterable)
7440 self.assertIsInstance([], typing.Iterable)
7441 self.assertNotIsInstance(42, typing.Iterable)
7442 # Just in case, also test issubclass() a few times.
7443 self.assertIsSubclass(list, typing.Iterable)
7444 self.assertIsSubclass(list, typing.Iterable)
7445
7446 def test_iterator(self):
7447 it = iter([])
7448 self.assertIsInstance(it, typing.Iterator)
7449 self.assertNotIsInstance(42, typing.Iterator)
7450
7451 def test_awaitable(self):
7452 async def foo() -> typing.Awaitable[int]:
7453 return await AwaitableWrapper(42)
7454 g = foo()
7455 self.assertIsInstance(g, typing.Awaitable)
7456 self.assertNotIsInstance(foo, typing.Awaitable)
7457 g.send(None) # Run foo() till completion, to avoid warning.
7458
7459 def test_coroutine(self):
7460 async def foo():
7461 return
7462 g = foo()
7463 self.assertIsInstance(g, typing.Coroutine)
7464 with self.assertRaises(TypeError):
7465 isinstance(g, typing.Coroutine[int])
7466 self.assertNotIsInstance(foo, typing.Coroutine)
7467 try:
7468 g.send(None)
7469 except StopIteration:
7470 pass
7471
7472 def test_async_iterable(self):
7473 base_it = range(10) # type: Iterator[int]
7474 it = AsyncIteratorWrapper(base_it)
7475 self.assertIsInstance(it, typing.AsyncIterable)
7476 self.assertIsInstance(it, typing.AsyncIterable)
7477 self.assertNotIsInstance(42, typing.AsyncIterable)
7478
7479 def test_async_iterator(self):
7480 base_it = range(10) # type: Iterator[int]
7481 it = AsyncIteratorWrapper(base_it)
7482 self.assertIsInstance(it, typing.AsyncIterator)
7483 self.assertNotIsInstance(42, typing.AsyncIterator)
7484
7485 def test_sized(self):
7486 self.assertIsInstance([], typing.Sized)
7487 self.assertNotIsInstance(42, typing.Sized)
7488
7489 def test_container(self):
7490 self.assertIsInstance([], typing.Container)
7491 self.assertNotIsInstance(42, typing.Container)
7492
7493 def test_collection(self):
7494 self.assertIsInstance(tuple(), typing.Collection)
7495 self.assertIsInstance(frozenset(), typing.Collection)
7496 self.assertIsSubclass(dict, typing.Collection)
7497 self.assertNotIsInstance(42, typing.Collection)
7498
7499 def test_abstractset(self):
7500 self.assertIsInstance(set(), typing.AbstractSet)
7501 self.assertNotIsInstance(42, typing.AbstractSet)
7502
7503 def test_mutableset(self):
7504 self.assertIsInstance(set(), typing.MutableSet)
7505 self.assertNotIsInstance(frozenset(), typing.MutableSet)
7506
7507 def test_mapping(self):
7508 self.assertIsInstance({}, typing.Mapping)
7509 self.assertNotIsInstance(42, typing.Mapping)
7510
7511 def test_mutablemapping(self):
7512 self.assertIsInstance({}, typing.MutableMapping)
7513 self.assertNotIsInstance(42, typing.MutableMapping)
7514
7515 def test_sequence(self):
7516 self.assertIsInstance([], typing.Sequence)
7517 self.assertNotIsInstance(42, typing.Sequence)
7518
7519 def test_mutablesequence(self):
7520 self.assertIsInstance([], typing.MutableSequence)
7521 self.assertNotIsInstance((), typing.MutableSequence)
7522
7523 def test_bytestring(self):
7524 with self.assertWarns(DeprecationWarning):
7525 self.assertIsInstance(b'', typing.ByteString)
7526 with self.assertWarns(DeprecationWarning):
7527 self.assertIsInstance(bytearray(b''), typing.ByteString)
7528 with self.assertWarns(DeprecationWarning):
7529 class Foo(typing.ByteString): ...
7530 with self.assertWarns(DeprecationWarning):
7531 class Bar(typing.ByteString, typing.Awaitable): ...
7532
7533 def test_list(self):
7534 self.assertIsSubclass(list, typing.List)
7535
7536 def test_deque(self):
7537 self.assertIsSubclass(collections.deque, typing.Deque)
7538 class MyDeque(typing.Deque[int]): ...
7539 self.assertIsInstance(MyDeque(), collections.deque)
7540
7541 def test_counter(self):
7542 self.assertIsSubclass(collections.Counter, typing.Counter)
7543
7544 def test_set(self):
7545 self.assertIsSubclass(set, typing.Set)
7546 self.assertNotIsSubclass(frozenset, typing.Set)
7547
7548 def test_frozenset(self):
7549 self.assertIsSubclass(frozenset, typing.FrozenSet)
7550 self.assertNotIsSubclass(set, typing.FrozenSet)
7551
7552 def test_dict(self):
7553 self.assertIsSubclass(dict, typing.Dict)
7554
7555 def test_dict_subscribe(self):
7556 K = TypeVar('K')
7557 V = TypeVar('V')
7558 self.assertEqual(Dict[K, V][str, int], Dict[str, int])
7559 self.assertEqual(Dict[K, int][str], Dict[str, int])
7560 self.assertEqual(Dict[str, V][int], Dict[str, int])
7561 self.assertEqual(Dict[K, List[V]][str, int], Dict[str, List[int]])
7562 self.assertEqual(Dict[K, List[int]][str], Dict[str, List[int]])
7563 self.assertEqual(Dict[K, list[V]][str, int], Dict[str, list[int]])
7564 self.assertEqual(Dict[K, list[int]][str], Dict[str, list[int]])
7565
7566 def test_no_list_instantiation(self):
7567 with self.assertRaises(TypeError):
7568 typing.List()
7569 with self.assertRaises(TypeError):
7570 typing.List[T]()
7571 with self.assertRaises(TypeError):
7572 typing.List[int]()
7573
7574 def test_list_subclass(self):
7575
7576 class MyList(typing.List[int]):
7577 pass
7578
7579 a = MyList()
7580 self.assertIsInstance(a, MyList)
7581 self.assertIsInstance(a, typing.Sequence)
7582
7583 self.assertIsSubclass(MyList, list)
7584 self.assertNotIsSubclass(list, MyList)
7585
7586 def test_no_dict_instantiation(self):
7587 with self.assertRaises(TypeError):
7588 typing.Dict()
7589 with self.assertRaises(TypeError):
7590 typing.Dict[KT, VT]()
7591 with self.assertRaises(TypeError):
7592 typing.Dict[str, int]()
7593
7594 def test_dict_subclass(self):
7595
7596 class MyDict(typing.Dict[str, int]):
7597 pass
7598
7599 d = MyDict()
7600 self.assertIsInstance(d, MyDict)
7601 self.assertIsInstance(d, typing.MutableMapping)
7602
7603 self.assertIsSubclass(MyDict, dict)
7604 self.assertNotIsSubclass(dict, MyDict)
7605
7606 def test_defaultdict_instantiation(self):
7607 self.assertIs(type(typing.DefaultDict()), collections.defaultdict)
7608 self.assertIs(type(typing.DefaultDict[KT, VT]()), collections.defaultdict)
7609 self.assertIs(type(typing.DefaultDict[str, int]()), collections.defaultdict)
7610
7611 def test_defaultdict_subclass(self):
7612
7613 class MyDefDict(typing.DefaultDict[str, int]):
7614 pass
7615
7616 dd = MyDefDict()
7617 self.assertIsInstance(dd, MyDefDict)
7618
7619 self.assertIsSubclass(MyDefDict, collections.defaultdict)
7620 self.assertNotIsSubclass(collections.defaultdict, MyDefDict)
7621
7622 def test_ordereddict_instantiation(self):
7623 self.assertIs(type(typing.OrderedDict()), collections.OrderedDict)
7624 self.assertIs(type(typing.OrderedDict[KT, VT]()), collections.OrderedDict)
7625 self.assertIs(type(typing.OrderedDict[str, int]()), collections.OrderedDict)
7626
7627 def test_ordereddict_subclass(self):
7628
7629 class MyOrdDict(typing.OrderedDict[str, int]):
7630 pass
7631
7632 od = MyOrdDict()
7633 self.assertIsInstance(od, MyOrdDict)
7634
7635 self.assertIsSubclass(MyOrdDict, collections.OrderedDict)
7636 self.assertNotIsSubclass(collections.OrderedDict, MyOrdDict)
7637
7638 def test_chainmap_instantiation(self):
7639 self.assertIs(type(typing.ChainMap()), collections.ChainMap)
7640 self.assertIs(type(typing.ChainMap[KT, VT]()), collections.ChainMap)
7641 self.assertIs(type(typing.ChainMap[str, int]()), collections.ChainMap)
7642 class CM(typing.ChainMap[KT, VT]): ...
7643 self.assertIs(type(CM[int, str]()), CM)
7644
7645 def test_chainmap_subclass(self):
7646
7647 class MyChainMap(typing.ChainMap[str, int]):
7648 pass
7649
7650 cm = MyChainMap()
7651 self.assertIsInstance(cm, MyChainMap)
7652
7653 self.assertIsSubclass(MyChainMap, collections.ChainMap)
7654 self.assertNotIsSubclass(collections.ChainMap, MyChainMap)
7655
7656 def test_deque_instantiation(self):
7657 self.assertIs(type(typing.Deque()), collections.deque)
7658 self.assertIs(type(typing.Deque[T]()), collections.deque)
7659 self.assertIs(type(typing.Deque[int]()), collections.deque)
7660 class D(typing.Deque[T]): ...
7661 self.assertIs(type(D[int]()), D)
7662
7663 def test_counter_instantiation(self):
7664 self.assertIs(type(typing.Counter()), collections.Counter)
7665 self.assertIs(type(typing.Counter[T]()), collections.Counter)
7666 self.assertIs(type(typing.Counter[int]()), collections.Counter)
7667 class C(typing.Counter[T]): ...
7668 self.assertIs(type(C[int]()), C)
7669
7670 def test_counter_subclass_instantiation(self):
7671
7672 class MyCounter(typing.Counter[int]):
7673 pass
7674
7675 d = MyCounter()
7676 self.assertIsInstance(d, MyCounter)
7677 self.assertIsInstance(d, typing.Counter)
7678 self.assertIsInstance(d, collections.Counter)
7679
7680 def test_no_set_instantiation(self):
7681 with self.assertRaises(TypeError):
7682 typing.Set()
7683 with self.assertRaises(TypeError):
7684 typing.Set[T]()
7685 with self.assertRaises(TypeError):
7686 typing.Set[int]()
7687
7688 def test_set_subclass_instantiation(self):
7689
7690 class MySet(typing.Set[int]):
7691 pass
7692
7693 d = MySet()
7694 self.assertIsInstance(d, MySet)
7695
7696 def test_no_frozenset_instantiation(self):
7697 with self.assertRaises(TypeError):
7698 typing.FrozenSet()
7699 with self.assertRaises(TypeError):
7700 typing.FrozenSet[T]()
7701 with self.assertRaises(TypeError):
7702 typing.FrozenSet[int]()
7703
7704 def test_frozenset_subclass_instantiation(self):
7705
7706 class MyFrozenSet(typing.FrozenSet[int]):
7707 pass
7708
7709 d = MyFrozenSet()
7710 self.assertIsInstance(d, MyFrozenSet)
7711
7712 def test_no_tuple_instantiation(self):
7713 with self.assertRaises(TypeError):
7714 Tuple()
7715 with self.assertRaises(TypeError):
7716 Tuple[T]()
7717 with self.assertRaises(TypeError):
7718 Tuple[int]()
7719
7720 def test_generator(self):
7721 def foo():
7722 yield 42
7723 g = foo()
7724 self.assertIsSubclass(type(g), typing.Generator)
7725
7726 def test_generator_default(self):
7727 g1 = typing.Generator[int]
7728 g2 = typing.Generator[int, None, None]
7729 self.assertEqual(get_args(g1), (int, type(None), type(None)))
7730 self.assertEqual(get_args(g1), get_args(g2))
7731
7732 g3 = typing.Generator[int, float]
7733 g4 = typing.Generator[int, float, None]
7734 self.assertEqual(get_args(g3), (int, float, type(None)))
7735 self.assertEqual(get_args(g3), get_args(g4))
7736
7737 def test_no_generator_instantiation(self):
7738 with self.assertRaises(TypeError):
7739 typing.Generator()
7740 with self.assertRaises(TypeError):
7741 typing.Generator[T, T, T]()
7742 with self.assertRaises(TypeError):
7743 typing.Generator[int, int, int]()
7744
7745 def test_async_generator(self):
7746 async def f():
7747 yield 42
7748 g = f()
7749 self.assertIsSubclass(type(g), typing.AsyncGenerator)
7750
7751 def test_no_async_generator_instantiation(self):
7752 with self.assertRaises(TypeError):
7753 typing.AsyncGenerator()
7754 with self.assertRaises(TypeError):
7755 typing.AsyncGenerator[T, T]()
7756 with self.assertRaises(TypeError):
7757 typing.AsyncGenerator[int, int]()
7758
7759 def test_subclassing(self):
7760
7761 class MMA(typing.MutableMapping):
7762 pass
7763
7764 with self.assertRaises(TypeError): # It's abstract
7765 MMA()
7766
7767 class MMC(MMA):
7768 def __getitem__(self, k):
7769 return None
7770 def __setitem__(self, k, v):
7771 pass
7772 def __delitem__(self, k):
7773 pass
7774 def __iter__(self):
7775 return iter(())
7776 def __len__(self):
7777 return 0
7778
7779 self.assertEqual(len(MMC()), 0)
7780 self.assertTrue(callable(MMC.update))
7781 self.assertIsInstance(MMC(), typing.Mapping)
7782
7783 class MMB(typing.MutableMapping[KT, VT]):
7784 def __getitem__(self, k):
7785 return None
7786 def __setitem__(self, k, v):
7787 pass
7788 def __delitem__(self, k):
7789 pass
7790 def __iter__(self):
7791 return iter(())
7792 def __len__(self):
7793 return 0
7794
7795 self.assertEqual(len(MMB()), 0)
7796 self.assertEqual(len(MMB[str, str]()), 0)
7797 self.assertEqual(len(MMB[KT, VT]()), 0)
7798
7799 self.assertNotIsSubclass(dict, MMA)
7800 self.assertNotIsSubclass(dict, MMB)
7801
7802 self.assertIsSubclass(MMA, typing.Mapping)
7803 self.assertIsSubclass(MMB, typing.Mapping)
7804 self.assertIsSubclass(MMC, typing.Mapping)
7805
7806 self.assertIsInstance(MMB[KT, VT](), typing.Mapping)
7807 self.assertIsInstance(MMB[KT, VT](), collections.abc.Mapping)
7808
7809 self.assertIsSubclass(MMA, collections.abc.Mapping)
7810 self.assertIsSubclass(MMB, collections.abc.Mapping)
7811 self.assertIsSubclass(MMC, collections.abc.Mapping)
7812
7813 with self.assertRaises(TypeError):
7814 issubclass(MMB[str, str], typing.Mapping)
7815 self.assertIsSubclass(MMC, MMA)
7816
7817 class I(typing.Iterable): ...
7818 self.assertNotIsSubclass(list, I)
7819
7820 class G(typing.Generator[int, int, int]): ...
7821 def g(): yield 0
7822 self.assertIsSubclass(G, typing.Generator)
7823 self.assertIsSubclass(G, typing.Iterable)
7824 self.assertIsSubclass(G, collections.abc.Generator)
7825 self.assertIsSubclass(G, collections.abc.Iterable)
7826 self.assertNotIsSubclass(type(g), G)
7827
7828 def test_subclassing_async_generator(self):
7829 class G(typing.AsyncGenerator[int, int]):
7830 def asend(self, value):
7831 pass
7832 def athrow(self, typ, val=None, tb=None):
7833 pass
7834
7835 async def g(): yield 0
7836
7837 self.assertIsSubclass(G, typing.AsyncGenerator)
7838 self.assertIsSubclass(G, typing.AsyncIterable)
7839 self.assertIsSubclass(G, collections.abc.AsyncGenerator)
7840 self.assertIsSubclass(G, collections.abc.AsyncIterable)
7841 self.assertNotIsSubclass(type(g), G)
7842
7843 instance = G()
7844 self.assertIsInstance(instance, typing.AsyncGenerator)
7845 self.assertIsInstance(instance, typing.AsyncIterable)
7846 self.assertIsInstance(instance, collections.abc.AsyncGenerator)
7847 self.assertIsInstance(instance, collections.abc.AsyncIterable)
7848 self.assertNotIsInstance(type(g), G)
7849 self.assertNotIsInstance(g, G)
7850
7851 def test_subclassing_subclasshook(self):
7852
7853 class Base(typing.Iterable):
7854 @classmethod
7855 def __subclasshook__(cls, other):
7856 if other.__name__ == 'Foo':
7857 return True
7858 else:
7859 return False
7860
7861 class C(Base): ...
7862 class Foo: ...
7863 class Bar: ...
7864 self.assertIsSubclass(Foo, Base)
7865 self.assertIsSubclass(Foo, C)
7866 self.assertNotIsSubclass(Bar, C)
7867
7868 def test_subclassing_register(self):
7869
7870 class A(typing.Container): ...
7871 class B(A): ...
7872
7873 class C: ...
7874 A.register(C)
7875 self.assertIsSubclass(C, A)
7876 self.assertNotIsSubclass(C, B)
7877
7878 class D: ...
7879 B.register(D)
7880 self.assertIsSubclass(D, A)
7881 self.assertIsSubclass(D, B)
7882
7883 class M(): ...
7884 collections.abc.MutableMapping.register(M)
7885 self.assertIsSubclass(M, typing.Mapping)
7886
7887 def test_collections_as_base(self):
7888
7889 class M(collections.abc.Mapping): ...
7890 self.assertIsSubclass(M, typing.Mapping)
7891 self.assertIsSubclass(M, typing.Iterable)
7892
7893 class S(collections.abc.MutableSequence): ...
7894 self.assertIsSubclass(S, typing.MutableSequence)
7895 self.assertIsSubclass(S, typing.Iterable)
7896
7897 class I(collections.abc.Iterable): ...
7898 self.assertIsSubclass(I, typing.Iterable)
7899
7900 class A(collections.abc.Mapping, metaclass=abc.ABCMeta): ...
7901 class B: ...
7902 A.register(B)
7903 self.assertIsSubclass(B, typing.Mapping)
7904
7905 def test_or_and_ror(self):
7906 self.assertEqual(typing.Sized | typing.Awaitable, Union[typing.Sized, typing.Awaitable])
7907 self.assertEqual(typing.Coroutine | typing.Hashable, Union[typing.Coroutine, typing.Hashable])
7908
7909
7910 class OtherABCTests(BaseTestCase):
7911
7912 def test_contextmanager(self):
7913 @contextlib.contextmanager
7914 def manager():
7915 yield 42
7916
7917 cm = manager()
7918 self.assertIsInstance(cm, typing.ContextManager)
7919 self.assertNotIsInstance(42, typing.ContextManager)
7920
7921 def test_contextmanager_type_params(self):
7922 cm1 = typing.ContextManager[int]
7923 self.assertEqual(get_args(cm1), (int, bool | None))
7924 cm2 = typing.ContextManager[int, None]
7925 self.assertEqual(get_args(cm2), (int, types.NoneType))
7926
7927 type gen_cm[T1, T2] = typing.ContextManager[T1, T2]
7928 self.assertEqual(get_args(gen_cm.__value__[int, None]), (int, types.NoneType))
7929
7930 def test_async_contextmanager(self):
7931 class NotACM:
7932 pass
7933 self.assertIsInstance(ACM(), typing.AsyncContextManager)
7934 self.assertNotIsInstance(NotACM(), typing.AsyncContextManager)
7935 @contextlib.contextmanager
7936 def manager():
7937 yield 42
7938
7939 cm = manager()
7940 self.assertNotIsInstance(cm, typing.AsyncContextManager)
7941 self.assertEqual(typing.AsyncContextManager[int].__args__, (int, bool | None))
7942 with self.assertRaises(TypeError):
7943 isinstance(42, typing.AsyncContextManager[int])
7944 with self.assertRaises(TypeError):
7945 typing.AsyncContextManager[int, str, float]
7946
7947 def test_asynccontextmanager_type_params(self):
7948 cm1 = typing.AsyncContextManager[int]
7949 self.assertEqual(get_args(cm1), (int, bool | None))
7950 cm2 = typing.AsyncContextManager[int, None]
7951 self.assertEqual(get_args(cm2), (int, types.NoneType))
7952
7953
7954 class TypeTests(BaseTestCase):
7955
7956 def test_type_basic(self):
7957
7958 class User: pass
7959 class BasicUser(User): pass
7960 class ProUser(User): pass
7961
7962 def new_user(user_class: Type[User]) -> User:
7963 return user_class()
7964
7965 new_user(BasicUser)
7966
7967 def test_type_typevar(self):
7968
7969 class User: pass
7970 class BasicUser(User): pass
7971 class ProUser(User): pass
7972
7973 U = TypeVar('U', bound=User)
7974
7975 def new_user(user_class: Type[U]) -> U:
7976 return user_class()
7977
7978 new_user(BasicUser)
7979
7980 def test_type_optional(self):
7981 A = Optional[Type[BaseException]]
7982
7983 def foo(a: A) -> Optional[BaseException]:
7984 if a is None:
7985 return None
7986 else:
7987 return a()
7988
7989 self.assertIsInstance(foo(KeyboardInterrupt), KeyboardInterrupt)
7990 self.assertIsNone(foo(None))
7991
7992
7993 class TestModules(TestCase):
7994 func_names = ['_idfunc']
7995
7996 def test_c_functions(self):
7997 for fname in self.func_names:
7998 self.assertEqual(getattr(typing, fname).__module__, '_typing')
7999
8000
8001 class NewTypeTests(BaseTestCase):
8002 @classmethod
8003 def setUpClass(cls):
8004 global UserId
8005 UserId = typing.NewType('UserId', int)
8006 cls.UserName = typing.NewType(cls.__qualname__ + '.UserName', str)
8007
8008 @classmethod
8009 def tearDownClass(cls):
8010 global UserId
8011 del UserId
8012 del cls.UserName
8013
8014 def test_basic(self):
8015 self.assertIsInstance(UserId(5), int)
8016 self.assertIsInstance(self.UserName('Joe'), str)
8017 self.assertEqual(UserId(5) + 1, 6)
8018
8019 def test_errors(self):
8020 with self.assertRaises(TypeError):
8021 issubclass(UserId, int)
8022 with self.assertRaises(TypeError):
8023 class D(UserId):
8024 pass
8025
8026 def test_or(self):
8027 for cls in (int, self.UserName):
8028 with self.subTest(cls=cls):
8029 self.assertEqual(UserId | cls, typing.Union[UserId, cls])
8030 self.assertEqual(cls | UserId, typing.Union[cls, UserId])
8031
8032 self.assertEqual(typing.get_args(UserId | cls), (UserId, cls))
8033 self.assertEqual(typing.get_args(cls | UserId), (cls, UserId))
8034
8035 def test_special_attrs(self):
8036 self.assertEqual(UserId.__name__, 'UserId')
8037 self.assertEqual(UserId.__qualname__, 'UserId')
8038 self.assertEqual(UserId.__module__, __name__)
8039 self.assertEqual(UserId.__supertype__, int)
8040
8041 UserName = self.UserName
8042 self.assertEqual(UserName.__name__, 'UserName')
8043 self.assertEqual(UserName.__qualname__,
8044 self.__class__.__qualname__ + '.UserName')
8045 self.assertEqual(UserName.__module__, __name__)
8046 self.assertEqual(UserName.__supertype__, str)
8047
8048 def test_repr(self):
8049 self.assertEqual(repr(UserId), f'{__name__}.UserId')
8050 self.assertEqual(repr(self.UserName),
8051 f'{__name__}.{self.__class__.__qualname__}.UserName')
8052
8053 def test_pickle(self):
8054 UserAge = typing.NewType('UserAge', float)
8055 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
8056 with self.subTest(proto=proto):
8057 pickled = pickle.dumps(UserId, proto)
8058 loaded = pickle.loads(pickled)
8059 self.assertIs(loaded, UserId)
8060
8061 pickled = pickle.dumps(self.UserName, proto)
8062 loaded = pickle.loads(pickled)
8063 self.assertIs(loaded, self.UserName)
8064
8065 with self.assertRaises(pickle.PicklingError):
8066 pickle.dumps(UserAge, proto)
8067
8068 def test_missing__name__(self):
8069 code = ("import typing\n"
8070 "NT = typing.NewType('NT', int)\n"
8071 )
8072 exec(code, {})
8073
8074 def test_error_message_when_subclassing(self):
8075 with self.assertRaisesRegex(
8076 TypeError,
8077 re.escape(
8078 "Cannot subclass an instance of NewType. Perhaps you were looking for: "
8079 "`ProUserId = NewType('ProUserId', UserId)`"
8080 )
8081 ):
8082 class ProUserId(UserId):
8083 ...
8084
8085
8086 class NamedTupleTests(BaseTestCase):
8087 class NestedEmployee(NamedTuple):
8088 name: str
8089 cool: int
8090
8091 def test_basics(self):
8092 Emp = NamedTuple('Emp', [('name', str), ('id', int)])
8093 self.assertIsSubclass(Emp, tuple)
8094 joe = Emp('Joe', 42)
8095 jim = Emp(name='Jim', id=1)
8096 self.assertIsInstance(joe, Emp)
8097 self.assertIsInstance(joe, tuple)
8098 self.assertEqual(joe.name, 'Joe')
8099 self.assertEqual(joe.id, 42)
8100 self.assertEqual(jim.name, 'Jim')
8101 self.assertEqual(jim.id, 1)
8102 self.assertEqual(Emp.__name__, 'Emp')
8103 self.assertEqual(Emp._fields, ('name', 'id'))
8104 self.assertEqual(Emp.__annotations__,
8105 collections.OrderedDict([('name', str), ('id', int)]))
8106
8107 def test_annotation_usage(self):
8108 tim = CoolEmployee('Tim', 9000)
8109 self.assertIsInstance(tim, CoolEmployee)
8110 self.assertIsInstance(tim, tuple)
8111 self.assertEqual(tim.name, 'Tim')
8112 self.assertEqual(tim.cool, 9000)
8113 self.assertEqual(CoolEmployee.__name__, 'CoolEmployee')
8114 self.assertEqual(CoolEmployee._fields, ('name', 'cool'))
8115 self.assertEqual(CoolEmployee.__annotations__,
8116 collections.OrderedDict(name=str, cool=int))
8117
8118 def test_annotation_usage_with_default(self):
8119 jelle = CoolEmployeeWithDefault('Jelle')
8120 self.assertIsInstance(jelle, CoolEmployeeWithDefault)
8121 self.assertIsInstance(jelle, tuple)
8122 self.assertEqual(jelle.name, 'Jelle')
8123 self.assertEqual(jelle.cool, 0)
8124 cooler_employee = CoolEmployeeWithDefault('Sjoerd', 1)
8125 self.assertEqual(cooler_employee.cool, 1)
8126
8127 self.assertEqual(CoolEmployeeWithDefault.__name__, 'CoolEmployeeWithDefault')
8128 self.assertEqual(CoolEmployeeWithDefault._fields, ('name', 'cool'))
8129 self.assertEqual(CoolEmployeeWithDefault.__annotations__,
8130 dict(name=str, cool=int))
8131 self.assertEqual(CoolEmployeeWithDefault._field_defaults, dict(cool=0))
8132
8133 with self.assertRaises(TypeError):
8134 class NonDefaultAfterDefault(NamedTuple):
8135 x: int = 3
8136 y: int
8137
8138 def test_annotation_usage_with_methods(self):
8139 self.assertEqual(XMeth(1).double(), 2)
8140 self.assertEqual(XMeth(42).x, XMeth(42)[0])
8141 self.assertEqual(str(XRepr(42)), '42 -> 1')
8142 self.assertEqual(XRepr(1, 2) + XRepr(3), 0)
8143
8144 with self.assertRaises(AttributeError):
8145 class XMethBad(NamedTuple):
8146 x: int
8147 def _fields(self):
8148 return 'no chance for this'
8149
8150 with self.assertRaises(AttributeError):
8151 class XMethBad2(NamedTuple):
8152 x: int
8153 def _source(self):
8154 return 'no chance for this as well'
8155
8156 def test_annotation_type_check(self):
8157 # These are rejected by _type_check
8158 with self.assertRaises(TypeError):
8159 class X(NamedTuple):
8160 a: Final
8161 with self.assertRaises(TypeError):
8162 class Y(NamedTuple):
8163 a: (1, 2)
8164
8165 # Conversion by _type_convert
8166 class Z(NamedTuple):
8167 a: None
8168 b: "str"
8169 annos = {'a': type(None), 'b': EqualToForwardRef("str")}
8170 self.assertEqual(Z.__annotations__, annos)
8171 self.assertEqual(Z.__annotate__(annotationlib.Format.VALUE), annos)
8172 self.assertEqual(Z.__annotate__(annotationlib.Format.FORWARDREF), annos)
8173 self.assertEqual(Z.__annotate__(annotationlib.Format.STRING), {"a": "None", "b": "str"})
8174
8175 def test_future_annotations(self):
8176 code = """
8177 from __future__ import annotations
8178 from typing import NamedTuple
8179 class X(NamedTuple):
8180 a: int
8181 b: None
8182 """
8183 ns = run_code(textwrap.dedent(code))
8184 X = ns['X']
8185 self.assertEqual(X.__annotations__, {'a': EqualToForwardRef("int"), 'b': EqualToForwardRef("None")})
8186
8187 def test_deferred_annotations(self):
8188 class X(NamedTuple):
8189 y: undefined
8190
8191 self.assertEqual(X._fields, ('y',))
8192 with self.assertRaises(NameError):
8193 X.__annotations__
8194
8195 undefined = int
8196 self.assertEqual(X.__annotations__, {'y': int})
8197
8198 def test_multiple_inheritance(self):
8199 class A:
8200 pass
8201 with self.assertRaises(TypeError):
8202 class X(NamedTuple, A):
8203 x: int
8204 with self.assertRaises(TypeError):
8205 class Y(NamedTuple, tuple):
8206 x: int
8207 with self.assertRaises(TypeError):
8208 class Z(NamedTuple, NamedTuple):
8209 x: int
8210 class B(NamedTuple):
8211 x: int
8212 with self.assertRaises(TypeError):
8213 class C(NamedTuple, B):
8214 y: str
8215
8216 def test_generic(self):
8217 class X(NamedTuple, Generic[T]):
8218 x: T
8219 self.assertEqual(X.__bases__, (tuple, Generic))
8220 self.assertEqual(X.__orig_bases__, (NamedTuple, Generic[T]))
8221 self.assertEqual(X.__mro__, (X, tuple, Generic, object))
8222
8223 class Y(Generic[T], NamedTuple):
8224 x: T
8225 self.assertEqual(Y.__bases__, (Generic, tuple))
8226 self.assertEqual(Y.__orig_bases__, (Generic[T], NamedTuple))
8227 self.assertEqual(Y.__mro__, (Y, Generic, tuple, object))
8228
8229 for G in X, Y:
8230 with self.subTest(type=G):
8231 self.assertEqual(G.__parameters__, (T,))
8232 self.assertEqual(G[T].__args__, (T,))
8233 self.assertEqual(get_args(G[T]), (T,))
8234 A = G[int]
8235 self.assertIs(A.__origin__, G)
8236 self.assertEqual(A.__args__, (int,))
8237 self.assertEqual(get_args(A), (int,))
8238 self.assertEqual(A.__parameters__, ())
8239
8240 a = A(3)
8241 self.assertIs(type(a), G)
8242 self.assertEqual(a.x, 3)
8243
8244 with self.assertRaises(TypeError):
8245 G[int, str]
8246
8247 def test_generic_pep695(self):
8248 class X[T](NamedTuple):
8249 x: T
8250 T, = X.__type_params__
8251 self.assertIsInstance(T, TypeVar)
8252 self.assertEqual(T.__name__, 'T')
8253 self.assertEqual(X.__bases__, (tuple, Generic))
8254 self.assertEqual(X.__orig_bases__, (NamedTuple, Generic[T]))
8255 self.assertEqual(X.__mro__, (X, tuple, Generic, object))
8256 self.assertEqual(X.__parameters__, (T,))
8257 self.assertEqual(X[str].__args__, (str,))
8258 self.assertEqual(X[str].__parameters__, ())
8259
8260 def test_non_generic_subscript(self):
8261 # For backward compatibility, subscription works
8262 # on arbitrary NamedTuple types.
8263 class Group(NamedTuple):
8264 key: T
8265 group: list[T]
8266 A = Group[int]
8267 self.assertEqual(A.__origin__, Group)
8268 self.assertEqual(A.__parameters__, ())
8269 self.assertEqual(A.__args__, (int,))
8270 a = A(1, [2])
8271 self.assertIs(type(a), Group)
8272 self.assertEqual(a, (1, [2]))
8273
8274 def test_namedtuple_keyword_usage(self):
8275 with self.assertWarnsRegex(
8276 DeprecationWarning,
8277 "Creating NamedTuple classes using keyword arguments is deprecated"
8278 ):
8279 LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int)
8280
8281 nick = LocalEmployee('Nick', 25)
8282 self.assertIsInstance(nick, tuple)
8283 self.assertEqual(nick.name, 'Nick')
8284 self.assertEqual(LocalEmployee.__name__, 'LocalEmployee')
8285 self.assertEqual(LocalEmployee._fields, ('name', 'age'))
8286 self.assertEqual(LocalEmployee.__annotations__, dict(name=str, age=int))
8287
8288 with self.assertRaisesRegex(
8289 TypeError,
8290 "Either list of fields or keywords can be provided to NamedTuple, not both"
8291 ):
8292 NamedTuple('Name', [('x', int)], y=str)
8293
8294 with self.assertRaisesRegex(
8295 TypeError,
8296 "Either list of fields or keywords can be provided to NamedTuple, not both"
8297 ):
8298 NamedTuple('Name', [], y=str)
8299
8300 with self.assertRaisesRegex(
8301 TypeError,
8302 (
8303 r"Cannot pass `None` as the 'fields' parameter "
8304 r"and also specify fields using keyword arguments"
8305 )
8306 ):
8307 NamedTuple('Name', None, x=int)
8308
8309 def test_namedtuple_special_keyword_names(self):
8310 with self.assertWarnsRegex(
8311 DeprecationWarning,
8312 "Creating NamedTuple classes using keyword arguments is deprecated"
8313 ):
8314 NT = NamedTuple("NT", cls=type, self=object, typename=str, fields=list)
8315
8316 self.assertEqual(NT.__name__, 'NT')
8317 self.assertEqual(NT._fields, ('cls', 'self', 'typename', 'fields'))
8318 a = NT(cls=str, self=42, typename='foo', fields=[('bar', tuple)])
8319 self.assertEqual(a.cls, str)
8320 self.assertEqual(a.self, 42)
8321 self.assertEqual(a.typename, 'foo')
8322 self.assertEqual(a.fields, [('bar', tuple)])
8323
8324 def test_empty_namedtuple(self):
8325 expected_warning = re.escape(
8326 "Failing to pass a value for the 'fields' parameter is deprecated "
8327 "and will be disallowed in Python 3.15. "
8328 "To create a NamedTuple class with 0 fields "
8329 "using the functional syntax, "
8330 "pass an empty list, e.g. `NT1 = NamedTuple('NT1', [])`."
8331 )
8332 with self.assertWarnsRegex(DeprecationWarning, fr"^{expected_warning}$"):
8333 NT1 = NamedTuple('NT1')
8334
8335 expected_warning = re.escape(
8336 "Passing `None` as the 'fields' parameter is deprecated "
8337 "and will be disallowed in Python 3.15. "
8338 "To create a NamedTuple class with 0 fields "
8339 "using the functional syntax, "
8340 "pass an empty list, e.g. `NT2 = NamedTuple('NT2', [])`."
8341 )
8342 with self.assertWarnsRegex(DeprecationWarning, fr"^{expected_warning}$"):
8343 NT2 = NamedTuple('NT2', None)
8344
8345 NT3 = NamedTuple('NT2', [])
8346
8347 class CNT(NamedTuple):
8348 pass # empty body
8349
8350 for struct in NT1, NT2, NT3, CNT:
8351 with self.subTest(struct=struct):
8352 self.assertEqual(struct._fields, ())
8353 self.assertEqual(struct._field_defaults, {})
8354 self.assertEqual(struct.__annotations__, {})
8355 self.assertIsInstance(struct(), struct)
8356
8357 def test_namedtuple_errors(self):
8358 with self.assertRaises(TypeError):
8359 NamedTuple.__new__()
8360
8361 with self.assertRaisesRegex(
8362 TypeError,
8363 "missing 1 required positional argument"
8364 ):
8365 NamedTuple()
8366
8367 with self.assertRaisesRegex(
8368 TypeError,
8369 "takes from 1 to 2 positional arguments but 3 were given"
8370 ):
8371 NamedTuple('Emp', [('name', str)], None)
8372
8373 with self.assertRaisesRegex(
8374 ValueError,
8375 "Field names cannot start with an underscore"
8376 ):
8377 NamedTuple('Emp', [('_name', str)])
8378
8379 with self.assertRaisesRegex(
8380 TypeError,
8381 "missing 1 required positional argument: 'typename'"
8382 ):
8383 NamedTuple(typename='Emp', name=str, id=int)
8384
8385 def test_copy_and_pickle(self):
8386 global Emp # pickle wants to reference the class by name
8387 Emp = NamedTuple('Emp', [('name', str), ('cool', int)])
8388 for cls in Emp, CoolEmployee, self.NestedEmployee:
8389 with self.subTest(cls=cls):
8390 jane = cls('jane', 37)
8391 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
8392 z = pickle.dumps(jane, proto)
8393 jane2 = pickle.loads(z)
8394 self.assertEqual(jane2, jane)
8395 self.assertIsInstance(jane2, cls)
8396
8397 jane2 = copy(jane)
8398 self.assertEqual(jane2, jane)
8399 self.assertIsInstance(jane2, cls)
8400
8401 jane2 = deepcopy(jane)
8402 self.assertEqual(jane2, jane)
8403 self.assertIsInstance(jane2, cls)
8404
8405 def test_orig_bases(self):
8406 T = TypeVar('T')
8407
8408 class SimpleNamedTuple(NamedTuple):
8409 pass
8410
8411 class GenericNamedTuple(NamedTuple, Generic[T]):
8412 pass
8413
8414 self.assertEqual(SimpleNamedTuple.__orig_bases__, (NamedTuple,))
8415 self.assertEqual(GenericNamedTuple.__orig_bases__, (NamedTuple, Generic[T]))
8416
8417 CallNamedTuple = NamedTuple('CallNamedTuple', [])
8418
8419 self.assertEqual(CallNamedTuple.__orig_bases__, (NamedTuple,))
8420
8421 def test_setname_called_on_values_in_class_dictionary(self):
8422 class Vanilla:
8423 def __set_name__(self, owner, name):
8424 self.name = name
8425
8426 class Foo(NamedTuple):
8427 attr = Vanilla()
8428
8429 foo = Foo()
8430 self.assertEqual(len(foo), 0)
8431 self.assertNotIn('attr', Foo._fields)
8432 self.assertIsInstance(foo.attr, Vanilla)
8433 self.assertEqual(foo.attr.name, "attr")
8434
8435 class Bar(NamedTuple):
8436 attr: Vanilla = Vanilla()
8437
8438 bar = Bar()
8439 self.assertEqual(len(bar), 1)
8440 self.assertIn('attr', Bar._fields)
8441 self.assertIsInstance(bar.attr, Vanilla)
8442 self.assertEqual(bar.attr.name, "attr")
8443
8444 def test_setname_raises_the_same_as_on_other_classes(self):
8445 class CustomException(BaseException): pass
8446
8447 class Annoying:
8448 def __set_name__(self, owner, name):
8449 raise CustomException
8450
8451 annoying = Annoying()
8452
8453 with self.assertRaises(CustomException) as cm:
8454 class NormalClass:
8455 attr = annoying
8456 normal_exception = cm.exception
8457
8458 with self.assertRaises(CustomException) as cm:
8459 class NamedTupleClass(NamedTuple):
8460 attr = annoying
8461 namedtuple_exception = cm.exception
8462
8463 self.assertIs(type(namedtuple_exception), CustomException)
8464 self.assertIs(type(namedtuple_exception), type(normal_exception))
8465
8466 self.assertEqual(len(namedtuple_exception.__notes__), 1)
8467 self.assertEqual(
8468 len(namedtuple_exception.__notes__), len(normal_exception.__notes__)
8469 )
8470
8471 expected_note = (
8472 "Error calling __set_name__ on 'Annoying' instance "
8473 "'attr' in 'NamedTupleClass'"
8474 )
8475 self.assertEqual(namedtuple_exception.__notes__[0], expected_note)
8476 self.assertEqual(
8477 namedtuple_exception.__notes__[0],
8478 normal_exception.__notes__[0].replace("NormalClass", "NamedTupleClass")
8479 )
8480
8481 def test_strange_errors_when_accessing_set_name_itself(self):
8482 class CustomException(Exception): pass
8483
8484 class Meta(type):
8485 def __getattribute__(self, attr):
8486 if attr == "__set_name__":
8487 raise CustomException
8488 return object.__getattribute__(self, attr)
8489
8490 class VeryAnnoying(metaclass=Meta): pass
8491
8492 very_annoying = VeryAnnoying()
8493
8494 with self.assertRaises(CustomException):
8495 class Foo(NamedTuple):
8496 attr = very_annoying
8497
8498 def test_super_explicitly_disallowed(self):
8499 expected_message = (
8500 "uses of super() and __class__ are unsupported "
8501 "in methods of NamedTuple subclasses"
8502 )
8503
8504 with self.assertRaises(TypeError, msg=expected_message):
8505 class ThisWontWork(NamedTuple):
8506 def __repr__(self):
8507 return super().__repr__()
8508
8509 with self.assertRaises(TypeError, msg=expected_message):
8510 class ThisWontWorkEither(NamedTuple):
8511 @property
8512 def name(self):
8513 return __class__.__name__
8514
8515
8516 class TypedDictTests(BaseTestCase):
8517 def test_basics_functional_syntax(self):
8518 Emp = TypedDict('Emp', {'name': str, 'id': int})
8519 self.assertIsSubclass(Emp, dict)
8520 self.assertIsSubclass(Emp, typing.MutableMapping)
8521 self.assertNotIsSubclass(Emp, collections.abc.Sequence)
8522 jim = Emp(name='Jim', id=1)
8523 self.assertIs(type(jim), dict)
8524 self.assertEqual(jim['name'], 'Jim')
8525 self.assertEqual(jim['id'], 1)
8526 self.assertEqual(Emp.__name__, 'Emp')
8527 self.assertEqual(Emp.__module__, __name__)
8528 self.assertEqual(Emp.__bases__, (dict,))
8529 annos = {'name': str, 'id': int}
8530 self.assertEqual(Emp.__annotations__, annos)
8531 self.assertEqual(Emp.__annotate__(annotationlib.Format.VALUE), annos)
8532 self.assertEqual(Emp.__annotate__(annotationlib.Format.FORWARDREF), annos)
8533 self.assertEqual(Emp.__annotate__(annotationlib.Format.STRING), {'name': 'str', 'id': 'int'})
8534 self.assertEqual(Emp.__total__, True)
8535 self.assertEqual(Emp.__required_keys__, {'name', 'id'})
8536 self.assertIsInstance(Emp.__required_keys__, frozenset)
8537 self.assertEqual(Emp.__optional_keys__, set())
8538 self.assertIsInstance(Emp.__optional_keys__, frozenset)
8539
8540 def test_typeddict_create_errors(self):
8541 with self.assertRaises(TypeError):
8542 TypedDict.__new__()
8543 with self.assertRaises(TypeError):
8544 TypedDict()
8545 with self.assertRaises(TypeError):
8546 TypedDict('Emp', [('name', str)], None)
8547 with self.assertRaises(TypeError):
8548 TypedDict(_typename='Emp')
8549 with self.assertRaises(TypeError):
8550 TypedDict('Emp', name=str, id=int)
8551
8552 def test_typeddict_errors(self):
8553 Emp = TypedDict('Emp', {'name': str, 'id': int})
8554 self.assertEqual(TypedDict.__module__, 'typing')
8555 jim = Emp(name='Jim', id=1)
8556 with self.assertRaises(TypeError):
8557 isinstance({}, Emp)
8558 with self.assertRaises(TypeError):
8559 isinstance(jim, Emp)
8560 with self.assertRaises(TypeError):
8561 issubclass(dict, Emp)
8562 with self.assertRaises(TypeError):
8563 TypedDict('Hi', [('x', int)], y=int)
8564
8565 def test_py36_class_syntax_usage(self):
8566 self.assertEqual(LabelPoint2D.__name__, 'LabelPoint2D')
8567 self.assertEqual(LabelPoint2D.__module__, __name__)
8568 self.assertEqual(LabelPoint2D.__annotations__, {'x': int, 'y': int, 'label': str})
8569 self.assertEqual(LabelPoint2D.__bases__, (dict,))
8570 self.assertEqual(LabelPoint2D.__total__, True)
8571 self.assertNotIsSubclass(LabelPoint2D, typing.Sequence)
8572 not_origin = Point2D(x=0, y=1)
8573 self.assertEqual(not_origin['x'], 0)
8574 self.assertEqual(not_origin['y'], 1)
8575 other = LabelPoint2D(x=0, y=1, label='hi')
8576 self.assertEqual(other['label'], 'hi')
8577
8578 def test_pickle(self):
8579 global EmpD # pickle wants to reference the class by name
8580 EmpD = TypedDict('EmpD', {'name': str, 'id': int})
8581 jane = EmpD({'name': 'jane', 'id': 37})
8582 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
8583 z = pickle.dumps(jane, proto)
8584 jane2 = pickle.loads(z)
8585 self.assertEqual(jane2, jane)
8586 self.assertEqual(jane2, {'name': 'jane', 'id': 37})
8587 ZZ = pickle.dumps(EmpD, proto)
8588 EmpDnew = pickle.loads(ZZ)
8589 self.assertEqual(EmpDnew({'name': 'jane', 'id': 37}), jane)
8590
8591 def test_pickle_generic(self):
8592 point = Point2DGeneric(a=5.0, b=3.0)
8593 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
8594 z = pickle.dumps(point, proto)
8595 point2 = pickle.loads(z)
8596 self.assertEqual(point2, point)
8597 self.assertEqual(point2, {'a': 5.0, 'b': 3.0})
8598 ZZ = pickle.dumps(Point2DGeneric, proto)
8599 Point2DGenericNew = pickle.loads(ZZ)
8600 self.assertEqual(Point2DGenericNew({'a': 5.0, 'b': 3.0}), point)
8601
8602 def test_optional(self):
8603 EmpD = TypedDict('EmpD', {'name': str, 'id': int})
8604
8605 self.assertEqual(typing.Optional[EmpD], typing.Union[None, EmpD])
8606 self.assertNotEqual(typing.List[EmpD], typing.Tuple[EmpD])
8607
8608 def test_total(self):
8609 D = TypedDict('D', {'x': int}, total=False)
8610 self.assertEqual(D(), {})
8611 self.assertEqual(D(x=1), {'x': 1})
8612 self.assertEqual(D.__total__, False)
8613 self.assertEqual(D.__required_keys__, frozenset())
8614 self.assertIsInstance(D.__required_keys__, frozenset)
8615 self.assertEqual(D.__optional_keys__, {'x'})
8616 self.assertIsInstance(D.__optional_keys__, frozenset)
8617
8618 self.assertEqual(Options(), {})
8619 self.assertEqual(Options(log_level=2), {'log_level': 2})
8620 self.assertEqual(Options.__total__, False)
8621 self.assertEqual(Options.__required_keys__, frozenset())
8622 self.assertEqual(Options.__optional_keys__, {'log_level', 'log_path'})
8623
8624 def test_total_inherits_non_total(self):
8625 class TD1(TypedDict, total=False):
8626 a: int
8627
8628 self.assertIs(TD1.__total__, False)
8629
8630 class TD2(TD1):
8631 b: str
8632
8633 self.assertIs(TD2.__total__, True)
8634
8635 def test_total_with_assigned_value(self):
8636 class TD(TypedDict):
8637 __total__ = "some_value"
8638
8639 self.assertIs(TD.__total__, True)
8640
8641 class TD2(TypedDict, total=True):
8642 __total__ = "some_value"
8643
8644 self.assertIs(TD2.__total__, True)
8645
8646 class TD3(TypedDict, total=False):
8647 __total__ = "some value"
8648
8649 self.assertIs(TD3.__total__, False)
8650
8651 def test_optional_keys(self):
8652 class Point2Dor3D(Point2D, total=False):
8653 z: int
8654
8655 self.assertEqual(Point2Dor3D.__required_keys__, frozenset(['x', 'y']))
8656 self.assertIsInstance(Point2Dor3D.__required_keys__, frozenset)
8657 self.assertEqual(Point2Dor3D.__optional_keys__, frozenset(['z']))
8658 self.assertIsInstance(Point2Dor3D.__optional_keys__, frozenset)
8659
8660 def test_keys_inheritance(self):
8661 class BaseAnimal(TypedDict):
8662 name: str
8663
8664 class Animal(BaseAnimal, total=False):
8665 voice: str
8666 tail: bool
8667
8668 class Cat(Animal):
8669 fur_color: str
8670
8671 self.assertEqual(BaseAnimal.__required_keys__, frozenset(['name']))
8672 self.assertEqual(BaseAnimal.__optional_keys__, frozenset([]))
8673 self.assertEqual(BaseAnimal.__annotations__, {'name': str})
8674
8675 self.assertEqual(Animal.__required_keys__, frozenset(['name']))
8676 self.assertEqual(Animal.__optional_keys__, frozenset(['tail', 'voice']))
8677 self.assertEqual(Animal.__annotations__, {
8678 'name': str,
8679 'tail': bool,
8680 'voice': str,
8681 })
8682
8683 self.assertEqual(Cat.__required_keys__, frozenset(['name', 'fur_color']))
8684 self.assertEqual(Cat.__optional_keys__, frozenset(['tail', 'voice']))
8685 self.assertEqual(Cat.__annotations__, {
8686 'fur_color': str,
8687 'name': str,
8688 'tail': bool,
8689 'voice': str,
8690 })
8691
8692 def test_keys_inheritance_with_same_name(self):
8693 class NotTotal(TypedDict, total=False):
8694 a: int
8695
8696 class Total(NotTotal):
8697 a: int
8698
8699 self.assertEqual(NotTotal.__required_keys__, frozenset())
8700 self.assertEqual(NotTotal.__optional_keys__, frozenset(['a']))
8701 self.assertEqual(Total.__required_keys__, frozenset(['a']))
8702 self.assertEqual(Total.__optional_keys__, frozenset())
8703
8704 class Base(TypedDict):
8705 a: NotRequired[int]
8706 b: Required[int]
8707
8708 class Child(Base):
8709 a: Required[int]
8710 b: NotRequired[int]
8711
8712 self.assertEqual(Base.__required_keys__, frozenset(['b']))
8713 self.assertEqual(Base.__optional_keys__, frozenset(['a']))
8714 self.assertEqual(Child.__required_keys__, frozenset(['a']))
8715 self.assertEqual(Child.__optional_keys__, frozenset(['b']))
8716
8717 def test_multiple_inheritance_with_same_key(self):
8718 class Base1(TypedDict):
8719 a: NotRequired[int]
8720
8721 class Base2(TypedDict):
8722 a: Required[str]
8723
8724 class Child(Base1, Base2):
8725 pass
8726
8727 # Last base wins
8728 self.assertEqual(Child.__annotations__, {'a': Required[str]})
8729 self.assertEqual(Child.__required_keys__, frozenset(['a']))
8730 self.assertEqual(Child.__optional_keys__, frozenset())
8731
8732 def test_inheritance_pep563(self):
8733 def _make_td(future, class_name, annos, base, extra_names=None):
8734 lines = []
8735 if future:
8736 lines.append('from __future__ import annotations')
8737 lines.append('from typing import TypedDict')
8738 lines.append(f'class {class_name}({base}):')
8739 for name, anno in annos.items():
8740 lines.append(f' {name}: {anno}')
8741 code = '\n'.join(lines)
8742 ns = run_code(code, extra_names)
8743 return ns[class_name]
8744
8745 for base_future in (True, False):
8746 for child_future in (True, False):
8747 with self.subTest(base_future=base_future, child_future=child_future):
8748 base = _make_td(
8749 base_future, "Base", {"base": "int"}, "TypedDict"
8750 )
8751 self.assertIsNotNone(base.__annotate__)
8752 child = _make_td(
8753 child_future, "Child", {"child": "int"}, "Base", {"Base": base}
8754 )
8755 base_anno = ForwardRef("int", module="builtins", owner=base) if base_future else int
8756 child_anno = ForwardRef("int", module="builtins", owner=child) if child_future else int
8757 self.assertEqual(base.__annotations__, {'base': base_anno})
8758 self.assertEqual(
8759 child.__annotations__, {'child': child_anno, 'base': base_anno}
8760 )
8761
8762 def test_required_notrequired_keys(self):
8763 self.assertEqual(NontotalMovie.__required_keys__,
8764 frozenset({"title"}))
8765 self.assertEqual(NontotalMovie.__optional_keys__,
8766 frozenset({"year"}))
8767
8768 self.assertEqual(TotalMovie.__required_keys__,
8769 frozenset({"title"}))
8770 self.assertEqual(TotalMovie.__optional_keys__,
8771 frozenset({"year"}))
8772
8773 self.assertEqual(_typed_dict_helper.VeryAnnotated.__required_keys__,
8774 frozenset())
8775 self.assertEqual(_typed_dict_helper.VeryAnnotated.__optional_keys__,
8776 frozenset({"a"}))
8777
8778 self.assertEqual(AnnotatedMovie.__required_keys__,
8779 frozenset({"title"}))
8780 self.assertEqual(AnnotatedMovie.__optional_keys__,
8781 frozenset({"year"}))
8782
8783 self.assertEqual(WeirdlyQuotedMovie.__required_keys__,
8784 frozenset({"title"}))
8785 self.assertEqual(WeirdlyQuotedMovie.__optional_keys__,
8786 frozenset({"year"}))
8787
8788 self.assertEqual(ChildTotalMovie.__required_keys__,
8789 frozenset({"title"}))
8790 self.assertEqual(ChildTotalMovie.__optional_keys__,
8791 frozenset({"year"}))
8792
8793 self.assertEqual(ChildDeeplyAnnotatedMovie.__required_keys__,
8794 frozenset({"title"}))
8795 self.assertEqual(ChildDeeplyAnnotatedMovie.__optional_keys__,
8796 frozenset({"year"}))
8797
8798 def test_multiple_inheritance(self):
8799 class One(TypedDict):
8800 one: int
8801 class Two(TypedDict):
8802 two: str
8803 class Untotal(TypedDict, total=False):
8804 untotal: str
8805 Inline = TypedDict('Inline', {'inline': bool})
8806 class Regular:
8807 pass
8808
8809 class Child(One, Two):
8810 child: bool
8811 self.assertEqual(
8812 Child.__required_keys__,
8813 frozenset(['one', 'two', 'child']),
8814 )
8815 self.assertEqual(
8816 Child.__optional_keys__,
8817 frozenset([]),
8818 )
8819 self.assertEqual(
8820 Child.__annotations__,
8821 {'one': int, 'two': str, 'child': bool},
8822 )
8823
8824 class ChildWithOptional(One, Untotal):
8825 child: bool
8826 self.assertEqual(
8827 ChildWithOptional.__required_keys__,
8828 frozenset(['one', 'child']),
8829 )
8830 self.assertEqual(
8831 ChildWithOptional.__optional_keys__,
8832 frozenset(['untotal']),
8833 )
8834 self.assertEqual(
8835 ChildWithOptional.__annotations__,
8836 {'one': int, 'untotal': str, 'child': bool},
8837 )
8838
8839 class ChildWithTotalFalse(One, Untotal, total=False):
8840 child: bool
8841 self.assertEqual(
8842 ChildWithTotalFalse.__required_keys__,
8843 frozenset(['one']),
8844 )
8845 self.assertEqual(
8846 ChildWithTotalFalse.__optional_keys__,
8847 frozenset(['untotal', 'child']),
8848 )
8849 self.assertEqual(
8850 ChildWithTotalFalse.__annotations__,
8851 {'one': int, 'untotal': str, 'child': bool},
8852 )
8853
8854 class ChildWithInlineAndOptional(Untotal, Inline):
8855 child: bool
8856 self.assertEqual(
8857 ChildWithInlineAndOptional.__required_keys__,
8858 frozenset(['inline', 'child']),
8859 )
8860 self.assertEqual(
8861 ChildWithInlineAndOptional.__optional_keys__,
8862 frozenset(['untotal']),
8863 )
8864 self.assertEqual(
8865 ChildWithInlineAndOptional.__annotations__,
8866 {'inline': bool, 'untotal': str, 'child': bool},
8867 )
8868
8869 wrong_bases = [
8870 (One, Regular),
8871 (Regular, One),
8872 (One, Two, Regular),
8873 (Inline, Regular),
8874 (Untotal, Regular),
8875 ]
8876 for bases in wrong_bases:
8877 with self.subTest(bases=bases):
8878 with self.assertRaisesRegex(
8879 TypeError,
8880 'cannot inherit from both a TypedDict type and a non-TypedDict',
8881 ):
8882 class Wrong(*bases):
8883 pass
8884
8885 def test_is_typeddict(self):
8886 self.assertIs(is_typeddict(Point2D), True)
8887 self.assertIs(is_typeddict(Union[str, int]), False)
8888 # classes, not instances
8889 self.assertIs(is_typeddict(Point2D()), False)
8890 call_based = TypedDict('call_based', {'a': int})
8891 self.assertIs(is_typeddict(call_based), True)
8892 self.assertIs(is_typeddict(call_based()), False)
8893
8894 T = TypeVar("T")
8895 class BarGeneric(TypedDict, Generic[T]):
8896 a: T
8897 self.assertIs(is_typeddict(BarGeneric), True)
8898 self.assertIs(is_typeddict(BarGeneric[int]), False)
8899 self.assertIs(is_typeddict(BarGeneric()), False)
8900
8901 class NewGeneric[T](TypedDict):
8902 a: T
8903 self.assertIs(is_typeddict(NewGeneric), True)
8904 self.assertIs(is_typeddict(NewGeneric[int]), False)
8905 self.assertIs(is_typeddict(NewGeneric()), False)
8906
8907 # The TypedDict constructor is not itself a TypedDict
8908 self.assertIs(is_typeddict(TypedDict), False)
8909
8910 def test_get_type_hints(self):
8911 self.assertEqual(
8912 get_type_hints(Bar),
8913 {'a': typing.Optional[int], 'b': int}
8914 )
8915
8916 def test_get_type_hints_generic(self):
8917 self.assertEqual(
8918 get_type_hints(BarGeneric),
8919 {'a': typing.Optional[T], 'b': int}
8920 )
8921
8922 class FooBarGeneric(BarGeneric[int]):
8923 c: str
8924
8925 self.assertEqual(
8926 get_type_hints(FooBarGeneric),
8927 {'a': typing.Optional[T], 'b': int, 'c': str}
8928 )
8929
8930 def test_pep695_generic_typeddict(self):
8931 class A[T](TypedDict):
8932 a: T
8933
8934 T, = A.__type_params__
8935 self.assertIsInstance(T, TypeVar)
8936 self.assertEqual(T.__name__, 'T')
8937 self.assertEqual(A.__bases__, (Generic, dict))
8938 self.assertEqual(A.__orig_bases__, (TypedDict, Generic[T]))
8939 self.assertEqual(A.__mro__, (A, Generic, dict, object))
8940 self.assertEqual(A.__annotations__, {'a': T})
8941 self.assertEqual(A.__annotate__(annotationlib.Format.STRING), {'a': 'T'})
8942 self.assertEqual(A.__parameters__, (T,))
8943 self.assertEqual(A[str].__parameters__, ())
8944 self.assertEqual(A[str].__args__, (str,))
8945
8946 def test_generic_inheritance(self):
8947 class A(TypedDict, Generic[T]):
8948 a: T
8949
8950 self.assertEqual(A.__bases__, (Generic, dict))
8951 self.assertEqual(A.__orig_bases__, (TypedDict, Generic[T]))
8952 self.assertEqual(A.__mro__, (A, Generic, dict, object))
8953 self.assertEqual(A.__annotations__, {'a': T})
8954 self.assertEqual(A.__annotate__(annotationlib.Format.STRING), {'a': 'T'})
8955 self.assertEqual(A.__parameters__, (T,))
8956 self.assertEqual(A[str].__parameters__, ())
8957 self.assertEqual(A[str].__args__, (str,))
8958
8959 class A2(Generic[T], TypedDict):
8960 a: T
8961
8962 self.assertEqual(A2.__bases__, (Generic, dict))
8963 self.assertEqual(A2.__orig_bases__, (Generic[T], TypedDict))
8964 self.assertEqual(A2.__mro__, (A2, Generic, dict, object))
8965 self.assertEqual(A2.__annotations__, {'a': T})
8966 self.assertEqual(A2.__annotate__(annotationlib.Format.STRING), {'a': 'T'})
8967 self.assertEqual(A2.__parameters__, (T,))
8968 self.assertEqual(A2[str].__parameters__, ())
8969 self.assertEqual(A2[str].__args__, (str,))
8970
8971 class B(A[KT], total=False):
8972 b: KT
8973
8974 self.assertEqual(B.__bases__, (Generic, dict))
8975 self.assertEqual(B.__orig_bases__, (A[KT],))
8976 self.assertEqual(B.__mro__, (B, Generic, dict, object))
8977 self.assertEqual(B.__annotations__, {'a': T, 'b': KT})
8978 self.assertEqual(B.__annotate__(annotationlib.Format.STRING), {'a': 'T', 'b': 'KT'})
8979 self.assertEqual(B.__parameters__, (KT,))
8980 self.assertEqual(B.__total__, False)
8981 self.assertEqual(B.__optional_keys__, frozenset(['b']))
8982 self.assertEqual(B.__required_keys__, frozenset(['a']))
8983
8984 self.assertEqual(B[str].__parameters__, ())
8985 self.assertEqual(B[str].__args__, (str,))
8986 self.assertEqual(B[str].__origin__, B)
8987
8988 class C(B[int]):
8989 c: int
8990
8991 self.assertEqual(C.__bases__, (Generic, dict))
8992 self.assertEqual(C.__orig_bases__, (B[int],))
8993 self.assertEqual(C.__mro__, (C, Generic, dict, object))
8994 self.assertEqual(C.__parameters__, ())
8995 self.assertEqual(C.__total__, True)
8996 self.assertEqual(C.__optional_keys__, frozenset(['b']))
8997 self.assertEqual(C.__required_keys__, frozenset(['a', 'c']))
8998 self.assertEqual(C.__annotations__, {
8999 'a': T,
9000 'b': KT,
9001 'c': int,
9002 })
9003 self.assertEqual(C.__annotate__(annotationlib.Format.STRING), {
9004 'a': 'T',
9005 'b': 'KT',
9006 'c': 'int',
9007 })
9008 with self.assertRaises(TypeError):
9009 C[str]
9010
9011
9012 class Point3D(Point2DGeneric[T], Generic[T, KT]):
9013 c: KT
9014
9015 self.assertEqual(Point3D.__bases__, (Generic, dict))
9016 self.assertEqual(Point3D.__orig_bases__, (Point2DGeneric[T], Generic[T, KT]))
9017 self.assertEqual(Point3D.__mro__, (Point3D, Generic, dict, object))
9018 self.assertEqual(Point3D.__parameters__, (T, KT))
9019 self.assertEqual(Point3D.__total__, True)
9020 self.assertEqual(Point3D.__optional_keys__, frozenset())
9021 self.assertEqual(Point3D.__required_keys__, frozenset(['a', 'b', 'c']))
9022 self.assertEqual(Point3D.__annotations__, {
9023 'a': T,
9024 'b': T,
9025 'c': KT,
9026 })
9027 self.assertEqual(Point3D.__annotate__(annotationlib.Format.STRING), {
9028 'a': 'T',
9029 'b': 'T',
9030 'c': 'KT',
9031 })
9032 self.assertEqual(Point3D[int, str].__origin__, Point3D)
9033
9034 with self.assertRaises(TypeError):
9035 Point3D[int]
9036
9037 with self.assertRaises(TypeError):
9038 class Point3D(Point2DGeneric[T], Generic[KT]):
9039 c: KT
9040
9041 def test_implicit_any_inheritance(self):
9042 class A(TypedDict, Generic[T]):
9043 a: T
9044
9045 class B(A[KT], total=False):
9046 b: KT
9047
9048 class WithImplicitAny(B):
9049 c: int
9050
9051 self.assertEqual(WithImplicitAny.__bases__, (Generic, dict,))
9052 self.assertEqual(WithImplicitAny.__mro__, (WithImplicitAny, Generic, dict, object))
9053 # Consistent with GenericTests.test_implicit_any
9054 self.assertEqual(WithImplicitAny.__parameters__, ())
9055 self.assertEqual(WithImplicitAny.__total__, True)
9056 self.assertEqual(WithImplicitAny.__optional_keys__, frozenset(['b']))
9057 self.assertEqual(WithImplicitAny.__required_keys__, frozenset(['a', 'c']))
9058 self.assertEqual(WithImplicitAny.__annotations__, {
9059 'a': T,
9060 'b': KT,
9061 'c': int,
9062 })
9063 self.assertEqual(WithImplicitAny.__annotate__(annotationlib.Format.STRING), {
9064 'a': 'T',
9065 'b': 'KT',
9066 'c': 'int',
9067 })
9068 with self.assertRaises(TypeError):
9069 WithImplicitAny[str]
9070
9071 def test_non_generic_subscript(self):
9072 # For backward compatibility, subscription works
9073 # on arbitrary TypedDict types.
9074 class TD(TypedDict):
9075 a: T
9076 A = TD[int]
9077 self.assertEqual(A.__origin__, TD)
9078 self.assertEqual(A.__parameters__, ())
9079 self.assertEqual(A.__args__, (int,))
9080 a = A(a = 1)
9081 self.assertIs(type(a), dict)
9082 self.assertEqual(a, {'a': 1})
9083
9084 def test_orig_bases(self):
9085 T = TypeVar('T')
9086
9087 class Parent(TypedDict):
9088 pass
9089
9090 class Child(Parent):
9091 pass
9092
9093 class OtherChild(Parent):
9094 pass
9095
9096 class MixedChild(Child, OtherChild, Parent):
9097 pass
9098
9099 class GenericParent(TypedDict, Generic[T]):
9100 pass
9101
9102 class GenericChild(GenericParent[int]):
9103 pass
9104
9105 class OtherGenericChild(GenericParent[str]):
9106 pass
9107
9108 class MixedGenericChild(GenericChild, OtherGenericChild, GenericParent[float]):
9109 pass
9110
9111 class MultipleGenericBases(GenericParent[int], GenericParent[float]):
9112 pass
9113
9114 CallTypedDict = TypedDict('CallTypedDict', {})
9115
9116 self.assertEqual(Parent.__orig_bases__, (TypedDict,))
9117 self.assertEqual(Child.__orig_bases__, (Parent,))
9118 self.assertEqual(OtherChild.__orig_bases__, (Parent,))
9119 self.assertEqual(MixedChild.__orig_bases__, (Child, OtherChild, Parent,))
9120 self.assertEqual(GenericParent.__orig_bases__, (TypedDict, Generic[T]))
9121 self.assertEqual(GenericChild.__orig_bases__, (GenericParent[int],))
9122 self.assertEqual(OtherGenericChild.__orig_bases__, (GenericParent[str],))
9123 self.assertEqual(MixedGenericChild.__orig_bases__, (GenericChild, OtherGenericChild, GenericParent[float]))
9124 self.assertEqual(MultipleGenericBases.__orig_bases__, (GenericParent[int], GenericParent[float]))
9125 self.assertEqual(CallTypedDict.__orig_bases__, (TypedDict,))
9126
9127 def test_zero_fields_typeddicts(self):
9128 T1 = TypedDict("T1", {})
9129 class T2(TypedDict): pass
9130 class T3[tvar](TypedDict): pass
9131 S = TypeVar("S")
9132 class T4(TypedDict, Generic[S]): pass
9133
9134 expected_warning = re.escape(
9135 "Failing to pass a value for the 'fields' parameter is deprecated "
9136 "and will be disallowed in Python 3.15. "
9137 "To create a TypedDict class with 0 fields "
9138 "using the functional syntax, "
9139 "pass an empty dictionary, e.g. `T5 = TypedDict('T5', {})`."
9140 )
9141 with self.assertWarnsRegex(DeprecationWarning, fr"^{expected_warning}$"):
9142 T5 = TypedDict('T5')
9143
9144 expected_warning = re.escape(
9145 "Passing `None` as the 'fields' parameter is deprecated "
9146 "and will be disallowed in Python 3.15. "
9147 "To create a TypedDict class with 0 fields "
9148 "using the functional syntax, "
9149 "pass an empty dictionary, e.g. `T6 = TypedDict('T6', {})`."
9150 )
9151 with self.assertWarnsRegex(DeprecationWarning, fr"^{expected_warning}$"):
9152 T6 = TypedDict('T6', None)
9153
9154 for klass in T1, T2, T3, T4, T5, T6:
9155 with self.subTest(klass=klass.__name__):
9156 self.assertEqual(klass.__annotations__, {})
9157 self.assertEqual(klass.__required_keys__, set())
9158 self.assertEqual(klass.__optional_keys__, set())
9159 self.assertIsInstance(klass(), dict)
9160
9161 def test_readonly_inheritance(self):
9162 class Base1(TypedDict):
9163 a: ReadOnly[int]
9164
9165 class Child1(Base1):
9166 b: str
9167
9168 self.assertEqual(Child1.__readonly_keys__, frozenset({'a'}))
9169 self.assertEqual(Child1.__mutable_keys__, frozenset({'b'}))
9170
9171 class Base2(TypedDict):
9172 a: int
9173
9174 class Child2(Base2):
9175 b: ReadOnly[str]
9176
9177 self.assertEqual(Child2.__readonly_keys__, frozenset({'b'}))
9178 self.assertEqual(Child2.__mutable_keys__, frozenset({'a'}))
9179
9180 def test_cannot_make_mutable_key_readonly(self):
9181 class Base(TypedDict):
9182 a: int
9183
9184 with self.assertRaises(TypeError):
9185 class Child(Base):
9186 a: ReadOnly[int]
9187
9188 def test_can_make_readonly_key_mutable(self):
9189 class Base(TypedDict):
9190 a: ReadOnly[int]
9191
9192 class Child(Base):
9193 a: int
9194
9195 self.assertEqual(Child.__readonly_keys__, frozenset())
9196 self.assertEqual(Child.__mutable_keys__, frozenset({'a'}))
9197
9198 def test_combine_qualifiers(self):
9199 class AllTheThings(TypedDict):
9200 a: Annotated[Required[ReadOnly[int]], "why not"]
9201 b: Required[Annotated[ReadOnly[int], "why not"]]
9202 c: ReadOnly[NotRequired[Annotated[int, "why not"]]]
9203 d: NotRequired[Annotated[int, "why not"]]
9204
9205 self.assertEqual(AllTheThings.__required_keys__, frozenset({'a', 'b'}))
9206 self.assertEqual(AllTheThings.__optional_keys__, frozenset({'c', 'd'}))
9207 self.assertEqual(AllTheThings.__readonly_keys__, frozenset({'a', 'b', 'c'}))
9208 self.assertEqual(AllTheThings.__mutable_keys__, frozenset({'d'}))
9209
9210 self.assertEqual(
9211 get_type_hints(AllTheThings, include_extras=False),
9212 {'a': int, 'b': int, 'c': int, 'd': int},
9213 )
9214 self.assertEqual(
9215 get_type_hints(AllTheThings, include_extras=True),
9216 {
9217 'a': Annotated[Required[ReadOnly[int]], 'why not'],
9218 'b': Required[Annotated[ReadOnly[int], 'why not']],
9219 'c': ReadOnly[NotRequired[Annotated[int, 'why not']]],
9220 'd': NotRequired[Annotated[int, 'why not']],
9221 },
9222 )
9223
9224 def test_annotations(self):
9225 # _type_check is applied
9226 with self.assertRaisesRegex(TypeError, "Plain typing.Final is not valid as type argument"):
9227 class X(TypedDict):
9228 a: Final
9229
9230 # _type_convert is applied
9231 class Y(TypedDict):
9232 a: None
9233 b: "int"
9234 fwdref = EqualToForwardRef('int', module=__name__)
9235 self.assertEqual(Y.__annotations__, {'a': type(None), 'b': fwdref})
9236 self.assertEqual(Y.__annotate__(annotationlib.Format.FORWARDREF), {'a': type(None), 'b': fwdref})
9237
9238 # _type_check is also applied later
9239 class Z(TypedDict):
9240 a: undefined
9241
9242 with self.assertRaises(NameError):
9243 Z.__annotations__
9244
9245 undefined = Final
9246 with self.assertRaisesRegex(TypeError, "Plain typing.Final is not valid as type argument"):
9247 Z.__annotations__
9248
9249 undefined = None
9250 self.assertEqual(Z.__annotations__, {'a': type(None)})
9251
9252 def test_deferred_evaluation(self):
9253 class A(TypedDict):
9254 x: NotRequired[undefined]
9255 y: ReadOnly[undefined]
9256 z: Required[undefined]
9257
9258 self.assertEqual(A.__required_keys__, frozenset({'y', 'z'}))
9259 self.assertEqual(A.__optional_keys__, frozenset({'x'}))
9260 self.assertEqual(A.__readonly_keys__, frozenset({'y'}))
9261 self.assertEqual(A.__mutable_keys__, frozenset({'x', 'z'}))
9262
9263 with self.assertRaises(NameError):
9264 A.__annotations__
9265
9266 self.assertEqual(
9267 A.__annotate__(annotationlib.Format.STRING),
9268 {'x': 'NotRequired[undefined]', 'y': 'ReadOnly[undefined]',
9269 'z': 'Required[undefined]'},
9270 )
9271
9272
9273 class RequiredTests(BaseTestCase):
9274
9275 def test_basics(self):
9276 with self.assertRaises(TypeError):
9277 Required[NotRequired]
9278 with self.assertRaises(TypeError):
9279 Required[int, str]
9280 with self.assertRaises(TypeError):
9281 Required[int][str]
9282
9283 def test_repr(self):
9284 self.assertEqual(repr(Required), 'typing.Required')
9285 cv = Required[int]
9286 self.assertEqual(repr(cv), 'typing.Required[int]')
9287 cv = Required[Employee]
9288 self.assertEqual(repr(cv), f'typing.Required[{__name__}.Employee]')
9289
9290 def test_cannot_subclass(self):
9291 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
9292 class C(type(Required)):
9293 pass
9294 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
9295 class D(type(Required[int])):
9296 pass
9297 with self.assertRaisesRegex(TypeError,
9298 r'Cannot subclass typing\.Required'):
9299 class E(Required):
9300 pass
9301 with self.assertRaisesRegex(TypeError,
9302 r'Cannot subclass typing\.Required\[int\]'):
9303 class F(Required[int]):
9304 pass
9305
9306 def test_cannot_init(self):
9307 with self.assertRaises(TypeError):
9308 Required()
9309 with self.assertRaises(TypeError):
9310 type(Required)()
9311 with self.assertRaises(TypeError):
9312 type(Required[Optional[int]])()
9313
9314 def test_no_isinstance(self):
9315 with self.assertRaises(TypeError):
9316 isinstance(1, Required[int])
9317 with self.assertRaises(TypeError):
9318 issubclass(int, Required)
9319
9320
9321 class NotRequiredTests(BaseTestCase):
9322
9323 def test_basics(self):
9324 with self.assertRaises(TypeError):
9325 NotRequired[Required]
9326 with self.assertRaises(TypeError):
9327 NotRequired[int, str]
9328 with self.assertRaises(TypeError):
9329 NotRequired[int][str]
9330
9331 def test_repr(self):
9332 self.assertEqual(repr(NotRequired), 'typing.NotRequired')
9333 cv = NotRequired[int]
9334 self.assertEqual(repr(cv), 'typing.NotRequired[int]')
9335 cv = NotRequired[Employee]
9336 self.assertEqual(repr(cv), f'typing.NotRequired[{__name__}.Employee]')
9337
9338 def test_cannot_subclass(self):
9339 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
9340 class C(type(NotRequired)):
9341 pass
9342 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
9343 class D(type(NotRequired[int])):
9344 pass
9345 with self.assertRaisesRegex(TypeError,
9346 r'Cannot subclass typing\.NotRequired'):
9347 class E(NotRequired):
9348 pass
9349 with self.assertRaisesRegex(TypeError,
9350 r'Cannot subclass typing\.NotRequired\[int\]'):
9351 class F(NotRequired[int]):
9352 pass
9353
9354 def test_cannot_init(self):
9355 with self.assertRaises(TypeError):
9356 NotRequired()
9357 with self.assertRaises(TypeError):
9358 type(NotRequired)()
9359 with self.assertRaises(TypeError):
9360 type(NotRequired[Optional[int]])()
9361
9362 def test_no_isinstance(self):
9363 with self.assertRaises(TypeError):
9364 isinstance(1, NotRequired[int])
9365 with self.assertRaises(TypeError):
9366 issubclass(int, NotRequired)
9367
9368
9369 class IOTests(BaseTestCase):
9370
9371 def test_io(self):
9372
9373 def stuff(a: IO) -> AnyStr:
9374 return a.readline()
9375
9376 a = stuff.__annotations__['a']
9377 self.assertEqual(a.__parameters__, (AnyStr,))
9378
9379 def test_textio(self):
9380
9381 def stuff(a: TextIO) -> str:
9382 return a.readline()
9383
9384 a = stuff.__annotations__['a']
9385 self.assertEqual(a.__parameters__, ())
9386
9387 def test_binaryio(self):
9388
9389 def stuff(a: BinaryIO) -> bytes:
9390 return a.readline()
9391
9392 a = stuff.__annotations__['a']
9393 self.assertEqual(a.__parameters__, ())
9394
9395
9396 class RETests(BaseTestCase):
9397 # Much of this is really testing _TypeAlias.
9398
9399 def test_basics(self):
9400 pat = re.compile('[a-z]+', re.I)
9401 self.assertIsSubclass(pat.__class__, Pattern)
9402 self.assertIsSubclass(type(pat), Pattern)
9403 self.assertIsInstance(pat, Pattern)
9404
9405 mat = pat.search('12345abcde.....')
9406 self.assertIsSubclass(mat.__class__, Match)
9407 self.assertIsSubclass(type(mat), Match)
9408 self.assertIsInstance(mat, Match)
9409
9410 # these should just work
9411 Pattern[Union[str, bytes]]
9412 Match[Union[bytes, str]]
9413
9414 def test_alias_equality(self):
9415 self.assertEqual(Pattern[str], Pattern[str])
9416 self.assertNotEqual(Pattern[str], Pattern[bytes])
9417 self.assertNotEqual(Pattern[str], Match[str])
9418 self.assertNotEqual(Pattern[str], str)
9419
9420 def test_errors(self):
9421 m = Match[Union[str, bytes]]
9422 with self.assertRaises(TypeError):
9423 m[str]
9424 with self.assertRaises(TypeError):
9425 # We don't support isinstance().
9426 isinstance(42, Pattern[str])
9427 with self.assertRaises(TypeError):
9428 # We don't support issubclass().
9429 issubclass(Pattern[bytes], Pattern[str])
9430
9431 def test_repr(self):
9432 self.assertEqual(repr(Pattern), 'typing.Pattern')
9433 self.assertEqual(repr(Pattern[str]), 'typing.Pattern[str]')
9434 self.assertEqual(repr(Pattern[bytes]), 'typing.Pattern[bytes]')
9435 self.assertEqual(repr(Match), 'typing.Match')
9436 self.assertEqual(repr(Match[str]), 'typing.Match[str]')
9437 self.assertEqual(repr(Match[bytes]), 'typing.Match[bytes]')
9438
9439 def test_cannot_subclass(self):
9440 with self.assertRaisesRegex(
9441 TypeError,
9442 r"type 're\.Match' is not an acceptable base type",
9443 ):
9444 class A(typing.Match):
9445 pass
9446 with self.assertRaisesRegex(
9447 TypeError,
9448 r"type 're\.Pattern' is not an acceptable base type",
9449 ):
9450 class B(typing.Pattern):
9451 pass
9452
9453
9454 class AnnotatedTests(BaseTestCase):
9455
9456 def test_new(self):
9457 with self.assertRaisesRegex(
9458 TypeError, 'Cannot instantiate typing.Annotated',
9459 ):
9460 Annotated()
9461
9462 def test_repr(self):
9463 self.assertEqual(
9464 repr(Annotated[int, 4, 5]),
9465 "typing.Annotated[int, 4, 5]"
9466 )
9467 self.assertEqual(
9468 repr(Annotated[List[int], 4, 5]),
9469 "typing.Annotated[typing.List[int], 4, 5]"
9470 )
9471
9472 def test_dir(self):
9473 dir_items = set(dir(Annotated[int, 4]))
9474 for required_item in [
9475 '__args__', '__parameters__', '__origin__',
9476 '__metadata__',
9477 ]:
9478 with self.subTest(required_item=required_item):
9479 self.assertIn(required_item, dir_items)
9480
9481 def test_flatten(self):
9482 A = Annotated[Annotated[int, 4], 5]
9483 self.assertEqual(A, Annotated[int, 4, 5])
9484 self.assertEqual(A.__metadata__, (4, 5))
9485 self.assertEqual(A.__origin__, int)
9486
9487 def test_deduplicate_from_union(self):
9488 # Regular:
9489 self.assertEqual(get_args(Annotated[int, 1] | int),
9490 (Annotated[int, 1], int))
9491 self.assertEqual(get_args(Union[Annotated[int, 1], int]),
9492 (Annotated[int, 1], int))
9493 self.assertEqual(get_args(Annotated[int, 1] | Annotated[int, 2] | int),
9494 (Annotated[int, 1], Annotated[int, 2], int))
9495 self.assertEqual(get_args(Union[Annotated[int, 1], Annotated[int, 2], int]),
9496 (Annotated[int, 1], Annotated[int, 2], int))
9497 self.assertEqual(get_args(Annotated[int, 1] | Annotated[str, 1] | int),
9498 (Annotated[int, 1], Annotated[str, 1], int))
9499 self.assertEqual(get_args(Union[Annotated[int, 1], Annotated[str, 1], int]),
9500 (Annotated[int, 1], Annotated[str, 1], int))
9501
9502 # Duplicates:
9503 self.assertEqual(Annotated[int, 1] | Annotated[int, 1] | int,
9504 Annotated[int, 1] | int)
9505 self.assertEqual(Union[Annotated[int, 1], Annotated[int, 1], int],
9506 Union[Annotated[int, 1], int])
9507
9508 # Unhashable metadata:
9509 self.assertEqual(get_args(str | Annotated[int, {}] | Annotated[int, set()] | int),
9510 (str, Annotated[int, {}], Annotated[int, set()], int))
9511 self.assertEqual(get_args(Union[str, Annotated[int, {}], Annotated[int, set()], int]),
9512 (str, Annotated[int, {}], Annotated[int, set()], int))
9513 self.assertEqual(get_args(str | Annotated[int, {}] | Annotated[str, {}] | int),
9514 (str, Annotated[int, {}], Annotated[str, {}], int))
9515 self.assertEqual(get_args(Union[str, Annotated[int, {}], Annotated[str, {}], int]),
9516 (str, Annotated[int, {}], Annotated[str, {}], int))
9517
9518 self.assertEqual(get_args(Annotated[int, 1] | str | Annotated[str, {}] | int),
9519 (Annotated[int, 1], str, Annotated[str, {}], int))
9520 self.assertEqual(get_args(Union[Annotated[int, 1], str, Annotated[str, {}], int]),
9521 (Annotated[int, 1], str, Annotated[str, {}], int))
9522
9523 import dataclasses
9524 @dataclasses.dataclass
9525 class ValueRange:
9526 lo: int
9527 hi: int
9528 v = ValueRange(1, 2)
9529 self.assertEqual(get_args(Annotated[int, v] | None),
9530 (Annotated[int, v], types.NoneType))
9531 self.assertEqual(get_args(Union[Annotated[int, v], None]),
9532 (Annotated[int, v], types.NoneType))
9533 self.assertEqual(get_args(Optional[Annotated[int, v]]),
9534 (Annotated[int, v], types.NoneType))
9535
9536 # Unhashable metadata duplicated:
9537 self.assertEqual(Annotated[int, {}] | Annotated[int, {}] | int,
9538 Annotated[int, {}] | int)
9539 self.assertEqual(Annotated[int, {}] | Annotated[int, {}] | int,
9540 int | Annotated[int, {}])
9541 self.assertEqual(Union[Annotated[int, {}], Annotated[int, {}], int],
9542 Union[Annotated[int, {}], int])
9543 self.assertEqual(Union[Annotated[int, {}], Annotated[int, {}], int],
9544 Union[int, Annotated[int, {}]])
9545
9546 def test_order_in_union(self):
9547 expr1 = Annotated[int, 1] | str | Annotated[str, {}] | int
9548 for args in itertools.permutations(get_args(expr1)):
9549 with self.subTest(args=args):
9550 self.assertEqual(expr1, reduce(operator.or_, args))
9551
9552 expr2 = Union[Annotated[int, 1], str, Annotated[str, {}], int]
9553 for args in itertools.permutations(get_args(expr2)):
9554 with self.subTest(args=args):
9555 self.assertEqual(expr2, Union[args])
9556
9557 def test_specialize(self):
9558 L = Annotated[List[T], "my decoration"]
9559 LI = Annotated[List[int], "my decoration"]
9560 self.assertEqual(L[int], Annotated[List[int], "my decoration"])
9561 self.assertEqual(L[int].__metadata__, ("my decoration",))
9562 self.assertEqual(L[int].__origin__, List[int])
9563 with self.assertRaises(TypeError):
9564 LI[int]
9565 with self.assertRaises(TypeError):
9566 L[int, float]
9567
9568 def test_hash_eq(self):
9569 self.assertEqual(len({Annotated[int, 4, 5], Annotated[int, 4, 5]}), 1)
9570 self.assertNotEqual(Annotated[int, 4, 5], Annotated[int, 5, 4])
9571 self.assertNotEqual(Annotated[int, 4, 5], Annotated[str, 4, 5])
9572 self.assertNotEqual(Annotated[int, 4], Annotated[int, 4, 4])
9573 self.assertEqual(
9574 {Annotated[int, 4, 5], Annotated[int, 4, 5], Annotated[T, 4, 5]},
9575 {Annotated[int, 4, 5], Annotated[T, 4, 5]}
9576 )
9577 # Unhashable `metadata` raises `TypeError`:
9578 a1 = Annotated[int, []]
9579 with self.assertRaises(TypeError):
9580 hash(a1)
9581
9582 class A:
9583 __hash__ = None
9584 a2 = Annotated[int, A()]
9585 with self.assertRaises(TypeError):
9586 hash(a2)
9587
9588 def test_instantiate(self):
9589 class C:
9590 classvar = 4
9591
9592 def __init__(self, x):
9593 self.x = x
9594
9595 def __eq__(self, other):
9596 if not isinstance(other, C):
9597 return NotImplemented
9598 return other.x == self.x
9599
9600 A = Annotated[C, "a decoration"]
9601 a = A(5)
9602 c = C(5)
9603 self.assertEqual(a, c)
9604 self.assertEqual(a.x, c.x)
9605 self.assertEqual(a.classvar, c.classvar)
9606
9607 def test_instantiate_generic(self):
9608 MyCount = Annotated[typing.Counter[T], "my decoration"]
9609 self.assertEqual(MyCount([4, 4, 5]), {4: 2, 5: 1})
9610 self.assertEqual(MyCount[int]([4, 4, 5]), {4: 2, 5: 1})
9611
9612 def test_instantiate_immutable(self):
9613 class C:
9614 def __setattr__(self, key, value):
9615 raise Exception("should be ignored")
9616
9617 A = Annotated[C, "a decoration"]
9618 # gh-115165: This used to cause RuntimeError to be raised
9619 # when we tried to set `__orig_class__` on the `C` instance
9620 # returned by the `A()` call
9621 self.assertIsInstance(A(), C)
9622
9623 def test_cannot_instantiate_forward(self):
9624 A = Annotated["int", (5, 6)]
9625 with self.assertRaises(TypeError):
9626 A(5)
9627
9628 def test_cannot_instantiate_type_var(self):
9629 A = Annotated[T, (5, 6)]
9630 with self.assertRaises(TypeError):
9631 A(5)
9632
9633 def test_cannot_getattr_typevar(self):
9634 with self.assertRaises(AttributeError):
9635 Annotated[T, (5, 7)].x
9636
9637 def test_attr_passthrough(self):
9638 class C:
9639 classvar = 4
9640
9641 A = Annotated[C, "a decoration"]
9642 self.assertEqual(A.classvar, 4)
9643 A.x = 5
9644 self.assertEqual(C.x, 5)
9645
9646 def test_special_form_containment(self):
9647 class C:
9648 classvar: Annotated[ClassVar[int], "a decoration"] = 4
9649 const: Annotated[Final[int], "Const"] = 4
9650
9651 self.assertEqual(get_type_hints(C, globals())['classvar'], ClassVar[int])
9652 self.assertEqual(get_type_hints(C, globals())['const'], Final[int])
9653
9654 def test_special_forms_nesting(self):
9655 # These are uncommon types and are to ensure runtime
9656 # is lax on validation. See gh-89547 for more context.
9657 class CF:
9658 x: ClassVar[Final[int]]
9659
9660 class FC:
9661 x: Final[ClassVar[int]]
9662
9663 class ACF:
9664 x: Annotated[ClassVar[Final[int]], "a decoration"]
9665
9666 class CAF:
9667 x: ClassVar[Annotated[Final[int], "a decoration"]]
9668
9669 class AFC:
9670 x: Annotated[Final[ClassVar[int]], "a decoration"]
9671
9672 class FAC:
9673 x: Final[Annotated[ClassVar[int], "a decoration"]]
9674
9675 self.assertEqual(get_type_hints(CF, globals())['x'], ClassVar[Final[int]])
9676 self.assertEqual(get_type_hints(FC, globals())['x'], Final[ClassVar[int]])
9677 self.assertEqual(get_type_hints(ACF, globals())['x'], ClassVar[Final[int]])
9678 self.assertEqual(get_type_hints(CAF, globals())['x'], ClassVar[Final[int]])
9679 self.assertEqual(get_type_hints(AFC, globals())['x'], Final[ClassVar[int]])
9680 self.assertEqual(get_type_hints(FAC, globals())['x'], Final[ClassVar[int]])
9681
9682 def test_cannot_subclass(self):
9683 with self.assertRaisesRegex(TypeError, "Cannot subclass .*Annotated"):
9684 class C(Annotated):
9685 pass
9686
9687 def test_cannot_check_instance(self):
9688 with self.assertRaises(TypeError):
9689 isinstance(5, Annotated[int, "positive"])
9690
9691 def test_cannot_check_subclass(self):
9692 with self.assertRaises(TypeError):
9693 issubclass(int, Annotated[int, "positive"])
9694
9695 def test_too_few_type_args(self):
9696 with self.assertRaisesRegex(TypeError, 'at least two arguments'):
9697 Annotated[int]
9698
9699 def test_pickle(self):
9700 samples = [typing.Any, typing.Union[int, str],
9701 typing.Optional[str], Tuple[int, ...],
9702 typing.Callable[[str], bytes]]
9703
9704 for t in samples:
9705 x = Annotated[t, "a"]
9706
9707 for prot in range(pickle.HIGHEST_PROTOCOL + 1):
9708 with self.subTest(protocol=prot, type=t):
9709 pickled = pickle.dumps(x, prot)
9710 restored = pickle.loads(pickled)
9711 self.assertEqual(x, restored)
9712
9713 global _Annotated_test_G
9714
9715 class _Annotated_test_G(Generic[T]):
9716 x = 1
9717
9718 G = Annotated[_Annotated_test_G[int], "A decoration"]
9719 G.foo = 42
9720 G.bar = 'abc'
9721
9722 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
9723 z = pickle.dumps(G, proto)
9724 x = pickle.loads(z)
9725 self.assertEqual(x.foo, 42)
9726 self.assertEqual(x.bar, 'abc')
9727 self.assertEqual(x.x, 1)
9728
9729 def test_subst(self):
9730 dec = "a decoration"
9731 dec2 = "another decoration"
9732
9733 S = Annotated[T, dec2]
9734 self.assertEqual(S[int], Annotated[int, dec2])
9735
9736 self.assertEqual(S[Annotated[int, dec]], Annotated[int, dec, dec2])
9737 L = Annotated[List[T], dec]
9738
9739 self.assertEqual(L[int], Annotated[List[int], dec])
9740 with self.assertRaises(TypeError):
9741 L[int, int]
9742
9743 self.assertEqual(S[L[int]], Annotated[List[int], dec, dec2])
9744
9745 D = Annotated[typing.Dict[KT, VT], dec]
9746 self.assertEqual(D[str, int], Annotated[typing.Dict[str, int], dec])
9747 with self.assertRaises(TypeError):
9748 D[int]
9749
9750 It = Annotated[int, dec]
9751 with self.assertRaises(TypeError):
9752 It[None]
9753
9754 LI = L[int]
9755 with self.assertRaises(TypeError):
9756 LI[None]
9757
9758 def test_typevar_subst(self):
9759 dec = "a decoration"
9760 Ts = TypeVarTuple('Ts')
9761 T = TypeVar('T')
9762 T1 = TypeVar('T1')
9763 T2 = TypeVar('T2')
9764
9765 A = Annotated[tuple[*Ts], dec]
9766 self.assertEqual(A[int], Annotated[tuple[int], dec])
9767 self.assertEqual(A[str, int], Annotated[tuple[str, int], dec])
9768 with self.assertRaises(TypeError):
9769 Annotated[*Ts, dec]
9770
9771 B = Annotated[Tuple[Unpack[Ts]], dec]
9772 self.assertEqual(B[int], Annotated[Tuple[int], dec])
9773 self.assertEqual(B[str, int], Annotated[Tuple[str, int], dec])
9774 with self.assertRaises(TypeError):
9775 Annotated[Unpack[Ts], dec]
9776
9777 C = Annotated[tuple[T, *Ts], dec]
9778 self.assertEqual(C[int], Annotated[tuple[int], dec])
9779 self.assertEqual(C[int, str], Annotated[tuple[int, str], dec])
9780 self.assertEqual(
9781 C[int, str, float],
9782 Annotated[tuple[int, str, float], dec]
9783 )
9784 with self.assertRaises(TypeError):
9785 C[()]
9786
9787 D = Annotated[Tuple[T, Unpack[Ts]], dec]
9788 self.assertEqual(D[int], Annotated[Tuple[int], dec])
9789 self.assertEqual(D[int, str], Annotated[Tuple[int, str], dec])
9790 self.assertEqual(
9791 D[int, str, float],
9792 Annotated[Tuple[int, str, float], dec]
9793 )
9794 with self.assertRaises(TypeError):
9795 D[()]
9796
9797 E = Annotated[tuple[*Ts, T], dec]
9798 self.assertEqual(E[int], Annotated[tuple[int], dec])
9799 self.assertEqual(E[int, str], Annotated[tuple[int, str], dec])
9800 self.assertEqual(
9801 E[int, str, float],
9802 Annotated[tuple[int, str, float], dec]
9803 )
9804 with self.assertRaises(TypeError):
9805 E[()]
9806
9807 F = Annotated[Tuple[Unpack[Ts], T], dec]
9808 self.assertEqual(F[int], Annotated[Tuple[int], dec])
9809 self.assertEqual(F[int, str], Annotated[Tuple[int, str], dec])
9810 self.assertEqual(
9811 F[int, str, float],
9812 Annotated[Tuple[int, str, float], dec]
9813 )
9814 with self.assertRaises(TypeError):
9815 F[()]
9816
9817 G = Annotated[tuple[T1, *Ts, T2], dec]
9818 self.assertEqual(G[int, str], Annotated[tuple[int, str], dec])
9819 self.assertEqual(
9820 G[int, str, float],
9821 Annotated[tuple[int, str, float], dec]
9822 )
9823 self.assertEqual(
9824 G[int, str, bool, float],
9825 Annotated[tuple[int, str, bool, float], dec]
9826 )
9827 with self.assertRaises(TypeError):
9828 G[int]
9829
9830 H = Annotated[Tuple[T1, Unpack[Ts], T2], dec]
9831 self.assertEqual(H[int, str], Annotated[Tuple[int, str], dec])
9832 self.assertEqual(
9833 H[int, str, float],
9834 Annotated[Tuple[int, str, float], dec]
9835 )
9836 self.assertEqual(
9837 H[int, str, bool, float],
9838 Annotated[Tuple[int, str, bool, float], dec]
9839 )
9840 with self.assertRaises(TypeError):
9841 H[int]
9842
9843 # Now let's try creating an alias from an alias.
9844
9845 Ts2 = TypeVarTuple('Ts2')
9846 T3 = TypeVar('T3')
9847 T4 = TypeVar('T4')
9848
9849 # G is Annotated[tuple[T1, *Ts, T2], dec].
9850 I = G[T3, *Ts2, T4]
9851 J = G[T3, Unpack[Ts2], T4]
9852
9853 for x, y in [
9854 (I, Annotated[tuple[T3, *Ts2, T4], dec]),
9855 (J, Annotated[tuple[T3, Unpack[Ts2], T4], dec]),
9856 (I[int, str], Annotated[tuple[int, str], dec]),
9857 (J[int, str], Annotated[tuple[int, str], dec]),
9858 (I[int, str, float], Annotated[tuple[int, str, float], dec]),
9859 (J[int, str, float], Annotated[tuple[int, str, float], dec]),
9860 (I[int, str, bool, float],
9861 Annotated[tuple[int, str, bool, float], dec]),
9862 (J[int, str, bool, float],
9863 Annotated[tuple[int, str, bool, float], dec]),
9864 ]:
9865 self.assertEqual(x, y)
9866
9867 with self.assertRaises(TypeError):
9868 I[int]
9869 with self.assertRaises(TypeError):
9870 J[int]
9871
9872 def test_annotated_in_other_types(self):
9873 X = List[Annotated[T, 5]]
9874 self.assertEqual(X[int], List[Annotated[int, 5]])
9875
9876 def test_annotated_mro(self):
9877 class X(Annotated[int, (1, 10)]): ...
9878 self.assertEqual(X.__mro__, (X, int, object),
9879 "Annotated should be transparent.")
9880
9881 def test_annotated_cached_with_types(self):
9882 class A(str): ...
9883 class B(str): ...
9884
9885 field_a1 = Annotated[str, A("X")]
9886 field_a2 = Annotated[str, B("X")]
9887 a1_metadata = field_a1.__metadata__[0]
9888 a2_metadata = field_a2.__metadata__[0]
9889
9890 self.assertIs(type(a1_metadata), A)
9891 self.assertEqual(a1_metadata, A("X"))
9892 self.assertIs(type(a2_metadata), B)
9893 self.assertEqual(a2_metadata, B("X"))
9894 self.assertIsNot(type(a1_metadata), type(a2_metadata))
9895
9896 field_b1 = Annotated[str, A("Y")]
9897 field_b2 = Annotated[str, B("Y")]
9898 b1_metadata = field_b1.__metadata__[0]
9899 b2_metadata = field_b2.__metadata__[0]
9900
9901 self.assertIs(type(b1_metadata), A)
9902 self.assertEqual(b1_metadata, A("Y"))
9903 self.assertIs(type(b2_metadata), B)
9904 self.assertEqual(b2_metadata, B("Y"))
9905 self.assertIsNot(type(b1_metadata), type(b2_metadata))
9906
9907 field_c1 = Annotated[int, 1]
9908 field_c2 = Annotated[int, 1.0]
9909 field_c3 = Annotated[int, True]
9910
9911 self.assertIs(type(field_c1.__metadata__[0]), int)
9912 self.assertIs(type(field_c2.__metadata__[0]), float)
9913 self.assertIs(type(field_c3.__metadata__[0]), bool)
9914
9915 def test_forwardref_partial_evaluation(self):
9916 # Test that Annotated partially evaluates if it contains a ForwardRef
9917 # See: https://github.com/python/cpython/issues/137706
9918 def f(x: Annotated[undefined, '']): pass
9919
9920 ann = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)
9921
9922 # Test that the attributes are retrievable from the partially evaluated annotation
9923 x_ann = ann['x']
9924 self.assertIs(get_origin(x_ann), Annotated)
9925 self.assertEqual(x_ann.__origin__, EqualToForwardRef('undefined', owner=f))
9926 self.assertEqual(x_ann.__metadata__, ('',))
9927
9928
9929 class TypeAliasTests(BaseTestCase):
9930 def test_canonical_usage_with_variable_annotation(self):
9931 Alias: TypeAlias = Employee
9932
9933 def test_canonical_usage_with_type_comment(self):
9934 Alias = Employee # type: TypeAlias
9935
9936 def test_cannot_instantiate(self):
9937 with self.assertRaises(TypeError):
9938 TypeAlias()
9939
9940 def test_no_isinstance(self):
9941 with self.assertRaises(TypeError):
9942 isinstance(42, TypeAlias)
9943
9944 def test_stringized_usage(self):
9945 class A:
9946 a: "TypeAlias"
9947 self.assertEqual(get_type_hints(A), {'a': TypeAlias})
9948
9949 def test_no_issubclass(self):
9950 with self.assertRaises(TypeError):
9951 issubclass(Employee, TypeAlias)
9952
9953 with self.assertRaises(TypeError):
9954 issubclass(TypeAlias, Employee)
9955
9956 def test_cannot_subclass(self):
9957 with self.assertRaisesRegex(TypeError,
9958 r'Cannot subclass typing\.TypeAlias'):
9959 class C(TypeAlias):
9960 pass
9961
9962 with self.assertRaises(TypeError):
9963 class D(type(TypeAlias)):
9964 pass
9965
9966 def test_repr(self):
9967 self.assertEqual(repr(TypeAlias), 'typing.TypeAlias')
9968
9969 def test_cannot_subscript(self):
9970 with self.assertRaises(TypeError):
9971 TypeAlias[int]
9972
9973
9974 class ParamSpecTests(BaseTestCase):
9975
9976 def test_basic_plain(self):
9977 P = ParamSpec('P')
9978 self.assertEqual(P, P)
9979 self.assertIsInstance(P, ParamSpec)
9980 self.assertEqual(P.__name__, 'P')
9981 self.assertEqual(P.__module__, __name__)
9982
9983 def test_basic_with_exec(self):
9984 ns = {}
9985 exec('from typing import ParamSpec; P = ParamSpec("P")', ns, ns)
9986 P = ns['P']
9987 self.assertIsInstance(P, ParamSpec)
9988 self.assertEqual(P.__name__, 'P')
9989 self.assertIs(P.__module__, None)
9990
9991 def test_valid_uses(self):
9992 P = ParamSpec('P')
9993 T = TypeVar('T')
9994 C1 = Callable[P, int]
9995 self.assertEqual(C1.__args__, (P, int))
9996 self.assertEqual(C1.__parameters__, (P,))
9997 C2 = Callable[P, T]
9998 self.assertEqual(C2.__args__, (P, T))
9999 self.assertEqual(C2.__parameters__, (P, T))
10000 # Test collections.abc.Callable too.
10001 C3 = collections.abc.Callable[P, int]
10002 self.assertEqual(C3.__args__, (P, int))
10003 self.assertEqual(C3.__parameters__, (P,))
10004 C4 = collections.abc.Callable[P, T]
10005 self.assertEqual(C4.__args__, (P, T))
10006 self.assertEqual(C4.__parameters__, (P, T))
10007
10008 def test_args_kwargs(self):
10009 P = ParamSpec('P')
10010 P_2 = ParamSpec('P_2')
10011 self.assertIn('args', dir(P))
10012 self.assertIn('kwargs', dir(P))
10013 self.assertIsInstance(P.args, ParamSpecArgs)
10014 self.assertIsInstance(P.kwargs, ParamSpecKwargs)
10015 self.assertIs(P.args.__origin__, P)
10016 self.assertIs(P.kwargs.__origin__, P)
10017 self.assertEqual(P.args, P.args)
10018 self.assertEqual(P.kwargs, P.kwargs)
10019 self.assertNotEqual(P.args, P_2.args)
10020 self.assertNotEqual(P.kwargs, P_2.kwargs)
10021 self.assertNotEqual(P.args, P.kwargs)
10022 self.assertNotEqual(P.kwargs, P.args)
10023 self.assertNotEqual(P.args, P_2.kwargs)
10024 self.assertEqual(repr(P.args), "P.args")
10025 self.assertEqual(repr(P.kwargs), "P.kwargs")
10026
10027 def test_stringized(self):
10028 P = ParamSpec('P')
10029 class C(Generic[P]):
10030 func: Callable["P", int]
10031 def foo(self, *args: "P.args", **kwargs: "P.kwargs"):
10032 pass
10033
10034 self.assertEqual(gth(C, globals(), locals()), {"func": Callable[P, int]})
10035 self.assertEqual(
10036 gth(C.foo, globals(), locals()), {"args": P.args, "kwargs": P.kwargs}
10037 )
10038
10039 def test_user_generics(self):
10040 T = TypeVar("T")
10041 P = ParamSpec("P")
10042 P_2 = ParamSpec("P_2")
10043
10044 class X(Generic[T, P]):
10045 f: Callable[P, int]
10046 x: T
10047 G1 = X[int, P_2]
10048 self.assertEqual(G1.__args__, (int, P_2))
10049 self.assertEqual(G1.__parameters__, (P_2,))
10050 with self.assertRaisesRegex(TypeError, "few arguments for"):
10051 X[int]
10052 with self.assertRaisesRegex(TypeError, "many arguments for"):
10053 X[int, P_2, str]
10054
10055 G2 = X[int, Concatenate[int, P_2]]
10056 self.assertEqual(G2.__args__, (int, Concatenate[int, P_2]))
10057 self.assertEqual(G2.__parameters__, (P_2,))
10058
10059 G3 = X[int, [int, bool]]
10060 self.assertEqual(G3.__args__, (int, (int, bool)))
10061 self.assertEqual(G3.__parameters__, ())
10062
10063 G4 = X[int, ...]
10064 self.assertEqual(G4.__args__, (int, Ellipsis))
10065 self.assertEqual(G4.__parameters__, ())
10066
10067 class Z(Generic[P]):
10068 f: Callable[P, int]
10069
10070 G5 = Z[[int, str, bool]]
10071 self.assertEqual(G5.__args__, ((int, str, bool),))
10072 self.assertEqual(G5.__parameters__, ())
10073
10074 G6 = Z[int, str, bool]
10075 self.assertEqual(G6.__args__, ((int, str, bool),))
10076 self.assertEqual(G6.__parameters__, ())
10077
10078 # G5 and G6 should be equivalent according to the PEP
10079 self.assertEqual(G5.__args__, G6.__args__)
10080 self.assertEqual(G5.__origin__, G6.__origin__)
10081 self.assertEqual(G5.__parameters__, G6.__parameters__)
10082 self.assertEqual(G5, G6)
10083
10084 G7 = Z[int]
10085 self.assertEqual(G7.__args__, ((int,),))
10086 self.assertEqual(G7.__parameters__, ())
10087
10088 with self.assertRaisesRegex(TypeError, "many arguments for"):
10089 Z[[int, str], bool]
10090 with self.assertRaisesRegex(TypeError, "many arguments for"):
10091 Z[P_2, bool]
10092
10093 def test_multiple_paramspecs_in_user_generics(self):
10094 P = ParamSpec("P")
10095 P2 = ParamSpec("P2")
10096
10097 class X(Generic[P, P2]):
10098 f: Callable[P, int]
10099 g: Callable[P2, str]
10100
10101 G1 = X[[int, str], [bytes]]
10102 G2 = X[[int], [str, bytes]]
10103 self.assertNotEqual(G1, G2)
10104 self.assertEqual(G1.__args__, ((int, str), (bytes,)))
10105 self.assertEqual(G2.__args__, ((int,), (str, bytes)))
10106
10107 def test_typevartuple_and_paramspecs_in_user_generics(self):
10108 Ts = TypeVarTuple("Ts")
10109 P = ParamSpec("P")
10110
10111 class X(Generic[*Ts, P]):
10112 f: Callable[P, int]
10113 g: Tuple[*Ts]
10114
10115 G1 = X[int, [bytes]]
10116 self.assertEqual(G1.__args__, (int, (bytes,)))
10117 G2 = X[int, str, [bytes]]
10118 self.assertEqual(G2.__args__, (int, str, (bytes,)))
10119 G3 = X[[bytes]]
10120 self.assertEqual(G3.__args__, ((bytes,),))
10121 G4 = X[[]]
10122 self.assertEqual(G4.__args__, ((),))
10123 with self.assertRaises(TypeError):
10124 X[()]
10125
10126 class Y(Generic[P, *Ts]):
10127 f: Callable[P, int]
10128 g: Tuple[*Ts]
10129
10130 G1 = Y[[bytes], int]
10131 self.assertEqual(G1.__args__, ((bytes,), int))
10132 G2 = Y[[bytes], int, str]
10133 self.assertEqual(G2.__args__, ((bytes,), int, str))
10134 G3 = Y[[bytes]]
10135 self.assertEqual(G3.__args__, ((bytes,),))
10136 G4 = Y[[]]
10137 self.assertEqual(G4.__args__, ((),))
10138 with self.assertRaises(TypeError):
10139 Y[()]
10140
10141 def test_typevartuple_and_paramspecs_in_generic_aliases(self):
10142 P = ParamSpec('P')
10143 T = TypeVar('T')
10144 Ts = TypeVarTuple('Ts')
10145
10146 for C in Callable, collections.abc.Callable:
10147 with self.subTest(generic=C):
10148 A = C[P, Tuple[*Ts]]
10149 B = A[[int, str], bytes, float]
10150 self.assertEqual(B.__args__, (int, str, Tuple[bytes, float]))
10151
10152 class X(Generic[T, P]):
10153 pass
10154
10155 A = X[Tuple[*Ts], P]
10156 B = A[bytes, float, [int, str]]
10157 self.assertEqual(B.__args__, (Tuple[bytes, float], (int, str,)))
10158
10159 class Y(Generic[P, T]):
10160 pass
10161
10162 A = Y[P, Tuple[*Ts]]
10163 B = A[[int, str], bytes, float]
10164 self.assertEqual(B.__args__, ((int, str,), Tuple[bytes, float]))
10165
10166 def test_var_substitution(self):
10167 P = ParamSpec("P")
10168 subst = P.__typing_subst__
10169 self.assertEqual(subst((int, str)), (int, str))
10170 self.assertEqual(subst([int, str]), (int, str))
10171 self.assertEqual(subst([None]), (type(None),))
10172 self.assertIs(subst(...), ...)
10173 self.assertIs(subst(P), P)
10174 self.assertEqual(subst(Concatenate[int, P]), Concatenate[int, P])
10175
10176 def test_bad_var_substitution(self):
10177 T = TypeVar('T')
10178 P = ParamSpec('P')
10179 bad_args = (42, int, None, T, int|str, Union[int, str])
10180 for arg in bad_args:
10181 with self.subTest(arg=arg):
10182 with self.assertRaises(TypeError):
10183 P.__typing_subst__(arg)
10184 with self.assertRaises(TypeError):
10185 typing.Callable[P, T][arg, str]
10186 with self.assertRaises(TypeError):
10187 collections.abc.Callable[P, T][arg, str]
10188
10189 def test_type_var_subst_for_other_type_vars(self):
10190 T = TypeVar('T')
10191 T2 = TypeVar('T2')
10192 P = ParamSpec('P')
10193 P2 = ParamSpec('P2')
10194 Ts = TypeVarTuple('Ts')
10195
10196 class Base(Generic[P]):
10197 pass
10198
10199 A1 = Base[T]
10200 self.assertEqual(A1.__parameters__, (T,))
10201 self.assertEqual(A1.__args__, ((T,),))
10202 self.assertEqual(A1[int], Base[int])
10203
10204 A2 = Base[[T]]
10205 self.assertEqual(A2.__parameters__, (T,))
10206 self.assertEqual(A2.__args__, ((T,),))
10207 self.assertEqual(A2[int], Base[int])
10208
10209 A3 = Base[[int, T]]
10210 self.assertEqual(A3.__parameters__, (T,))
10211 self.assertEqual(A3.__args__, ((int, T),))
10212 self.assertEqual(A3[str], Base[[int, str]])
10213
10214 A4 = Base[[T, int, T2]]
10215 self.assertEqual(A4.__parameters__, (T, T2))
10216 self.assertEqual(A4.__args__, ((T, int, T2),))
10217 self.assertEqual(A4[str, bool], Base[[str, int, bool]])
10218
10219 A5 = Base[[*Ts, int]]
10220 self.assertEqual(A5.__parameters__, (Ts,))
10221 self.assertEqual(A5.__args__, ((*Ts, int),))
10222 self.assertEqual(A5[str, bool], Base[[str, bool, int]])
10223
10224 A5_2 = Base[[int, *Ts]]
10225 self.assertEqual(A5_2.__parameters__, (Ts,))
10226 self.assertEqual(A5_2.__args__, ((int, *Ts),))
10227 self.assertEqual(A5_2[str, bool], Base[[int, str, bool]])
10228
10229 A6 = Base[[T, *Ts]]
10230 self.assertEqual(A6.__parameters__, (T, Ts))
10231 self.assertEqual(A6.__args__, ((T, *Ts),))
10232 self.assertEqual(A6[int, str, bool], Base[[int, str, bool]])
10233
10234 A7 = Base[[T, T]]
10235 self.assertEqual(A7.__parameters__, (T,))
10236 self.assertEqual(A7.__args__, ((T, T),))
10237 self.assertEqual(A7[int], Base[[int, int]])
10238
10239 A8 = Base[[T, list[T]]]
10240 self.assertEqual(A8.__parameters__, (T,))
10241 self.assertEqual(A8.__args__, ((T, list[T]),))
10242 self.assertEqual(A8[int], Base[[int, list[int]]])
10243
10244 A9 = Base[[Tuple[*Ts], *Ts]]
10245 self.assertEqual(A9.__parameters__, (Ts,))
10246 self.assertEqual(A9.__args__, ((Tuple[*Ts], *Ts),))
10247 self.assertEqual(A9[int, str], Base[Tuple[int, str], int, str])
10248
10249 A10 = Base[P2]
10250 self.assertEqual(A10.__parameters__, (P2,))
10251 self.assertEqual(A10.__args__, (P2,))
10252 self.assertEqual(A10[[int, str]], Base[[int, str]])
10253
10254 class DoubleP(Generic[P, P2]):
10255 pass
10256
10257 B1 = DoubleP[P, P2]
10258 self.assertEqual(B1.__parameters__, (P, P2))
10259 self.assertEqual(B1.__args__, (P, P2))
10260 self.assertEqual(B1[[int, str], [bool]], DoubleP[[int, str], [bool]])
10261 self.assertEqual(B1[[], []], DoubleP[[], []])
10262
10263 B2 = DoubleP[[int, str], P2]
10264 self.assertEqual(B2.__parameters__, (P2,))
10265 self.assertEqual(B2.__args__, ((int, str), P2))
10266 self.assertEqual(B2[[bool, bool]], DoubleP[[int, str], [bool, bool]])
10267 self.assertEqual(B2[[]], DoubleP[[int, str], []])
10268
10269 B3 = DoubleP[P, [bool, bool]]
10270 self.assertEqual(B3.__parameters__, (P,))
10271 self.assertEqual(B3.__args__, (P, (bool, bool)))
10272 self.assertEqual(B3[[int, str]], DoubleP[[int, str], [bool, bool]])
10273 self.assertEqual(B3[[]], DoubleP[[], [bool, bool]])
10274
10275 B4 = DoubleP[[T, int], [bool, T2]]
10276 self.assertEqual(B4.__parameters__, (T, T2))
10277 self.assertEqual(B4.__args__, ((T, int), (bool, T2)))
10278 self.assertEqual(B4[str, float], DoubleP[[str, int], [bool, float]])
10279
10280 B5 = DoubleP[[*Ts, int], [bool, T2]]
10281 self.assertEqual(B5.__parameters__, (Ts, T2))
10282 self.assertEqual(B5.__args__, ((*Ts, int), (bool, T2)))
10283 self.assertEqual(B5[str, bytes, float],
10284 DoubleP[[str, bytes, int], [bool, float]])
10285
10286 B6 = DoubleP[[T, int], [bool, *Ts]]
10287 self.assertEqual(B6.__parameters__, (T, Ts))
10288 self.assertEqual(B6.__args__, ((T, int), (bool, *Ts)))
10289 self.assertEqual(B6[str, bytes, float],
10290 DoubleP[[str, int], [bool, bytes, float]])
10291
10292 class PandT(Generic[P, T]):
10293 pass
10294
10295 C1 = PandT[P, T]
10296 self.assertEqual(C1.__parameters__, (P, T))
10297 self.assertEqual(C1.__args__, (P, T))
10298 self.assertEqual(C1[[int, str], bool], PandT[[int, str], bool])
10299
10300 C2 = PandT[[int, T], T]
10301 self.assertEqual(C2.__parameters__, (T,))
10302 self.assertEqual(C2.__args__, ((int, T), T))
10303 self.assertEqual(C2[str], PandT[[int, str], str])
10304
10305 C3 = PandT[[int, *Ts], T]
10306 self.assertEqual(C3.__parameters__, (Ts, T))
10307 self.assertEqual(C3.__args__, ((int, *Ts), T))
10308 self.assertEqual(C3[str, bool, bytes], PandT[[int, str, bool], bytes])
10309
10310 def test_paramspec_in_nested_generics(self):
10311 # Although ParamSpec should not be found in __parameters__ of most
10312 # generics, they probably should be found when nested in
10313 # a valid location.
10314 T = TypeVar("T")
10315 P = ParamSpec("P")
10316 C1 = Callable[P, T]
10317 G1 = List[C1]
10318 G2 = list[C1]
10319 G3 = list[C1] | int
10320 self.assertEqual(G1.__parameters__, (P, T))
10321 self.assertEqual(G2.__parameters__, (P, T))
10322 self.assertEqual(G3.__parameters__, (P, T))
10323 C = Callable[[int, str], float]
10324 self.assertEqual(G1[[int, str], float], List[C])
10325 self.assertEqual(G2[[int, str], float], list[C])
10326 self.assertEqual(G3[[int, str], float], list[C] | int)
10327
10328 def test_paramspec_gets_copied(self):
10329 # bpo-46581
10330 P = ParamSpec('P')
10331 P2 = ParamSpec('P2')
10332 C1 = Callable[P, int]
10333 self.assertEqual(C1.__parameters__, (P,))
10334 self.assertEqual(C1[P2].__parameters__, (P2,))
10335 self.assertEqual(C1[str].__parameters__, ())
10336 self.assertEqual(C1[str, T].__parameters__, (T,))
10337 self.assertEqual(C1[Concatenate[str, P2]].__parameters__, (P2,))
10338 self.assertEqual(C1[Concatenate[T, P2]].__parameters__, (T, P2))
10339 self.assertEqual(C1[...].__parameters__, ())
10340
10341 C2 = Callable[Concatenate[str, P], int]
10342 self.assertEqual(C2.__parameters__, (P,))
10343 self.assertEqual(C2[P2].__parameters__, (P2,))
10344 self.assertEqual(C2[str].__parameters__, ())
10345 self.assertEqual(C2[str, T].__parameters__, (T,))
10346 self.assertEqual(C2[Concatenate[str, P2]].__parameters__, (P2,))
10347 self.assertEqual(C2[Concatenate[T, P2]].__parameters__, (T, P2))
10348
10349 def test_cannot_subclass(self):
10350 with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpec'):
10351 class C(ParamSpec): pass
10352 with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpecArgs'):
10353 class D(ParamSpecArgs): pass
10354 with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpecKwargs'):
10355 class E(ParamSpecKwargs): pass
10356 P = ParamSpec('P')
10357 with self.assertRaisesRegex(TypeError,
10358 CANNOT_SUBCLASS_INSTANCE % 'ParamSpec'):
10359 class F(P): pass
10360 with self.assertRaisesRegex(TypeError,
10361 CANNOT_SUBCLASS_INSTANCE % 'ParamSpecArgs'):
10362 class G(P.args): pass
10363 with self.assertRaisesRegex(TypeError,
10364 CANNOT_SUBCLASS_INSTANCE % 'ParamSpecKwargs'):
10365 class H(P.kwargs): pass
10366
10367
10368 class ConcatenateTests(BaseTestCase):
10369 def test_basics(self):
10370 P = ParamSpec('P')
10371 class MyClass: ...
10372 c = Concatenate[MyClass, P]
10373 self.assertNotEqual(c, Concatenate)
10374
10375 def test_dir(self):
10376 P = ParamSpec('P')
10377 dir_items = set(dir(Concatenate[int, P]))
10378 for required_item in [
10379 '__args__', '__parameters__', '__origin__',
10380 ]:
10381 with self.subTest(required_item=required_item):
10382 self.assertIn(required_item, dir_items)
10383
10384 def test_valid_uses(self):
10385 P = ParamSpec('P')
10386 T = TypeVar('T')
10387 C1 = Callable[Concatenate[int, P], int]
10388 self.assertEqual(C1.__args__, (Concatenate[int, P], int))
10389 self.assertEqual(C1.__parameters__, (P,))
10390 C2 = Callable[Concatenate[int, T, P], T]
10391 self.assertEqual(C2.__args__, (Concatenate[int, T, P], T))
10392 self.assertEqual(C2.__parameters__, (T, P))
10393
10394 # Test collections.abc.Callable too.
10395 C3 = collections.abc.Callable[Concatenate[int, P], int]
10396 self.assertEqual(C3.__args__, (Concatenate[int, P], int))
10397 self.assertEqual(C3.__parameters__, (P,))
10398 C4 = collections.abc.Callable[Concatenate[int, T, P], T]
10399 self.assertEqual(C4.__args__, (Concatenate[int, T, P], T))
10400 self.assertEqual(C4.__parameters__, (T, P))
10401
10402 def test_invalid_uses(self):
10403 with self.assertRaisesRegex(TypeError, 'Concatenate of no types'):
10404 Concatenate[()]
10405 with self.assertRaisesRegex(
10406 TypeError,
10407 (
10408 'The last parameter to Concatenate should be a '
10409 'ParamSpec variable or ellipsis'
10410 ),
10411 ):
10412 Concatenate[int]
10413
10414 def test_var_substitution(self):
10415 T = TypeVar('T')
10416 P = ParamSpec('P')
10417 P2 = ParamSpec('P2')
10418 C = Concatenate[T, P]
10419 self.assertEqual(C[int, P2], Concatenate[int, P2])
10420 self.assertEqual(C[int, [str, float]], (int, str, float))
10421 self.assertEqual(C[int, []], (int,))
10422 self.assertEqual(C[int, Concatenate[str, P2]],
10423 Concatenate[int, str, P2])
10424 self.assertEqual(C[int, ...], Concatenate[int, ...])
10425
10426 C = Concatenate[int, P]
10427 self.assertEqual(C[P2], Concatenate[int, P2])
10428 self.assertEqual(C[[str, float]], (int, str, float))
10429 self.assertEqual(C[str, float], (int, str, float))
10430 self.assertEqual(C[[]], (int,))
10431 self.assertEqual(C[Concatenate[str, P2]], Concatenate[int, str, P2])
10432 self.assertEqual(C[...], Concatenate[int, ...])
10433
10434
10435 class TypeGuardTests(BaseTestCase):
10436 def test_basics(self):
10437 TypeGuard[int] # OK
10438
10439 def foo(arg) -> TypeGuard[int]: ...
10440 self.assertEqual(gth(foo), {'return': TypeGuard[int]})
10441
10442 with self.assertRaises(TypeError):
10443 TypeGuard[int, str]
10444
10445 def test_repr(self):
10446 self.assertEqual(repr(TypeGuard), 'typing.TypeGuard')
10447 cv = TypeGuard[int]
10448 self.assertEqual(repr(cv), 'typing.TypeGuard[int]')
10449 cv = TypeGuard[Employee]
10450 self.assertEqual(repr(cv), 'typing.TypeGuard[%s.Employee]' % __name__)
10451 cv = TypeGuard[tuple[int]]
10452 self.assertEqual(repr(cv), 'typing.TypeGuard[tuple[int]]')
10453
10454 def test_cannot_subclass(self):
10455 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
10456 class C(type(TypeGuard)):
10457 pass
10458 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
10459 class D(type(TypeGuard[int])):
10460 pass
10461 with self.assertRaisesRegex(TypeError,
10462 r'Cannot subclass typing\.TypeGuard'):
10463 class E(TypeGuard):
10464 pass
10465 with self.assertRaisesRegex(TypeError,
10466 r'Cannot subclass typing\.TypeGuard\[int\]'):
10467 class F(TypeGuard[int]):
10468 pass
10469
10470 def test_cannot_init(self):
10471 with self.assertRaises(TypeError):
10472 TypeGuard()
10473 with self.assertRaises(TypeError):
10474 type(TypeGuard)()
10475 with self.assertRaises(TypeError):
10476 type(TypeGuard[Optional[int]])()
10477
10478 def test_no_isinstance(self):
10479 with self.assertRaises(TypeError):
10480 isinstance(1, TypeGuard[int])
10481 with self.assertRaises(TypeError):
10482 issubclass(int, TypeGuard)
10483
10484
10485 class TypeIsTests(BaseTestCase):
10486 def test_basics(self):
10487 TypeIs[int] # OK
10488
10489 def foo(arg) -> TypeIs[int]: ...
10490 self.assertEqual(gth(foo), {'return': TypeIs[int]})
10491
10492 with self.assertRaises(TypeError):
10493 TypeIs[int, str]
10494
10495 def test_repr(self):
10496 self.assertEqual(repr(TypeIs), 'typing.TypeIs')
10497 cv = TypeIs[int]
10498 self.assertEqual(repr(cv), 'typing.TypeIs[int]')
10499 cv = TypeIs[Employee]
10500 self.assertEqual(repr(cv), 'typing.TypeIs[%s.Employee]' % __name__)
10501 cv = TypeIs[tuple[int]]
10502 self.assertEqual(repr(cv), 'typing.TypeIs[tuple[int]]')
10503
10504 def test_cannot_subclass(self):
10505 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
10506 class C(type(TypeIs)):
10507 pass
10508 with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
10509 class D(type(TypeIs[int])):
10510 pass
10511 with self.assertRaisesRegex(TypeError,
10512 r'Cannot subclass typing\.TypeIs'):
10513 class E(TypeIs):
10514 pass
10515 with self.assertRaisesRegex(TypeError,
10516 r'Cannot subclass typing\.TypeIs\[int\]'):
10517 class F(TypeIs[int]):
10518 pass
10519
10520 def test_cannot_init(self):
10521 with self.assertRaises(TypeError):
10522 TypeIs()
10523 with self.assertRaises(TypeError):
10524 type(TypeIs)()
10525 with self.assertRaises(TypeError):
10526 type(TypeIs[Optional[int]])()
10527
10528 def test_no_isinstance(self):
10529 with self.assertRaises(TypeError):
10530 isinstance(1, TypeIs[int])
10531 with self.assertRaises(TypeError):
10532 issubclass(int, TypeIs)
10533
10534
10535 SpecialAttrsP = typing.ParamSpec('SpecialAttrsP')
10536 SpecialAttrsT = typing.TypeVar('SpecialAttrsT', int, float, complex)
10537
10538
10539 class SpecialAttrsTests(BaseTestCase):
10540
10541 def test_special_attrs(self):
10542 cls_to_check = {
10543 # ABC classes
10544 typing.AbstractSet: 'AbstractSet',
10545 typing.AsyncContextManager: 'AsyncContextManager',
10546 typing.AsyncGenerator: 'AsyncGenerator',
10547 typing.AsyncIterable: 'AsyncIterable',
10548 typing.AsyncIterator: 'AsyncIterator',
10549 typing.Awaitable: 'Awaitable',
10550 typing.ByteString: 'ByteString',
10551 typing.Callable: 'Callable',
10552 typing.ChainMap: 'ChainMap',
10553 typing.Collection: 'Collection',
10554 typing.Container: 'Container',
10555 typing.ContextManager: 'ContextManager',
10556 typing.Coroutine: 'Coroutine',
10557 typing.Counter: 'Counter',
10558 typing.DefaultDict: 'DefaultDict',
10559 typing.Deque: 'Deque',
10560 typing.Dict: 'Dict',
10561 typing.FrozenSet: 'FrozenSet',
10562 typing.Generator: 'Generator',
10563 typing.Hashable: 'Hashable',
10564 typing.ItemsView: 'ItemsView',
10565 typing.Iterable: 'Iterable',
10566 typing.Iterator: 'Iterator',
10567 typing.KeysView: 'KeysView',
10568 typing.List: 'List',
10569 typing.Mapping: 'Mapping',
10570 typing.MappingView: 'MappingView',
10571 typing.MutableMapping: 'MutableMapping',
10572 typing.MutableSequence: 'MutableSequence',
10573 typing.MutableSet: 'MutableSet',
10574 typing.OrderedDict: 'OrderedDict',
10575 typing.Reversible: 'Reversible',
10576 typing.Sequence: 'Sequence',
10577 typing.Set: 'Set',
10578 typing.Sized: 'Sized',
10579 typing.Tuple: 'Tuple',
10580 typing.Type: 'Type',
10581 typing.ValuesView: 'ValuesView',
10582 # Subscribed ABC classes
10583 typing.AbstractSet[Any]: 'AbstractSet',
10584 typing.AsyncContextManager[Any, Any]: 'AsyncContextManager',
10585 typing.AsyncGenerator[Any, Any]: 'AsyncGenerator',
10586 typing.AsyncIterable[Any]: 'AsyncIterable',
10587 typing.AsyncIterator[Any]: 'AsyncIterator',
10588 typing.Awaitable[Any]: 'Awaitable',
10589 typing.Callable[[], Any]: 'Callable',
10590 typing.Callable[..., Any]: 'Callable',
10591 typing.ChainMap[Any, Any]: 'ChainMap',
10592 typing.Collection[Any]: 'Collection',
10593 typing.Container[Any]: 'Container',
10594 typing.ContextManager[Any, Any]: 'ContextManager',
10595 typing.Coroutine[Any, Any, Any]: 'Coroutine',
10596 typing.Counter[Any]: 'Counter',
10597 typing.DefaultDict[Any, Any]: 'DefaultDict',
10598 typing.Deque[Any]: 'Deque',
10599 typing.Dict[Any, Any]: 'Dict',
10600 typing.FrozenSet[Any]: 'FrozenSet',
10601 typing.Generator[Any, Any, Any]: 'Generator',
10602 typing.ItemsView[Any, Any]: 'ItemsView',
10603 typing.Iterable[Any]: 'Iterable',
10604 typing.Iterator[Any]: 'Iterator',
10605 typing.KeysView[Any]: 'KeysView',
10606 typing.List[Any]: 'List',
10607 typing.Mapping[Any, Any]: 'Mapping',
10608 typing.MappingView[Any]: 'MappingView',
10609 typing.MutableMapping[Any, Any]: 'MutableMapping',
10610 typing.MutableSequence[Any]: 'MutableSequence',
10611 typing.MutableSet[Any]: 'MutableSet',
10612 typing.OrderedDict[Any, Any]: 'OrderedDict',
10613 typing.Reversible[Any]: 'Reversible',
10614 typing.Sequence[Any]: 'Sequence',
10615 typing.Set[Any]: 'Set',
10616 typing.Tuple[Any]: 'Tuple',
10617 typing.Tuple[Any, ...]: 'Tuple',
10618 typing.Type[Any]: 'Type',
10619 typing.ValuesView[Any]: 'ValuesView',
10620 # Special Forms
10621 typing.Annotated: 'Annotated',
10622 typing.Any: 'Any',
10623 typing.ClassVar: 'ClassVar',
10624 typing.Concatenate: 'Concatenate',
10625 typing.Final: 'Final',
10626 typing.Literal: 'Literal',
10627 typing.NewType: 'NewType',
10628 typing.NoReturn: 'NoReturn',
10629 typing.Never: 'Never',
10630 typing.Optional: 'Optional',
10631 typing.TypeAlias: 'TypeAlias',
10632 typing.TypeGuard: 'TypeGuard',
10633 typing.TypeIs: 'TypeIs',
10634 typing.TypeVar: 'TypeVar',
10635 typing.Self: 'Self',
10636 # Subscripted special forms
10637 typing.Annotated[Any, "Annotation"]: 'Annotated',
10638 typing.Annotated[int, 'Annotation']: 'Annotated',
10639 typing.ClassVar[Any]: 'ClassVar',
10640 typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate',
10641 typing.Final[Any]: 'Final',
10642 typing.Literal[Any]: 'Literal',
10643 typing.Literal[1, 2]: 'Literal',
10644 typing.Literal[True, 2]: 'Literal',
10645 typing.Optional[Any]: 'Union',
10646 typing.TypeGuard[Any]: 'TypeGuard',
10647 typing.TypeIs[Any]: 'TypeIs',
10648 typing.Union[Any]: 'Any',
10649 typing.Union[int, float]: 'Union',
10650 # Incompatible special forms (tested in test_special_attrs2)
10651 # - typing.NewType('TypeName', Any)
10652 # - typing.ParamSpec('SpecialAttrsP')
10653 # - typing.TypeVar('T')
10654 }
10655
10656 for cls, name in cls_to_check.items():
10657 with self.subTest(cls=cls):
10658 self.assertEqual(cls.__name__, name, str(cls))
10659 self.assertEqual(cls.__qualname__, name, str(cls))
10660 self.assertEqual(cls.__module__, 'typing', str(cls))
10661 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10662 s = pickle.dumps(cls, proto)
10663 loaded = pickle.loads(s)
10664 if isinstance(cls, Union):
10665 self.assertEqual(cls, loaded)
10666 else:
10667 self.assertIs(cls, loaded)
10668
10669 TypeName = typing.NewType('SpecialAttrsTests.TypeName', Any)
10670
10671 def test_special_attrs2(self):
10672 self.assertEqual(SpecialAttrsTests.TypeName.__name__, 'TypeName')
10673 self.assertEqual(
10674 SpecialAttrsTests.TypeName.__qualname__,
10675 'SpecialAttrsTests.TypeName',
10676 )
10677 self.assertEqual(
10678 SpecialAttrsTests.TypeName.__module__,
10679 __name__,
10680 )
10681 # NewTypes are picklable assuming correct qualname information.
10682 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10683 s = pickle.dumps(SpecialAttrsTests.TypeName, proto)
10684 loaded = pickle.loads(s)
10685 self.assertIs(SpecialAttrsTests.TypeName, loaded)
10686
10687 # Type variables don't support non-global instantiation per PEP 484
10688 # restriction that "The argument to TypeVar() must be a string equal
10689 # to the variable name to which it is assigned". Thus, providing
10690 # __qualname__ is unnecessary.
10691 self.assertEqual(SpecialAttrsT.__name__, 'SpecialAttrsT')
10692 self.assertNotHasAttr(SpecialAttrsT, '__qualname__')
10693 self.assertEqual(SpecialAttrsT.__module__, __name__)
10694 # Module-level type variables are picklable.
10695 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10696 s = pickle.dumps(SpecialAttrsT, proto)
10697 loaded = pickle.loads(s)
10698 self.assertIs(SpecialAttrsT, loaded)
10699
10700 self.assertEqual(SpecialAttrsP.__name__, 'SpecialAttrsP')
10701 self.assertNotHasAttr(SpecialAttrsP, '__qualname__')
10702 self.assertEqual(SpecialAttrsP.__module__, __name__)
10703 # Module-level ParamSpecs are picklable.
10704 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10705 s = pickle.dumps(SpecialAttrsP, proto)
10706 loaded = pickle.loads(s)
10707 self.assertIs(SpecialAttrsP, loaded)
10708
10709 def test_genericalias_dir(self):
10710 class Foo(Generic[T]):
10711 def bar(self):
10712 pass
10713 baz = 3
10714 __magic__ = 4
10715
10716 # The class attributes of the original class should be visible even
10717 # in dir() of the GenericAlias. See bpo-45755.
10718 dir_items = set(dir(Foo[int]))
10719 for required_item in [
10720 'bar', 'baz',
10721 '__args__', '__parameters__', '__origin__',
10722 ]:
10723 with self.subTest(required_item=required_item):
10724 self.assertIn(required_item, dir_items)
10725 self.assertNotIn('__magic__', dir_items)
10726
10727
10728 class RevealTypeTests(BaseTestCase):
10729 def test_reveal_type(self):
10730 obj = object()
10731 with captured_stderr() as stderr:
10732 self.assertIs(obj, reveal_type(obj))
10733 self.assertEqual(stderr.getvalue(), "Runtime type is 'object'\n")
10734
10735
10736 class DataclassTransformTests(BaseTestCase):
10737 def test_decorator(self):
10738 def create_model(*, frozen: bool = False, kw_only: bool = True):
10739 return lambda cls: cls
10740
10741 decorated = dataclass_transform(kw_only_default=True, order_default=False)(create_model)
10742
10743 class CustomerModel:
10744 id: int
10745
10746 self.assertIs(decorated, create_model)
10747 self.assertEqual(
10748 decorated.__dataclass_transform__,
10749 {
10750 "eq_default": True,
10751 "order_default": False,
10752 "kw_only_default": True,
10753 "frozen_default": False,
10754 "field_specifiers": (),
10755 "kwargs": {},
10756 }
10757 )
10758 self.assertIs(
10759 decorated(frozen=True, kw_only=False)(CustomerModel),
10760 CustomerModel
10761 )
10762
10763 def test_base_class(self):
10764 class ModelBase:
10765 def __init_subclass__(cls, *, frozen: bool = False): ...
10766
10767 Decorated = dataclass_transform(
10768 eq_default=True,
10769 order_default=True,
10770 # Arbitrary unrecognized kwargs are accepted at runtime.
10771 make_everything_awesome=True,
10772 )(ModelBase)
10773
10774 class CustomerModel(Decorated, frozen=True):
10775 id: int
10776
10777 self.assertIs(Decorated, ModelBase)
10778 self.assertEqual(
10779 Decorated.__dataclass_transform__,
10780 {
10781 "eq_default": True,
10782 "order_default": True,
10783 "kw_only_default": False,
10784 "frozen_default": False,
10785 "field_specifiers": (),
10786 "kwargs": {"make_everything_awesome": True},
10787 }
10788 )
10789 self.assertIsSubclass(CustomerModel, Decorated)
10790
10791 def test_metaclass(self):
10792 class Field: ...
10793
10794 class ModelMeta(type):
10795 def __new__(
10796 cls, name, bases, namespace, *, init: bool = True,
10797 ):
10798 return super().__new__(cls, name, bases, namespace)
10799
10800 Decorated = dataclass_transform(
10801 order_default=True, frozen_default=True, field_specifiers=(Field,)
10802 )(ModelMeta)
10803
10804 class ModelBase(metaclass=Decorated): ...
10805
10806 class CustomerModel(ModelBase, init=False):
10807 id: int
10808
10809 self.assertIs(Decorated, ModelMeta)
10810 self.assertEqual(
10811 Decorated.__dataclass_transform__,
10812 {
10813 "eq_default": True,
10814 "order_default": True,
10815 "kw_only_default": False,
10816 "frozen_default": True,
10817 "field_specifiers": (Field,),
10818 "kwargs": {},
10819 }
10820 )
10821 self.assertIsInstance(CustomerModel, Decorated)
10822
10823
10824 class NoDefaultTests(BaseTestCase):
10825 def test_pickling(self):
10826 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
10827 s = pickle.dumps(NoDefault, proto)
10828 loaded = pickle.loads(s)
10829 self.assertIs(NoDefault, loaded)
10830
10831 def test_constructor(self):
10832 self.assertIs(NoDefault, type(NoDefault)())
10833 with self.assertRaises(TypeError):
10834 type(NoDefault)(1)
10835
10836 def test_repr(self):
10837 self.assertEqual(repr(NoDefault), 'typing.NoDefault')
10838
10839 @requires_docstrings
10840 def test_doc(self):
10841 self.assertIsInstance(NoDefault.__doc__, str)
10842
10843 def test_class(self):
10844 self.assertIs(NoDefault.__class__, type(NoDefault))
10845
10846 def test_no_call(self):
10847 with self.assertRaises(TypeError):
10848 NoDefault()
10849
10850 def test_no_attributes(self):
10851 with self.assertRaises(AttributeError):
10852 NoDefault.foo = 3
10853 with self.assertRaises(AttributeError):
10854 NoDefault.foo
10855
10856 # TypeError is consistent with the behavior of NoneType
10857 with self.assertRaises(TypeError):
10858 type(NoDefault).foo = 3
10859 with self.assertRaises(AttributeError):
10860 type(NoDefault).foo
10861
10862 def test_no_subclassing(self):
10863 with self.assertRaises(TypeError):
10864 class Test(type(NoDefault)): ...
10865
10866
10867 class AllTests(BaseTestCase):
10868 """Tests for __all__."""
10869
10870 def test_all(self):
10871 from typing import __all__ as a
10872 # Just spot-check the first and last of every category.
10873 self.assertIn('AbstractSet', a)
10874 self.assertIn('ValuesView', a)
10875 self.assertIn('cast', a)
10876 self.assertIn('overload', a)
10877 # Context managers.
10878 self.assertIn('ContextManager', a)
10879 self.assertIn('AsyncContextManager', a)
10880 # Check that former namespaces io and re are not exported.
10881 self.assertNotIn('io', a)
10882 self.assertNotIn('re', a)
10883 # Spot-check that stdlib modules aren't exported.
10884 self.assertNotIn('os', a)
10885 self.assertNotIn('sys', a)
10886 # Check that Text is defined.
10887 self.assertIn('Text', a)
10888 # Check previously missing classes.
10889 self.assertIn('SupportsBytes', a)
10890 self.assertIn('SupportsComplex', a)
10891
10892 def test_all_exported_names(self):
10893 # ensure all dynamically created objects are actualised
10894 for name in typing.__all__:
10895 getattr(typing, name)
10896
10897 actual_all = set(typing.__all__)
10898 computed_all = {
10899 k for k, v in vars(typing).items()
10900 # explicitly exported, not a thing with __module__
10901 if k in actual_all or (
10902 # avoid private names
10903 not k.startswith('_') and
10904 # there's a few types and metaclasses that aren't exported
10905 not k.endswith(('Meta', '_contra', '_co')) and
10906 not k.upper() == k and
10907 # but export all things that have __module__ == 'typing'
10908 getattr(v, '__module__', None) == typing.__name__
10909 )
10910 }
10911 self.assertSetEqual(computed_all, actual_all)
10912
10913
10914 class TypeIterationTests(BaseTestCase):
10915 _UNITERABLE_TYPES = (
10916 Any,
10917 Union,
10918 Union[str, int],
10919 Union[str, T],
10920 List,
10921 Tuple,
10922 Callable,
10923 Callable[..., T],
10924 Callable[[T], str],
10925 Annotated,
10926 Annotated[T, ''],
10927 )
10928
10929 def test_cannot_iterate(self):
10930 expected_error_regex = "object is not iterable"
10931 for test_type in self._UNITERABLE_TYPES:
10932 with self.subTest(type=test_type):
10933 with self.assertRaisesRegex(TypeError, expected_error_regex):
10934 iter(test_type)
10935 with self.assertRaisesRegex(TypeError, expected_error_regex):
10936 list(test_type)
10937 with self.assertRaisesRegex(TypeError, expected_error_regex):
10938 for _ in test_type:
10939 pass
10940
10941 def test_is_not_instance_of_iterable(self):
10942 for type_to_test in self._UNITERABLE_TYPES:
10943 self.assertNotIsInstance(type_to_test, collections.abc.Iterable)
10944
10945
10946 class UnionGenericAliasTests(BaseTestCase):
10947 def test_constructor(self):
10948 # Used e.g. in typer, pydantic
10949 with self.assertWarns(DeprecationWarning):
10950 inst = typing._UnionGenericAlias(typing.Union, (int, str))
10951 self.assertEqual(inst, int | str)
10952 with self.assertWarns(DeprecationWarning):
10953 # name is accepted but ignored
10954 inst = typing._UnionGenericAlias(typing.Union, (int, None), name="Optional")
10955 self.assertEqual(inst, int | None)
10956
10957 def test_isinstance(self):
10958 # Used e.g. in pydantic
10959 with self.assertWarns(DeprecationWarning):
10960 self.assertTrue(isinstance(Union[int, str], typing._UnionGenericAlias))
10961 with self.assertWarns(DeprecationWarning):
10962 self.assertFalse(isinstance(int, typing._UnionGenericAlias))
10963
10964 def test_eq(self):
10965 # type(t) == _UnionGenericAlias is used in vyos
10966 with self.assertWarns(DeprecationWarning):
10967 self.assertEqual(Union, typing._UnionGenericAlias)
10968 with self.assertWarns(DeprecationWarning):
10969 self.assertEqual(typing._UnionGenericAlias, typing._UnionGenericAlias)
10970 with self.assertWarns(DeprecationWarning):
10971 self.assertNotEqual(int, typing._UnionGenericAlias)
10972
10973 def test_hashable(self):
10974 self.assertEqual(hash(typing._UnionGenericAlias), hash(Union))
10975
10976
10977 def load_tests(loader, tests, pattern):
10978 import doctest
10979 tests.addTests(doctest.DocTestSuite(typing))
10980 return tests
10981
10982
10983 if __name__ == '__main__':
10984 main()