|
1 | | -import contextlib |
| 1 | +import abc |
2 | 2 | import collections |
3 | | -from collections import defaultdict |
4 | | -from functools import lru_cache, wraps |
| 3 | +import contextlib |
5 | 4 | import inspect |
6 | 5 | import itertools |
7 | 6 | import pickle |
8 | 7 | import re |
9 | 8 | import sys |
10 | | -import warnings |
11 | | -from unittest import TestCase, main, skipUnless, skip |
12 | | -from unittest.mock import patch |
13 | | -from copy import copy, deepcopy |
14 | | - |
15 | | -from typing import Any, NoReturn, Never, assert_never |
16 | | -from typing import overload, get_overloads, clear_overloads |
17 | | -from typing import TypeVar, TypeVarTuple, Unpack, AnyStr |
18 | | -from typing import T, KT, VT # Not in __all__. |
19 | | -from typing import Union, Optional, Literal |
20 | | -from typing import Tuple, List, Dict, MutableMapping |
21 | | -from typing import Callable |
22 | | -from typing import Generic, ClassVar, Final, final, Protocol |
23 | | -from typing import assert_type, cast, runtime_checkable |
24 | | -from typing import get_type_hints |
25 | | -from typing import get_origin, get_args |
26 | | -from typing import override |
27 | | -from typing import is_typeddict |
28 | | -from typing import reveal_type |
29 | | -from typing import dataclass_transform |
30 | | -from typing import no_type_check, no_type_check_decorator |
31 | | -from typing import Type |
32 | | -from typing import NamedTuple, NotRequired, Required, TypedDict |
33 | | -from typing import IO, TextIO, BinaryIO |
34 | | -from typing import Pattern, Match |
35 | | -from typing import Annotated, ForwardRef |
36 | | -from typing import Self, LiteralString |
37 | | -from typing import TypeAlias |
38 | | -from typing import ParamSpec, Concatenate, ParamSpecArgs, ParamSpecKwargs |
39 | | -from typing import TypeGuard |
40 | | -import abc |
41 | 9 | import textwrap |
| 10 | +import types |
42 | 11 | import typing |
| 12 | +import warnings |
43 | 13 | import weakref |
44 | | -import types |
45 | | - |
46 | | -from test.support import import_helper, captured_stderr, cpython_only |
47 | | -from test import mod_generics_cache |
48 | | -from test import _typed_dict_helper |
49 | | - |
| 14 | +from collections import defaultdict |
| 15 | +from copy import copy, deepcopy |
| 16 | +from functools import lru_cache, wraps |
| 17 | +from test import _typed_dict_helper, mod_generics_cache |
| 18 | +from test.support import captured_stderr, cpython_only, import_helper |
| 19 | +from typing import (IO, KT, VT, Annotated, Any, AnyStr, # Not in __all__. |
| 20 | + BinaryIO, Callable, ClassVar, Concatenate, Dict, Final, |
| 21 | + ForwardRef, Generic, List, Literal, LiteralString, Match, |
| 22 | + MutableMapping, NamedTuple, Never, NoReturn, NotRequired, |
| 23 | + Optional, ParamSpec, ParamSpecArgs, ParamSpecKwargs, |
| 24 | + Pattern, Protocol, Required, Self, T, TextIO, Tuple, Type, |
| 25 | + TypeAlias, TypedDict, TypeGuard, TypeVar, TypeVarTuple, |
| 26 | + Union, Unpack, assert_never, assert_type, cast, |
| 27 | + clear_overloads, dataclass_transform, final, get_args, |
| 28 | + get_origin, get_overloads, get_type_hints, is_typeddict, |
| 29 | + no_type_check, no_type_check_decorator, overload, override, |
| 30 | + reveal_type, runtime_checkable) |
| 31 | +from unittest import TestCase, main, skip, skipUnless |
| 32 | +from unittest.mock import patch |
50 | 33 |
|
51 | 34 | py_typing = import_helper.import_fresh_module('typing', blocked=['_typing']) |
52 | 35 | c_typing = import_helper.import_fresh_module('typing', fresh=['_typing']) |
@@ -447,6 +430,22 @@ def test_bound_errors(self): |
447 | 430 | with self.assertRaises(TypeError): |
448 | 431 | TypeVar('X', str, float, bound=Employee) |
449 | 432 |
|
| 433 | + def test_default_error(self): |
| 434 | + with self.assertRaises(TypeError): |
| 435 | + TypeVar('X', default=Union) |
| 436 | + |
| 437 | + def test_default_ordering(self): |
| 438 | + T = TypeVar("T") |
| 439 | + U = TypeVar("U", default=int) |
| 440 | + V = TypeVar("V", default=float) |
| 441 | + |
| 442 | + class Foo(Generic[T, U]): ... |
| 443 | + with self.assertRaises(TypeError): |
| 444 | + class Bar(Generic[U, T]): ... |
| 445 | + |
| 446 | + class Baz(Foo[V]): ... |
| 447 | + |
| 448 | + |
450 | 449 | def test_missing__name__(self): |
451 | 450 | # See bpo-39942 |
452 | 451 | code = ("import typing\n" |
@@ -3698,22 +3697,24 @@ def test_immutability_by_copy_and_pickle(self): |
3698 | 3697 | TPB = TypeVar('TPB', bound=int) |
3699 | 3698 | TPV = TypeVar('TPV', bytes, str) |
3700 | 3699 | PP = ParamSpec('PP') |
3701 | | - for X in [TP, TPB, TPV, PP, |
| 3700 | + TD = TypeVar('TD', default=int) |
| 3701 | + for X in [TP, TPB, TPV, PP, TD, |
3702 | 3702 | List, typing.Mapping, ClassVar, typing.Iterable, |
3703 | 3703 | Union, Any, Tuple, Callable]: |
3704 | 3704 | with self.subTest(thing=X): |
3705 | 3705 | self.assertIs(copy(X), X) |
3706 | 3706 | self.assertIs(deepcopy(X), X) |
3707 | 3707 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
3708 | 3708 | self.assertIs(pickle.loads(pickle.dumps(X, proto)), X) |
3709 | | - del TP, TPB, TPV, PP |
| 3709 | + del TP, TPB, TPV, PP, TD |
3710 | 3710 |
|
3711 | 3711 | # Check that local type variables are copyable. |
3712 | 3712 | TL = TypeVar('TL') |
3713 | 3713 | TLB = TypeVar('TLB', bound=int) |
3714 | 3714 | TLV = TypeVar('TLV', bytes, str) |
3715 | 3715 | PL = ParamSpec('PL') |
3716 | | - for X in [TL, TLB, TLV, PL]: |
| 3716 | + TDL = TypeVar('TDL', default=int) |
| 3717 | + for X in [TL, TLB, TLV, PL, TDL]: |
3717 | 3718 | with self.subTest(thing=X): |
3718 | 3719 | self.assertIs(copy(X), X) |
3719 | 3720 | self.assertIs(deepcopy(X), X) |
@@ -4415,6 +4416,7 @@ def test_errors(self): |
4415 | 4416 | # We need this to make sure that `@no_type_check` respects `__module__` attr: |
4416 | 4417 | from test import ann_module8 |
4417 | 4418 |
|
| 4419 | + |
4418 | 4420 | @no_type_check |
4419 | 4421 | class NoTypeCheck_Outer: |
4420 | 4422 | Inner = ann_module8.NoTypeCheck_Outer.Inner |
@@ -6981,7 +6983,7 @@ def stuff(a: BinaryIO) -> bytes: |
6981 | 6983 | def test_io_submodule(self): |
6982 | 6984 | with warnings.catch_warnings(record=True) as w: |
6983 | 6985 | warnings.filterwarnings("default", category=DeprecationWarning) |
6984 | | - from typing.io import IO, TextIO, BinaryIO, __all__, __name__ |
| 6986 | + from typing.io import IO, BinaryIO, TextIO, __all__, __name__ |
6985 | 6987 | self.assertIs(IO, typing.IO) |
6986 | 6988 | self.assertIs(TextIO, typing.TextIO) |
6987 | 6989 | self.assertIs(BinaryIO, typing.BinaryIO) |
@@ -8058,6 +8060,7 @@ class AllTests(BaseTestCase): |
8058 | 8060 |
|
8059 | 8061 | def test_all(self): |
8060 | 8062 | from typing import __all__ as a |
| 8063 | + |
8061 | 8064 | # Just spot-check the first and last of every category. |
8062 | 8065 | self.assertIn('AbstractSet', a) |
8063 | 8066 | self.assertIn('ValuesView', a) |
|
0 commit comments