From 6631801cb51053e43e67aeb1405954b7743d45a4 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Sat, 5 Feb 2022 13:23:25 -0800 Subject: [PATCH 1/7] remove callable() check from typing._type_check We also remove all the tests that check for integer literals --- Lib/test/test_types.py | 6 ------ Lib/test/test_typing.py | 23 ----------------------- Lib/typing.py | 2 -- 3 files changed, 31 deletions(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index c54854eeb5ad22..a6398b81467623 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -831,12 +831,6 @@ def test_union_copy(self): self.assertEqual(copied.__args__, orig.__args__) self.assertEqual(copied.__parameters__, orig.__parameters__) - def test_union_parameter_substitution_errors(self): - T = typing.TypeVar("T") - x = int | T - with self.assertRaises(TypeError): - x[42] - def test_or_type_operator_with_forward(self): T = typing.TypeVar('T') ForwardAfter = T | 'Forward' diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 85f74064458f2d..6ec5296df99d77 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -239,8 +239,6 @@ def test_cannot_instantiate_vars(self): TypeVar('A')() def test_bound_errors(self): - with self.assertRaises(TypeError): - TypeVar('X', bound=42) with self.assertRaises(TypeError): TypeVar('X', str, float, bound=Employee) @@ -1994,9 +1992,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]]) @@ -2497,8 +2492,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): @@ -2537,8 +2530,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): @@ -2923,14 +2914,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]'): @@ -4222,8 +4205,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) @@ -4349,10 +4330,6 @@ def test_typeddict_errors(self): isinstance(jim, Emp) with self.assertRaises(TypeError): issubclass(dict, Emp) - 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) diff --git a/Lib/typing.py b/Lib/typing.py index 0cf9755022e9b8..104b38bd22fb0c 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -180,8 +180,6 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms= raise TypeError(f"Plain {arg} is not valid as type argument") if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)): return arg - if not callable(arg): - raise TypeError(f"{msg} Got {arg!r:.100}.") return arg From bff7b6e5d6c2f18d18c60d78d0b4d963fb027df4 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Sat, 5 Feb 2022 13:58:27 -0800 Subject: [PATCH 2/7] add tuple check: covers cases like Final[int, str] --- Lib/test/test_typing.py | 3 +++ Lib/typing.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 6ec5296df99d77..1710bfb3529c9f 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -5043,6 +5043,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 104b38bd22fb0c..a9b72243764af6 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -180,6 +180,8 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms= raise TypeError(f"Plain {arg} is not valid as type argument") if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)): return arg + if type(arg) is tuple: + raise TypeError(f"{msg} Got {arg!r:.100}.") return arg From 8e98706dc68655eaefd136bc4b871de3412badb5 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Sat, 5 Feb 2022 14:01:08 -0800 Subject: [PATCH 3/7] remove list literal type checks --- Lib/test/test_typing.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 1710bfb3529c9f..45e8d1ff053864 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4240,8 +4240,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 @@ -4314,11 +4312,8 @@ 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) - with self.assertRaises(TypeError): - TypedDict('Emp', _fields={'name': str, 'id': int}) def test_typeddict_errors(self): Emp = TypedDict('Emp', {'name': str, 'id': int}) From 43a19bb2a5d4ddca171a07e2fec21e32ab031cc4 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 5 Feb 2022 22:14:44 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-02-05-22-14-44.bpo-46644.P--1Cz.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-02-05-22-14-44.bpo-46644.P--1Cz.rst 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..84bf0212d3b6eb --- /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. Patch by Gregory Beauregard. \ No newline at end of file From 6c20542cc833c83fbb9603f1580d815cf0711b50 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Sat, 5 Feb 2022 14:19:13 -0800 Subject: [PATCH 5/7] remove list of types exempt from callable check --- Lib/typing.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index a9b72243764af6..0c987428814d2a 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -178,8 +178,6 @@ 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)): - return arg if type(arg) is tuple: raise TypeError(f"{msg} Got {arg!r:.100}.") return arg From 2dbab9709c0d4f1013b50f902f26ba92f6f04873 Mon Sep 17 00:00:00 2001 From: GBeauregard Date: Sat, 5 Feb 2022 17:57:07 -0800 Subject: [PATCH 6/7] add alternative tests --- Lib/test/test_types.py | 6 ++++++ Lib/test/test_typing.py | 2 ++ 2 files changed, 8 insertions(+) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index a6398b81467623..220d4b7f1b890c 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -831,6 +831,12 @@ def test_union_copy(self): self.assertEqual(copied.__args__, orig.__args__) self.assertEqual(copied.__parameters__, orig.__parameters__) + def test_union_parameter_substitution_errors(self): + T = typing.TypeVar("T") + x = int | T + with self.assertRaises(TypeError): + x[int, str] + def test_or_type_operator_with_forward(self): T = typing.TypeVar('T') ForwardAfter = T | 'Forward' diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 45e8d1ff053864..131952ccfa676c 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -239,6 +239,8 @@ def test_cannot_instantiate_vars(self): TypeVar('A')() def test_bound_errors(self): + with self.assertRaises(TypeError): + TypeVar('X', bound=Union) with self.assertRaises(TypeError): TypeVar('X', str, float, bound=Employee) From fdcc4418409bf6455b384189a5ab2716cf92bff6 Mon Sep 17 00:00:00 2001 From: Gregory Beauregard Date: Sat, 5 Feb 2022 18:00:57 -0800 Subject: [PATCH 7/7] Update 2022-02-05-22-14-44.bpo-46644.P--1Cz.rst --- .../next/Library/2022-02-05-22-14-44.bpo-46644.P--1Cz.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 84bf0212d3b6eb..25a999fac8d378 100644 --- 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 @@ -1 +1 @@ -No longer require valid typeforms to be callable. Patch by Gregory Beauregard. \ No newline at end of file +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.