@@ -1673,7 +1673,9 @@ class Movie(TypedDict):
1673
1673
""" )
1674
1674
1675
1675
1676
- if sys .version_info [:2 ] >= (3 , 9 ):
1676
+ if hasattr (typing , "Unpack" ): # 3.11+
1677
+ Unpack = typing .Unpack
1678
+ elif sys .version_info [:2 ] >= (3 , 9 ):
1677
1679
class _UnpackSpecialForm (typing ._SpecialForm , _root = True ):
1678
1680
def __repr__ (self ):
1679
1681
return 'typing_extensions.' + self ._name
@@ -1729,84 +1731,87 @@ def _is_unpack(obj):
1729
1731
return isinstance (obj , _UnpackAlias )
1730
1732
1731
1733
1732
- class TypeVarTuple :
1733
- """Type variable tuple.
1734
+ if hasattr (typing , "TypeVarTuple" ): # 3.11+
1735
+ TypeVarTuple = typing .TypeVarTuple
1736
+ else :
1737
+ class TypeVarTuple :
1738
+ """Type variable tuple.
1734
1739
1735
- Usage::
1740
+ Usage::
1736
1741
1737
- Ts = TypeVarTuple('Ts')
1742
+ Ts = TypeVarTuple('Ts')
1738
1743
1739
- In the same way that a normal type variable is a stand-in for a single
1740
- type such as ``int``, a type variable *tuple* is a stand-in for a *tuple* type such as
1741
- ``Tuple[int, str]``.
1744
+ In the same way that a normal type variable is a stand-in for a single
1745
+ type such as ``int``, a type variable *tuple* is a stand-in for a *tuple*
1746
+ type such as ``Tuple[int, str]``.
1742
1747
1743
- Type variable tuples can be used in ``Generic`` declarations.
1744
- Consider the following example::
1748
+ Type variable tuples can be used in ``Generic`` declarations.
1749
+ Consider the following example::
1745
1750
1746
- class Array(Generic[*Ts]): ...
1751
+ class Array(Generic[*Ts]): ...
1747
1752
1748
- The ``Ts`` type variable tuple here behaves like ``tuple[T1, T2]``,
1749
- where ``T1`` and ``T2`` are type variables. To use these type variables
1750
- as type parameters of ``Array``, we must *unpack* the type variable tuple using
1751
- the star operator: ``*Ts``. The signature of ``Array`` then behaves
1752
- as if we had simply written ``class Array(Generic[T1, T2]): ...``.
1753
- In contrast to ``Generic[T1, T2]``, however, ``Generic[*Shape]`` allows
1754
- us to parameterise the class with an *arbitrary* number of type parameters.
1753
+ The ``Ts`` type variable tuple here behaves like ``tuple[T1, T2]``,
1754
+ where ``T1`` and ``T2`` are type variables. To use these type variables
1755
+ as type parameters of ``Array``, we must *unpack* the type variable tuple using
1756
+ the star operator: ``*Ts``. The signature of ``Array`` then behaves
1757
+ as if we had simply written ``class Array(Generic[T1, T2]): ...``.
1758
+ In contrast to ``Generic[T1, T2]``, however, ``Generic[*Shape]`` allows
1759
+ us to parameterise the class with an *arbitrary* number of type parameters.
1755
1760
1756
- Type variable tuples can be used anywhere a normal ``TypeVar`` can.
1757
- This includes class definitions, as shown above, as well as function
1758
- signatures and variable annotations::
1761
+ Type variable tuples can be used anywhere a normal ``TypeVar`` can.
1762
+ This includes class definitions, as shown above, as well as function
1763
+ signatures and variable annotations::
1759
1764
1760
- class Array(Generic[*Ts]):
1765
+ class Array(Generic[*Ts]):
1761
1766
1762
- def __init__(self, shape: Tuple[*Ts]):
1763
- self._shape: Tuple[*Ts] = shape
1767
+ def __init__(self, shape: Tuple[*Ts]):
1768
+ self._shape: Tuple[*Ts] = shape
1764
1769
1765
- def get_shape(self) -> Tuple[*Ts]:
1766
- return self._shape
1770
+ def get_shape(self) -> Tuple[*Ts]:
1771
+ return self._shape
1767
1772
1768
- shape = (Height(480), Width(640))
1769
- x: Array[Height, Width] = Array(shape)
1770
- y = abs(x) # Inferred type is Array[Height, Width]
1771
- z = x + x # ... is Array[Height, Width]
1772
- x.get_shape() # ... is tuple[Height, Width]
1773
+ shape = (Height(480), Width(640))
1774
+ x: Array[Height, Width] = Array(shape)
1775
+ y = abs(x) # Inferred type is Array[Height, Width]
1776
+ z = x + x # ... is Array[Height, Width]
1777
+ x.get_shape() # ... is tuple[Height, Width]
1773
1778
1774
- """
1779
+ """
1775
1780
1776
- # Trick Generic __parameters__.
1777
- __class__ = typing .TypeVar
1781
+ # Trick Generic __parameters__.
1782
+ __class__ = typing .TypeVar
1778
1783
1779
- def __iter__ (self ):
1780
- yield self .__unpacked__
1784
+ def __iter__ (self ):
1785
+ yield self .__unpacked__
1781
1786
1782
- def __init__ (self , name ):
1783
- self .__name__ = name
1787
+ def __init__ (self , name ):
1788
+ self .__name__ = name
1784
1789
1785
- # for pickling:
1786
- try :
1787
- def_mod = sys ._getframe (1 ).f_globals .get ('__name__' , '__main__' )
1788
- except (AttributeError , ValueError ):
1789
- def_mod = None
1790
- if def_mod != 'typing_extensions' :
1791
- self .__module__ = def_mod
1790
+ # for pickling:
1791
+ try :
1792
+ def_mod = sys ._getframe (1 ).f_globals .get ('__name__' , '__main__' )
1793
+ except (AttributeError , ValueError ):
1794
+ def_mod = None
1795
+ if def_mod != 'typing_extensions' :
1796
+ self .__module__ = def_mod
1792
1797
1793
- self .__unpacked__ = Unpack [self ]
1798
+ self .__unpacked__ = Unpack [self ]
1794
1799
1795
- def __repr__ (self ):
1796
- return self .__name__
1800
+ def __repr__ (self ):
1801
+ return self .__name__
1797
1802
1798
- def __hash__ (self ):
1799
- return object .__hash__ (self )
1803
+ def __hash__ (self ):
1804
+ return object .__hash__ (self )
1800
1805
1801
- def __eq__ (self , other ):
1802
- return self is other
1806
+ def __eq__ (self , other ):
1807
+ return self is other
1803
1808
1804
- def __reduce__ (self ):
1805
- return self .__name__
1809
+ def __reduce__ (self ):
1810
+ return self .__name__
1806
1811
1807
- def __init_subclass__ (self , * args , ** kwds ):
1808
- if '_root' not in kwds :
1809
- raise TypeError ("Cannot subclass special typing classes" )
1812
+ def __init_subclass__ (self , * args , ** kwds ):
1813
+ if '_root' not in kwds :
1814
+ raise TypeError ("Cannot subclass special typing classes" )
1810
1815
1811
1816
1812
1817
if hasattr (typing , "reveal_type" ):
0 commit comments