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