Skip to content

Commit 870b22b

Browse files
authored
bpo-46644: Remove callable() requirement from typing._type_check (GH-31151)
We also remove all the tests that check for integer literals.
1 parent a89c29f commit 870b22b

File tree

4 files changed

+7
-33
lines changed

4 files changed

+7
-33
lines changed

Lib/test/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ def test_union_parameter_substitution_errors(self):
875875
T = typing.TypeVar("T")
876876
x = int | T
877877
with self.assertRaises(TypeError):
878-
x[42]
878+
x[int, str]
879879

880880
def test_or_type_operator_with_forward(self):
881881
T = typing.TypeVar('T')

Lib/test/test_typing.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def test_cannot_instantiate_vars(self):
345345

346346
def test_bound_errors(self):
347347
with self.assertRaises(TypeError):
348-
TypeVar('X', bound=42)
348+
TypeVar('X', bound=Union)
349349
with self.assertRaises(TypeError):
350350
TypeVar('X', str, float, bound=Employee)
351351

@@ -2591,9 +2591,6 @@ def test_extended_generic_rules_eq(self):
25912591
class Base: ...
25922592
class Derived(Base): ...
25932593
self.assertEqual(Union[T, Base][Union[Base, Derived]], Union[Base, Derived])
2594-
with self.assertRaises(TypeError):
2595-
Union[T, int][1]
2596-
25972594
self.assertEqual(Callable[[T], T][KT], Callable[[KT], KT])
25982595
self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])
25992596

@@ -3136,8 +3133,6 @@ class Foo(obj):
31363133
class ClassVarTests(BaseTestCase):
31373134

31383135
def test_basics(self):
3139-
with self.assertRaises(TypeError):
3140-
ClassVar[1]
31413136
with self.assertRaises(TypeError):
31423137
ClassVar[int, str]
31433138
with self.assertRaises(TypeError):
@@ -3176,8 +3171,6 @@ class FinalTests(BaseTestCase):
31763171

31773172
def test_basics(self):
31783173
Final[int] # OK
3179-
with self.assertRaises(TypeError):
3180-
Final[1]
31813174
with self.assertRaises(TypeError):
31823175
Final[int, str]
31833176
with self.assertRaises(TypeError):
@@ -3591,14 +3584,6 @@ def foo(a: 'Node[T'):
35913584
with self.assertRaises(SyntaxError):
35923585
get_type_hints(foo)
35933586

3594-
def test_type_error(self):
3595-
3596-
def foo(a: Tuple['42']):
3597-
pass
3598-
3599-
with self.assertRaises(TypeError):
3600-
get_type_hints(foo)
3601-
36023587
def test_name_error(self):
36033588

36043589
def foo(a: 'Noode[T]'):
@@ -5011,8 +4996,6 @@ def test_namedtuple_keyword_usage(self):
50114996
self.assertEqual(LocalEmployee.__annotations__, dict(name=str, age=int))
50124997
with self.assertRaises(TypeError):
50134998
NamedTuple('Name', [('x', int)], y=str)
5014-
with self.assertRaises(TypeError):
5015-
NamedTuple('Name', x=1, y='a')
50164999

50175000
def test_namedtuple_special_keyword_names(self):
50185001
NT = NamedTuple("NT", cls=type, self=object, typename=str, fields=list)
@@ -5048,8 +5031,6 @@ def test_namedtuple_errors(self):
50485031
NamedTuple('Emp', [('_name', str)])
50495032
with self.assertRaises(TypeError):
50505033
NamedTuple(typename='Emp', name=str, id=int)
5051-
with self.assertRaises(TypeError):
5052-
NamedTuple('Emp', fields=[('name', str), ('id', int)])
50535034

50545035
def test_copy_and_pickle(self):
50555036
global Emp # pickle wants to reference the class by name
@@ -5124,7 +5105,6 @@ def test_typeddict_create_errors(self):
51245105
TypedDict()
51255106
with self.assertRaises(TypeError):
51265107
TypedDict('Emp', [('name', str)], None)
5127-
51285108
with self.assertRaises(TypeError):
51295109
TypedDict(_typename='Emp', name=str, id=int)
51305110

@@ -5138,13 +5118,6 @@ def test_typeddict_errors(self):
51385118
isinstance(jim, Emp)
51395119
with self.assertRaises(TypeError):
51405120
issubclass(dict, Emp)
5141-
# We raise a DeprecationWarning for the keyword syntax
5142-
# before the TypeError.
5143-
with self.assertWarns(DeprecationWarning):
5144-
with self.assertRaises(TypeError):
5145-
TypedDict('Hi', x=1)
5146-
with self.assertRaises(TypeError):
5147-
TypedDict('Hi', [('x', int), ('y', 1)])
51485121
with self.assertRaises(TypeError):
51495122
TypedDict('Hi', [('x', int)], y=int)
51505123

@@ -5916,6 +5889,9 @@ def test_basics(self):
59165889
def foo(arg) -> TypeGuard[int]: ...
59175890
self.assertEqual(gth(foo), {'return': TypeGuard[int]})
59185891

5892+
with self.assertRaises(TypeError):
5893+
TypeGuard[int, str]
5894+
59195895
def test_repr(self):
59205896
self.assertEqual(repr(TypeGuard), 'typing.TypeGuard')
59215897
cv = TypeGuard[int]

Lib/typing.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
185185
return arg
186186
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
187187
raise TypeError(f"Plain {arg} is not valid as type argument")
188-
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec,
189-
ParamSpecArgs, ParamSpecKwargs, TypeVarTuple)):
190-
return arg
191-
if not callable(arg):
188+
if type(arg) is tuple:
192189
raise TypeError(f"{msg} Got {arg!r:.100}.")
193190
return arg
194191

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

0 commit comments

Comments
 (0)