blob: 609f813b01eb214a6cf888af3f33e25ec2ef8e40 [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
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +0200379 isinstance(arg, TypingMeta) and arg._gorg in (Generic, _Protocol)
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800380 ):
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 Rossumbd5b9a02016-04-05 08:28:52 -0700852def _next_in_mro(cls):
853 """Helper for Generic.__new__.
854
855 Returns the class after the last occurrence of Generic or
856 Generic[...] in cls.__mro__.
857 """
858 next_in_mro = object
859 # Look for the last occurrence of Generic or Generic[...].
860 for i, c in enumerate(cls.__mro__[:-1]):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +0200861 if isinstance(c, GenericMeta) and c._gorg is Generic:
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800862 next_in_mro = cls.__mro__[i + 1]
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700863 return next_in_mro
864
865
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700866def _make_subclasshook(cls):
867 """Construct a __subclasshook__ callable that incorporates
868 the associated __extra__ class in subclass checks performed
869 against cls.
870 """
871 if isinstance(cls.__extra__, abc.ABCMeta):
872 # The logic mirrors that of ABCMeta.__subclasscheck__.
873 # Registered classes need not be checked here because
874 # cls and its extra share the same _abc_registry.
875 def __extrahook__(subclass):
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700876 res = cls.__extra__.__subclasshook__(subclass)
877 if res is not NotImplemented:
878 return res
879 if cls.__extra__ in subclass.__mro__:
880 return True
881 for scls in cls.__extra__.__subclasses__():
882 if isinstance(scls, GenericMeta):
883 continue
884 if issubclass(subclass, scls):
885 return True
886 return NotImplemented
887 else:
888 # For non-ABC extras we'll just call issubclass().
889 def __extrahook__(subclass):
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700890 if cls.__extra__ and issubclass(subclass, cls.__extra__):
891 return True
892 return NotImplemented
893 return __extrahook__
894
895
Guido van Rossum61f0a022016-11-29 09:46:21 -0800896def _no_slots_copy(dct):
897 """Internal helper: copy class __dict__ and clean slots class variables.
898 (They will be re-created if necessary by normal class machinery.)
899 """
900 dict_copy = dict(dct)
901 if '__slots__' in dict_copy:
902 for slot in dict_copy['__slots__']:
903 dict_copy.pop(slot, None)
904 return dict_copy
905
906
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700907class GenericMeta(TypingMeta, abc.ABCMeta):
Guido van Rossum83ec3022017-01-17 20:43:28 -0800908 """Metaclass for generic types.
909
910 This is a metaclass for typing.Generic and generic ABCs defined in
911 typing module. User defined subclasses of GenericMeta can override
912 __new__ and invoke super().__new__. Note that GenericMeta.__new__
913 has strict rules on what is allowed in its bases argument:
914 * plain Generic is disallowed in bases;
915 * Generic[...] should appear in bases at most once;
916 * if Generic[...] is present, then it should list all type variables
917 that appear in other bases.
918 In addition, type of all generic bases is erased, e.g., C[int] is
919 stripped to plain C.
920 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700921
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700922 def __new__(cls, name, bases, namespace,
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700923 tvars=None, args=None, origin=None, extra=None, orig_bases=None):
Guido van Rossum83ec3022017-01-17 20:43:28 -0800924 """Create a new generic class. GenericMeta.__new__ accepts
925 keyword arguments that are used for internal bookkeeping, therefore
926 an override should pass unused keyword arguments to super().
927 """
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700928 if tvars is not None:
929 # Called from __getitem__() below.
930 assert origin is not None
931 assert all(isinstance(t, TypeVar) for t in tvars), tvars
932 else:
933 # Called from class statement.
934 assert tvars is None, tvars
935 assert args is None, args
936 assert origin is None, origin
937
938 # Get the full set of tvars from the bases.
939 tvars = _type_vars(bases)
940 # Look for Generic[T1, ..., Tn].
941 # If found, tvars must be a subset of it.
942 # If not found, tvars is it.
943 # Also check for and reject plain Generic,
944 # and reject multiple Generic[...].
945 gvars = None
946 for base in bases:
947 if base is Generic:
948 raise TypeError("Cannot inherit from plain Generic")
949 if (isinstance(base, GenericMeta) and
950 base.__origin__ is Generic):
951 if gvars is not None:
952 raise TypeError(
953 "Cannot inherit from Generic[...] multiple types.")
954 gvars = base.__parameters__
955 if gvars is None:
956 gvars = tvars
957 else:
958 tvarset = set(tvars)
959 gvarset = set(gvars)
960 if not tvarset <= gvarset:
961 raise TypeError(
962 "Some type variables (%s) "
963 "are not listed in Generic[%s]" %
964 (", ".join(str(t) for t in tvars if t not in gvarset),
965 ", ".join(str(g) for g in gvars)))
966 tvars = gvars
967
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700968 initial_bases = bases
969 if extra is not None and type(extra) is abc.ABCMeta and extra not in bases:
970 bases = (extra,) + bases
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +0200971 bases = tuple(b._gorg if isinstance(b, GenericMeta) else b for b in bases)
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700972
973 # remove bare Generic from bases if there are other generic bases
974 if any(isinstance(b, GenericMeta) and b is not Generic for b in bases):
975 bases = tuple(b for b in bases if b is not Generic)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100976 namespace.update({'__origin__': origin, '__extra__': extra})
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700977 self = super().__new__(cls, name, bases, namespace, _root=True)
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +0200978 super(GenericMeta, self).__setattr__('_gorg',
979 self if not origin else origin._gorg)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700980 self.__parameters__ = tvars
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700981 # Be prepared that GenericMeta will be subclassed by TupleMeta
982 # and CallableMeta, those two allow ..., (), or [] in __args___.
983 self.__args__ = tuple(... if a is _TypingEllipsis else
984 () if a is _TypingEmpty else
985 a for a in args) if args else None
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700986 # Speed hack (https://github.com/python/typing/issues/196).
987 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700988 # Preserve base classes on subclassing (__bases__ are type erased now).
989 if orig_bases is None:
990 self.__orig_bases__ = initial_bases
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700991
992 # This allows unparameterized generic collections to be used
993 # with issubclass() and isinstance() in the same way as their
994 # collections.abc counterparts (e.g., isinstance([], Iterable)).
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800995 if (
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800996 '__subclasshook__' not in namespace and extra or
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100997 # allow overriding
998 getattr(self.__subclasshook__, '__name__', '') == '__extrahook__'
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800999 ):
Guido van Rossume2592672016-10-08 20:27:22 -07001000 self.__subclasshook__ = _make_subclasshook(self)
Guido van Rossumb47c9d22016-10-03 08:40:50 -07001001 if isinstance(extra, abc.ABCMeta):
1002 self._abc_registry = extra._abc_registry
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001003 self._abc_cache = extra._abc_cache
1004 elif origin is not None:
1005 self._abc_registry = origin._abc_registry
1006 self._abc_cache = origin._abc_cache
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001007
1008 if origin and hasattr(origin, '__qualname__'): # Fix for Python 3.2.
1009 self.__qualname__ = origin.__qualname__
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001010 self.__tree_hash__ = (hash(self._subs_tree()) if origin else
1011 super(GenericMeta, self).__hash__())
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001012 return self
1013
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001014 # _abc_negative_cache and _abc_negative_cache_version
1015 # realised as descriptors, since GenClass[t1, t2, ...] always
1016 # share subclass info with GenClass.
1017 # This is an important memory optimization.
1018 @property
1019 def _abc_negative_cache(self):
1020 if isinstance(self.__extra__, abc.ABCMeta):
1021 return self.__extra__._abc_negative_cache
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001022 return self._gorg._abc_generic_negative_cache
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001023
1024 @_abc_negative_cache.setter
1025 def _abc_negative_cache(self, value):
1026 if self.__origin__ is None:
1027 if isinstance(self.__extra__, abc.ABCMeta):
1028 self.__extra__._abc_negative_cache = value
1029 else:
1030 self._abc_generic_negative_cache = value
1031
1032 @property
1033 def _abc_negative_cache_version(self):
1034 if isinstance(self.__extra__, abc.ABCMeta):
1035 return self.__extra__._abc_negative_cache_version
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001036 return self._gorg._abc_generic_negative_cache_version
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001037
1038 @_abc_negative_cache_version.setter
1039 def _abc_negative_cache_version(self, value):
1040 if self.__origin__ is None:
1041 if isinstance(self.__extra__, abc.ABCMeta):
1042 self.__extra__._abc_negative_cache_version = value
1043 else:
1044 self._abc_generic_negative_cache_version = value
1045
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001046 def _get_type_vars(self, tvars):
1047 if self.__origin__ and self.__parameters__:
1048 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001049
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001050 def _eval_type(self, globalns, localns):
1051 ev_origin = (self.__origin__._eval_type(globalns, localns)
1052 if self.__origin__ else None)
1053 ev_args = tuple(_eval_type(a, globalns, localns) for a
1054 in self.__args__) if self.__args__ else None
1055 if ev_origin == self.__origin__ and ev_args == self.__args__:
1056 return self
1057 return self.__class__(self.__name__,
1058 self.__bases__,
Guido van Rossum61f0a022016-11-29 09:46:21 -08001059 _no_slots_copy(self.__dict__),
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001060 tvars=_type_vars(ev_args) if ev_args else None,
1061 args=ev_args,
1062 origin=ev_origin,
1063 extra=self.__extra__,
1064 orig_bases=self.__orig_bases__)
1065
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001066 def __repr__(self):
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001067 if self.__origin__ is None:
1068 return super().__repr__()
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001069 return self._tree_repr(self._subs_tree())
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001070
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001071 def _tree_repr(self, tree):
1072 arg_list = []
1073 for arg in tree[1:]:
1074 if arg == ():
1075 arg_list.append('()')
1076 elif not isinstance(arg, tuple):
1077 arg_list.append(_type_repr(arg))
1078 else:
1079 arg_list.append(arg[0]._tree_repr(arg))
1080 return super().__repr__() + '[%s]' % ', '.join(arg_list)
1081
1082 def _subs_tree(self, tvars=None, args=None):
1083 if self.__origin__ is None:
1084 return self
1085 tree_args = _subs_tree(self, tvars, args)
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001086 return (self._gorg,) + tuple(tree_args)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001087
1088 def __eq__(self, other):
1089 if not isinstance(other, GenericMeta):
1090 return NotImplemented
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001091 if self.__origin__ is None or other.__origin__ is None:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001092 return self is other
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001093 return self.__tree_hash__ == other.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001094
1095 def __hash__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001096 return self.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001097
Guido van Rossum4cefe742016-09-27 15:20:12 -07001098 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001099 def __getitem__(self, params):
1100 if not isinstance(params, tuple):
1101 params = (params,)
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001102 if not params and self._gorg is not Tuple:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001103 raise TypeError(
1104 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001105 msg = "Parameters to generic types must be types."
1106 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001107 if self is Generic:
1108 # Generic can only be subscripted with unique type variables.
1109 if not all(isinstance(p, TypeVar) for p in params):
1110 raise TypeError(
1111 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +02001112 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -07001113 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001114 "Parameters to Generic[...] must all be unique")
1115 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001116 args = params
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001117 elif self in (Tuple, Callable):
1118 tvars = _type_vars(params)
1119 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001120 elif self is _Protocol:
1121 # _Protocol is internal, don't check anything.
1122 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001123 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001124 elif self.__origin__ in (Generic, _Protocol):
1125 # Can't subscript Generic[...] or _Protocol[...].
1126 raise TypeError("Cannot subscript already-subscripted %s" %
1127 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001128 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001129 # Subscripting a regular Generic subclass.
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001130 _check_generic(self, params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001131 tvars = _type_vars(params)
1132 args = params
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001133
1134 prepend = (self,) if self.__origin__ is None else ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001135 return self.__class__(self.__name__,
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001136 prepend + self.__bases__,
Guido van Rossum61f0a022016-11-29 09:46:21 -08001137 _no_slots_copy(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001138 tvars=tvars,
1139 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001140 origin=self,
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001141 extra=self.__extra__,
1142 orig_bases=self.__orig_bases__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001143
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001144 def __subclasscheck__(self, cls):
1145 if self.__origin__ is not None:
1146 if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']:
1147 raise TypeError("Parameterized generics cannot be used with class "
1148 "or instance checks")
1149 return False
1150 if self is Generic:
1151 raise TypeError("Class %r cannot be used with class "
1152 "or instance checks" % self)
1153 return super().__subclasscheck__(cls)
1154
Guido van Rossum1b669102015-09-04 12:15:54 -07001155 def __instancecheck__(self, instance):
1156 # Since we extend ABC.__subclasscheck__ and
1157 # ABC.__instancecheck__ inlines the cache checking done by the
1158 # latter, we must extend __instancecheck__ too. For simplicity
1159 # we just skip the cache check -- instance checks for generic
1160 # classes are supposed to be rare anyways.
Guido van Rossumb47c9d22016-10-03 08:40:50 -07001161 return issubclass(instance.__class__, self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001162
Guido van Rossumb7dedc82016-10-29 12:44:29 -07001163 def __copy__(self):
Guido van Rossum61f0a022016-11-29 09:46:21 -08001164 return self.__class__(self.__name__, self.__bases__,
1165 _no_slots_copy(self.__dict__),
Guido van Rossumb7dedc82016-10-29 12:44:29 -07001166 self.__parameters__, self.__args__, self.__origin__,
1167 self.__extra__, self.__orig_bases__)
1168
Ivan Levkivskyiabb3b8a2017-02-24 04:03:28 +01001169 def __setattr__(self, attr, value):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001170 # We consider all the subscripted generics as proxies for original class
Ivan Levkivskyi365cb5b2017-02-24 18:28:26 +01001171 if (
1172 attr.startswith('__') and attr.endswith('__') or
1173 attr.startswith('_abc_')
1174 ):
Ivan Levkivskyiabb3b8a2017-02-24 04:03:28 +01001175 super(GenericMeta, self).__setattr__(attr, value)
1176 else:
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001177 super(GenericMeta, self._gorg).__setattr__(attr, value)
Ivan Levkivskyiabb3b8a2017-02-24 04:03:28 +01001178
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001179
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001180# Prevent checks for Generic to crash when defining Generic.
1181Generic = None
1182
1183
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001184def _generic_new(base_cls, cls, *args, **kwds):
1185 # Assure type is erased on instantiation,
1186 # but attempt to store it in __orig_class__
1187 if cls.__origin__ is None:
1188 return base_cls.__new__(cls)
1189 else:
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001190 origin = cls._gorg
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001191 obj = base_cls.__new__(origin)
1192 try:
1193 obj.__orig_class__ = cls
1194 except AttributeError:
1195 pass
1196 obj.__init__(*args, **kwds)
1197 return obj
1198
1199
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001200class Generic(metaclass=GenericMeta):
1201 """Abstract base class for generic types.
1202
Guido van Rossumb24569a2016-11-20 18:01:29 -08001203 A generic type is typically declared by inheriting from
1204 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001205 For example, a generic mapping type might be defined as::
1206
1207 class Mapping(Generic[KT, VT]):
1208 def __getitem__(self, key: KT) -> VT:
1209 ...
1210 # Etc.
1211
1212 This class can then be used as follows::
1213
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001214 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001215 try:
1216 return mapping[key]
1217 except KeyError:
1218 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001219 """
1220
Guido van Rossumd70fe632015-08-05 12:11:06 +02001221 __slots__ = ()
1222
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001223 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001224 if cls._gorg is Generic:
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001225 raise TypeError("Type Generic cannot be instantiated; "
1226 "it can be used only as a base class")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001227 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
1228
1229
1230class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001231 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1232 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001233 to sneak in where prohibited.
1234 """
1235
1236
1237class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001238 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001239
1240
1241class TupleMeta(GenericMeta):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001242 """Metaclass for Tuple (internal)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001243
1244 @_tp_cache
1245 def __getitem__(self, parameters):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001246 if self.__origin__ is not None or self._gorg is not Tuple:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001247 # Normal generic rules apply if this is not the first subscription
1248 # or a subscription of a subclass.
1249 return super().__getitem__(parameters)
1250 if parameters == ():
1251 return super().__getitem__((_TypingEmpty,))
1252 if not isinstance(parameters, tuple):
1253 parameters = (parameters,)
1254 if len(parameters) == 2 and parameters[1] is ...:
1255 msg = "Tuple[t, ...]: t must be a type."
1256 p = _type_check(parameters[0], msg)
1257 return super().__getitem__((p, _TypingEllipsis))
1258 msg = "Tuple[t0, t1, ...]: each t must be a type."
1259 parameters = tuple(_type_check(p, msg) for p in parameters)
1260 return super().__getitem__(parameters)
1261
1262 def __instancecheck__(self, obj):
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001263 if self.__args__ is None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001264 return isinstance(obj, tuple)
1265 raise TypeError("Parameterized Tuple cannot be used "
1266 "with isinstance().")
1267
1268 def __subclasscheck__(self, cls):
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001269 if self.__args__ is None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001270 return issubclass(cls, tuple)
1271 raise TypeError("Parameterized Tuple cannot be used "
1272 "with issubclass().")
1273
1274
1275class Tuple(tuple, extra=tuple, metaclass=TupleMeta):
1276 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
1277
1278 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1279 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1280 of an int, a float and a string.
1281
1282 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1283 """
1284
1285 __slots__ = ()
1286
1287 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001288 if cls._gorg is Tuple:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001289 raise TypeError("Type Tuple cannot be instantiated; "
1290 "use tuple() instead")
1291 return _generic_new(tuple, cls, *args, **kwds)
1292
1293
1294class CallableMeta(GenericMeta):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001295 """Metaclass for Callable (internal)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001296
1297 def __repr__(self):
1298 if self.__origin__ is None:
1299 return super().__repr__()
1300 return self._tree_repr(self._subs_tree())
1301
1302 def _tree_repr(self, tree):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001303 if self._gorg is not Callable:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001304 return super()._tree_repr(tree)
1305 # For actual Callable (not its subclass) we override
1306 # super()._tree_repr() for nice formatting.
1307 arg_list = []
1308 for arg in tree[1:]:
Guido van Rossum991d14f2016-11-09 13:12:51 -08001309 if not isinstance(arg, tuple):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001310 arg_list.append(_type_repr(arg))
1311 else:
1312 arg_list.append(arg[0]._tree_repr(arg))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001313 if arg_list[0] == '...':
1314 return repr(tree[0]) + '[..., %s]' % arg_list[1]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001315 return (repr(tree[0]) +
1316 '[[%s], %s]' % (', '.join(arg_list[:-1]), arg_list[-1]))
1317
1318 def __getitem__(self, parameters):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001319 """A thin wrapper around __getitem_inner__ to provide the latter
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001320 with hashable arguments to improve speed.
1321 """
1322
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001323 if self.__origin__ is not None or self._gorg is not Callable:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001324 return super().__getitem__(parameters)
1325 if not isinstance(parameters, tuple) or len(parameters) != 2:
1326 raise TypeError("Callable must be used as "
1327 "Callable[[arg, ...], result].")
1328 args, result = parameters
Guido van Rossum991d14f2016-11-09 13:12:51 -08001329 if args is Ellipsis:
1330 parameters = (Ellipsis, result)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001331 else:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001332 if not isinstance(args, list):
1333 raise TypeError("Callable[args, result]: args must be a list."
1334 " Got %.100r." % (args,))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001335 parameters = (tuple(args), result)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001336 return self.__getitem_inner__(parameters)
1337
1338 @_tp_cache
1339 def __getitem_inner__(self, parameters):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001340 args, result = parameters
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001341 msg = "Callable[args, result]: result must be a type."
1342 result = _type_check(result, msg)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001343 if args is Ellipsis:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001344 return super().__getitem__((_TypingEllipsis, result))
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001345 msg = "Callable[[arg, ...], result]: each arg must be a type."
1346 args = tuple(_type_check(arg, msg) for arg in args)
1347 parameters = args + (result,)
1348 return super().__getitem__(parameters)
1349
1350
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001351class Callable(extra=collections_abc.Callable, metaclass=CallableMeta):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001352 """Callable type; Callable[[int], str] is a function of (int) -> str.
1353
1354 The subscription syntax must always be used with exactly two
1355 values: the argument list and the return type. The argument list
Guido van Rossumb24569a2016-11-20 18:01:29 -08001356 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001357
1358 There is no syntax to indicate optional or keyword arguments,
1359 such function types are rarely used as callback types.
1360 """
1361
1362 __slots__ = ()
1363
1364 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001365 if cls._gorg is Callable:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001366 raise TypeError("Type Callable cannot be instantiated; "
1367 "use a non-abstract subclass instead")
1368 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001369
1370
Guido van Rossum4cefe742016-09-27 15:20:12 -07001371class _ClassVar(_FinalTypingBase, _root=True):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001372 """Special type construct to mark class variables.
1373
1374 An annotation wrapped in ClassVar indicates that a given
1375 attribute is intended to be used as a class variable and
1376 should not be set on instances of that class. Usage::
1377
1378 class Starship:
1379 stats: ClassVar[Dict[str, int]] = {} # class variable
1380 damage: int = 10 # instance variable
1381
1382 ClassVar accepts only types and cannot be further subscribed.
1383
1384 Note that ClassVar is not a class itself, and should not
1385 be used with isinstance() or issubclass().
1386 """
1387
Guido van Rossum4cefe742016-09-27 15:20:12 -07001388 __slots__ = ('__type__',)
1389
1390 def __init__(self, tp=None, **kwds):
1391 self.__type__ = tp
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001392
1393 def __getitem__(self, item):
1394 cls = type(self)
1395 if self.__type__ is None:
1396 return cls(_type_check(item,
Guido van Rossum4cefe742016-09-27 15:20:12 -07001397 '{} accepts only single type.'.format(cls.__name__[1:])),
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001398 _root=True)
1399 raise TypeError('{} cannot be further subscripted'
1400 .format(cls.__name__[1:]))
1401
1402 def _eval_type(self, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001403 new_tp = _eval_type(self.__type__, globalns, localns)
1404 if new_tp == self.__type__:
1405 return self
1406 return type(self)(new_tp, _root=True)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001407
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001408 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001409 r = super().__repr__()
1410 if self.__type__ is not None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001411 r += '[{}]'.format(_type_repr(self.__type__))
Guido van Rossum4cefe742016-09-27 15:20:12 -07001412 return r
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001413
1414 def __hash__(self):
1415 return hash((type(self).__name__, self.__type__))
1416
1417 def __eq__(self, other):
1418 if not isinstance(other, _ClassVar):
1419 return NotImplemented
1420 if self.__type__ is not None:
1421 return self.__type__ == other.__type__
1422 return self is other
1423
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001424
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001425ClassVar = _ClassVar(_root=True)
1426
1427
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001428def cast(typ, val):
1429 """Cast a value to a type.
1430
1431 This returns the value unchanged. To the type checker this
1432 signals that the return value has the designated type, but at
1433 runtime we intentionally don't check anything (we want this
1434 to be as fast as possible).
1435 """
1436 return val
1437
1438
1439def _get_defaults(func):
1440 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001441 try:
1442 code = func.__code__
1443 except AttributeError:
1444 # Some built-in functions don't have __code__, __defaults__, etc.
1445 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001446 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001447 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001448 arg_names = arg_names[:pos_count]
1449 defaults = func.__defaults__ or ()
1450 kwdefaults = func.__kwdefaults__
1451 res = dict(kwdefaults) if kwdefaults else {}
1452 pos_offset = pos_count - len(defaults)
1453 for name, value in zip(arg_names[pos_offset:], defaults):
1454 assert name not in res
1455 res[name] = value
1456 return res
1457
1458
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001459_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1460 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001461 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001462
1463
Guido van Rossum991d14f2016-11-09 13:12:51 -08001464def get_type_hints(obj, globalns=None, localns=None):
1465 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001466
Guido van Rossum991d14f2016-11-09 13:12:51 -08001467 This is often the same as obj.__annotations__, but it handles
1468 forward references encoded as string literals, and if necessary
1469 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001470
Guido van Rossum991d14f2016-11-09 13:12:51 -08001471 The argument may be a module, class, method, or function. The annotations
1472 are returned as a dictionary. For classes, annotations include also
1473 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001474
Guido van Rossum991d14f2016-11-09 13:12:51 -08001475 TypeError is raised if the argument is not of a type that can contain
1476 annotations, and an empty dictionary is returned if no annotations are
1477 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001478
Guido van Rossum991d14f2016-11-09 13:12:51 -08001479 BEWARE -- the behavior of globalns and localns is counterintuitive
1480 (unless you are familiar with how eval() and exec() work). The
1481 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001482
Guido van Rossum991d14f2016-11-09 13:12:51 -08001483 - If no dict arguments are passed, an attempt is made to use the
1484 globals from obj, and these are also used as the locals. If the
1485 object does not appear to have globals, an exception is raised.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001486
Guido van Rossum991d14f2016-11-09 13:12:51 -08001487 - If one dict argument is passed, it is used for both globals and
1488 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001489
Guido van Rossum991d14f2016-11-09 13:12:51 -08001490 - If two dict arguments are passed, they specify globals and
1491 locals, respectively.
1492 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001493
Guido van Rossum991d14f2016-11-09 13:12:51 -08001494 if getattr(obj, '__no_type_check__', None):
1495 return {}
1496 if globalns is None:
1497 globalns = getattr(obj, '__globals__', {})
1498 if localns is None:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001499 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001500 elif localns is None:
1501 localns = globalns
1502 # Classes require a special treatment.
1503 if isinstance(obj, type):
1504 hints = {}
1505 for base in reversed(obj.__mro__):
1506 ann = base.__dict__.get('__annotations__', {})
1507 for name, value in ann.items():
1508 if value is None:
1509 value = type(None)
1510 if isinstance(value, str):
1511 value = _ForwardRef(value)
1512 value = _eval_type(value, globalns, localns)
1513 hints[name] = value
1514 return hints
1515 hints = getattr(obj, '__annotations__', None)
1516 if hints is None:
1517 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001518 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001519 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001520 else:
1521 raise TypeError('{!r} is not a module, class, method, '
1522 'or function.'.format(obj))
1523 defaults = _get_defaults(obj)
1524 hints = dict(hints)
1525 for name, value in hints.items():
1526 if value is None:
1527 value = type(None)
1528 if isinstance(value, str):
1529 value = _ForwardRef(value)
1530 value = _eval_type(value, globalns, localns)
1531 if name in defaults and defaults[name] is None:
1532 value = Optional[value]
1533 hints[name] = value
1534 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001535
1536
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001537def no_type_check(arg):
1538 """Decorator to indicate that annotations are not type hints.
1539
1540 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001541 applies recursively to all methods and classes defined in that class
1542 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001543
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001544 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001545 """
1546 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001547 arg_attrs = arg.__dict__.copy()
1548 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001549 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001550 arg_attrs.pop(attr)
1551 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001552 if isinstance(obj, types.FunctionType):
1553 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001554 if isinstance(obj, type):
1555 no_type_check(obj)
1556 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001557 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001558 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001559 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001560 return arg
1561
1562
1563def no_type_check_decorator(decorator):
1564 """Decorator to give another decorator the @no_type_check effect.
1565
1566 This wraps the decorator with something that wraps the decorated
1567 function in @no_type_check.
1568 """
1569
1570 @functools.wraps(decorator)
1571 def wrapped_decorator(*args, **kwds):
1572 func = decorator(*args, **kwds)
1573 func = no_type_check(func)
1574 return func
1575
1576 return wrapped_decorator
1577
1578
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001579def _overload_dummy(*args, **kwds):
1580 """Helper for @overload to raise when called."""
1581 raise NotImplementedError(
1582 "You should not call an overloaded function. "
1583 "A series of @overload-decorated functions "
1584 "outside a stub module should always be followed "
1585 "by an implementation that is not @overload-ed.")
1586
1587
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001588def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001589 """Decorator for overloaded functions/methods.
1590
1591 In a stub file, place two or more stub definitions for the same
1592 function in a row, each decorated with @overload. For example:
1593
1594 @overload
1595 def utf8(value: None) -> None: ...
1596 @overload
1597 def utf8(value: bytes) -> bytes: ...
1598 @overload
1599 def utf8(value: str) -> bytes: ...
1600
1601 In a non-stub file (i.e. a regular .py file), do the same but
1602 follow it with an implementation. The implementation should *not*
1603 be decorated with @overload. For example:
1604
1605 @overload
1606 def utf8(value: None) -> None: ...
1607 @overload
1608 def utf8(value: bytes) -> bytes: ...
1609 @overload
1610 def utf8(value: str) -> bytes: ...
1611 def utf8(value):
1612 # implementation goes here
1613 """
1614 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001615
1616
1617class _ProtocolMeta(GenericMeta):
1618 """Internal metaclass for _Protocol.
1619
1620 This exists so _Protocol classes can be generic without deriving
1621 from Generic.
1622 """
1623
Guido van Rossumd70fe632015-08-05 12:11:06 +02001624 def __instancecheck__(self, obj):
Guido van Rossumca4b2522016-11-19 10:32:41 -08001625 if _Protocol not in self.__bases__:
1626 return super().__instancecheck__(obj)
Guido van Rossumd70fe632015-08-05 12:11:06 +02001627 raise TypeError("Protocols cannot be used with isinstance().")
1628
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001629 def __subclasscheck__(self, cls):
1630 if not self._is_protocol:
1631 # No structural checks since this isn't a protocol.
1632 return NotImplemented
1633
1634 if self is _Protocol:
1635 # Every class is a subclass of the empty protocol.
1636 return True
1637
1638 # Find all attributes defined in the protocol.
1639 attrs = self._get_protocol_attrs()
1640
1641 for attr in attrs:
1642 if not any(attr in d.__dict__ for d in cls.__mro__):
1643 return False
1644 return True
1645
1646 def _get_protocol_attrs(self):
1647 # Get all Protocol base classes.
1648 protocol_bases = []
1649 for c in self.__mro__:
1650 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1651 protocol_bases.append(c)
1652
1653 # Get attributes included in protocol.
1654 attrs = set()
1655 for base in protocol_bases:
1656 for attr in base.__dict__.keys():
1657 # Include attributes not defined in any non-protocol bases.
1658 for c in self.__mro__:
1659 if (c is not base and attr in c.__dict__ and
1660 not getattr(c, '_is_protocol', False)):
1661 break
1662 else:
1663 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001664 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001665 attr != '__annotations__' and
1666 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001667 attr != '_is_protocol' and
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001668 attr != '_gorg' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001669 attr != '__dict__' and
1670 attr != '__args__' and
1671 attr != '__slots__' and
1672 attr != '_get_protocol_attrs' and
1673 attr != '__next_in_mro__' and
1674 attr != '__parameters__' and
1675 attr != '__origin__' and
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001676 attr != '__orig_bases__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001677 attr != '__extra__' and
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001678 attr != '__tree_hash__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001679 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001680 attrs.add(attr)
1681
1682 return attrs
1683
1684
1685class _Protocol(metaclass=_ProtocolMeta):
1686 """Internal base class for protocol classes.
1687
Guido van Rossumb24569a2016-11-20 18:01:29 -08001688 This implements a simple-minded structural issubclass check
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001689 (similar but more general than the one-offs in collections.abc
1690 such as Hashable).
1691 """
1692
Guido van Rossumd70fe632015-08-05 12:11:06 +02001693 __slots__ = ()
1694
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001695 _is_protocol = True
1696
1697
1698# Various ABCs mimicking those in collections.abc.
1699# A few are simply re-exported for completeness.
1700
1701Hashable = collections_abc.Hashable # Not generic.
1702
1703
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001704if hasattr(collections_abc, 'Awaitable'):
1705 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1706 __slots__ = ()
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001707
1708 __all__.append('Awaitable')
1709
1710
1711if hasattr(collections_abc, 'Coroutine'):
1712 class Coroutine(Awaitable[V_co], Generic[T_co, T_contra, V_co],
1713 extra=collections_abc.Coroutine):
1714 __slots__ = ()
1715
1716 __all__.append('Coroutine')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001717
1718
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001719if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001720
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001721 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1722 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001723
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001724 class AsyncIterator(AsyncIterable[T_co],
1725 extra=collections_abc.AsyncIterator):
1726 __slots__ = ()
1727
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001728 __all__.append('AsyncIterable')
1729 __all__.append('AsyncIterator')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001730
1731
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001732class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001733 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001734
1735
1736class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001737 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001738
1739
1740class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001741 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001742
1743 @abstractmethod
1744 def __int__(self) -> int:
1745 pass
1746
1747
1748class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001749 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001750
1751 @abstractmethod
1752 def __float__(self) -> float:
1753 pass
1754
1755
1756class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001757 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001758
1759 @abstractmethod
1760 def __complex__(self) -> complex:
1761 pass
1762
1763
1764class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001765 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001766
1767 @abstractmethod
1768 def __bytes__(self) -> bytes:
1769 pass
1770
1771
Guido van Rossumd70fe632015-08-05 12:11:06 +02001772class SupportsAbs(_Protocol[T_co]):
1773 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001774
1775 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001776 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001777 pass
1778
1779
Guido van Rossumd70fe632015-08-05 12:11:06 +02001780class SupportsRound(_Protocol[T_co]):
1781 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001782
1783 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001784 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001785 pass
1786
1787
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001788if hasattr(collections_abc, 'Reversible'):
1789 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1790 __slots__ = ()
1791else:
1792 class Reversible(_Protocol[T_co]):
1793 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001794
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001795 @abstractmethod
1796 def __reversed__(self) -> 'Iterator[T_co]':
1797 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001798
1799
1800Sized = collections_abc.Sized # Not generic.
1801
1802
1803class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001804 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001805
1806
Guido van Rossumefa798d2016-08-23 11:01:50 -07001807if hasattr(collections_abc, 'Collection'):
1808 class Collection(Sized, Iterable[T_co], Container[T_co],
1809 extra=collections_abc.Collection):
1810 __slots__ = ()
1811
1812 __all__.append('Collection')
1813
1814
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001815# Callable was defined earlier.
1816
Guido van Rossumefa798d2016-08-23 11:01:50 -07001817if hasattr(collections_abc, 'Collection'):
1818 class AbstractSet(Collection[T_co],
1819 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001820 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001821else:
1822 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1823 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001824 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001825
1826
1827class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001828 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001829
1830
Guido van Rossumefa798d2016-08-23 11:01:50 -07001831# NOTE: It is only covariant in the value type.
1832if hasattr(collections_abc, 'Collection'):
1833 class Mapping(Collection[KT], Generic[KT, VT_co],
1834 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001835 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001836else:
1837 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1838 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001839 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001840
1841
1842class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001843 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001844
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001845
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001846if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001847 if hasattr(collections_abc, 'Collection'):
1848 class Sequence(Reversible[T_co], Collection[T_co],
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001849 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001850 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001851 else:
1852 class Sequence(Sized, Reversible[T_co], Container[T_co],
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001853 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001854 __slots__ = ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001855else:
1856 class Sequence(Sized, Iterable[T_co], Container[T_co],
1857 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001858 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001859
1860
1861class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001862 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001863
1864
1865class ByteString(Sequence[int], extra=collections_abc.ByteString):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001866 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001867
1868
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001869class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001870
Guido van Rossum4cefe742016-09-27 15:20:12 -07001871 __slots__ = ()
1872
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001873 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001874 if cls._gorg is List:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001875 raise TypeError("Type List cannot be instantiated; "
1876 "use list() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001877 return _generic_new(list, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001878
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001879
Raymond Hettinger80490522017-01-16 22:42:37 -08001880class Deque(collections.deque, MutableSequence[T], extra=collections.deque):
1881
1882 __slots__ = ()
1883
1884 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001885 if cls._gorg is Deque:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001886 return collections.deque(*args, **kwds)
Raymond Hettinger80490522017-01-16 22:42:37 -08001887 return _generic_new(collections.deque, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001888
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001889
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001890class Set(set, MutableSet[T], extra=set):
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):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001895 if cls._gorg is Set:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001896 raise TypeError("Type Set cannot be instantiated; "
1897 "use set() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001898 return _generic_new(set, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001899
1900
Guido van Rossum4cefe742016-09-27 15:20:12 -07001901class FrozenSet(frozenset, AbstractSet[T_co], extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001902 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001903
1904 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001905 if cls._gorg is FrozenSet:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001906 raise TypeError("Type FrozenSet cannot be instantiated; "
1907 "use frozenset() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001908 return _generic_new(frozenset, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001909
1910
1911class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001912 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001913
1914
Guido van Rossumd70fe632015-08-05 12:11:06 +02001915class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001916 extra=collections_abc.KeysView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001917 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001918
1919
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001920class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001921 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001922 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001923 extra=collections_abc.ItemsView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001924 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001925
1926
1927class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001928 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001929
1930
Brett Cannonf3ad0422016-04-15 10:51:30 -07001931if hasattr(contextlib, 'AbstractContextManager'):
1932 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1933 __slots__ = ()
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +02001934else:
1935 class ContextManager(Generic[T_co]):
1936 __slots__ = ()
1937
1938 def __enter__(self):
1939 return self
1940
1941 @abc.abstractmethod
1942 def __exit__(self, exc_type, exc_value, traceback):
1943 return None
1944
1945 @classmethod
1946 def __subclasshook__(cls, C):
1947 if cls is ContextManager:
1948 # In Python 3.6+, it is possible to set a method to None to
1949 # explicitly indicate that the class does not implement an ABC
1950 # (https://bugs.python.org/issue25958), but we do not support
1951 # that pattern here because this fallback class is only used
1952 # in Python 3.5 and earlier.
1953 if (any("__enter__" in B.__dict__ for B in C.__mro__) and
1954 any("__exit__" in B.__dict__ for B in C.__mro__)):
1955 return True
1956 return NotImplemented
1957
1958
1959if hasattr(contextlib, 'AbstractAsyncContextManager'):
1960 class AsyncContextManager(Generic[T_co],
1961 extra=contextlib.AbstractAsyncContextManager):
1962 __slots__ = ()
1963
1964 __all__.append('AsyncContextManager')
1965elif sys.version_info[:2] >= (3, 5):
1966 exec("""
1967class AsyncContextManager(Generic[T_co]):
1968 __slots__ = ()
1969
1970 async def __aenter__(self):
1971 return self
1972
1973 @abc.abstractmethod
1974 async def __aexit__(self, exc_type, exc_value, traceback):
1975 return None
1976
1977 @classmethod
1978 def __subclasshook__(cls, C):
1979 if cls is AsyncContextManager:
1980 if sys.version_info[:2] >= (3, 6):
1981 return _collections_abc._check_methods(C, "__aenter__", "__aexit__")
1982 if (any("__aenter__" in B.__dict__ for B in C.__mro__) and
1983 any("__aexit__" in B.__dict__ for B in C.__mro__)):
1984 return True
1985 return NotImplemented
1986
1987__all__.append('AsyncContextManager')
1988""")
Brett Cannonf3ad0422016-04-15 10:51:30 -07001989
1990
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001991class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001992
Guido van Rossum4cefe742016-09-27 15:20:12 -07001993 __slots__ = ()
1994
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001995 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001996 if cls._gorg is Dict:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001997 raise TypeError("Type Dict cannot be instantiated; "
1998 "use dict() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001999 return _generic_new(dict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002000
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002001
Guido van Rossum1cea70f2016-05-18 08:35:00 -07002002class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
2003 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07002004
Guido van Rossum4cefe742016-09-27 15:20:12 -07002005 __slots__ = ()
2006
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07002007 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002008 if cls._gorg is DefaultDict:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002009 return collections.defaultdict(*args, **kwds)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002010 return _generic_new(collections.defaultdict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002011
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002012
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002013class Counter(collections.Counter, Dict[T, int], extra=collections.Counter):
2014
2015 __slots__ = ()
2016
2017 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002018 if cls._gorg is Counter:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002019 return collections.Counter(*args, **kwds)
2020 return _generic_new(collections.Counter, cls, *args, **kwds)
2021
2022
2023if hasattr(collections, 'ChainMap'):
2024 # ChainMap only exists in 3.3+
2025 __all__.append('ChainMap')
2026
2027 class ChainMap(collections.ChainMap, MutableMapping[KT, VT],
2028 extra=collections.ChainMap):
2029
2030 __slots__ = ()
2031
2032 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002033 if cls._gorg is ChainMap:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002034 return collections.ChainMap(*args, **kwds)
2035 return _generic_new(collections.ChainMap, cls, *args, **kwds)
2036
2037
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002038# Determine what base class to use for Generator.
2039if hasattr(collections_abc, 'Generator'):
2040 # Sufficiently recent versions of 3.5 have a Generator ABC.
2041 _G_base = collections_abc.Generator
2042else:
2043 # Fall back on the exact type.
2044 _G_base = types.GeneratorType
2045
2046
2047class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
2048 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02002049 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002050
2051 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002052 if cls._gorg is Generator:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002053 raise TypeError("Type Generator cannot be instantiated; "
2054 "create a subclass instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002055 return _generic_new(_G_base, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002056
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002057
Guido van Rossume9ed5602017-01-18 13:10:31 -08002058if hasattr(collections_abc, 'AsyncGenerator'):
2059 class AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra],
2060 extra=collections_abc.AsyncGenerator):
2061 __slots__ = ()
2062
2063 __all__.append('AsyncGenerator')
2064
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002065
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002066# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07002067CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002068
2069
Guido van Rossumb22c7082016-05-26 09:56:19 -07002070# This is not a real generic class. Don't use outside annotations.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002071class Type(Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07002072 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002073
2074 For example, suppose we have the following classes::
2075
2076 class User: ... # Abstract base for User classes
2077 class BasicUser(User): ...
2078 class ProUser(User): ...
2079 class TeamUser(User): ...
2080
2081 And a function that takes a class argument that's a subclass of
2082 User and returns an instance of the corresponding class::
2083
2084 U = TypeVar('U', bound=User)
2085 def new_user(user_class: Type[U]) -> U:
2086 user = user_class()
2087 # (Here we could write the user object to a database)
2088 return user
2089
2090 joe = new_user(BasicUser)
2091
2092 At this point the type checker knows that joe has type BasicUser.
2093 """
2094
Guido van Rossum4cefe742016-09-27 15:20:12 -07002095 __slots__ = ()
2096
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002097
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002098def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08002099 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
2100 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002101 nm_tpl = collections.namedtuple(name, [n for n, t in types])
Guido van Rossum83ec3022017-01-17 20:43:28 -08002102 # Prior to PEP 526, only _field_types attribute was assigned.
2103 # Now, both __annotations__ and _field_types are used to maintain compatibility.
2104 nm_tpl.__annotations__ = nm_tpl._field_types = collections.OrderedDict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08002105 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002106 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08002107 except (AttributeError, ValueError):
2108 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002109 return nm_tpl
2110
2111
Guido van Rossum2f841442016-11-15 09:48:06 -08002112_PY36 = sys.version_info[:2] >= (3, 6)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002113
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002114# attributes prohibited to set in NamedTuple class syntax
2115_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
2116 '_fields', '_field_defaults', '_field_types',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02002117 '_make', '_replace', '_asdict', '_source')
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002118
2119_special = ('__module__', '__name__', '__qualname__', '__annotations__')
2120
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002121
Guido van Rossum2f841442016-11-15 09:48:06 -08002122class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002123
Guido van Rossum2f841442016-11-15 09:48:06 -08002124 def __new__(cls, typename, bases, ns):
2125 if ns.get('_root', False):
2126 return super().__new__(cls, typename, bases, ns)
2127 if not _PY36:
2128 raise TypeError("Class syntax for NamedTuple is only supported"
2129 " in Python 3.6+")
2130 types = ns.get('__annotations__', {})
Guido van Rossum3c268be2017-01-18 08:03:50 -08002131 nm_tpl = _make_nmtuple(typename, types.items())
2132 defaults = []
2133 defaults_dict = {}
2134 for field_name in types:
2135 if field_name in ns:
2136 default_value = ns[field_name]
2137 defaults.append(default_value)
2138 defaults_dict[field_name] = default_value
2139 elif defaults:
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002140 raise TypeError("Non-default namedtuple field {field_name} cannot "
2141 "follow default field(s) {default_names}"
Guido van Rossum3c268be2017-01-18 08:03:50 -08002142 .format(field_name=field_name,
2143 default_names=', '.join(defaults_dict.keys())))
2144 nm_tpl.__new__.__defaults__ = tuple(defaults)
2145 nm_tpl._field_defaults = defaults_dict
Guido van Rossum95919c02017-01-22 17:47:20 -08002146 # update from user namespace without overriding special namedtuple attributes
2147 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002148 if key in _prohibited:
2149 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2150 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002151 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002152 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002153
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002154
Guido van Rossum2f841442016-11-15 09:48:06 -08002155class NamedTuple(metaclass=NamedTupleMeta):
2156 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002157
Guido van Rossum2f841442016-11-15 09:48:06 -08002158 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002159
Guido van Rossum2f841442016-11-15 09:48:06 -08002160 class Employee(NamedTuple):
2161 name: str
2162 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002163
Guido van Rossum2f841442016-11-15 09:48:06 -08002164 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002165
Guido van Rossum2f841442016-11-15 09:48:06 -08002166 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002167
Guido van Rossum83ec3022017-01-17 20:43:28 -08002168 The resulting class has extra __annotations__ and _field_types
2169 attributes, giving an ordered dict mapping field names to types.
2170 __annotations__ should be preferred, while _field_types
2171 is kept to maintain pre PEP 526 compatibility. (The field names
Guido van Rossum2f841442016-11-15 09:48:06 -08002172 are in the _fields attribute, which is part of the namedtuple
2173 API.) Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002174
Guido van Rossum2f841442016-11-15 09:48:06 -08002175 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002176
Guido van Rossum2f841442016-11-15 09:48:06 -08002177 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002178
Guido van Rossum2f841442016-11-15 09:48:06 -08002179 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2180 """
2181 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002182
Guido van Rossum2f841442016-11-15 09:48:06 -08002183 def __new__(self, typename, fields=None, **kwargs):
2184 if kwargs and not _PY36:
2185 raise TypeError("Keyword syntax for NamedTuple is only supported"
2186 " in Python 3.6+")
2187 if fields is None:
2188 fields = kwargs.items()
2189 elif kwargs:
2190 raise TypeError("Either list of fields or keywords"
2191 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002192 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002193
2194
Guido van Rossum91185fe2016-06-08 11:19:11 -07002195def NewType(name, tp):
2196 """NewType creates simple unique types with almost zero
2197 runtime overhead. NewType(name, tp) is considered a subtype of tp
2198 by static type checkers. At runtime, NewType(name, tp) returns
2199 a dummy function that simply returns its argument. Usage::
2200
2201 UserId = NewType('UserId', int)
2202
2203 def name_by_id(user_id: UserId) -> str:
2204 ...
2205
2206 UserId('user') # Fails type check
2207
2208 name_by_id(42) # Fails type check
2209 name_by_id(UserId(42)) # OK
2210
2211 num = UserId(5) + 1 # type: int
2212 """
2213
2214 def new_type(x):
2215 return x
2216
2217 new_type.__name__ = name
2218 new_type.__supertype__ = tp
2219 return new_type
2220
2221
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002222# Python-version-specific alias (Python 2: unicode; Python 3: str)
2223Text = str
2224
2225
Guido van Rossum91185fe2016-06-08 11:19:11 -07002226# Constant that's True when type checking, but False here.
2227TYPE_CHECKING = False
2228
2229
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002230class IO(Generic[AnyStr]):
2231 """Generic base class for TextIO and BinaryIO.
2232
2233 This is an abstract, generic version of the return of open().
2234
2235 NOTE: This does not distinguish between the different possible
2236 classes (text vs. binary, read vs. write vs. read/write,
2237 append-only, unbuffered). The TextIO and BinaryIO subclasses
2238 below capture the distinctions between text vs. binary, which is
2239 pervasive in the interface; however we currently do not offer a
2240 way to track the other distinctions in the type system.
2241 """
2242
Guido van Rossumd70fe632015-08-05 12:11:06 +02002243 __slots__ = ()
2244
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002245 @abstractproperty
2246 def mode(self) -> str:
2247 pass
2248
2249 @abstractproperty
2250 def name(self) -> str:
2251 pass
2252
2253 @abstractmethod
2254 def close(self) -> None:
2255 pass
2256
2257 @abstractmethod
2258 def closed(self) -> bool:
2259 pass
2260
2261 @abstractmethod
2262 def fileno(self) -> int:
2263 pass
2264
2265 @abstractmethod
2266 def flush(self) -> None:
2267 pass
2268
2269 @abstractmethod
2270 def isatty(self) -> bool:
2271 pass
2272
2273 @abstractmethod
2274 def read(self, n: int = -1) -> AnyStr:
2275 pass
2276
2277 @abstractmethod
2278 def readable(self) -> bool:
2279 pass
2280
2281 @abstractmethod
2282 def readline(self, limit: int = -1) -> AnyStr:
2283 pass
2284
2285 @abstractmethod
2286 def readlines(self, hint: int = -1) -> List[AnyStr]:
2287 pass
2288
2289 @abstractmethod
2290 def seek(self, offset: int, whence: int = 0) -> int:
2291 pass
2292
2293 @abstractmethod
2294 def seekable(self) -> bool:
2295 pass
2296
2297 @abstractmethod
2298 def tell(self) -> int:
2299 pass
2300
2301 @abstractmethod
2302 def truncate(self, size: int = None) -> int:
2303 pass
2304
2305 @abstractmethod
2306 def writable(self) -> bool:
2307 pass
2308
2309 @abstractmethod
2310 def write(self, s: AnyStr) -> int:
2311 pass
2312
2313 @abstractmethod
2314 def writelines(self, lines: List[AnyStr]) -> None:
2315 pass
2316
2317 @abstractmethod
2318 def __enter__(self) -> 'IO[AnyStr]':
2319 pass
2320
2321 @abstractmethod
2322 def __exit__(self, type, value, traceback) -> None:
2323 pass
2324
2325
2326class BinaryIO(IO[bytes]):
2327 """Typed version of the return of open() in binary mode."""
2328
Guido van Rossumd70fe632015-08-05 12:11:06 +02002329 __slots__ = ()
2330
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002331 @abstractmethod
2332 def write(self, s: Union[bytes, bytearray]) -> int:
2333 pass
2334
2335 @abstractmethod
2336 def __enter__(self) -> 'BinaryIO':
2337 pass
2338
2339
2340class TextIO(IO[str]):
2341 """Typed version of the return of open() in text mode."""
2342
Guido van Rossumd70fe632015-08-05 12:11:06 +02002343 __slots__ = ()
2344
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002345 @abstractproperty
2346 def buffer(self) -> BinaryIO:
2347 pass
2348
2349 @abstractproperty
2350 def encoding(self) -> str:
2351 pass
2352
2353 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08002354 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002355 pass
2356
2357 @abstractproperty
2358 def line_buffering(self) -> bool:
2359 pass
2360
2361 @abstractproperty
2362 def newlines(self) -> Any:
2363 pass
2364
2365 @abstractmethod
2366 def __enter__(self) -> 'TextIO':
2367 pass
2368
2369
2370class io:
2371 """Wrapper namespace for IO generic classes."""
2372
2373 __all__ = ['IO', 'TextIO', 'BinaryIO']
2374 IO = IO
2375 TextIO = TextIO
2376 BinaryIO = BinaryIO
2377
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002378
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002379io.__name__ = __name__ + '.io'
2380sys.modules[io.__name__] = io
2381
2382
2383Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
2384 lambda p: p.pattern)
2385Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
2386 lambda m: m.re.pattern)
2387
2388
2389class re:
2390 """Wrapper namespace for re type aliases."""
2391
2392 __all__ = ['Pattern', 'Match']
2393 Pattern = Pattern
2394 Match = Match
2395
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002396
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002397re.__name__ = __name__ + '.re'
2398sys.modules[re.__name__] = re