@@ -1603,7 +1603,9 @@ class Movie(TypedDict):
1603
1603
""" )
1604
1604
1605
1605
1606
- if sys .version_info [:2 ] >= (3 , 9 ):
1606
+ if hasattr (typing , "Unpack" ): # 3.11+
1607
+ Unpack = typing .Unpack
1608
+ elif sys .version_info [:2 ] >= (3 , 9 ):
1607
1609
class _UnpackSpecialForm (typing ._SpecialForm , _root = True ):
1608
1610
def __repr__ (self ):
1609
1611
return 'typing_extensions.' + self ._name
@@ -1659,84 +1661,87 @@ def _is_unpack(obj):
1659
1661
return isinstance (obj , _UnpackAlias )
1660
1662
1661
1663
1662
- class TypeVarTuple :
1663
- """Type variable tuple.
1664
+ if hasattr (typing , "TypeVarTuple" ): # 3.11+
1665
+ TypeVarTuple = typing .TypeVarTuple
1666
+ else :
1667
+ class TypeVarTuple :
1668
+ """Type variable tuple.
1664
1669
1665
- Usage::
1670
+ Usage::
1666
1671
1667
- Ts = TypeVarTuple('Ts')
1672
+ Ts = TypeVarTuple('Ts')
1668
1673
1669
- In the same way that a normal type variable is a stand-in for a single
1670
- type such as ``int``, a type variable *tuple* is a stand-in for a *tuple* type such as
1671
- ``Tuple[int, str]``.
1674
+ In the same way that a normal type variable is a stand-in for a single
1675
+ type such as ``int``, a type variable *tuple* is a stand-in for a *tuple* type such as
1676
+ ``Tuple[int, str]``.
1672
1677
1673
- Type variable tuples can be used in ``Generic`` declarations.
1674
- Consider the following example::
1678
+ Type variable tuples can be used in ``Generic`` declarations.
1679
+ Consider the following example::
1675
1680
1676
- class Array(Generic[*Ts]): ...
1681
+ class Array(Generic[*Ts]): ...
1677
1682
1678
- The ``Ts`` type variable tuple here behaves like ``tuple[T1, T2]``,
1679
- where ``T1`` and ``T2`` are type variables. To use these type variables
1680
- as type parameters of ``Array``, we must *unpack* the type variable tuple using
1681
- the star operator: ``*Ts``. The signature of ``Array`` then behaves
1682
- as if we had simply written ``class Array(Generic[T1, T2]): ...``.
1683
- In contrast to ``Generic[T1, T2]``, however, ``Generic[*Shape]`` allows
1684
- us to parameterise the class with an *arbitrary* number of type parameters.
1683
+ The ``Ts`` type variable tuple here behaves like ``tuple[T1, T2]``,
1684
+ where ``T1`` and ``T2`` are type variables. To use these type variables
1685
+ as type parameters of ``Array``, we must *unpack* the type variable tuple using
1686
+ the star operator: ``*Ts``. The signature of ``Array`` then behaves
1687
+ as if we had simply written ``class Array(Generic[T1, T2]): ...``.
1688
+ In contrast to ``Generic[T1, T2]``, however, ``Generic[*Shape]`` allows
1689
+ us to parameterise the class with an *arbitrary* number of type parameters.
1685
1690
1686
- Type variable tuples can be used anywhere a normal ``TypeVar`` can.
1687
- This includes class definitions, as shown above, as well as function
1688
- signatures and variable annotations::
1691
+ Type variable tuples can be used anywhere a normal ``TypeVar`` can.
1692
+ This includes class definitions, as shown above, as well as function
1693
+ signatures and variable annotations::
1689
1694
1690
- class Array(Generic[*Ts]):
1695
+ class Array(Generic[*Ts]):
1691
1696
1692
- def __init__(self, shape: Tuple[*Ts]):
1693
- self._shape: Tuple[*Ts] = shape
1697
+ def __init__(self, shape: Tuple[*Ts]):
1698
+ self._shape: Tuple[*Ts] = shape
1694
1699
1695
- def get_shape(self) -> Tuple[*Ts]:
1696
- return self._shape
1700
+ def get_shape(self) -> Tuple[*Ts]:
1701
+ return self._shape
1697
1702
1698
- shape = (Height(480), Width(640))
1699
- x: Array[Height, Width] = Array(shape)
1700
- y = abs(x) # Inferred type is Array[Height, Width]
1701
- z = x + x # ... is Array[Height, Width]
1702
- x.get_shape() # ... is tuple[Height, Width]
1703
+ shape = (Height(480), Width(640))
1704
+ x: Array[Height, Width] = Array(shape)
1705
+ y = abs(x) # Inferred type is Array[Height, Width]
1706
+ z = x + x # ... is Array[Height, Width]
1707
+ x.get_shape() # ... is tuple[Height, Width]
1703
1708
1704
- """
1709
+ """
1705
1710
1706
- # Trick Generic __parameters__.
1707
- __class__ = typing .TypeVar
1711
+ # Trick Generic __parameters__.
1712
+ __class__ = typing .TypeVar
1708
1713
1709
- def __iter__ (self ):
1710
- yield self .__unpacked__
1714
+ def __iter__ (self ):
1715
+ yield self .__unpacked__
1711
1716
1712
- def __init__ (self , name ):
1713
- self .__name__ = name
1717
+ def __init__ (self , name ):
1718
+ self .__name__ = name
1714
1719
1715
- # for pickling:
1716
- try :
1717
- def_mod = sys ._getframe (1 ).f_globals .get ('__name__' , '__main__' )
1718
- except (AttributeError , ValueError ):
1719
- def_mod = None
1720
- if def_mod != 'typing_extensions' :
1721
- self .__module__ = def_mod
1720
+ # for pickling:
1721
+ try :
1722
+ def_mod = sys ._getframe (1 ).f_globals .get ('__name__' , '__main__' )
1723
+ except (AttributeError , ValueError ):
1724
+ def_mod = None
1725
+ if def_mod != 'typing_extensions' :
1726
+ self .__module__ = def_mod
1722
1727
1723
- self .__unpacked__ = Unpack [self ]
1728
+ self .__unpacked__ = Unpack [self ]
1724
1729
1725
- def __repr__ (self ):
1726
- return self .__name__
1730
+ def __repr__ (self ):
1731
+ return self .__name__
1727
1732
1728
- def __hash__ (self ):
1729
- return object .__hash__ (self )
1733
+ def __hash__ (self ):
1734
+ return object .__hash__ (self )
1730
1735
1731
- def __eq__ (self , other ):
1732
- return self is other
1736
+ def __eq__ (self , other ):
1737
+ return self is other
1733
1738
1734
- def __reduce__ (self ):
1735
- return self .__name__
1739
+ def __reduce__ (self ):
1740
+ return self .__name__
1736
1741
1737
- def __init_subclass__ (self , * args , ** kwds ):
1738
- if '_root' not in kwds :
1739
- raise TypeError ("Cannot subclass special typing classes" )
1742
+ def __init_subclass__ (self , * args , ** kwds ):
1743
+ if '_root' not in kwds :
1744
+ raise TypeError ("Cannot subclass special typing classes" )
1740
1745
1741
1746
1742
1747
if hasattr (typing , "reveal_type" ):
0 commit comments