diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index b8b1ce96f93c3a..f8b239117f513f 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -875,7 +875,7 @@ def test_union_parameter_substitution_errors(self): T = typing.TypeVar("T") x = int | T with self.assertRaises(TypeError): - x[42] + x[int, str] def test_or_type_operator_with_forward(self): T = typing.TypeVar('T') diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 5c1e907070ee41..27805301e00681 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -345,7 +345,7 @@ def test_cannot_instantiate_vars(self): def test_bound_errors(self): with self.assertRaises(TypeError): - TypeVar('X', bound=42) + TypeVar('X', bound=Union) with self.assertRaises(TypeError): TypeVar('X', str, float, bound=Employee) @@ -2544,9 +2544,6 @@ def test_extended_generic_rules_eq(self): class Base: ... class Derived(Base): ... self.assertEqual(Union[T, Base][Union[Base, Derived]], Union[Base, Derived]) - with self.assertRaises(TypeError): - Union[T, int][1] - self.assertEqual(Callable[[T], T][KT], Callable[[KT], KT]) self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]]) @@ -3086,8 +3083,6 @@ class Foo(obj): class ClassVarTests(BaseTestCase): def test_basics(self): - with self.assertRaises(TypeError): - ClassVar[1] with self.assertRaises(TypeError): ClassVar[int, str] with self.assertRaises(TypeError): @@ -3126,8 +3121,6 @@ class FinalTests(BaseTestCase): def test_basics(self): Final[int] # OK - with self.assertRaises(TypeError): - Final[1] with self.assertRaises(TypeError): Final[int, str] with self.assertRaises(TypeError): @@ -3541,14 +3534,6 @@ def foo(a: 'Node[T'): with self.assertRaises(SyntaxError): get_type_hints(foo) - def test_type_error(self): - - def foo(a: Tuple['42']): - pass - - with self.assertRaises(TypeError): - get_type_hints(foo) - def test_name_error(self): def foo(a: 'Noode[T]'): @@ -4961,8 +4946,6 @@ def test_namedtuple_keyword_usage(self): self.assertEqual(LocalEmployee.__annotations__, dict(name=str, age=int)) with self.assertRaises(TypeError): NamedTuple('Name', [('x', int)], y=str) - with self.assertRaises(TypeError): - NamedTuple('Name', x=1, y='a') def test_namedtuple_special_keyword_names(self): NT = NamedTuple("NT", cls=type, self=object, typename=str, fields=list) @@ -4998,8 +4981,6 @@ def test_namedtuple_errors(self): NamedTuple('Emp', [('_name', str)]) with self.assertRaises(TypeError): NamedTuple(typename='Emp', name=str, id=int) - with self.assertRaises(TypeError): - NamedTuple('Emp', fields=[('name', str), ('id', int)]) def test_copy_and_pickle(self): global Emp # pickle wants to reference the class by name @@ -5074,7 +5055,6 @@ def test_typeddict_create_errors(self): TypedDict() with self.assertRaises(TypeError): TypedDict('Emp', [('name', str)], None) - with self.assertRaises(TypeError): TypedDict(_typename='Emp', name=str, id=int) @@ -5088,13 +5068,6 @@ def test_typeddict_errors(self): isinstance(jim, Emp) with self.assertRaises(TypeError): issubclass(dict, Emp) - # We raise a DeprecationWarning for the keyword syntax - # before the TypeError. - with self.assertWarns(DeprecationWarning): - with self.assertRaises(TypeError): - TypedDict('Hi', x=1) - with self.assertRaises(TypeError): - TypedDict('Hi', [('x', int), ('y', 1)]) with self.assertRaises(TypeError): TypedDict('Hi', [('x', int)], y=int) @@ -5848,6 +5821,9 @@ def test_basics(self): def foo(arg) -> TypeGuard[int]: ... self.assertEqual(gth(foo), {'return': TypeGuard[int]}) + with self.assertRaises(TypeError): + TypeGuard[int, str] + def test_repr(self): self.assertEqual(repr(TypeGuard), 'typing.TypeGuard') cv = TypeGuard[int] diff --git a/Lib/typing.py b/Lib/typing.py index abb8bcefc5c04a..1ee53bbd44cd9e 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -183,10 +183,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms= return arg if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol): raise TypeError(f"Plain {arg} is not valid as type argument") - if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec, - ParamSpecArgs, ParamSpecKwargs, TypeVarTuple)): - return arg - if not callable(arg): + if type(arg) is tuple: raise TypeError(f"{msg} Got {arg!r:.100}.") return arg diff --git a/Misc/NEWS.d/next/Library/2022-02-05-22-14-44.bpo-46644.P--1Cz.rst b/Misc/NEWS.d/next/Library/2022-02-05-22-14-44.bpo-46644.P--1Cz.rst new file mode 100644 index 00000000000000..25a999fac8d378 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-02-05-22-14-44.bpo-46644.P--1Cz.rst @@ -0,0 +1 @@ +No longer require valid typeforms to be callable. This allows :data:`typing.Annotated` to wrap :data:`typing.ParamSpecArgs` and :data:`dataclasses.InitVar`. Patch by Gregory Beauregard.