Repository: python/typing_extensions

Issue:
`_UnpackAlias` `__class__` attribute
Is there a reason to have the `__class__` attribute of the `_UnpackAlias` class set to `typing.TypeVar`?

https://github.com/python/typing_extensions/blob/3f47bf98d0b3b0149e55ba4e4eb8bdb855739a36/src/typing_extensions.py#L2659-L2660

It doesn't seem to be present on `typing`.

Hints from issue comments:
@Daraan: This is a needed for <3.11 when e.g. using `class Bar(Generic[typing_extensions.Unpack[Ts]])` to be compatible with <3.11 `typing` `isinstance(typing_extensions.Unpack[...], TypeVar)` check during `Generic.__class_getitem__`.

I think for 3.11 we could safely remove the changed attribute from the backport.@Viicos: Thanks, that clarifies it!

Solution patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 690bf5d..a02f48c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,9 @@ aliases that have a `Concatenate` special form as their argument.
   to reflect Python 3.13+ behavior: A value assigned to `__total__` in the class body of a
   `TypedDict` will be overwritten by the `total` argument of the `TypedDict` constructor.
   Patch by [Daraan](https://github.com/Daraan), backporting a CPython PR by Jelle Zijlstra.
+- Fix for Python 3.11 that now `isinstance(typing_extensions.Unpack[...], TypeVar)` 
+  evaluates to `False`, however still `True` for <3.11.
+  Patch by [Daraan](https://github.com/Daraan)
 
 # Release 4.12.2 (June 7, 2024)
 
diff --git a/src/typing_extensions.py b/src/typing_extensions.py
index fe492a3..9589f03 100644
--- a/src/typing_extensions.py
+++ b/src/typing_extensions.py
@@ -2657,7 +2657,9 @@ elif sys.version_info[:2] >= (3, 9):  # 3.9+
             self.__doc__ = _UNPACK_DOC
 
     class _UnpackAlias(typing._GenericAlias, _root=True):
-        __class__ = typing.TypeVar
+        if sys.version_info < (3, 11):
+            # needed for compatibility with Generic[Unpack[Ts]]
+            __class__ = typing.TypeVar
 
         @property
         def __typing_unpacked_tuple_args__(self):


Test patch:
diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py
index 17ce28f..beab005 100644
--- a/src/test_typing_extensions.py
+++ b/src/test_typing_extensions.py
@@ -6172,6 +6172,12 @@ class UnpackTests(BaseTestCase):
         self.assertEqual(nested_tuple_bare, TupleAliasTsT[Unpack[Tuple[str, int]], object])
         self.assertEqual(nested_tuple_bare, TupleAliasTsT[Unpack[Tuple[str]], Unpack[Tuple[int]], object])
 
+    @skipUnless(TYPING_3_11_0, "Needed for backport")
+    def test_type_var_inheritance(self):
+        Ts = TypeVarTuple("Ts")
+        self.assertFalse(isinstance(Unpack[Ts], TypeVar))
+        self.assertFalse(isinstance(Unpack[Ts], typing.TypeVar))
+
 
 class TypeVarTupleTests(BaseTestCase):