From c6d87474ae51f00ba9af4cc6a43c42a0559543a5 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 24 Nov 2021 09:54:40 -0800 Subject: [PATCH] fix 3.10 --- typing_extensions/src/typing_extensions.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/typing_extensions/src/typing_extensions.py b/typing_extensions/src/typing_extensions.py index 097f8842d..4566b9e4c 100644 --- a/typing_extensions/src/typing_extensions.py +++ b/typing_extensions/src/typing_extensions.py @@ -2328,16 +2328,25 @@ def add_batch_axis( item = typing._type_check(parameters, f'{self._name} accepts only single type') return _UnpackAlias(self, (item,)) - def _collect_type_vars(types): + # We have to do some monkey patching to deal with the dual nature of + # Unpack/TypeVarTuple: + # - We want Unpack to be a kind of TypeVar so it gets accepted in + # Generic[Unpack[Ts]] + # - We want it to *not* be treated as a TypeVar for the purposes of + # counting generic parameters, so that when we subscript a generic, + # the runtime doesn't try to substitute the Unpack with the subscripted type. + def _collect_type_vars(types, typevar_types=None): """Collect all type variable contained in types in order of first appearance (lexicographic order). For example:: _collect_type_vars((T, List[S, T])) == (T, S) """ + if typevar_types is None: + typevar_types = typing.TypeVar tvars = [] for t in types: if ( - isinstance(t, typing.TypeVar) + isinstance(t, typevar_types) and t not in tvars and not isinstance(t, _UnpackAlias) ):