@@ -2048,40 +2048,61 @@ def __eq__(self, other):
2048
2048
2049
2049
TypeGuard = _TypeGuard (_root = True )
2050
2050
2051
+ if not hasattr (typing , "Self" ) and sys .version_info [:2 ] >= (3 , 7 ):
2052
+ # Vendored from cpython typing._SpecialFrom
2053
+ class _SpecialForm (typing ._Final , _root = True ):
2054
+ __slots__ = ('_name' , '__doc__' , '_getitem' )
2051
2055
2052
- if hasattr (typing , "Self" ):
2053
- Self = typing .Self
2056
+ def __init__ (self , getitem ):
2057
+ self ._getitem = getitem
2058
+ self ._name = getitem .__name__
2059
+ self .__doc__ = getitem .__doc__
2060
+
2061
+ def __getattr__ (self , item ):
2062
+ if item in {'__name__' , '__qualname__' }:
2063
+ return self ._name
2064
+
2065
+ raise AttributeError (item )
2066
+
2067
+ def __mro_entries__ (self , bases ):
2068
+ raise TypeError (f"Cannot subclass { self !r} " )
2054
2069
2055
- elif sys .version_info [:2 ] >= (3 , 9 ):
2056
- class _SelfForm (typing ._SpecialForm , _root = True ):
2057
2070
def __repr__ (self ):
2058
- return 'typing_extensions .' + self ._name
2071
+ return 'typing .' + self ._name
2059
2072
2060
- @_SelfForm
2061
- def Self (self , params ):
2062
- """Used to spell the type of "self" in classes.
2073
+ def __reduce__ (self ):
2074
+ return self ._name
2063
2075
2064
- Example::
2076
+ def __call__ (self , * args , ** kwds ):
2077
+ raise TypeError (f"Cannot instantiate { self !r} " )
2065
2078
2066
- from typing import Self
2079
+ def __or__ (self , other ):
2080
+ return Union [self , other ]
2067
2081
2068
- class ReturnsSelf:
2069
- def parse(self, data: bytes) -> Self:
2070
- ...
2071
- return self
2082
+ def __ror__ (self , other ):
2083
+ return Union [other , self ]
2072
2084
2073
- """
2085
+ def __instancecheck__ (self , obj ):
2086
+ raise TypeError (f"{ self } cannot be used with isinstance()" )
2074
2087
2075
- raise TypeError (f"{ self } is not subscriptable" )
2088
+ def __subclasscheck__ (self , cls ):
2089
+ raise TypeError (f"{ self } cannot be used with issubclass()" )
2090
+
2091
+ @typing ._tp_cache
2092
+ def __getitem__ (self , parameters ):
2093
+ return self ._getitem (self , parameters )
2094
+
2095
+ if hasattr (typing , "Self" ):
2096
+ Self = typing .Self
2076
2097
2077
2098
elif sys .version_info [:2 ] >= (3 , 7 ):
2078
- class _SelfForm (typing . _SpecialForm , _root = True ):
2099
+ class _SelfForm (_SpecialForm , _root = True ):
2079
2100
def __repr__ (self ):
2080
2101
return 'typing_extensions.' + self ._name
2081
2102
2082
- Self = _SelfForm (
2083
- " Self" ,
2084
- doc = """Used to spell the type of "self" in classes.
2103
+ @ _SelfForm
2104
+ def Self ( self , params ):
2105
+ """Used to spell the type of "self" in classes.
2085
2106
2086
2107
Example::
2087
2108
@@ -2093,7 +2114,8 @@ def parse(self, data: bytes) -> Self:
2093
2114
return self
2094
2115
2095
2116
"""
2096
- )
2117
+
2118
+ raise TypeError (f"{ self } is not subscriptable" )
2097
2119
else :
2098
2120
class _Self (typing ._FinalTypingBase , _root = True ):
2099
2121
"""Used to spell the type of "self" in classes.
0 commit comments