blob: c487afcb5b42f8460b8077d50061cc181de62362 [file] [log] [blame]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001import abc
2from abc import abstractmethod, abstractproperty
3import collections
Brett Cannonf3ad0422016-04-15 10:51:30 -07004import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07005import functools
6import re as stdlib_re # Avoid confusion with the re we export.
7import sys
8import types
9try:
10 import collections.abc as collections_abc
11except ImportError:
12 import collections as collections_abc # Fallback for PY3.2.
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020013if sys.version_info[:2] >= (3, 6):
14 import _collections_abc # Needed for private function _check_methods # noqa
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010015try:
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020016 from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010017except ImportError:
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020018 WrapperDescriptorType = type(object.__init__)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010019 MethodWrapperType = type(object().__str__)
20 MethodDescriptorType = type(str.join)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070021
22
23# Please keep __all__ alphabetized within each category.
24__all__ = [
25 # Super-special typing primitives.
26 'Any',
27 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070028 'ClassVar',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070029 'Generic',
30 'Optional',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070031 'Tuple',
32 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070033 'TypeVar',
34 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070035
36 # ABCs (from collections.abc).
37 'AbstractSet', # collections.abc.Set.
Guido van Rossum83ec3022017-01-17 20:43:28 -080038 'GenericMeta', # subclass of abc.ABCMeta and a metaclass
39 # for 'Generic' and ABCs below.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070040 'ByteString',
41 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020042 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070043 'Hashable',
44 'ItemsView',
45 'Iterable',
46 'Iterator',
47 'KeysView',
48 'Mapping',
49 'MappingView',
50 'MutableMapping',
51 'MutableSequence',
52 'MutableSet',
53 'Sequence',
54 'Sized',
55 'ValuesView',
Guido van Rossum62fe1bb2016-10-29 16:05:26 -070056 # The following are added depending on presence
57 # of their non-generic counterparts in stdlib:
58 # Awaitable,
59 # AsyncIterator,
60 # AsyncIterable,
61 # Coroutine,
62 # Collection,
Guido van Rossume9ed5602017-01-18 13:10:31 -080063 # AsyncGenerator,
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020064 # AsyncContextManager
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070065
66 # Structural checks, a.k.a. protocols.
67 'Reversible',
68 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020069 'SupportsBytes',
70 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070071 'SupportsFloat',
72 'SupportsInt',
73 'SupportsRound',
74
75 # Concrete collection types.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010076 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080077 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070078 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070079 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070080 'List',
81 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070082 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070083 'NamedTuple', # Not really a type.
84 'Generator',
85
86 # One-off things.
87 'AnyStr',
88 'cast',
89 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -070090 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070091 'no_type_check',
92 'no_type_check_decorator',
93 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -070094 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -070095 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096]
97
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070098# The pseudo-submodules 're' and 'io' are part of the public
99# namespace, but excluded from __all__ because they might stomp on
100# legitimate imports of those modules.
101
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700102
103def _qualname(x):
104 if sys.version_info[:2] >= (3, 3):
105 return x.__qualname__
106 else:
107 # Fall back to just name.
108 return x.__name__
109
110
Guido van Rossum4cefe742016-09-27 15:20:12 -0700111def _trim_name(nm):
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800112 whitelist = ('_TypeAlias', '_ForwardRef', '_TypingBase', '_FinalTypingBase')
113 if nm.startswith('_') and nm not in whitelist:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700114 nm = nm[1:]
115 return nm
116
117
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700118class TypingMeta(type):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800119 """Metaclass for most types defined in typing module
120 (not a part of public API).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700121
122 This overrides __new__() to require an extra keyword parameter
123 '_root', which serves as a guard against naive subclassing of the
124 typing classes. Any legitimate class defined using a metaclass
Guido van Rossumb24569a2016-11-20 18:01:29 -0800125 derived from TypingMeta must pass _root=True.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700126
Guido van Rossumb24569a2016-11-20 18:01:29 -0800127 This also defines a dummy constructor (all the work for most typing
128 constructs is done in __new__) and a nicer repr().
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700129 """
130
131 _is_protocol = False
132
133 def __new__(cls, name, bases, namespace, *, _root=False):
134 if not _root:
135 raise TypeError("Cannot subclass %s" %
136 (', '.join(map(_type_repr, bases)) or '()'))
137 return super().__new__(cls, name, bases, namespace)
138
139 def __init__(self, *args, **kwds):
140 pass
141
142 def _eval_type(self, globalns, localns):
143 """Override this in subclasses to interpret forward references.
144
Guido van Rossumb24569a2016-11-20 18:01:29 -0800145 For example, List['C'] is internally stored as
146 List[_ForwardRef('C')], which should evaluate to List[C],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700147 where C is an object found in globalns or localns (searching
148 localns first, of course).
149 """
150 return self
151
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700152 def _get_type_vars(self, tvars):
153 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700154
155 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700156 qname = _trim_name(_qualname(self))
157 return '%s.%s' % (self.__module__, qname)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700158
159
Guido van Rossum4cefe742016-09-27 15:20:12 -0700160class _TypingBase(metaclass=TypingMeta, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800161 """Internal indicator of special typing constructs."""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700162
Guido van Rossum83ec3022017-01-17 20:43:28 -0800163 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700164
Guido van Rossum4cefe742016-09-27 15:20:12 -0700165 def __init__(self, *args, **kwds):
166 pass
167
168 def __new__(cls, *args, **kwds):
169 """Constructor.
170
171 This only exists to give a better error message in case
172 someone tries to subclass a special typing object (not a good idea).
173 """
174 if (len(args) == 3 and
175 isinstance(args[0], str) and
176 isinstance(args[1], tuple)):
177 # Close enough.
178 raise TypeError("Cannot subclass %r" % cls)
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700179 return super().__new__(cls)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700180
181 # Things that are not classes also need these.
182 def _eval_type(self, globalns, localns):
183 return self
184
185 def _get_type_vars(self, tvars):
186 pass
187
188 def __repr__(self):
189 cls = type(self)
190 qname = _trim_name(_qualname(cls))
191 return '%s.%s' % (cls.__module__, qname)
192
193 def __call__(self, *args, **kwds):
194 raise TypeError("Cannot instantiate %r" % type(self))
195
196
197class _FinalTypingBase(_TypingBase, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800198 """Internal mix-in class to prevent instantiation.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700199
200 Prevents instantiation unless _root=True is given in class call.
Guido van Rossumb24569a2016-11-20 18:01:29 -0800201 It is used to create pseudo-singleton instances Any, Union, Optional, etc.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700202 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700203
Guido van Rossumd70fe632015-08-05 12:11:06 +0200204 __slots__ = ()
205
Guido van Rossum4cefe742016-09-27 15:20:12 -0700206 def __new__(cls, *args, _root=False, **kwds):
207 self = super().__new__(cls, *args, **kwds)
208 if _root is True:
209 return self
210 raise TypeError("Cannot instantiate %r" % cls)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700211
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700212 def __reduce__(self):
213 return _trim_name(type(self).__name__)
214
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700215
Guido van Rossum4cefe742016-09-27 15:20:12 -0700216class _ForwardRef(_TypingBase, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800217 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700218
Guido van Rossum4cefe742016-09-27 15:20:12 -0700219 __slots__ = ('__forward_arg__', '__forward_code__',
Guido van Rossumc7b92952016-11-10 08:24:06 -0800220 '__forward_evaluated__', '__forward_value__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700221
222 def __init__(self, arg):
223 super().__init__(arg)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700224 if not isinstance(arg, str):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800225 raise TypeError('Forward reference must be a string -- got %r' % (arg,))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700226 try:
227 code = compile(arg, '<string>', 'eval')
228 except SyntaxError:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800229 raise SyntaxError('Forward reference must be an expression -- got %r' %
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700230 (arg,))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700231 self.__forward_arg__ = arg
232 self.__forward_code__ = code
233 self.__forward_evaluated__ = False
234 self.__forward_value__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700235
236 def _eval_type(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800237 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700238 if globalns is None and localns is None:
239 globalns = localns = {}
240 elif globalns is None:
241 globalns = localns
242 elif localns is None:
243 localns = globalns
244 self.__forward_value__ = _type_check(
245 eval(self.__forward_code__, globalns, localns),
246 "Forward references must evaluate to types.")
247 self.__forward_evaluated__ = True
248 return self.__forward_value__
249
Guido van Rossum4cefe742016-09-27 15:20:12 -0700250 def __eq__(self, other):
251 if not isinstance(other, _ForwardRef):
252 return NotImplemented
253 return (self.__forward_arg__ == other.__forward_arg__ and
Guido van Rossumc7b92952016-11-10 08:24:06 -0800254 self.__forward_value__ == other.__forward_value__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700255
256 def __hash__(self):
Guido van Rossumc7b92952016-11-10 08:24:06 -0800257 return hash((self.__forward_arg__, self.__forward_value__))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700258
Guido van Rossumd70fe632015-08-05 12:11:06 +0200259 def __instancecheck__(self, obj):
260 raise TypeError("Forward references cannot be used with isinstance().")
261
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700262 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700263 raise TypeError("Forward references cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700264
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700265 def __repr__(self):
266 return '_ForwardRef(%r)' % (self.__forward_arg__,)
267
268
Guido van Rossum4cefe742016-09-27 15:20:12 -0700269class _TypeAlias(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700270 """Internal helper class for defining generic variants of concrete types.
271
Guido van Rossum4cefe742016-09-27 15:20:12 -0700272 Note that this is not a type; let's call it a pseudo-type. It cannot
273 be used in instance and subclass checks in parameterized form, i.e.
274 ``isinstance(42, Match[str])`` raises ``TypeError`` instead of returning
275 ``False``.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700276 """
277
Guido van Rossumd70fe632015-08-05 12:11:06 +0200278 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
279
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700280 def __init__(self, name, type_var, impl_type, type_checker):
281 """Initializer.
282
283 Args:
284 name: The name, e.g. 'Pattern'.
285 type_var: The type parameter, e.g. AnyStr, or the
286 specific type, e.g. str.
287 impl_type: The implementation type.
288 type_checker: Function that takes an impl_type instance.
289 and returns a value that should be a type_var instance.
290 """
291 assert isinstance(name, str), repr(name)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700292 assert isinstance(impl_type, type), repr(impl_type)
293 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700294 assert isinstance(type_var, (type, _TypingBase)), repr(type_var)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700295 self.name = name
296 self.type_var = type_var
297 self.impl_type = impl_type
298 self.type_checker = type_checker
299
300 def __repr__(self):
301 return "%s[%s]" % (self.name, _type_repr(self.type_var))
302
303 def __getitem__(self, parameter):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700304 if not isinstance(self.type_var, TypeVar):
305 raise TypeError("%s cannot be further parameterized." % self)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700306 if self.type_var.__constraints__ and isinstance(parameter, type):
307 if not issubclass(parameter, self.type_var.__constraints__):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700308 raise TypeError("%s is not a valid substitution for %s." %
309 (parameter, self.type_var))
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700310 if isinstance(parameter, TypeVar) and parameter is not self.type_var:
311 raise TypeError("%s cannot be re-parameterized." % self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700312 return self.__class__(self.name, parameter,
313 self.impl_type, self.type_checker)
314
Guido van Rossum4cefe742016-09-27 15:20:12 -0700315 def __eq__(self, other):
316 if not isinstance(other, _TypeAlias):
317 return NotImplemented
318 return self.name == other.name and self.type_var == other.type_var
319
320 def __hash__(self):
321 return hash((self.name, self.type_var))
322
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700323 def __instancecheck__(self, obj):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700324 if not isinstance(self.type_var, TypeVar):
325 raise TypeError("Parameterized type aliases cannot be used "
326 "with isinstance().")
327 return isinstance(obj, self.impl_type)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700328
329 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700330 if not isinstance(self.type_var, TypeVar):
331 raise TypeError("Parameterized type aliases cannot be used "
332 "with issubclass().")
333 return issubclass(cls, self.impl_type)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700334
335
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700336def _get_type_vars(types, tvars):
337 for t in types:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700338 if isinstance(t, TypingMeta) or isinstance(t, _TypingBase):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700339 t._get_type_vars(tvars)
340
341
342def _type_vars(types):
343 tvars = []
344 _get_type_vars(types, tvars)
345 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700346
347
348def _eval_type(t, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700349 if isinstance(t, TypingMeta) or isinstance(t, _TypingBase):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700350 return t._eval_type(globalns, localns)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700351 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700352
353
354def _type_check(arg, msg):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800355 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700356
357 As a special case, accept None and return type(None) instead.
358 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
359
360 The msg argument is a human-readable error message, e.g.
361
362 "Union[arg, ...]: arg should be a type."
363
364 We append the repr() of the actual value (truncated to 100 chars).
365 """
366 if arg is None:
367 return type(None)
368 if isinstance(arg, str):
369 arg = _ForwardRef(arg)
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800370 if (
371 isinstance(arg, _TypingBase) and type(arg).__name__ == '_ClassVar' or
372 not isinstance(arg, (type, _TypingBase)) and not callable(arg)
373 ):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700374 raise TypeError(msg + " Got %.100r." % (arg,))
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700375 # Bare Union etc. are not valid as type arguments
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800376 if (
377 type(arg).__name__ in ('_Union', '_Optional') and
378 not getattr(arg, '__origin__', None) or
379 isinstance(arg, TypingMeta) and _gorg(arg) in (Generic, _Protocol)
380 ):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700381 raise TypeError("Plain %s is not valid as type argument" % arg)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700382 return arg
383
384
385def _type_repr(obj):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800386 """Return the repr() of an object, special-casing types (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700387
388 If obj is a type, we return a shorter version than the default
389 type.__repr__, based on the module and qualified name, which is
390 typically enough to uniquely identify a type. For everything
391 else, we fall back on repr(obj).
392 """
393 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
394 if obj.__module__ == 'builtins':
395 return _qualname(obj)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700396 return '%s.%s' % (obj.__module__, _qualname(obj))
397 if obj is ...:
398 return('...')
399 if isinstance(obj, types.FunctionType):
400 return obj.__name__
401 return repr(obj)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700402
403
Guido van Rossum4cefe742016-09-27 15:20:12 -0700404class _Any(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700405 """Special type indicating an unconstrained type.
406
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700407 - Any is compatible with every type.
408 - Any assumed to have all methods.
409 - All values assumed to be instances of Any.
410
411 Note that all the above statements are true from the point of view of
412 static type checkers. At runtime, Any should not be used with instance
413 or class checks.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700414 """
415
Guido van Rossumd70fe632015-08-05 12:11:06 +0200416 __slots__ = ()
417
Guido van Rossum4cefe742016-09-27 15:20:12 -0700418 def __instancecheck__(self, obj):
419 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700420
Guido van Rossum4cefe742016-09-27 15:20:12 -0700421 def __subclasscheck__(self, cls):
422 raise TypeError("Any cannot be used with issubclass().")
423
424
425Any = _Any(_root=True)
426
427
Ivan Levkivskyif06e0212017-05-02 19:14:07 +0200428class _NoReturn(_FinalTypingBase, _root=True):
429 """Special type indicating functions that never return.
430 Example::
431
432 from typing import NoReturn
433
434 def stop() -> NoReturn:
435 raise Exception('no way')
436
437 This type is invalid in other positions, e.g., ``List[NoReturn]``
438 will fail in static type checkers.
439 """
440
441 __slots__ = ()
442
443 def __instancecheck__(self, obj):
444 raise TypeError("NoReturn cannot be used with isinstance().")
445
446 def __subclasscheck__(self, cls):
447 raise TypeError("NoReturn cannot be used with issubclass().")
448
449
450NoReturn = _NoReturn(_root=True)
451
452
Guido van Rossum4cefe742016-09-27 15:20:12 -0700453class TypeVar(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700454 """Type variable.
455
456 Usage::
457
458 T = TypeVar('T') # Can be anything
459 A = TypeVar('A', str, bytes) # Must be str or bytes
460
461 Type variables exist primarily for the benefit of static type
462 checkers. They serve as the parameters for generic types as well
463 as for generic function definitions. See class Generic for more
464 information on generic types. Generic functions work as follows:
465
Guido van Rossumb24569a2016-11-20 18:01:29 -0800466 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700467 '''Return a list containing n references to x.'''
468 return [x]*n
469
470 def longest(x: A, y: A) -> A:
471 '''Return the longest of two strings.'''
472 return x if len(x) >= len(y) else y
473
474 The latter example's signature is essentially the overloading
475 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
476 that if the arguments are instances of some subclass of str,
477 the return type is still plain str.
478
Guido van Rossumb24569a2016-11-20 18:01:29 -0800479 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700480
Guido van Rossumefa798d2016-08-23 11:01:50 -0700481 Type variables defined with covariant=True or contravariant=True
482 can be used do declare covariant or contravariant generic types.
483 See PEP 484 for more details. By default generic types are invariant
484 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700485
486 Type variables can be introspected. e.g.:
487
488 T.__name__ == 'T'
489 T.__constraints__ == ()
490 T.__covariant__ == False
491 T.__contravariant__ = False
492 A.__constraints__ == (str, bytes)
493 """
494
Guido van Rossum4cefe742016-09-27 15:20:12 -0700495 __slots__ = ('__name__', '__bound__', '__constraints__',
496 '__covariant__', '__contravariant__')
497
498 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800499 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700500 super().__init__(name, *constraints, bound=bound,
501 covariant=covariant, contravariant=contravariant)
502 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700503 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700504 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700505 self.__covariant__ = bool(covariant)
506 self.__contravariant__ = bool(contravariant)
507 if constraints and bound is not None:
508 raise TypeError("Constraints cannot be combined with bound=...")
509 if constraints and len(constraints) == 1:
510 raise TypeError("A single constraint is not allowed")
511 msg = "TypeVar(name, constraint, ...): constraints must be types."
512 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
513 if bound:
514 self.__bound__ = _type_check(bound, "Bound must be a type.")
515 else:
516 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700517
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700518 def _get_type_vars(self, tvars):
519 if self not in tvars:
520 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700521
522 def __repr__(self):
523 if self.__covariant__:
524 prefix = '+'
525 elif self.__contravariant__:
526 prefix = '-'
527 else:
528 prefix = '~'
529 return prefix + self.__name__
530
531 def __instancecheck__(self, instance):
532 raise TypeError("Type variables cannot be used with isinstance().")
533
534 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700535 raise TypeError("Type variables cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700536
537
538# Some unconstrained type variables. These are used by the container types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700539# (These are not for export.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700540T = TypeVar('T') # Any type.
541KT = TypeVar('KT') # Key type.
542VT = TypeVar('VT') # Value type.
543T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
544V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700545VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
546T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
547
548# A useful type variable with constraints. This represents string types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700549# (This one *is* for export!)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700550AnyStr = TypeVar('AnyStr', bytes, str)
551
552
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700553def _replace_arg(arg, tvars, args):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800554 """An internal helper function: replace arg if it is a type variable
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700555 found in tvars with corresponding substitution from args or
556 with corresponding substitution sub-tree if arg is a generic type.
557 """
558
559 if tvars is None:
560 tvars = []
Guido van Rossum83ec3022017-01-17 20:43:28 -0800561 if hasattr(arg, '_subs_tree') and isinstance(arg, (GenericMeta, _TypingBase)):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700562 return arg._subs_tree(tvars, args)
563 if isinstance(arg, TypeVar):
564 for i, tvar in enumerate(tvars):
565 if arg == tvar:
566 return args[i]
567 return arg
568
569
Guido van Rossum83ec3022017-01-17 20:43:28 -0800570# Special typing constructs Union, Optional, Generic, Callable and Tuple
571# use three special attributes for internal bookkeeping of generic types:
572# * __parameters__ is a tuple of unique free type parameters of a generic
573# type, for example, Dict[T, T].__parameters__ == (T,);
574# * __origin__ keeps a reference to a type that was subscripted,
575# e.g., Union[T, int].__origin__ == Union;
576# * __args__ is a tuple of all arguments used in subscripting,
577# e.g., Dict[T, int].__args__ == (T, int).
578
579
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700580def _subs_tree(cls, tvars=None, args=None):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800581 """An internal helper function: calculate substitution tree
582 for generic cls after replacing its type parameters with
583 substitutions in tvars -> args (if any).
584 Repeat the same following __origin__'s.
585
586 Return a list of arguments with all possible substitutions
587 performed. Arguments that are generic classes themselves are represented
588 as tuples (so that no new classes are created by this function).
589 For example: _subs_tree(List[Tuple[int, T]][str]) == [(Tuple, int, str)]
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700590 """
591
592 if cls.__origin__ is None:
593 return cls
594 # Make of chain of origins (i.e. cls -> cls.__origin__)
595 current = cls.__origin__
596 orig_chain = []
597 while current.__origin__ is not None:
598 orig_chain.append(current)
599 current = current.__origin__
600 # Replace type variables in __args__ if asked ...
601 tree_args = []
602 for arg in cls.__args__:
603 tree_args.append(_replace_arg(arg, tvars, args))
604 # ... then continue replacing down the origin chain.
605 for ocls in orig_chain:
606 new_tree_args = []
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800607 for arg in ocls.__args__:
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700608 new_tree_args.append(_replace_arg(arg, ocls.__parameters__, tree_args))
609 tree_args = new_tree_args
610 return tree_args
611
612
613def _remove_dups_flatten(parameters):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800614 """An internal helper for Union creation and substitution: flatten Union's
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700615 among parameters, then remove duplicates and strict subclasses.
616 """
617
618 # Flatten out Union[Union[...], ...].
619 params = []
620 for p in parameters:
621 if isinstance(p, _Union) and p.__origin__ is Union:
622 params.extend(p.__args__)
623 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
624 params.extend(p[1:])
625 else:
626 params.append(p)
627 # Weed out strict duplicates, preserving the first of each occurrence.
628 all_params = set(params)
629 if len(all_params) < len(params):
630 new_params = []
631 for t in params:
632 if t in all_params:
633 new_params.append(t)
634 all_params.remove(t)
635 params = new_params
636 assert not all_params, all_params
637 # Weed out subclasses.
638 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
639 # If object is present it will be sole survivor among proper classes.
640 # Never discard type variables.
641 # (In particular, Union[str, AnyStr] != AnyStr.)
642 all_params = set(params)
643 for t1 in params:
644 if not isinstance(t1, type):
645 continue
646 if any(isinstance(t2, type) and issubclass(t1, t2)
647 for t2 in all_params - {t1}
648 if not (isinstance(t2, GenericMeta) and
649 t2.__origin__ is not None)):
650 all_params.remove(t1)
651 return tuple(t for t in params if t in all_params)
652
653
654def _check_generic(cls, parameters):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800655 # Check correct count for parameters of a generic cls (internal helper).
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700656 if not cls.__parameters__:
657 raise TypeError("%s is not a generic class" % repr(cls))
658 alen = len(parameters)
659 elen = len(cls.__parameters__)
660 if alen != elen:
661 raise TypeError("Too %s parameters for %s; actual %s, expected %s" %
662 ("many" if alen > elen else "few", repr(cls), alen, elen))
663
664
Guido van Rossum9b107562016-11-09 13:23:04 -0800665_cleanups = []
666
667
Guido van Rossum4cefe742016-09-27 15:20:12 -0700668def _tp_cache(func):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800669 """Internal wrapper caching __getitem__ of generic types with a fallback to
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700670 original function for non-hashable arguments.
671 """
672
Guido van Rossum4cefe742016-09-27 15:20:12 -0700673 cached = functools.lru_cache()(func)
Guido van Rossum9b107562016-11-09 13:23:04 -0800674 _cleanups.append(cached.cache_clear)
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800675
Guido van Rossum4cefe742016-09-27 15:20:12 -0700676 @functools.wraps(func)
677 def inner(*args, **kwds):
678 try:
679 return cached(*args, **kwds)
680 except TypeError:
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700681 pass # All real errors (not unhashable args) are raised below.
Guido van Rossum4cefe742016-09-27 15:20:12 -0700682 return func(*args, **kwds)
683 return inner
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700684
685
Guido van Rossum4cefe742016-09-27 15:20:12 -0700686class _Union(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700687 """Union type; Union[X, Y] means either X or Y.
688
689 To define a union, use e.g. Union[int, str]. Details:
690
691 - The arguments must be types and there must be at least one.
692
693 - None as an argument is a special case and is replaced by
694 type(None).
695
696 - Unions of unions are flattened, e.g.::
697
698 Union[Union[int, str], float] == Union[int, str, float]
699
700 - Unions of a single argument vanish, e.g.::
701
702 Union[int] == int # The constructor actually returns int
703
704 - Redundant arguments are skipped, e.g.::
705
706 Union[int, str, int] == Union[int, str]
707
708 - When comparing unions, the argument order is ignored, e.g.::
709
710 Union[int, str] == Union[str, int]
711
712 - When two arguments have a subclass relationship, the least
713 derived argument is kept, e.g.::
714
715 class Employee: pass
716 class Manager(Employee): pass
717 Union[int, Employee, Manager] == Union[int, Employee]
718 Union[Manager, int, Employee] == Union[int, Employee]
719 Union[Employee, Manager] == Employee
720
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700721 - Similar for object::
722
723 Union[int, object] == object
724
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700725 - You cannot subclass or instantiate a union.
726
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700727 - You can use Optional[X] as a shorthand for Union[X, None].
728 """
729
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700730 __slots__ = ('__parameters__', '__args__', '__origin__', '__tree_hash__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700731
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700732 def __new__(cls, parameters=None, origin=None, *args, _root=False):
733 self = super().__new__(cls, parameters, origin, *args, _root=_root)
734 if origin is None:
735 self.__parameters__ = None
736 self.__args__ = None
737 self.__origin__ = None
738 self.__tree_hash__ = hash(frozenset(('Union',)))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700739 return self
740 if not isinstance(parameters, tuple):
741 raise TypeError("Expected parameters=<tuple>")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700742 if origin is Union:
743 parameters = _remove_dups_flatten(parameters)
744 # It's not a union if there's only one type left.
745 if len(parameters) == 1:
746 return parameters[0]
747 self.__parameters__ = _type_vars(parameters)
748 self.__args__ = parameters
749 self.__origin__ = origin
750 # Pre-calculate the __hash__ on instantiation.
751 # This improves speed for complex substitutions.
752 subs_tree = self._subs_tree()
753 if isinstance(subs_tree, tuple):
754 self.__tree_hash__ = hash(frozenset(subs_tree))
755 else:
756 self.__tree_hash__ = hash(subs_tree)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700757 return self
758
759 def _eval_type(self, globalns, localns):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700760 if self.__args__ is None:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700761 return self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700762 ev_args = tuple(_eval_type(t, globalns, localns) for t in self.__args__)
763 ev_origin = _eval_type(self.__origin__, globalns, localns)
764 if ev_args == self.__args__ and ev_origin == self.__origin__:
765 # Everything is already evaluated.
766 return self
767 return self.__class__(ev_args, ev_origin, _root=True)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700768
769 def _get_type_vars(self, tvars):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700770 if self.__origin__ and self.__parameters__:
771 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700772
773 def __repr__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700774 if self.__origin__ is None:
775 return super().__repr__()
776 tree = self._subs_tree()
777 if not isinstance(tree, tuple):
778 return repr(tree)
779 return tree[0]._tree_repr(tree)
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700780
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700781 def _tree_repr(self, tree):
782 arg_list = []
783 for arg in tree[1:]:
784 if not isinstance(arg, tuple):
785 arg_list.append(_type_repr(arg))
786 else:
787 arg_list.append(arg[0]._tree_repr(arg))
788 return super().__repr__() + '[%s]' % ', '.join(arg_list)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700789
790 @_tp_cache
791 def __getitem__(self, parameters):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700792 if parameters == ():
793 raise TypeError("Cannot take a Union of no types.")
794 if not isinstance(parameters, tuple):
795 parameters = (parameters,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700796 if self.__origin__ is None:
797 msg = "Union[arg, ...]: each arg must be a type."
798 else:
799 msg = "Parameters to generic types must be types."
800 parameters = tuple(_type_check(p, msg) for p in parameters)
801 if self is not Union:
802 _check_generic(self, parameters)
803 return self.__class__(parameters, origin=self, _root=True)
804
805 def _subs_tree(self, tvars=None, args=None):
806 if self is Union:
807 return Union # Nothing to substitute
808 tree_args = _subs_tree(self, tvars, args)
809 tree_args = _remove_dups_flatten(tree_args)
810 if len(tree_args) == 1:
811 return tree_args[0] # Union of a single type is that type
812 return (Union,) + tree_args
Guido van Rossum4cefe742016-09-27 15:20:12 -0700813
814 def __eq__(self, other):
Guido van Rossum83ec3022017-01-17 20:43:28 -0800815 if isinstance(other, _Union):
816 return self.__tree_hash__ == other.__tree_hash__
817 elif self is not Union:
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700818 return self._subs_tree() == other
Guido van Rossum83ec3022017-01-17 20:43:28 -0800819 else:
820 return self is other
Guido van Rossum4cefe742016-09-27 15:20:12 -0700821
822 def __hash__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700823 return self.__tree_hash__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700824
825 def __instancecheck__(self, obj):
826 raise TypeError("Unions cannot be used with isinstance().")
827
828 def __subclasscheck__(self, cls):
829 raise TypeError("Unions cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700830
831
Guido van Rossum4cefe742016-09-27 15:20:12 -0700832Union = _Union(_root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700833
834
Guido van Rossum4cefe742016-09-27 15:20:12 -0700835class _Optional(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700836 """Optional type.
837
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700838 Optional[X] is equivalent to Union[X, None].
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700839 """
840
Guido van Rossumd70fe632015-08-05 12:11:06 +0200841 __slots__ = ()
842
Guido van Rossum4cefe742016-09-27 15:20:12 -0700843 @_tp_cache
844 def __getitem__(self, arg):
845 arg = _type_check(arg, "Optional[t] requires a single type.")
846 return Union[arg, type(None)]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700847
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700848
Guido van Rossum4cefe742016-09-27 15:20:12 -0700849Optional = _Optional(_root=True)
850
851
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700852def _gorg(a):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800853 """Return the farthest origin of a generic class (internal helper)."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700854 assert isinstance(a, GenericMeta)
855 while a.__origin__ is not None:
856 a = a.__origin__
857 return a
858
859
860def _geqv(a, b):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800861 """Return whether two generic classes are equivalent (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700862
863 The intention is to consider generic class X and any of its
Guido van Rossumb24569a2016-11-20 18:01:29 -0800864 parameterized forms (X[T], X[int], etc.) as equivalent.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700865
866 However, X is not equivalent to a subclass of X.
867
868 The relation is reflexive, symmetric and transitive.
869 """
870 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
871 # Reduce each to its origin.
872 return _gorg(a) is _gorg(b)
873
874
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700875def _next_in_mro(cls):
876 """Helper for Generic.__new__.
877
878 Returns the class after the last occurrence of Generic or
879 Generic[...] in cls.__mro__.
880 """
881 next_in_mro = object
882 # Look for the last occurrence of Generic or Generic[...].
883 for i, c in enumerate(cls.__mro__[:-1]):
884 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800885 next_in_mro = cls.__mro__[i + 1]
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700886 return next_in_mro
887
888
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700889def _make_subclasshook(cls):
890 """Construct a __subclasshook__ callable that incorporates
891 the associated __extra__ class in subclass checks performed
892 against cls.
893 """
894 if isinstance(cls.__extra__, abc.ABCMeta):
895 # The logic mirrors that of ABCMeta.__subclasscheck__.
896 # Registered classes need not be checked here because
897 # cls and its extra share the same _abc_registry.
898 def __extrahook__(subclass):
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700899 res = cls.__extra__.__subclasshook__(subclass)
900 if res is not NotImplemented:
901 return res
902 if cls.__extra__ in subclass.__mro__:
903 return True
904 for scls in cls.__extra__.__subclasses__():
905 if isinstance(scls, GenericMeta):
906 continue
907 if issubclass(subclass, scls):
908 return True
909 return NotImplemented
910 else:
911 # For non-ABC extras we'll just call issubclass().
912 def __extrahook__(subclass):
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700913 if cls.__extra__ and issubclass(subclass, cls.__extra__):
914 return True
915 return NotImplemented
916 return __extrahook__
917
918
Guido van Rossum61f0a022016-11-29 09:46:21 -0800919def _no_slots_copy(dct):
920 """Internal helper: copy class __dict__ and clean slots class variables.
921 (They will be re-created if necessary by normal class machinery.)
922 """
923 dict_copy = dict(dct)
924 if '__slots__' in dict_copy:
925 for slot in dict_copy['__slots__']:
926 dict_copy.pop(slot, None)
927 return dict_copy
928
929
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700930class GenericMeta(TypingMeta, abc.ABCMeta):
Guido van Rossum83ec3022017-01-17 20:43:28 -0800931 """Metaclass for generic types.
932
933 This is a metaclass for typing.Generic and generic ABCs defined in
934 typing module. User defined subclasses of GenericMeta can override
935 __new__ and invoke super().__new__. Note that GenericMeta.__new__
936 has strict rules on what is allowed in its bases argument:
937 * plain Generic is disallowed in bases;
938 * Generic[...] should appear in bases at most once;
939 * if Generic[...] is present, then it should list all type variables
940 that appear in other bases.
941 In addition, type of all generic bases is erased, e.g., C[int] is
942 stripped to plain C.
943 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700944
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700945 def __new__(cls, name, bases, namespace,
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700946 tvars=None, args=None, origin=None, extra=None, orig_bases=None):
Guido van Rossum83ec3022017-01-17 20:43:28 -0800947 """Create a new generic class. GenericMeta.__new__ accepts
948 keyword arguments that are used for internal bookkeeping, therefore
949 an override should pass unused keyword arguments to super().
950 """
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700951 if tvars is not None:
952 # Called from __getitem__() below.
953 assert origin is not None
954 assert all(isinstance(t, TypeVar) for t in tvars), tvars
955 else:
956 # Called from class statement.
957 assert tvars is None, tvars
958 assert args is None, args
959 assert origin is None, origin
960
961 # Get the full set of tvars from the bases.
962 tvars = _type_vars(bases)
963 # Look for Generic[T1, ..., Tn].
964 # If found, tvars must be a subset of it.
965 # If not found, tvars is it.
966 # Also check for and reject plain Generic,
967 # and reject multiple Generic[...].
968 gvars = None
969 for base in bases:
970 if base is Generic:
971 raise TypeError("Cannot inherit from plain Generic")
972 if (isinstance(base, GenericMeta) and
973 base.__origin__ is Generic):
974 if gvars is not None:
975 raise TypeError(
976 "Cannot inherit from Generic[...] multiple types.")
977 gvars = base.__parameters__
978 if gvars is None:
979 gvars = tvars
980 else:
981 tvarset = set(tvars)
982 gvarset = set(gvars)
983 if not tvarset <= gvarset:
984 raise TypeError(
985 "Some type variables (%s) "
986 "are not listed in Generic[%s]" %
987 (", ".join(str(t) for t in tvars if t not in gvarset),
988 ", ".join(str(g) for g in gvars)))
989 tvars = gvars
990
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700991 initial_bases = bases
992 if extra is not None and type(extra) is abc.ABCMeta and extra not in bases:
993 bases = (extra,) + bases
994 bases = tuple(_gorg(b) if isinstance(b, GenericMeta) else b for b in bases)
995
996 # remove bare Generic from bases if there are other generic bases
997 if any(isinstance(b, GenericMeta) and b is not Generic for b in bases):
998 bases = tuple(b for b in bases if b is not Generic)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100999 namespace.update({'__origin__': origin, '__extra__': extra})
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001000 self = super().__new__(cls, name, bases, namespace, _root=True)
1001
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001002 self.__parameters__ = tvars
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001003 # Be prepared that GenericMeta will be subclassed by TupleMeta
1004 # and CallableMeta, those two allow ..., (), or [] in __args___.
1005 self.__args__ = tuple(... if a is _TypingEllipsis else
1006 () if a is _TypingEmpty else
1007 a for a in args) if args else None
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001008 # Speed hack (https://github.com/python/typing/issues/196).
1009 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001010 # Preserve base classes on subclassing (__bases__ are type erased now).
1011 if orig_bases is None:
1012 self.__orig_bases__ = initial_bases
Guido van Rossumb47c9d22016-10-03 08:40:50 -07001013
1014 # This allows unparameterized generic collections to be used
1015 # with issubclass() and isinstance() in the same way as their
1016 # collections.abc counterparts (e.g., isinstance([], Iterable)).
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001017 if (
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001018 '__subclasshook__' not in namespace and extra or
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001019 # allow overriding
1020 getattr(self.__subclasshook__, '__name__', '') == '__extrahook__'
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001021 ):
Guido van Rossume2592672016-10-08 20:27:22 -07001022 self.__subclasshook__ = _make_subclasshook(self)
Guido van Rossumb47c9d22016-10-03 08:40:50 -07001023 if isinstance(extra, abc.ABCMeta):
1024 self._abc_registry = extra._abc_registry
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001025 self._abc_cache = extra._abc_cache
1026 elif origin is not None:
1027 self._abc_registry = origin._abc_registry
1028 self._abc_cache = origin._abc_cache
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001029
1030 if origin and hasattr(origin, '__qualname__'): # Fix for Python 3.2.
1031 self.__qualname__ = origin.__qualname__
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001032 self.__tree_hash__ = (hash(self._subs_tree()) if origin else
1033 super(GenericMeta, self).__hash__())
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001034 return self
1035
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001036 # _abc_negative_cache and _abc_negative_cache_version
1037 # realised as descriptors, since GenClass[t1, t2, ...] always
1038 # share subclass info with GenClass.
1039 # This is an important memory optimization.
1040 @property
1041 def _abc_negative_cache(self):
1042 if isinstance(self.__extra__, abc.ABCMeta):
1043 return self.__extra__._abc_negative_cache
1044 return _gorg(self)._abc_generic_negative_cache
1045
1046 @_abc_negative_cache.setter
1047 def _abc_negative_cache(self, value):
1048 if self.__origin__ is None:
1049 if isinstance(self.__extra__, abc.ABCMeta):
1050 self.__extra__._abc_negative_cache = value
1051 else:
1052 self._abc_generic_negative_cache = value
1053
1054 @property
1055 def _abc_negative_cache_version(self):
1056 if isinstance(self.__extra__, abc.ABCMeta):
1057 return self.__extra__._abc_negative_cache_version
1058 return _gorg(self)._abc_generic_negative_cache_version
1059
1060 @_abc_negative_cache_version.setter
1061 def _abc_negative_cache_version(self, value):
1062 if self.__origin__ is None:
1063 if isinstance(self.__extra__, abc.ABCMeta):
1064 self.__extra__._abc_negative_cache_version = value
1065 else:
1066 self._abc_generic_negative_cache_version = value
1067
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001068 def _get_type_vars(self, tvars):
1069 if self.__origin__ and self.__parameters__:
1070 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001071
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001072 def _eval_type(self, globalns, localns):
1073 ev_origin = (self.__origin__._eval_type(globalns, localns)
1074 if self.__origin__ else None)
1075 ev_args = tuple(_eval_type(a, globalns, localns) for a
1076 in self.__args__) if self.__args__ else None
1077 if ev_origin == self.__origin__ and ev_args == self.__args__:
1078 return self
1079 return self.__class__(self.__name__,
1080 self.__bases__,
Guido van Rossum61f0a022016-11-29 09:46:21 -08001081 _no_slots_copy(self.__dict__),
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001082 tvars=_type_vars(ev_args) if ev_args else None,
1083 args=ev_args,
1084 origin=ev_origin,
1085 extra=self.__extra__,
1086 orig_bases=self.__orig_bases__)
1087
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001088 def __repr__(self):
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001089 if self.__origin__ is None:
1090 return super().__repr__()
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001091 return self._tree_repr(self._subs_tree())
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001092
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001093 def _tree_repr(self, tree):
1094 arg_list = []
1095 for arg in tree[1:]:
1096 if arg == ():
1097 arg_list.append('()')
1098 elif not isinstance(arg, tuple):
1099 arg_list.append(_type_repr(arg))
1100 else:
1101 arg_list.append(arg[0]._tree_repr(arg))
1102 return super().__repr__() + '[%s]' % ', '.join(arg_list)
1103
1104 def _subs_tree(self, tvars=None, args=None):
1105 if self.__origin__ is None:
1106 return self
1107 tree_args = _subs_tree(self, tvars, args)
1108 return (_gorg(self),) + tuple(tree_args)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001109
1110 def __eq__(self, other):
1111 if not isinstance(other, GenericMeta):
1112 return NotImplemented
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001113 if self.__origin__ is None or other.__origin__ is None:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001114 return self is other
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001115 return self.__tree_hash__ == other.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001116
1117 def __hash__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001118 return self.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001119
Guido van Rossum4cefe742016-09-27 15:20:12 -07001120 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001121 def __getitem__(self, params):
1122 if not isinstance(params, tuple):
1123 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001124 if not params and not _gorg(self) is Tuple:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001125 raise TypeError(
1126 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001127 msg = "Parameters to generic types must be types."
1128 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001129 if self is Generic:
1130 # Generic can only be subscripted with unique type variables.
1131 if not all(isinstance(p, TypeVar) for p in params):
1132 raise TypeError(
1133 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +02001134 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -07001135 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001136 "Parameters to Generic[...] must all be unique")
1137 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001138 args = params
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001139 elif self in (Tuple, Callable):
1140 tvars = _type_vars(params)
1141 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001142 elif self is _Protocol:
1143 # _Protocol is internal, don't check anything.
1144 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001145 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001146 elif self.__origin__ in (Generic, _Protocol):
1147 # Can't subscript Generic[...] or _Protocol[...].
1148 raise TypeError("Cannot subscript already-subscripted %s" %
1149 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001150 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001151 # Subscripting a regular Generic subclass.
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001152 _check_generic(self, params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001153 tvars = _type_vars(params)
1154 args = params
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001155
1156 prepend = (self,) if self.__origin__ is None else ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001157 return self.__class__(self.__name__,
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001158 prepend + self.__bases__,
Guido van Rossum61f0a022016-11-29 09:46:21 -08001159 _no_slots_copy(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001160 tvars=tvars,
1161 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001162 origin=self,
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001163 extra=self.__extra__,
1164 orig_bases=self.__orig_bases__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001165
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001166 def __subclasscheck__(self, cls):
1167 if self.__origin__ is not None:
1168 if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
1169 raise TypeError("Parameterized generics cannot be used with class "
1170 "or instance checks")
1171 return False
1172 if self is Generic:
1173 raise TypeError("Class %r cannot be used with class "
1174 "or instance checks" % self)
1175 return super().__subclasscheck__(cls)
1176
Guido van Rossum1b669102015-09-04 12:15:54 -07001177 def __instancecheck__(self, instance):
1178 # Since we extend ABC.__subclasscheck__ and
1179 # ABC.__instancecheck__ inlines the cache checking done by the
1180 # latter, we must extend __instancecheck__ too. For simplicity
1181 # we just skip the cache check -- instance checks for generic
1182 # classes are supposed to be rare anyways.
Guido van Rossumb47c9d22016-10-03 08:40:50 -07001183 return issubclass(instance.__class__, self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001184
Guido van Rossumb7dedc82016-10-29 12:44:29 -07001185 def __copy__(self):
Guido van Rossum61f0a022016-11-29 09:46:21 -08001186 return self.__class__(self.__name__, self.__bases__,
1187 _no_slots_copy(self.__dict__),
Guido van Rossumb7dedc82016-10-29 12:44:29 -07001188 self.__parameters__, self.__args__, self.__origin__,
1189 self.__extra__, self.__orig_bases__)
1190
Ivan Levkivskyiabb3b8a2017-02-24 04:03:28 +01001191 def __setattr__(self, attr, value):
1192 # We consider all the subscripted genrics as proxies for original class
Ivan Levkivskyi365cb5b2017-02-24 18:28:26 +01001193 if (
1194 attr.startswith('__') and attr.endswith('__') or
1195 attr.startswith('_abc_')
1196 ):
Ivan Levkivskyiabb3b8a2017-02-24 04:03:28 +01001197 super(GenericMeta, self).__setattr__(attr, value)
1198 else:
1199 super(GenericMeta, _gorg(self)).__setattr__(attr, value)
1200
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001201
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001202# Prevent checks for Generic to crash when defining Generic.
1203Generic = None
1204
1205
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001206def _generic_new(base_cls, cls, *args, **kwds):
1207 # Assure type is erased on instantiation,
1208 # but attempt to store it in __orig_class__
1209 if cls.__origin__ is None:
1210 return base_cls.__new__(cls)
1211 else:
1212 origin = _gorg(cls)
1213 obj = base_cls.__new__(origin)
1214 try:
1215 obj.__orig_class__ = cls
1216 except AttributeError:
1217 pass
1218 obj.__init__(*args, **kwds)
1219 return obj
1220
1221
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001222class Generic(metaclass=GenericMeta):
1223 """Abstract base class for generic types.
1224
Guido van Rossumb24569a2016-11-20 18:01:29 -08001225 A generic type is typically declared by inheriting from
1226 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001227 For example, a generic mapping type might be defined as::
1228
1229 class Mapping(Generic[KT, VT]):
1230 def __getitem__(self, key: KT) -> VT:
1231 ...
1232 # Etc.
1233
1234 This class can then be used as follows::
1235
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001236 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001237 try:
1238 return mapping[key]
1239 except KeyError:
1240 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001241 """
1242
Guido van Rossumd70fe632015-08-05 12:11:06 +02001243 __slots__ = ()
1244
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001245 def __new__(cls, *args, **kwds):
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001246 if _geqv(cls, Generic):
1247 raise TypeError("Type Generic cannot be instantiated; "
1248 "it can be used only as a base class")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001249 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
1250
1251
1252class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001253 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1254 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001255 to sneak in where prohibited.
1256 """
1257
1258
1259class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001260 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001261
1262
1263class TupleMeta(GenericMeta):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001264 """Metaclass for Tuple (internal)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001265
1266 @_tp_cache
1267 def __getitem__(self, parameters):
1268 if self.__origin__ is not None or not _geqv(self, Tuple):
1269 # Normal generic rules apply if this is not the first subscription
1270 # or a subscription of a subclass.
1271 return super().__getitem__(parameters)
1272 if parameters == ():
1273 return super().__getitem__((_TypingEmpty,))
1274 if not isinstance(parameters, tuple):
1275 parameters = (parameters,)
1276 if len(parameters) == 2 and parameters[1] is ...:
1277 msg = "Tuple[t, ...]: t must be a type."
1278 p = _type_check(parameters[0], msg)
1279 return super().__getitem__((p, _TypingEllipsis))
1280 msg = "Tuple[t0, t1, ...]: each t must be a type."
1281 parameters = tuple(_type_check(p, msg) for p in parameters)
1282 return super().__getitem__(parameters)
1283
1284 def __instancecheck__(self, obj):
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001285 if self.__args__ is None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001286 return isinstance(obj, tuple)
1287 raise TypeError("Parameterized Tuple cannot be used "
1288 "with isinstance().")
1289
1290 def __subclasscheck__(self, cls):
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001291 if self.__args__ is None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001292 return issubclass(cls, tuple)
1293 raise TypeError("Parameterized Tuple cannot be used "
1294 "with issubclass().")
1295
1296
1297class Tuple(tuple, extra=tuple, metaclass=TupleMeta):
1298 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
1299
1300 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1301 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1302 of an int, a float and a string.
1303
1304 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1305 """
1306
1307 __slots__ = ()
1308
1309 def __new__(cls, *args, **kwds):
1310 if _geqv(cls, Tuple):
1311 raise TypeError("Type Tuple cannot be instantiated; "
1312 "use tuple() instead")
1313 return _generic_new(tuple, cls, *args, **kwds)
1314
1315
1316class CallableMeta(GenericMeta):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001317 """Metaclass for Callable (internal)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001318
1319 def __repr__(self):
1320 if self.__origin__ is None:
1321 return super().__repr__()
1322 return self._tree_repr(self._subs_tree())
1323
1324 def _tree_repr(self, tree):
1325 if _gorg(self) is not Callable:
1326 return super()._tree_repr(tree)
1327 # For actual Callable (not its subclass) we override
1328 # super()._tree_repr() for nice formatting.
1329 arg_list = []
1330 for arg in tree[1:]:
Guido van Rossum991d14f2016-11-09 13:12:51 -08001331 if not isinstance(arg, tuple):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001332 arg_list.append(_type_repr(arg))
1333 else:
1334 arg_list.append(arg[0]._tree_repr(arg))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001335 if arg_list[0] == '...':
1336 return repr(tree[0]) + '[..., %s]' % arg_list[1]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001337 return (repr(tree[0]) +
1338 '[[%s], %s]' % (', '.join(arg_list[:-1]), arg_list[-1]))
1339
1340 def __getitem__(self, parameters):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001341 """A thin wrapper around __getitem_inner__ to provide the latter
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001342 with hashable arguments to improve speed.
1343 """
1344
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001345 if self.__origin__ is not None or not _geqv(self, Callable):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001346 return super().__getitem__(parameters)
1347 if not isinstance(parameters, tuple) or len(parameters) != 2:
1348 raise TypeError("Callable must be used as "
1349 "Callable[[arg, ...], result].")
1350 args, result = parameters
Guido van Rossum991d14f2016-11-09 13:12:51 -08001351 if args is Ellipsis:
1352 parameters = (Ellipsis, result)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001353 else:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001354 if not isinstance(args, list):
1355 raise TypeError("Callable[args, result]: args must be a list."
1356 " Got %.100r." % (args,))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001357 parameters = (tuple(args), result)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001358 return self.__getitem_inner__(parameters)
1359
1360 @_tp_cache
1361 def __getitem_inner__(self, parameters):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001362 args, result = parameters
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001363 msg = "Callable[args, result]: result must be a type."
1364 result = _type_check(result, msg)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001365 if args is Ellipsis:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001366 return super().__getitem__((_TypingEllipsis, result))
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001367 msg = "Callable[[arg, ...], result]: each arg must be a type."
1368 args = tuple(_type_check(arg, msg) for arg in args)
1369 parameters = args + (result,)
1370 return super().__getitem__(parameters)
1371
1372
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001373class Callable(extra=collections_abc.Callable, metaclass=CallableMeta):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001374 """Callable type; Callable[[int], str] is a function of (int) -> str.
1375
1376 The subscription syntax must always be used with exactly two
1377 values: the argument list and the return type. The argument list
Guido van Rossumb24569a2016-11-20 18:01:29 -08001378 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001379
1380 There is no syntax to indicate optional or keyword arguments,
1381 such function types are rarely used as callback types.
1382 """
1383
1384 __slots__ = ()
1385
1386 def __new__(cls, *args, **kwds):
1387 if _geqv(cls, Callable):
1388 raise TypeError("Type Callable cannot be instantiated; "
1389 "use a non-abstract subclass instead")
1390 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001391
1392
Guido van Rossum4cefe742016-09-27 15:20:12 -07001393class _ClassVar(_FinalTypingBase, _root=True):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001394 """Special type construct to mark class variables.
1395
1396 An annotation wrapped in ClassVar indicates that a given
1397 attribute is intended to be used as a class variable and
1398 should not be set on instances of that class. Usage::
1399
1400 class Starship:
1401 stats: ClassVar[Dict[str, int]] = {} # class variable
1402 damage: int = 10 # instance variable
1403
1404 ClassVar accepts only types and cannot be further subscribed.
1405
1406 Note that ClassVar is not a class itself, and should not
1407 be used with isinstance() or issubclass().
1408 """
1409
Guido van Rossum4cefe742016-09-27 15:20:12 -07001410 __slots__ = ('__type__',)
1411
1412 def __init__(self, tp=None, **kwds):
1413 self.__type__ = tp
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001414
1415 def __getitem__(self, item):
1416 cls = type(self)
1417 if self.__type__ is None:
1418 return cls(_type_check(item,
Guido van Rossum4cefe742016-09-27 15:20:12 -07001419 '{} accepts only single type.'.format(cls.__name__[1:])),
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001420 _root=True)
1421 raise TypeError('{} cannot be further subscripted'
1422 .format(cls.__name__[1:]))
1423
1424 def _eval_type(self, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001425 new_tp = _eval_type(self.__type__, globalns, localns)
1426 if new_tp == self.__type__:
1427 return self
1428 return type(self)(new_tp, _root=True)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001429
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001430 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001431 r = super().__repr__()
1432 if self.__type__ is not None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001433 r += '[{}]'.format(_type_repr(self.__type__))
Guido van Rossum4cefe742016-09-27 15:20:12 -07001434 return r
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001435
1436 def __hash__(self):
1437 return hash((type(self).__name__, self.__type__))
1438
1439 def __eq__(self, other):
1440 if not isinstance(other, _ClassVar):
1441 return NotImplemented
1442 if self.__type__ is not None:
1443 return self.__type__ == other.__type__
1444 return self is other
1445
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001446
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001447ClassVar = _ClassVar(_root=True)
1448
1449
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001450def cast(typ, val):
1451 """Cast a value to a type.
1452
1453 This returns the value unchanged. To the type checker this
1454 signals that the return value has the designated type, but at
1455 runtime we intentionally don't check anything (we want this
1456 to be as fast as possible).
1457 """
1458 return val
1459
1460
1461def _get_defaults(func):
1462 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001463 try:
1464 code = func.__code__
1465 except AttributeError:
1466 # Some built-in functions don't have __code__, __defaults__, etc.
1467 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001468 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001469 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001470 arg_names = arg_names[:pos_count]
1471 defaults = func.__defaults__ or ()
1472 kwdefaults = func.__kwdefaults__
1473 res = dict(kwdefaults) if kwdefaults else {}
1474 pos_offset = pos_count - len(defaults)
1475 for name, value in zip(arg_names[pos_offset:], defaults):
1476 assert name not in res
1477 res[name] = value
1478 return res
1479
1480
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001481_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1482 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001483 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001484
1485
Guido van Rossum991d14f2016-11-09 13:12:51 -08001486def get_type_hints(obj, globalns=None, localns=None):
1487 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001488
Guido van Rossum991d14f2016-11-09 13:12:51 -08001489 This is often the same as obj.__annotations__, but it handles
1490 forward references encoded as string literals, and if necessary
1491 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001492
Guido van Rossum991d14f2016-11-09 13:12:51 -08001493 The argument may be a module, class, method, or function. The annotations
1494 are returned as a dictionary. For classes, annotations include also
1495 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001496
Guido van Rossum991d14f2016-11-09 13:12:51 -08001497 TypeError is raised if the argument is not of a type that can contain
1498 annotations, and an empty dictionary is returned if no annotations are
1499 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001500
Guido van Rossum991d14f2016-11-09 13:12:51 -08001501 BEWARE -- the behavior of globalns and localns is counterintuitive
1502 (unless you are familiar with how eval() and exec() work). The
1503 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001504
Guido van Rossum991d14f2016-11-09 13:12:51 -08001505 - If no dict arguments are passed, an attempt is made to use the
1506 globals from obj, and these are also used as the locals. If the
1507 object does not appear to have globals, an exception is raised.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001508
Guido van Rossum991d14f2016-11-09 13:12:51 -08001509 - If one dict argument is passed, it is used for both globals and
1510 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001511
Guido van Rossum991d14f2016-11-09 13:12:51 -08001512 - If two dict arguments are passed, they specify globals and
1513 locals, respectively.
1514 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001515
Guido van Rossum991d14f2016-11-09 13:12:51 -08001516 if getattr(obj, '__no_type_check__', None):
1517 return {}
1518 if globalns is None:
1519 globalns = getattr(obj, '__globals__', {})
1520 if localns is None:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001521 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001522 elif localns is None:
1523 localns = globalns
1524 # Classes require a special treatment.
1525 if isinstance(obj, type):
1526 hints = {}
1527 for base in reversed(obj.__mro__):
1528 ann = base.__dict__.get('__annotations__', {})
1529 for name, value in ann.items():
1530 if value is None:
1531 value = type(None)
1532 if isinstance(value, str):
1533 value = _ForwardRef(value)
1534 value = _eval_type(value, globalns, localns)
1535 hints[name] = value
1536 return hints
1537 hints = getattr(obj, '__annotations__', None)
1538 if hints is None:
1539 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001540 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001541 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001542 else:
1543 raise TypeError('{!r} is not a module, class, method, '
1544 'or function.'.format(obj))
1545 defaults = _get_defaults(obj)
1546 hints = dict(hints)
1547 for name, value in hints.items():
1548 if value is None:
1549 value = type(None)
1550 if isinstance(value, str):
1551 value = _ForwardRef(value)
1552 value = _eval_type(value, globalns, localns)
1553 if name in defaults and defaults[name] is None:
1554 value = Optional[value]
1555 hints[name] = value
1556 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001557
1558
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001559def no_type_check(arg):
1560 """Decorator to indicate that annotations are not type hints.
1561
1562 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001563 applies recursively to all methods and classes defined in that class
1564 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001565
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001566 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001567 """
1568 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001569 arg_attrs = arg.__dict__.copy()
1570 for attr, val in arg.__dict__.items():
1571 if val in arg.__bases__:
1572 arg_attrs.pop(attr)
1573 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001574 if isinstance(obj, types.FunctionType):
1575 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001576 if isinstance(obj, type):
1577 no_type_check(obj)
1578 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001579 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001580 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001581 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001582 return arg
1583
1584
1585def no_type_check_decorator(decorator):
1586 """Decorator to give another decorator the @no_type_check effect.
1587
1588 This wraps the decorator with something that wraps the decorated
1589 function in @no_type_check.
1590 """
1591
1592 @functools.wraps(decorator)
1593 def wrapped_decorator(*args, **kwds):
1594 func = decorator(*args, **kwds)
1595 func = no_type_check(func)
1596 return func
1597
1598 return wrapped_decorator
1599
1600
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001601def _overload_dummy(*args, **kwds):
1602 """Helper for @overload to raise when called."""
1603 raise NotImplementedError(
1604 "You should not call an overloaded function. "
1605 "A series of @overload-decorated functions "
1606 "outside a stub module should always be followed "
1607 "by an implementation that is not @overload-ed.")
1608
1609
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001610def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001611 """Decorator for overloaded functions/methods.
1612
1613 In a stub file, place two or more stub definitions for the same
1614 function in a row, each decorated with @overload. For example:
1615
1616 @overload
1617 def utf8(value: None) -> None: ...
1618 @overload
1619 def utf8(value: bytes) -> bytes: ...
1620 @overload
1621 def utf8(value: str) -> bytes: ...
1622
1623 In a non-stub file (i.e. a regular .py file), do the same but
1624 follow it with an implementation. The implementation should *not*
1625 be decorated with @overload. For example:
1626
1627 @overload
1628 def utf8(value: None) -> None: ...
1629 @overload
1630 def utf8(value: bytes) -> bytes: ...
1631 @overload
1632 def utf8(value: str) -> bytes: ...
1633 def utf8(value):
1634 # implementation goes here
1635 """
1636 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001637
1638
1639class _ProtocolMeta(GenericMeta):
1640 """Internal metaclass for _Protocol.
1641
1642 This exists so _Protocol classes can be generic without deriving
1643 from Generic.
1644 """
1645
Guido van Rossumd70fe632015-08-05 12:11:06 +02001646 def __instancecheck__(self, obj):
Guido van Rossumca4b2522016-11-19 10:32:41 -08001647 if _Protocol not in self.__bases__:
1648 return super().__instancecheck__(obj)
Guido van Rossumd70fe632015-08-05 12:11:06 +02001649 raise TypeError("Protocols cannot be used with isinstance().")
1650
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001651 def __subclasscheck__(self, cls):
1652 if not self._is_protocol:
1653 # No structural checks since this isn't a protocol.
1654 return NotImplemented
1655
1656 if self is _Protocol:
1657 # Every class is a subclass of the empty protocol.
1658 return True
1659
1660 # Find all attributes defined in the protocol.
1661 attrs = self._get_protocol_attrs()
1662
1663 for attr in attrs:
1664 if not any(attr in d.__dict__ for d in cls.__mro__):
1665 return False
1666 return True
1667
1668 def _get_protocol_attrs(self):
1669 # Get all Protocol base classes.
1670 protocol_bases = []
1671 for c in self.__mro__:
1672 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1673 protocol_bases.append(c)
1674
1675 # Get attributes included in protocol.
1676 attrs = set()
1677 for base in protocol_bases:
1678 for attr in base.__dict__.keys():
1679 # Include attributes not defined in any non-protocol bases.
1680 for c in self.__mro__:
1681 if (c is not base and attr in c.__dict__ and
1682 not getattr(c, '_is_protocol', False)):
1683 break
1684 else:
1685 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001686 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001687 attr != '__annotations__' and
1688 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001689 attr != '_is_protocol' and
1690 attr != '__dict__' and
1691 attr != '__args__' and
1692 attr != '__slots__' and
1693 attr != '_get_protocol_attrs' and
1694 attr != '__next_in_mro__' and
1695 attr != '__parameters__' and
1696 attr != '__origin__' and
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001697 attr != '__orig_bases__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001698 attr != '__extra__' and
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001699 attr != '__tree_hash__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001700 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001701 attrs.add(attr)
1702
1703 return attrs
1704
1705
1706class _Protocol(metaclass=_ProtocolMeta):
1707 """Internal base class for protocol classes.
1708
Guido van Rossumb24569a2016-11-20 18:01:29 -08001709 This implements a simple-minded structural issubclass check
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001710 (similar but more general than the one-offs in collections.abc
1711 such as Hashable).
1712 """
1713
Guido van Rossumd70fe632015-08-05 12:11:06 +02001714 __slots__ = ()
1715
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001716 _is_protocol = True
1717
1718
1719# Various ABCs mimicking those in collections.abc.
1720# A few are simply re-exported for completeness.
1721
1722Hashable = collections_abc.Hashable # Not generic.
1723
1724
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001725if hasattr(collections_abc, 'Awaitable'):
1726 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1727 __slots__ = ()
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001728
1729 __all__.append('Awaitable')
1730
1731
1732if hasattr(collections_abc, 'Coroutine'):
1733 class Coroutine(Awaitable[V_co], Generic[T_co, T_contra, V_co],
1734 extra=collections_abc.Coroutine):
1735 __slots__ = ()
1736
1737 __all__.append('Coroutine')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001738
1739
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001740if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001741
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001742 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1743 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001744
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001745 class AsyncIterator(AsyncIterable[T_co],
1746 extra=collections_abc.AsyncIterator):
1747 __slots__ = ()
1748
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001749 __all__.append('AsyncIterable')
1750 __all__.append('AsyncIterator')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001751
1752
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001753class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001754 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001755
1756
1757class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001758 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001759
1760
1761class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001762 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001763
1764 @abstractmethod
1765 def __int__(self) -> int:
1766 pass
1767
1768
1769class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001770 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001771
1772 @abstractmethod
1773 def __float__(self) -> float:
1774 pass
1775
1776
1777class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001778 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001779
1780 @abstractmethod
1781 def __complex__(self) -> complex:
1782 pass
1783
1784
1785class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001786 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001787
1788 @abstractmethod
1789 def __bytes__(self) -> bytes:
1790 pass
1791
1792
Guido van Rossumd70fe632015-08-05 12:11:06 +02001793class SupportsAbs(_Protocol[T_co]):
1794 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001795
1796 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001797 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001798 pass
1799
1800
Guido van Rossumd70fe632015-08-05 12:11:06 +02001801class SupportsRound(_Protocol[T_co]):
1802 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001803
1804 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001805 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001806 pass
1807
1808
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001809if hasattr(collections_abc, 'Reversible'):
1810 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1811 __slots__ = ()
1812else:
1813 class Reversible(_Protocol[T_co]):
1814 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001815
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001816 @abstractmethod
1817 def __reversed__(self) -> 'Iterator[T_co]':
1818 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001819
1820
1821Sized = collections_abc.Sized # Not generic.
1822
1823
1824class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001825 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001826
1827
Guido van Rossumefa798d2016-08-23 11:01:50 -07001828if hasattr(collections_abc, 'Collection'):
1829 class Collection(Sized, Iterable[T_co], Container[T_co],
1830 extra=collections_abc.Collection):
1831 __slots__ = ()
1832
1833 __all__.append('Collection')
1834
1835
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001836# Callable was defined earlier.
1837
Guido van Rossumefa798d2016-08-23 11:01:50 -07001838if hasattr(collections_abc, 'Collection'):
1839 class AbstractSet(Collection[T_co],
1840 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001841 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001842else:
1843 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1844 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001845 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001846
1847
1848class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001849 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001850
1851
Guido van Rossumefa798d2016-08-23 11:01:50 -07001852# NOTE: It is only covariant in the value type.
1853if hasattr(collections_abc, 'Collection'):
1854 class Mapping(Collection[KT], Generic[KT, VT_co],
1855 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001856 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001857else:
1858 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1859 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001860 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001861
1862
1863class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001864 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001865
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001866
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001867if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001868 if hasattr(collections_abc, 'Collection'):
1869 class Sequence(Reversible[T_co], Collection[T_co],
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001870 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001871 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001872 else:
1873 class Sequence(Sized, Reversible[T_co], Container[T_co],
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001874 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001875 __slots__ = ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001876else:
1877 class Sequence(Sized, Iterable[T_co], Container[T_co],
1878 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001879 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001880
1881
1882class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001883 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001884
1885
1886class ByteString(Sequence[int], extra=collections_abc.ByteString):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001887 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001888
1889
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001890class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001891
Guido van Rossum4cefe742016-09-27 15:20:12 -07001892 __slots__ = ()
1893
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001894 def __new__(cls, *args, **kwds):
1895 if _geqv(cls, List):
1896 raise TypeError("Type List cannot be instantiated; "
1897 "use list() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001898 return _generic_new(list, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001899
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001900
Raymond Hettinger80490522017-01-16 22:42:37 -08001901class Deque(collections.deque, MutableSequence[T], extra=collections.deque):
1902
1903 __slots__ = ()
1904
1905 def __new__(cls, *args, **kwds):
1906 if _geqv(cls, Deque):
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001907 return collections.deque(*args, **kwds)
Raymond Hettinger80490522017-01-16 22:42:37 -08001908 return _generic_new(collections.deque, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001909
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001910
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001911class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001912
Guido van Rossum4cefe742016-09-27 15:20:12 -07001913 __slots__ = ()
1914
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001915 def __new__(cls, *args, **kwds):
1916 if _geqv(cls, Set):
1917 raise TypeError("Type Set cannot be instantiated; "
1918 "use set() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001919 return _generic_new(set, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001920
1921
Guido van Rossum4cefe742016-09-27 15:20:12 -07001922class FrozenSet(frozenset, AbstractSet[T_co], extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001923 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001924
1925 def __new__(cls, *args, **kwds):
1926 if _geqv(cls, FrozenSet):
1927 raise TypeError("Type FrozenSet cannot be instantiated; "
1928 "use frozenset() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001929 return _generic_new(frozenset, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001930
1931
1932class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001933 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001934
1935
Guido van Rossumd70fe632015-08-05 12:11:06 +02001936class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001937 extra=collections_abc.KeysView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001938 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001939
1940
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001941class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001942 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001943 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001944 extra=collections_abc.ItemsView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001945 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001946
1947
1948class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001949 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001950
1951
Brett Cannonf3ad0422016-04-15 10:51:30 -07001952if hasattr(contextlib, 'AbstractContextManager'):
1953 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1954 __slots__ = ()
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +02001955else:
1956 class ContextManager(Generic[T_co]):
1957 __slots__ = ()
1958
1959 def __enter__(self):
1960 return self
1961
1962 @abc.abstractmethod
1963 def __exit__(self, exc_type, exc_value, traceback):
1964 return None
1965
1966 @classmethod
1967 def __subclasshook__(cls, C):
1968 if cls is ContextManager:
1969 # In Python 3.6+, it is possible to set a method to None to
1970 # explicitly indicate that the class does not implement an ABC
1971 # (https://bugs.python.org/issue25958), but we do not support
1972 # that pattern here because this fallback class is only used
1973 # in Python 3.5 and earlier.
1974 if (any("__enter__" in B.__dict__ for B in C.__mro__) and
1975 any("__exit__" in B.__dict__ for B in C.__mro__)):
1976 return True
1977 return NotImplemented
1978
1979
1980if hasattr(contextlib, 'AbstractAsyncContextManager'):
1981 class AsyncContextManager(Generic[T_co],
1982 extra=contextlib.AbstractAsyncContextManager):
1983 __slots__ = ()
1984
1985 __all__.append('AsyncContextManager')
1986elif sys.version_info[:2] >= (3, 5):
1987 exec("""
1988class AsyncContextManager(Generic[T_co]):
1989 __slots__ = ()
1990
1991 async def __aenter__(self):
1992 return self
1993
1994 @abc.abstractmethod
1995 async def __aexit__(self, exc_type, exc_value, traceback):
1996 return None
1997
1998 @classmethod
1999 def __subclasshook__(cls, C):
2000 if cls is AsyncContextManager:
2001 if sys.version_info[:2] >= (3, 6):
2002 return _collections_abc._check_methods(C, "__aenter__", "__aexit__")
2003 if (any("__aenter__" in B.__dict__ for B in C.__mro__) and
2004 any("__aexit__" in B.__dict__ for B in C.__mro__)):
2005 return True
2006 return NotImplemented
2007
2008__all__.append('AsyncContextManager')
2009""")
Brett Cannonf3ad0422016-04-15 10:51:30 -07002010
2011
Guido van Rossum1cea70f2016-05-18 08:35:00 -07002012class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002013
Guido van Rossum4cefe742016-09-27 15:20:12 -07002014 __slots__ = ()
2015
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002016 def __new__(cls, *args, **kwds):
2017 if _geqv(cls, Dict):
2018 raise TypeError("Type Dict cannot be instantiated; "
2019 "use dict() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002020 return _generic_new(dict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002021
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002022
Guido van Rossum1cea70f2016-05-18 08:35:00 -07002023class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
2024 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07002025
Guido van Rossum4cefe742016-09-27 15:20:12 -07002026 __slots__ = ()
2027
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07002028 def __new__(cls, *args, **kwds):
2029 if _geqv(cls, DefaultDict):
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002030 return collections.defaultdict(*args, **kwds)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002031 return _generic_new(collections.defaultdict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002032
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002033
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002034class Counter(collections.Counter, Dict[T, int], extra=collections.Counter):
2035
2036 __slots__ = ()
2037
2038 def __new__(cls, *args, **kwds):
2039 if _geqv(cls, Counter):
2040 return collections.Counter(*args, **kwds)
2041 return _generic_new(collections.Counter, cls, *args, **kwds)
2042
2043
2044if hasattr(collections, 'ChainMap'):
2045 # ChainMap only exists in 3.3+
2046 __all__.append('ChainMap')
2047
2048 class ChainMap(collections.ChainMap, MutableMapping[KT, VT],
2049 extra=collections.ChainMap):
2050
2051 __slots__ = ()
2052
2053 def __new__(cls, *args, **kwds):
2054 if _geqv(cls, ChainMap):
2055 return collections.ChainMap(*args, **kwds)
2056 return _generic_new(collections.ChainMap, cls, *args, **kwds)
2057
2058
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002059# Determine what base class to use for Generator.
2060if hasattr(collections_abc, 'Generator'):
2061 # Sufficiently recent versions of 3.5 have a Generator ABC.
2062 _G_base = collections_abc.Generator
2063else:
2064 # Fall back on the exact type.
2065 _G_base = types.GeneratorType
2066
2067
2068class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
2069 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02002070 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002071
2072 def __new__(cls, *args, **kwds):
2073 if _geqv(cls, Generator):
2074 raise TypeError("Type Generator cannot be instantiated; "
2075 "create a subclass instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002076 return _generic_new(_G_base, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002077
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002078
Guido van Rossume9ed5602017-01-18 13:10:31 -08002079if hasattr(collections_abc, 'AsyncGenerator'):
2080 class AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra],
2081 extra=collections_abc.AsyncGenerator):
2082 __slots__ = ()
2083
2084 __all__.append('AsyncGenerator')
2085
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002086
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002087# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07002088CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002089
2090
Guido van Rossumb22c7082016-05-26 09:56:19 -07002091# This is not a real generic class. Don't use outside annotations.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002092class Type(Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07002093 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002094
2095 For example, suppose we have the following classes::
2096
2097 class User: ... # Abstract base for User classes
2098 class BasicUser(User): ...
2099 class ProUser(User): ...
2100 class TeamUser(User): ...
2101
2102 And a function that takes a class argument that's a subclass of
2103 User and returns an instance of the corresponding class::
2104
2105 U = TypeVar('U', bound=User)
2106 def new_user(user_class: Type[U]) -> U:
2107 user = user_class()
2108 # (Here we could write the user object to a database)
2109 return user
2110
2111 joe = new_user(BasicUser)
2112
2113 At this point the type checker knows that joe has type BasicUser.
2114 """
2115
Guido van Rossum4cefe742016-09-27 15:20:12 -07002116 __slots__ = ()
2117
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002118
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002119def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08002120 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
2121 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002122 nm_tpl = collections.namedtuple(name, [n for n, t in types])
Guido van Rossum83ec3022017-01-17 20:43:28 -08002123 # Prior to PEP 526, only _field_types attribute was assigned.
2124 # Now, both __annotations__ and _field_types are used to maintain compatibility.
2125 nm_tpl.__annotations__ = nm_tpl._field_types = collections.OrderedDict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08002126 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002127 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08002128 except (AttributeError, ValueError):
2129 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002130 return nm_tpl
2131
2132
Guido van Rossum2f841442016-11-15 09:48:06 -08002133_PY36 = sys.version_info[:2] >= (3, 6)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002134
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002135# attributes prohibited to set in NamedTuple class syntax
2136_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
2137 '_fields', '_field_defaults', '_field_types',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02002138 '_make', '_replace', '_asdict', '_source')
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002139
2140_special = ('__module__', '__name__', '__qualname__', '__annotations__')
2141
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002142
Guido van Rossum2f841442016-11-15 09:48:06 -08002143class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002144
Guido van Rossum2f841442016-11-15 09:48:06 -08002145 def __new__(cls, typename, bases, ns):
2146 if ns.get('_root', False):
2147 return super().__new__(cls, typename, bases, ns)
2148 if not _PY36:
2149 raise TypeError("Class syntax for NamedTuple is only supported"
2150 " in Python 3.6+")
2151 types = ns.get('__annotations__', {})
Guido van Rossum3c268be2017-01-18 08:03:50 -08002152 nm_tpl = _make_nmtuple(typename, types.items())
2153 defaults = []
2154 defaults_dict = {}
2155 for field_name in types:
2156 if field_name in ns:
2157 default_value = ns[field_name]
2158 defaults.append(default_value)
2159 defaults_dict[field_name] = default_value
2160 elif defaults:
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002161 raise TypeError("Non-default namedtuple field {field_name} cannot "
2162 "follow default field(s) {default_names}"
Guido van Rossum3c268be2017-01-18 08:03:50 -08002163 .format(field_name=field_name,
2164 default_names=', '.join(defaults_dict.keys())))
2165 nm_tpl.__new__.__defaults__ = tuple(defaults)
2166 nm_tpl._field_defaults = defaults_dict
Guido van Rossum95919c02017-01-22 17:47:20 -08002167 # update from user namespace without overriding special namedtuple attributes
2168 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002169 if key in _prohibited:
2170 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2171 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002172 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002173 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002174
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002175
Guido van Rossum2f841442016-11-15 09:48:06 -08002176class NamedTuple(metaclass=NamedTupleMeta):
2177 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002178
Guido van Rossum2f841442016-11-15 09:48:06 -08002179 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002180
Guido van Rossum2f841442016-11-15 09:48:06 -08002181 class Employee(NamedTuple):
2182 name: str
2183 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002184
Guido van Rossum2f841442016-11-15 09:48:06 -08002185 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002186
Guido van Rossum2f841442016-11-15 09:48:06 -08002187 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002188
Guido van Rossum83ec3022017-01-17 20:43:28 -08002189 The resulting class has extra __annotations__ and _field_types
2190 attributes, giving an ordered dict mapping field names to types.
2191 __annotations__ should be preferred, while _field_types
2192 is kept to maintain pre PEP 526 compatibility. (The field names
Guido van Rossum2f841442016-11-15 09:48:06 -08002193 are in the _fields attribute, which is part of the namedtuple
2194 API.) Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002195
Guido van Rossum2f841442016-11-15 09:48:06 -08002196 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002197
Guido van Rossum2f841442016-11-15 09:48:06 -08002198 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002199
Guido van Rossum2f841442016-11-15 09:48:06 -08002200 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2201 """
2202 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002203
Guido van Rossum2f841442016-11-15 09:48:06 -08002204 def __new__(self, typename, fields=None, **kwargs):
2205 if kwargs and not _PY36:
2206 raise TypeError("Keyword syntax for NamedTuple is only supported"
2207 " in Python 3.6+")
2208 if fields is None:
2209 fields = kwargs.items()
2210 elif kwargs:
2211 raise TypeError("Either list of fields or keywords"
2212 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002213 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002214
2215
Guido van Rossum91185fe2016-06-08 11:19:11 -07002216def NewType(name, tp):
2217 """NewType creates simple unique types with almost zero
2218 runtime overhead. NewType(name, tp) is considered a subtype of tp
2219 by static type checkers. At runtime, NewType(name, tp) returns
2220 a dummy function that simply returns its argument. Usage::
2221
2222 UserId = NewType('UserId', int)
2223
2224 def name_by_id(user_id: UserId) -> str:
2225 ...
2226
2227 UserId('user') # Fails type check
2228
2229 name_by_id(42) # Fails type check
2230 name_by_id(UserId(42)) # OK
2231
2232 num = UserId(5) + 1 # type: int
2233 """
2234
2235 def new_type(x):
2236 return x
2237
2238 new_type.__name__ = name
2239 new_type.__supertype__ = tp
2240 return new_type
2241
2242
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002243# Python-version-specific alias (Python 2: unicode; Python 3: str)
2244Text = str
2245
2246
Guido van Rossum91185fe2016-06-08 11:19:11 -07002247# Constant that's True when type checking, but False here.
2248TYPE_CHECKING = False
2249
2250
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002251class IO(Generic[AnyStr]):
2252 """Generic base class for TextIO and BinaryIO.
2253
2254 This is an abstract, generic version of the return of open().
2255
2256 NOTE: This does not distinguish between the different possible
2257 classes (text vs. binary, read vs. write vs. read/write,
2258 append-only, unbuffered). The TextIO and BinaryIO subclasses
2259 below capture the distinctions between text vs. binary, which is
2260 pervasive in the interface; however we currently do not offer a
2261 way to track the other distinctions in the type system.
2262 """
2263
Guido van Rossumd70fe632015-08-05 12:11:06 +02002264 __slots__ = ()
2265
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002266 @abstractproperty
2267 def mode(self) -> str:
2268 pass
2269
2270 @abstractproperty
2271 def name(self) -> str:
2272 pass
2273
2274 @abstractmethod
2275 def close(self) -> None:
2276 pass
2277
2278 @abstractmethod
2279 def closed(self) -> bool:
2280 pass
2281
2282 @abstractmethod
2283 def fileno(self) -> int:
2284 pass
2285
2286 @abstractmethod
2287 def flush(self) -> None:
2288 pass
2289
2290 @abstractmethod
2291 def isatty(self) -> bool:
2292 pass
2293
2294 @abstractmethod
2295 def read(self, n: int = -1) -> AnyStr:
2296 pass
2297
2298 @abstractmethod
2299 def readable(self) -> bool:
2300 pass
2301
2302 @abstractmethod
2303 def readline(self, limit: int = -1) -> AnyStr:
2304 pass
2305
2306 @abstractmethod
2307 def readlines(self, hint: int = -1) -> List[AnyStr]:
2308 pass
2309
2310 @abstractmethod
2311 def seek(self, offset: int, whence: int = 0) -> int:
2312 pass
2313
2314 @abstractmethod
2315 def seekable(self) -> bool:
2316 pass
2317
2318 @abstractmethod
2319 def tell(self) -> int:
2320 pass
2321
2322 @abstractmethod
2323 def truncate(self, size: int = None) -> int:
2324 pass
2325
2326 @abstractmethod
2327 def writable(self) -> bool:
2328 pass
2329
2330 @abstractmethod
2331 def write(self, s: AnyStr) -> int:
2332 pass
2333
2334 @abstractmethod
2335 def writelines(self, lines: List[AnyStr]) -> None:
2336 pass
2337
2338 @abstractmethod
2339 def __enter__(self) -> 'IO[AnyStr]':
2340 pass
2341
2342 @abstractmethod
2343 def __exit__(self, type, value, traceback) -> None:
2344 pass
2345
2346
2347class BinaryIO(IO[bytes]):
2348 """Typed version of the return of open() in binary mode."""
2349
Guido van Rossumd70fe632015-08-05 12:11:06 +02002350 __slots__ = ()
2351
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002352 @abstractmethod
2353 def write(self, s: Union[bytes, bytearray]) -> int:
2354 pass
2355
2356 @abstractmethod
2357 def __enter__(self) -> 'BinaryIO':
2358 pass
2359
2360
2361class TextIO(IO[str]):
2362 """Typed version of the return of open() in text mode."""
2363
Guido van Rossumd70fe632015-08-05 12:11:06 +02002364 __slots__ = ()
2365
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002366 @abstractproperty
2367 def buffer(self) -> BinaryIO:
2368 pass
2369
2370 @abstractproperty
2371 def encoding(self) -> str:
2372 pass
2373
2374 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08002375 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002376 pass
2377
2378 @abstractproperty
2379 def line_buffering(self) -> bool:
2380 pass
2381
2382 @abstractproperty
2383 def newlines(self) -> Any:
2384 pass
2385
2386 @abstractmethod
2387 def __enter__(self) -> 'TextIO':
2388 pass
2389
2390
2391class io:
2392 """Wrapper namespace for IO generic classes."""
2393
2394 __all__ = ['IO', 'TextIO', 'BinaryIO']
2395 IO = IO
2396 TextIO = TextIO
2397 BinaryIO = BinaryIO
2398
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002399
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002400io.__name__ = __name__ + '.io'
2401sys.modules[io.__name__] = io
2402
2403
2404Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
2405 lambda p: p.pattern)
2406Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
2407 lambda m: m.re.pattern)
2408
2409
2410class re:
2411 """Wrapper namespace for re type aliases."""
2412
2413 __all__ = ['Pattern', 'Match']
2414 Pattern = Pattern
2415 Match = Match
2416
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002417
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002418re.__name__ = __name__ + '.re'
2419sys.modules[re.__name__] = re