- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtypes.py
341 lines (287 loc) · 10.9 KB
/
types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
Define names for built-in types that aren't directly accessible as a builtin.
"""
importsys
# Iterators in Python aren't a matter of type but of protocol. A large
# and changing number of builtin types implement *some* flavor of
# iterator. Don't check the type! Use hasattr to check for both
# "__iter__" and "__next__" attributes instead.
def_f(): pass
FunctionType=type(_f)
LambdaType=type(lambda: None) # Same as FunctionType
CodeType=type(_f.__code__)
MappingProxyType=type(type.__dict__)
SimpleNamespace=type(sys.implementation)
def_cell_factory():
a=1
deff():
nonlocala
returnf.__closure__[0]
CellType=type(_cell_factory())
def_g():
yield1
GeneratorType=type(_g())
asyncdef_c(): pass
_c=_c()
CoroutineType=type(_c)
_c.close() # Prevent ResourceWarning
asyncdef_ag():
yield
_ag=_ag()
AsyncGeneratorType=type(_ag)
class_C:
def_m(self): pass
MethodType=type(_C()._m)
BuiltinFunctionType=type(len)
BuiltinMethodType=type([].append) # Same as BuiltinFunctionType
WrapperDescriptorType=type(object.__init__)
MethodWrapperType=type(object().__str__)
MethodDescriptorType=type(str.join)
ClassMethodDescriptorType=type(dict.__dict__['fromkeys'])
ModuleType=type(sys)
try:
raiseTypeError
exceptTypeErrorasexc:
TracebackType=type(exc.__traceback__)
FrameType=type(exc.__traceback__.tb_frame)
GetSetDescriptorType=type(FunctionType.__code__)
MemberDescriptorType=type(FunctionType.__globals__)
delsys, _f, _g, _C, _c, _ag, _cell_factory# Not for export
# Provide a PEP 3115 compliant mechanism for class creation
defnew_class(name, bases=(), kwds=None, exec_body=None):
"""Create a class object dynamically using the appropriate metaclass."""
resolved_bases=resolve_bases(bases)
meta, ns, kwds=prepare_class(name, resolved_bases, kwds)
ifexec_bodyisnotNone:
exec_body(ns)
ifresolved_basesisnotbases:
ns['__orig_bases__'] =bases
returnmeta(name, resolved_bases, ns, **kwds)
defresolve_bases(bases):
"""Resolve MRO entries dynamically as specified by PEP 560."""
new_bases=list(bases)
updated=False
shift=0
fori, baseinenumerate(bases):
ifisinstance(base, type):
continue
ifnothasattr(base, "__mro_entries__"):
continue
new_base=base.__mro_entries__(bases)
updated=True
ifnotisinstance(new_base, tuple):
raiseTypeError("__mro_entries__ must return a tuple")
else:
new_bases[i+shift:i+shift+1] =new_base
shift+=len(new_base) -1
ifnotupdated:
returnbases
returntuple(new_bases)
defprepare_class(name, bases=(), kwds=None):
"""Call the __prepare__ method of the appropriate metaclass.
Returns (metaclass, namespace, kwds) as a 3-tuple
*metaclass* is the appropriate metaclass
*namespace* is the prepared class namespace
*kwds* is an updated copy of the passed in kwds argument with any
'metaclass' entry removed. If no kwds argument is passed in, this will
be an empty dict.
"""
ifkwdsisNone:
kwds= {}
else:
kwds=dict(kwds) # Don't alter the provided mapping
if'metaclass'inkwds:
meta=kwds.pop('metaclass')
else:
ifbases:
meta=type(bases[0])
else:
meta=type
ifisinstance(meta, type):
# when meta is a type, we first determine the most-derived metaclass
# instead of invoking the initial candidate directly
meta=_calculate_meta(meta, bases)
ifhasattr(meta, '__prepare__'):
ns=meta.__prepare__(name, bases, **kwds)
else:
ns= {}
returnmeta, ns, kwds
def_calculate_meta(meta, bases):
"""Calculate the most derived metaclass."""
winner=meta
forbaseinbases:
base_meta=type(base)
ifissubclass(winner, base_meta):
continue
ifissubclass(base_meta, winner):
winner=base_meta
continue
# else:
raiseTypeError("metaclass conflict: "
"the metaclass of a derived class "
"must be a (non-strict) subclass "
"of the metaclasses of all its bases")
returnwinner
defget_original_bases(cls, /):
"""Return the class's "original" bases prior to modification by `__mro_entries__`.
Examples::
from typing import TypeVar, Generic, NamedTuple, TypedDict
T = TypeVar("T")
class Foo(Generic[T]): ...
class Bar(Foo[int], float): ...
class Baz(list[str]): ...
Eggs = NamedTuple("Eggs", [("a", int), ("b", str)])
Spam = TypedDict("Spam", {"a": int, "b": str})
assert get_original_bases(Bar) == (Foo[int], float)
assert get_original_bases(Baz) == (list[str],)
assert get_original_bases(Eggs) == (NamedTuple,)
assert get_original_bases(Spam) == (TypedDict,)
assert get_original_bases(int) == (object,)
"""
try:
returncls.__dict__.get("__orig_bases__", cls.__bases__)
exceptAttributeError:
raiseTypeError(
f"Expected an instance of type, not {type(cls).__name__!r}"
) fromNone
classDynamicClassAttribute:
"""Route attribute access on a class to __getattr__.
This is a descriptor, used to define attributes that act differently when
accessed through an instance and through a class. Instance access remains
normal, but access to an attribute through a class will be routed to the
class's __getattr__ method; this is done by raising AttributeError.
This allows one to have properties active on an instance, and have virtual
attributes on the class with the same name. (Enum used this between Python
versions 3.4 - 3.9 .)
Subclass from this to use a different method of accessing virtual attributes
and still be treated properly by the inspect module. (Enum uses this since
Python 3.10 .)
"""
def__init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget=fget
self.fset=fset
self.fdel=fdel
# next two lines make DynamicClassAttribute act the same as property
self.__doc__=docorfget.__doc__
self.overwrite_doc=docisNone
# support for abstract methods
self.__isabstractmethod__=bool(getattr(fget, '__isabstractmethod__', False))
def__get__(self, instance, ownerclass=None):
ifinstanceisNone:
ifself.__isabstractmethod__:
returnself
raiseAttributeError()
elifself.fgetisNone:
raiseAttributeError("unreadable attribute")
returnself.fget(instance)
def__set__(self, instance, value):
ifself.fsetisNone:
raiseAttributeError("can't set attribute")
self.fset(instance, value)
def__delete__(self, instance):
ifself.fdelisNone:
raiseAttributeError("can't delete attribute")
self.fdel(instance)
defgetter(self, fget):
fdoc=fget.__doc__ifself.overwrite_docelseNone
result=type(self)(fget, self.fset, self.fdel, fdocorself.__doc__)
result.overwrite_doc=self.overwrite_doc
returnresult
defsetter(self, fset):
result=type(self)(self.fget, fset, self.fdel, self.__doc__)
result.overwrite_doc=self.overwrite_doc
returnresult
defdeleter(self, fdel):
result=type(self)(self.fget, self.fset, fdel, self.__doc__)
result.overwrite_doc=self.overwrite_doc
returnresult
class_GeneratorWrapper:
# TODO: Implement this in C.
def__init__(self, gen):
self.__wrapped=gen
self.__isgen=gen.__class__isGeneratorType
self.__name__=getattr(gen, '__name__', None)
self.__qualname__=getattr(gen, '__qualname__', None)
defsend(self, val):
returnself.__wrapped.send(val)
defthrow(self, tp, *rest):
returnself.__wrapped.throw(tp, *rest)
defclose(self):
returnself.__wrapped.close()
@property
defgi_code(self):
returnself.__wrapped.gi_code
@property
defgi_frame(self):
returnself.__wrapped.gi_frame
@property
defgi_running(self):
returnself.__wrapped.gi_running
@property
defgi_yieldfrom(self):
returnself.__wrapped.gi_yieldfrom
cr_code=gi_code
cr_frame=gi_frame
cr_running=gi_running
cr_await=gi_yieldfrom
def__next__(self):
returnnext(self.__wrapped)
def__iter__(self):
ifself.__isgen:
returnself.__wrapped
returnself
__await__=__iter__
defcoroutine(func):
"""Convert regular generator function to a coroutine."""
ifnotcallable(func):
raiseTypeError('types.coroutine() expects a callable')
if (func.__class__isFunctionTypeand
getattr(func, '__code__', None).__class__isCodeType):
co_flags=func.__code__.co_flags
# Check if 'func' is a coroutine function.
# (0x180 == CO_COROUTINE | CO_ITERABLE_COROUTINE)
ifco_flags&0x180:
returnfunc
# Check if 'func' is a generator function.
# (0x20 == CO_GENERATOR)
ifco_flags&0x20:
# TODO: Implement this in C.
co=func.__code__
# 0x100 == CO_ITERABLE_COROUTINE
func.__code__=co.replace(co_flags=co.co_flags|0x100)
returnfunc
# The following code is primarily to support functions that
# return generator-like objects (for instance generators
# compiled with Cython).
# Delay functools and _collections_abc import for speeding up types import.
importfunctools
import_collections_abc
@functools.wraps(func)
defwrapped(*args, **kwargs):
coro=func(*args, **kwargs)
if (coro.__class__isCoroutineTypeor
coro.__class__isGeneratorTypeandcoro.gi_code.co_flags&0x100):
# 'coro' is a native coroutine object or an iterable coroutine
returncoro
if (isinstance(coro, _collections_abc.Generator) and
notisinstance(coro, _collections_abc.Coroutine)):
# 'coro' is either a pure Python generator iterator, or it
# implements collections.abc.Generator (and does not implement
# collections.abc.Coroutine).
return_GeneratorWrapper(coro)
# 'coro' is either an instance of collections.abc.Coroutine or
# some other object -- pass it through.
returncoro
returnwrapped
GenericAlias=type(list[int])
UnionType=type(int|str)
EllipsisType=type(Ellipsis)
NoneType=type(None)
NotImplementedType=type(NotImplemented)
def__getattr__(name):
ifname=='CapsuleType':
import_socket
returntype(_socket.CAPI)
raiseAttributeError(f"module {__name__!r} has no attribute {name!r}")
__all__= [nforninglobals() ifn[:1] !='_']
__all__+= ['CapsuleType']