Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6855,6 +6855,25 @@ def test_args_and_parameters(self):
self.assertEqual(t.__args__, (Unpack[Ts],))
self.assertEqual(t.__parameters__, (Ts,))

def test_empty_specialization(self):
T = TypeVar('T')
Ts = TypeVarTuple('Ts')

class A(Generic[Unpack[Ts]]): ...

alias = A[()]
self.assertEqual(alias.__args__, ())
self.assertEqual(alias.__parameters__, ())
self.assertIs(get_origin(alias), A)
self.assertEqual(get_args(alias), ())
self.assertIs(A[()], alias)

class B(Generic[T, Unpack[Ts]]): ...
with self.assertRaises(TypeError):
B[()]
with self.assertRaises(TypeError):
Generic[()]

def test_pickle(self):
global Ts, Ts_default # pickle wants to reference the class by name
Ts = TypeVarTuple('Ts')
Expand Down
28 changes: 28 additions & 0 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3905,5 +3905,33 @@ def type_repr(value):
# These are defined unconditionally because they are used in
# typing-extensions itself.
Generic = typing.Generic

# Python 3.10 rejects an empty parameter list before checking whether a
# generic's sole parameter is a TypeVarTuple.
if not hasattr(typing, "TypeVarTuple"):
_generic_class_getitem = typing.Generic.__class_getitem__.__func__

@typing._tp_cache
def _generic_class_getitem_with_empty_typevartuple(cls, params):
parameters = getattr(cls, "__parameters__", ())
if (
isinstance(params, tuple)
and not params
and len(parameters) == 1
and isinstance(parameters[0], TypeVarTuple)
and not parameters[0].has_default()
):
return typing._GenericAlias(
cls,
(),
_typevar_types=(typing.TypeVar, typing.ParamSpec),
_paramspec_tvars=True,
)
return _generic_class_getitem(cls, params)

typing.Generic.__class_getitem__ = classmethod(
_generic_class_getitem_with_empty_typevartuple
)

ForwardRef = typing.ForwardRef
Annotated = typing.Annotated
Loading