mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
bpo-46644: Remove callable() requirement from typing._type_check (GH-31151)
We also remove all the tests that check for integer literals.
This commit is contained in:
parent
a89c29fbcc
commit
870b22b9c4
4 changed files with 7 additions and 33 deletions
|
@ -875,7 +875,7 @@ class UnionTests(unittest.TestCase):
|
||||||
T = typing.TypeVar("T")
|
T = typing.TypeVar("T")
|
||||||
x = int | T
|
x = int | T
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
x[42]
|
x[int, str]
|
||||||
|
|
||||||
def test_or_type_operator_with_forward(self):
|
def test_or_type_operator_with_forward(self):
|
||||||
T = typing.TypeVar('T')
|
T = typing.TypeVar('T')
|
||||||
|
|
|
@ -345,7 +345,7 @@ class TypeVarTests(BaseTestCase):
|
||||||
|
|
||||||
def test_bound_errors(self):
|
def test_bound_errors(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
TypeVar('X', bound=42)
|
TypeVar('X', bound=Union)
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
TypeVar('X', str, float, bound=Employee)
|
TypeVar('X', str, float, bound=Employee)
|
||||||
|
|
||||||
|
@ -2591,9 +2591,6 @@ class GenericTests(BaseTestCase):
|
||||||
class Base: ...
|
class Base: ...
|
||||||
class Derived(Base): ...
|
class Derived(Base): ...
|
||||||
self.assertEqual(Union[T, Base][Union[Base, Derived]], Union[Base, Derived])
|
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[[T], T][KT], Callable[[KT], KT])
|
||||||
self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])
|
self.assertEqual(Callable[..., List[T]][int], Callable[..., List[int]])
|
||||||
|
|
||||||
|
@ -3136,8 +3133,6 @@ class GenericTests(BaseTestCase):
|
||||||
class ClassVarTests(BaseTestCase):
|
class ClassVarTests(BaseTestCase):
|
||||||
|
|
||||||
def test_basics(self):
|
def test_basics(self):
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
ClassVar[1]
|
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
ClassVar[int, str]
|
ClassVar[int, str]
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
|
@ -3176,8 +3171,6 @@ class FinalTests(BaseTestCase):
|
||||||
|
|
||||||
def test_basics(self):
|
def test_basics(self):
|
||||||
Final[int] # OK
|
Final[int] # OK
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
Final[1]
|
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
Final[int, str]
|
Final[int, str]
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
|
@ -3591,14 +3584,6 @@ class ForwardRefTests(BaseTestCase):
|
||||||
with self.assertRaises(SyntaxError):
|
with self.assertRaises(SyntaxError):
|
||||||
get_type_hints(foo)
|
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 test_name_error(self):
|
||||||
|
|
||||||
def foo(a: 'Noode[T]'):
|
def foo(a: 'Noode[T]'):
|
||||||
|
@ -5011,8 +4996,6 @@ class NamedTupleTests(BaseTestCase):
|
||||||
self.assertEqual(LocalEmployee.__annotations__, dict(name=str, age=int))
|
self.assertEqual(LocalEmployee.__annotations__, dict(name=str, age=int))
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
NamedTuple('Name', [('x', int)], y=str)
|
NamedTuple('Name', [('x', int)], y=str)
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
NamedTuple('Name', x=1, y='a')
|
|
||||||
|
|
||||||
def test_namedtuple_special_keyword_names(self):
|
def test_namedtuple_special_keyword_names(self):
|
||||||
NT = NamedTuple("NT", cls=type, self=object, typename=str, fields=list)
|
NT = NamedTuple("NT", cls=type, self=object, typename=str, fields=list)
|
||||||
|
@ -5048,8 +5031,6 @@ class NamedTupleTests(BaseTestCase):
|
||||||
NamedTuple('Emp', [('_name', str)])
|
NamedTuple('Emp', [('_name', str)])
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
NamedTuple(typename='Emp', name=str, id=int)
|
NamedTuple(typename='Emp', name=str, id=int)
|
||||||
with self.assertRaises(TypeError):
|
|
||||||
NamedTuple('Emp', fields=[('name', str), ('id', int)])
|
|
||||||
|
|
||||||
def test_copy_and_pickle(self):
|
def test_copy_and_pickle(self):
|
||||||
global Emp # pickle wants to reference the class by name
|
global Emp # pickle wants to reference the class by name
|
||||||
|
@ -5124,7 +5105,6 @@ class TypedDictTests(BaseTestCase):
|
||||||
TypedDict()
|
TypedDict()
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
TypedDict('Emp', [('name', str)], None)
|
TypedDict('Emp', [('name', str)], None)
|
||||||
|
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
TypedDict(_typename='Emp', name=str, id=int)
|
TypedDict(_typename='Emp', name=str, id=int)
|
||||||
|
|
||||||
|
@ -5138,13 +5118,6 @@ class TypedDictTests(BaseTestCase):
|
||||||
isinstance(jim, Emp)
|
isinstance(jim, Emp)
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
issubclass(dict, Emp)
|
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):
|
with self.assertRaises(TypeError):
|
||||||
TypedDict('Hi', [('x', int)], y=int)
|
TypedDict('Hi', [('x', int)], y=int)
|
||||||
|
|
||||||
|
@ -5916,6 +5889,9 @@ class TypeGuardTests(BaseTestCase):
|
||||||
def foo(arg) -> TypeGuard[int]: ...
|
def foo(arg) -> TypeGuard[int]: ...
|
||||||
self.assertEqual(gth(foo), {'return': TypeGuard[int]})
|
self.assertEqual(gth(foo), {'return': TypeGuard[int]})
|
||||||
|
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
TypeGuard[int, str]
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
self.assertEqual(repr(TypeGuard), 'typing.TypeGuard')
|
self.assertEqual(repr(TypeGuard), 'typing.TypeGuard')
|
||||||
cv = TypeGuard[int]
|
cv = TypeGuard[int]
|
||||||
|
|
|
@ -185,10 +185,7 @@ def _type_check(arg, msg, is_argument=True, module=None, *, allow_special_forms=
|
||||||
return arg
|
return arg
|
||||||
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
|
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
|
||||||
raise TypeError(f"Plain {arg} is not valid as type argument")
|
raise TypeError(f"Plain {arg} is not valid as type argument")
|
||||||
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec,
|
if type(arg) is tuple:
|
||||||
ParamSpecArgs, ParamSpecKwargs, TypeVarTuple)):
|
|
||||||
return arg
|
|
||||||
if not callable(arg):
|
|
||||||
raise TypeError(f"{msg} Got {arg!r:.100}.")
|
raise TypeError(f"{msg} Got {arg!r:.100}.")
|
||||||
return arg
|
return arg
|
||||||
|
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue