blob: c00a3a10e1fae1b1538fbb7f15ce7a0968d5adde [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
Łukasz Langaf350a262017-09-14 14:33:00 -04001484 globals from obj (or the respective module's globals for classes),
1485 and these are also used as the locals. If the object does not appear
1486 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001487
Guido van Rossum991d14f2016-11-09 13:12:51 -08001488 - If one dict argument is passed, it is used for both globals and
1489 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001490
Guido van Rossum991d14f2016-11-09 13:12:51 -08001491 - If two dict arguments are passed, they specify globals and
1492 locals, respectively.
1493 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001494
Guido van Rossum991d14f2016-11-09 13:12:51 -08001495 if getattr(obj, '__no_type_check__', None):
1496 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001497 # Classes require a special treatment.
1498 if isinstance(obj, type):
1499 hints = {}
1500 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001501 if globalns is None:
1502 base_globals = sys.modules[base.__module__].__dict__
1503 else:
1504 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001505 ann = base.__dict__.get('__annotations__', {})
1506 for name, value in ann.items():
1507 if value is None:
1508 value = type(None)
1509 if isinstance(value, str):
1510 value = _ForwardRef(value)
Łukasz Langaf350a262017-09-14 14:33:00 -04001511 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001512 hints[name] = value
1513 return hints
Łukasz Langaf350a262017-09-14 14:33:00 -04001514
1515 if globalns is None:
1516 if isinstance(obj, types.ModuleType):
1517 globalns = obj.__dict__
1518 else:
1519 globalns = getattr(obj, '__globals__', {})
1520 if localns is None:
1521 localns = globalns
1522 elif localns is None:
1523 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001524 hints = getattr(obj, '__annotations__', None)
1525 if hints is None:
1526 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001527 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001528 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001529 else:
1530 raise TypeError('{!r} is not a module, class, method, '
1531 'or function.'.format(obj))
1532 defaults = _get_defaults(obj)
1533 hints = dict(hints)
1534 for name, value in hints.items():
1535 if value is None:
1536 value = type(None)
1537 if isinstance(value, str):
1538 value = _ForwardRef(value)
1539 value = _eval_type(value, globalns, localns)
1540 if name in defaults and defaults[name] is None:
1541 value = Optional[value]
1542 hints[name] = value
1543 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001544
1545
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001546def no_type_check(arg):
1547 """Decorator to indicate that annotations are not type hints.
1548
1549 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001550 applies recursively to all methods and classes defined in that class
1551 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001552
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001553 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001554 """
1555 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001556 arg_attrs = arg.__dict__.copy()
1557 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001558 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001559 arg_attrs.pop(attr)
1560 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001561 if isinstance(obj, types.FunctionType):
1562 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001563 if isinstance(obj, type):
1564 no_type_check(obj)
1565 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001566 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001567 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001568 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001569 return arg
1570
1571
1572def no_type_check_decorator(decorator):
1573 """Decorator to give another decorator the @no_type_check effect.
1574
1575 This wraps the decorator with something that wraps the decorated
1576 function in @no_type_check.
1577 """
1578
1579 @functools.wraps(decorator)
1580 def wrapped_decorator(*args, **kwds):
1581 func = decorator(*args, **kwds)
1582 func = no_type_check(func)
1583 return func
1584
1585 return wrapped_decorator
1586
1587
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001588def _overload_dummy(*args, **kwds):
1589 """Helper for @overload to raise when called."""
1590 raise NotImplementedError(
1591 "You should not call an overloaded function. "
1592 "A series of @overload-decorated functions "
1593 "outside a stub module should always be followed "
1594 "by an implementation that is not @overload-ed.")
1595
1596
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001597def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001598 """Decorator for overloaded functions/methods.
1599
1600 In a stub file, place two or more stub definitions for the same
1601 function in a row, each decorated with @overload. For example:
1602
1603 @overload
1604 def utf8(value: None) -> None: ...
1605 @overload
1606 def utf8(value: bytes) -> bytes: ...
1607 @overload
1608 def utf8(value: str) -> bytes: ...
1609
1610 In a non-stub file (i.e. a regular .py file), do the same but
1611 follow it with an implementation. The implementation should *not*
1612 be decorated with @overload. For example:
1613
1614 @overload
1615 def utf8(value: None) -> None: ...
1616 @overload
1617 def utf8(value: bytes) -> bytes: ...
1618 @overload
1619 def utf8(value: str) -> bytes: ...
1620 def utf8(value):
1621 # implementation goes here
1622 """
1623 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001624
1625
1626class _ProtocolMeta(GenericMeta):
1627 """Internal metaclass for _Protocol.
1628
1629 This exists so _Protocol classes can be generic without deriving
1630 from Generic.
1631 """
1632
Guido van Rossumd70fe632015-08-05 12:11:06 +02001633 def __instancecheck__(self, obj):
Guido van Rossumca4b2522016-11-19 10:32:41 -08001634 if _Protocol not in self.__bases__:
1635 return super().__instancecheck__(obj)
Guido van Rossumd70fe632015-08-05 12:11:06 +02001636 raise TypeError("Protocols cannot be used with isinstance().")
1637
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001638 def __subclasscheck__(self, cls):
1639 if not self._is_protocol:
1640 # No structural checks since this isn't a protocol.
1641 return NotImplemented
1642
1643 if self is _Protocol:
1644 # Every class is a subclass of the empty protocol.
1645 return True
1646
1647 # Find all attributes defined in the protocol.
1648 attrs = self._get_protocol_attrs()
1649
1650 for attr in attrs:
1651 if not any(attr in d.__dict__ for d in cls.__mro__):
1652 return False
1653 return True
1654
1655 def _get_protocol_attrs(self):
1656 # Get all Protocol base classes.
1657 protocol_bases = []
1658 for c in self.__mro__:
1659 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1660 protocol_bases.append(c)
1661
1662 # Get attributes included in protocol.
1663 attrs = set()
1664 for base in protocol_bases:
1665 for attr in base.__dict__.keys():
1666 # Include attributes not defined in any non-protocol bases.
1667 for c in self.__mro__:
1668 if (c is not base and attr in c.__dict__ and
1669 not getattr(c, '_is_protocol', False)):
1670 break
1671 else:
1672 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001673 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001674 attr != '__annotations__' and
1675 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001676 attr != '_is_protocol' and
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001677 attr != '_gorg' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001678 attr != '__dict__' and
1679 attr != '__args__' and
1680 attr != '__slots__' and
1681 attr != '_get_protocol_attrs' and
1682 attr != '__next_in_mro__' and
1683 attr != '__parameters__' and
1684 attr != '__origin__' and
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001685 attr != '__orig_bases__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001686 attr != '__extra__' and
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001687 attr != '__tree_hash__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001688 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001689 attrs.add(attr)
1690
1691 return attrs
1692
1693
1694class _Protocol(metaclass=_ProtocolMeta):
1695 """Internal base class for protocol classes.
1696
Guido van Rossumb24569a2016-11-20 18:01:29 -08001697 This implements a simple-minded structural issubclass check
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001698 (similar but more general than the one-offs in collections.abc
1699 such as Hashable).
1700 """
1701
Guido van Rossumd70fe632015-08-05 12:11:06 +02001702 __slots__ = ()
1703
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001704 _is_protocol = True
1705
1706
1707# Various ABCs mimicking those in collections.abc.
1708# A few are simply re-exported for completeness.
1709
1710Hashable = collections_abc.Hashable # Not generic.
1711
1712
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001713if hasattr(collections_abc, 'Awaitable'):
1714 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1715 __slots__ = ()
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001716
1717 __all__.append('Awaitable')
1718
1719
1720if hasattr(collections_abc, 'Coroutine'):
1721 class Coroutine(Awaitable[V_co], Generic[T_co, T_contra, V_co],
1722 extra=collections_abc.Coroutine):
1723 __slots__ = ()
1724
1725 __all__.append('Coroutine')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001726
1727
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001728if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001729
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001730 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1731 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001732
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001733 class AsyncIterator(AsyncIterable[T_co],
1734 extra=collections_abc.AsyncIterator):
1735 __slots__ = ()
1736
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001737 __all__.append('AsyncIterable')
1738 __all__.append('AsyncIterator')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001739
1740
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001741class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001742 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001743
1744
1745class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001746 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001747
1748
1749class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001750 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001751
1752 @abstractmethod
1753 def __int__(self) -> int:
1754 pass
1755
1756
1757class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001758 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001759
1760 @abstractmethod
1761 def __float__(self) -> float:
1762 pass
1763
1764
1765class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001766 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001767
1768 @abstractmethod
1769 def __complex__(self) -> complex:
1770 pass
1771
1772
1773class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001774 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001775
1776 @abstractmethod
1777 def __bytes__(self) -> bytes:
1778 pass
1779
1780
Guido van Rossumd70fe632015-08-05 12:11:06 +02001781class SupportsAbs(_Protocol[T_co]):
1782 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001783
1784 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001785 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001786 pass
1787
1788
Guido van Rossumd70fe632015-08-05 12:11:06 +02001789class SupportsRound(_Protocol[T_co]):
1790 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001791
1792 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001793 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001794 pass
1795
1796
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001797if hasattr(collections_abc, 'Reversible'):
1798 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1799 __slots__ = ()
1800else:
1801 class Reversible(_Protocol[T_co]):
1802 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001803
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001804 @abstractmethod
1805 def __reversed__(self) -> 'Iterator[T_co]':
1806 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001807
1808
1809Sized = collections_abc.Sized # Not generic.
1810
1811
1812class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001813 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001814
1815
Guido van Rossumefa798d2016-08-23 11:01:50 -07001816if hasattr(collections_abc, 'Collection'):
1817 class Collection(Sized, Iterable[T_co], Container[T_co],
1818 extra=collections_abc.Collection):
1819 __slots__ = ()
1820
1821 __all__.append('Collection')
1822
1823
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001824# Callable was defined earlier.
1825
Guido van Rossumefa798d2016-08-23 11:01:50 -07001826if hasattr(collections_abc, 'Collection'):
1827 class AbstractSet(Collection[T_co],
1828 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001829 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001830else:
1831 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1832 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001833 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001834
1835
1836class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001837 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001838
1839
Guido van Rossumefa798d2016-08-23 11:01:50 -07001840# NOTE: It is only covariant in the value type.
1841if hasattr(collections_abc, 'Collection'):
1842 class Mapping(Collection[KT], Generic[KT, VT_co],
1843 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001844 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001845else:
1846 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1847 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001848 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001849
1850
1851class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001852 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001853
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001854
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001855if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001856 if hasattr(collections_abc, 'Collection'):
1857 class Sequence(Reversible[T_co], Collection[T_co],
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001858 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001859 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001860 else:
1861 class Sequence(Sized, Reversible[T_co], Container[T_co],
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001862 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001863 __slots__ = ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001864else:
1865 class Sequence(Sized, Iterable[T_co], Container[T_co],
1866 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001867 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001868
1869
1870class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001871 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001872
1873
1874class ByteString(Sequence[int], extra=collections_abc.ByteString):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001875 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001876
1877
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001878class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001879
Guido van Rossum4cefe742016-09-27 15:20:12 -07001880 __slots__ = ()
1881
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001882 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001883 if cls._gorg is List:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001884 raise TypeError("Type List cannot be instantiated; "
1885 "use list() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001886 return _generic_new(list, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001887
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001888
Raymond Hettinger80490522017-01-16 22:42:37 -08001889class Deque(collections.deque, MutableSequence[T], extra=collections.deque):
1890
1891 __slots__ = ()
1892
1893 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001894 if cls._gorg is Deque:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001895 return collections.deque(*args, **kwds)
Raymond Hettinger80490522017-01-16 22:42:37 -08001896 return _generic_new(collections.deque, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001897
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001898
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001899class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001900
Guido van Rossum4cefe742016-09-27 15:20:12 -07001901 __slots__ = ()
1902
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001903 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001904 if cls._gorg is Set:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001905 raise TypeError("Type Set cannot be instantiated; "
1906 "use set() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001907 return _generic_new(set, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001908
1909
Guido van Rossum4cefe742016-09-27 15:20:12 -07001910class FrozenSet(frozenset, AbstractSet[T_co], extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001911 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001912
1913 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001914 if cls._gorg is FrozenSet:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001915 raise TypeError("Type FrozenSet cannot be instantiated; "
1916 "use frozenset() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001917 return _generic_new(frozenset, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001918
1919
1920class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001921 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001922
1923
Guido van Rossumd70fe632015-08-05 12:11:06 +02001924class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001925 extra=collections_abc.KeysView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001926 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001927
1928
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001929class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001930 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001931 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001932 extra=collections_abc.ItemsView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001933 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001934
1935
1936class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001937 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001938
1939
Brett Cannonf3ad0422016-04-15 10:51:30 -07001940if hasattr(contextlib, 'AbstractContextManager'):
1941 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1942 __slots__ = ()
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +02001943else:
1944 class ContextManager(Generic[T_co]):
1945 __slots__ = ()
1946
1947 def __enter__(self):
1948 return self
1949
1950 @abc.abstractmethod
1951 def __exit__(self, exc_type, exc_value, traceback):
1952 return None
1953
1954 @classmethod
1955 def __subclasshook__(cls, C):
1956 if cls is ContextManager:
1957 # In Python 3.6+, it is possible to set a method to None to
1958 # explicitly indicate that the class does not implement an ABC
1959 # (https://bugs.python.org/issue25958), but we do not support
1960 # that pattern here because this fallback class is only used
1961 # in Python 3.5 and earlier.
1962 if (any("__enter__" in B.__dict__ for B in C.__mro__) and
1963 any("__exit__" in B.__dict__ for B in C.__mro__)):
1964 return True
1965 return NotImplemented
1966
1967
1968if hasattr(contextlib, 'AbstractAsyncContextManager'):
1969 class AsyncContextManager(Generic[T_co],
1970 extra=contextlib.AbstractAsyncContextManager):
1971 __slots__ = ()
1972
1973 __all__.append('AsyncContextManager')
1974elif sys.version_info[:2] >= (3, 5):
1975 exec("""
1976class AsyncContextManager(Generic[T_co]):
1977 __slots__ = ()
1978
1979 async def __aenter__(self):
1980 return self
1981
1982 @abc.abstractmethod
1983 async def __aexit__(self, exc_type, exc_value, traceback):
1984 return None
1985
1986 @classmethod
1987 def __subclasshook__(cls, C):
1988 if cls is AsyncContextManager:
1989 if sys.version_info[:2] >= (3, 6):
1990 return _collections_abc._check_methods(C, "__aenter__", "__aexit__")
1991 if (any("__aenter__" in B.__dict__ for B in C.__mro__) and
1992 any("__aexit__" in B.__dict__ for B in C.__mro__)):
1993 return True
1994 return NotImplemented
1995
1996__all__.append('AsyncContextManager')
1997""")
Brett Cannonf3ad0422016-04-15 10:51:30 -07001998
1999
Guido van Rossum1cea70f2016-05-18 08:35:00 -07002000class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002001
Guido van Rossum4cefe742016-09-27 15:20:12 -07002002 __slots__ = ()
2003
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002004 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002005 if cls._gorg is Dict:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002006 raise TypeError("Type Dict cannot be instantiated; "
2007 "use dict() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002008 return _generic_new(dict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002009
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002010
Guido van Rossum1cea70f2016-05-18 08:35:00 -07002011class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
2012 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07002013
Guido van Rossum4cefe742016-09-27 15:20:12 -07002014 __slots__ = ()
2015
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07002016 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002017 if cls._gorg is DefaultDict:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002018 return collections.defaultdict(*args, **kwds)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002019 return _generic_new(collections.defaultdict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002020
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002021
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002022class Counter(collections.Counter, Dict[T, int], extra=collections.Counter):
2023
2024 __slots__ = ()
2025
2026 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002027 if cls._gorg is Counter:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002028 return collections.Counter(*args, **kwds)
2029 return _generic_new(collections.Counter, cls, *args, **kwds)
2030
2031
2032if hasattr(collections, 'ChainMap'):
2033 # ChainMap only exists in 3.3+
2034 __all__.append('ChainMap')
2035
2036 class ChainMap(collections.ChainMap, MutableMapping[KT, VT],
2037 extra=collections.ChainMap):
2038
2039 __slots__ = ()
2040
2041 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002042 if cls._gorg is ChainMap:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002043 return collections.ChainMap(*args, **kwds)
2044 return _generic_new(collections.ChainMap, cls, *args, **kwds)
2045
2046
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002047# Determine what base class to use for Generator.
2048if hasattr(collections_abc, 'Generator'):
2049 # Sufficiently recent versions of 3.5 have a Generator ABC.
2050 _G_base = collections_abc.Generator
2051else:
2052 # Fall back on the exact type.
2053 _G_base = types.GeneratorType
2054
2055
2056class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
2057 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02002058 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002059
2060 def __new__(cls, *args, **kwds):
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02002061 if cls._gorg is Generator:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002062 raise TypeError("Type Generator cannot be instantiated; "
2063 "create a subclass instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07002064 return _generic_new(_G_base, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002065
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002066
Guido van Rossume9ed5602017-01-18 13:10:31 -08002067if hasattr(collections_abc, 'AsyncGenerator'):
2068 class AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra],
2069 extra=collections_abc.AsyncGenerator):
2070 __slots__ = ()
2071
2072 __all__.append('AsyncGenerator')
2073
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002074
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002075# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07002076CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002077
2078
Guido van Rossumb22c7082016-05-26 09:56:19 -07002079# This is not a real generic class. Don't use outside annotations.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002080class Type(Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07002081 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002082
2083 For example, suppose we have the following classes::
2084
2085 class User: ... # Abstract base for User classes
2086 class BasicUser(User): ...
2087 class ProUser(User): ...
2088 class TeamUser(User): ...
2089
2090 And a function that takes a class argument that's a subclass of
2091 User and returns an instance of the corresponding class::
2092
2093 U = TypeVar('U', bound=User)
2094 def new_user(user_class: Type[U]) -> U:
2095 user = user_class()
2096 # (Here we could write the user object to a database)
2097 return user
2098
2099 joe = new_user(BasicUser)
2100
2101 At this point the type checker knows that joe has type BasicUser.
2102 """
2103
Guido van Rossum4cefe742016-09-27 15:20:12 -07002104 __slots__ = ()
2105
Guido van Rossumeb9aca32016-05-24 16:38:22 -07002106
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002107def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08002108 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
2109 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002110 nm_tpl = collections.namedtuple(name, [n for n, t in types])
Guido van Rossum83ec3022017-01-17 20:43:28 -08002111 # Prior to PEP 526, only _field_types attribute was assigned.
2112 # Now, both __annotations__ and _field_types are used to maintain compatibility.
2113 nm_tpl.__annotations__ = nm_tpl._field_types = collections.OrderedDict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08002114 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002115 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08002116 except (AttributeError, ValueError):
2117 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002118 return nm_tpl
2119
2120
Guido van Rossum2f841442016-11-15 09:48:06 -08002121_PY36 = sys.version_info[:2] >= (3, 6)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002122
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002123# attributes prohibited to set in NamedTuple class syntax
2124_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__',
2125 '_fields', '_field_defaults', '_field_types',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02002126 '_make', '_replace', '_asdict', '_source')
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002127
2128_special = ('__module__', '__name__', '__qualname__', '__annotations__')
2129
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002130
Guido van Rossum2f841442016-11-15 09:48:06 -08002131class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002132
Guido van Rossum2f841442016-11-15 09:48:06 -08002133 def __new__(cls, typename, bases, ns):
2134 if ns.get('_root', False):
2135 return super().__new__(cls, typename, bases, ns)
2136 if not _PY36:
2137 raise TypeError("Class syntax for NamedTuple is only supported"
2138 " in Python 3.6+")
2139 types = ns.get('__annotations__', {})
Guido van Rossum3c268be2017-01-18 08:03:50 -08002140 nm_tpl = _make_nmtuple(typename, types.items())
2141 defaults = []
2142 defaults_dict = {}
2143 for field_name in types:
2144 if field_name in ns:
2145 default_value = ns[field_name]
2146 defaults.append(default_value)
2147 defaults_dict[field_name] = default_value
2148 elif defaults:
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002149 raise TypeError("Non-default namedtuple field {field_name} cannot "
2150 "follow default field(s) {default_names}"
Guido van Rossum3c268be2017-01-18 08:03:50 -08002151 .format(field_name=field_name,
2152 default_names=', '.join(defaults_dict.keys())))
2153 nm_tpl.__new__.__defaults__ = tuple(defaults)
2154 nm_tpl._field_defaults = defaults_dict
Guido van Rossum95919c02017-01-22 17:47:20 -08002155 # update from user namespace without overriding special namedtuple attributes
2156 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002157 if key in _prohibited:
2158 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2159 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002160 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002161 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002162
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002163
Guido van Rossum2f841442016-11-15 09:48:06 -08002164class NamedTuple(metaclass=NamedTupleMeta):
2165 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002166
Guido van Rossum2f841442016-11-15 09:48:06 -08002167 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002168
Guido van Rossum2f841442016-11-15 09:48:06 -08002169 class Employee(NamedTuple):
2170 name: str
2171 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002172
Guido van Rossum2f841442016-11-15 09:48:06 -08002173 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002174
Guido van Rossum2f841442016-11-15 09:48:06 -08002175 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002176
Guido van Rossum83ec3022017-01-17 20:43:28 -08002177 The resulting class has extra __annotations__ and _field_types
2178 attributes, giving an ordered dict mapping field names to types.
2179 __annotations__ should be preferred, while _field_types
2180 is kept to maintain pre PEP 526 compatibility. (The field names
Guido van Rossum2f841442016-11-15 09:48:06 -08002181 are in the _fields attribute, which is part of the namedtuple
2182 API.) Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002183
Guido van Rossum2f841442016-11-15 09:48:06 -08002184 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002185
Guido van Rossum2f841442016-11-15 09:48:06 -08002186 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002187
Guido van Rossum2f841442016-11-15 09:48:06 -08002188 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2189 """
2190 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002191
Guido van Rossum2f841442016-11-15 09:48:06 -08002192 def __new__(self, typename, fields=None, **kwargs):
2193 if kwargs and not _PY36:
2194 raise TypeError("Keyword syntax for NamedTuple is only supported"
2195 " in Python 3.6+")
2196 if fields is None:
2197 fields = kwargs.items()
2198 elif kwargs:
2199 raise TypeError("Either list of fields or keywords"
2200 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002201 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002202
2203
Guido van Rossum91185fe2016-06-08 11:19:11 -07002204def NewType(name, tp):
2205 """NewType creates simple unique types with almost zero
2206 runtime overhead. NewType(name, tp) is considered a subtype of tp
2207 by static type checkers. At runtime, NewType(name, tp) returns
2208 a dummy function that simply returns its argument. Usage::
2209
2210 UserId = NewType('UserId', int)
2211
2212 def name_by_id(user_id: UserId) -> str:
2213 ...
2214
2215 UserId('user') # Fails type check
2216
2217 name_by_id(42) # Fails type check
2218 name_by_id(UserId(42)) # OK
2219
2220 num = UserId(5) + 1 # type: int
2221 """
2222
2223 def new_type(x):
2224 return x
2225
2226 new_type.__name__ = name
2227 new_type.__supertype__ = tp
2228 return new_type
2229
2230
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002231# Python-version-specific alias (Python 2: unicode; Python 3: str)
2232Text = str
2233
2234
Guido van Rossum91185fe2016-06-08 11:19:11 -07002235# Constant that's True when type checking, but False here.
2236TYPE_CHECKING = False
2237
2238
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002239class IO(Generic[AnyStr]):
2240 """Generic base class for TextIO and BinaryIO.
2241
2242 This is an abstract, generic version of the return of open().
2243
2244 NOTE: This does not distinguish between the different possible
2245 classes (text vs. binary, read vs. write vs. read/write,
2246 append-only, unbuffered). The TextIO and BinaryIO subclasses
2247 below capture the distinctions between text vs. binary, which is
2248 pervasive in the interface; however we currently do not offer a
2249 way to track the other distinctions in the type system.
2250 """
2251
Guido van Rossumd70fe632015-08-05 12:11:06 +02002252 __slots__ = ()
2253
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002254 @abstractproperty
2255 def mode(self) -> str:
2256 pass
2257
2258 @abstractproperty
2259 def name(self) -> str:
2260 pass
2261
2262 @abstractmethod
2263 def close(self) -> None:
2264 pass
2265
2266 @abstractmethod
2267 def closed(self) -> bool:
2268 pass
2269
2270 @abstractmethod
2271 def fileno(self) -> int:
2272 pass
2273
2274 @abstractmethod
2275 def flush(self) -> None:
2276 pass
2277
2278 @abstractmethod
2279 def isatty(self) -> bool:
2280 pass
2281
2282 @abstractmethod
2283 def read(self, n: int = -1) -> AnyStr:
2284 pass
2285
2286 @abstractmethod
2287 def readable(self) -> bool:
2288 pass
2289
2290 @abstractmethod
2291 def readline(self, limit: int = -1) -> AnyStr:
2292 pass
2293
2294 @abstractmethod
2295 def readlines(self, hint: int = -1) -> List[AnyStr]:
2296 pass
2297
2298 @abstractmethod
2299 def seek(self, offset: int, whence: int = 0) -> int:
2300 pass
2301
2302 @abstractmethod
2303 def seekable(self) -> bool:
2304 pass
2305
2306 @abstractmethod
2307 def tell(self) -> int:
2308 pass
2309
2310 @abstractmethod
2311 def truncate(self, size: int = None) -> int:
2312 pass
2313
2314 @abstractmethod
2315 def writable(self) -> bool:
2316 pass
2317
2318 @abstractmethod
2319 def write(self, s: AnyStr) -> int:
2320 pass
2321
2322 @abstractmethod
2323 def writelines(self, lines: List[AnyStr]) -> None:
2324 pass
2325
2326 @abstractmethod
2327 def __enter__(self) -> 'IO[AnyStr]':
2328 pass
2329
2330 @abstractmethod
2331 def __exit__(self, type, value, traceback) -> None:
2332 pass
2333
2334
2335class BinaryIO(IO[bytes]):
2336 """Typed version of the return of open() in binary mode."""
2337
Guido van Rossumd70fe632015-08-05 12:11:06 +02002338 __slots__ = ()
2339
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002340 @abstractmethod
2341 def write(self, s: Union[bytes, bytearray]) -> int:
2342 pass
2343
2344 @abstractmethod
2345 def __enter__(self) -> 'BinaryIO':
2346 pass
2347
2348
2349class TextIO(IO[str]):
2350 """Typed version of the return of open() in text mode."""
2351
Guido van Rossumd70fe632015-08-05 12:11:06 +02002352 __slots__ = ()
2353
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002354 @abstractproperty
2355 def buffer(self) -> BinaryIO:
2356 pass
2357
2358 @abstractproperty
2359 def encoding(self) -> str:
2360 pass
2361
2362 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08002363 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002364 pass
2365
2366 @abstractproperty
2367 def line_buffering(self) -> bool:
2368 pass
2369
2370 @abstractproperty
2371 def newlines(self) -> Any:
2372 pass
2373
2374 @abstractmethod
2375 def __enter__(self) -> 'TextIO':
2376 pass
2377
2378
2379class io:
2380 """Wrapper namespace for IO generic classes."""
2381
2382 __all__ = ['IO', 'TextIO', 'BinaryIO']
2383 IO = IO
2384 TextIO = TextIO
2385 BinaryIO = BinaryIO
2386
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002387
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002388io.__name__ = __name__ + '.io'
2389sys.modules[io.__name__] = io
2390
2391
2392Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
2393 lambda p: p.pattern)
2394Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
2395 lambda m: m.re.pattern)
2396
2397
2398class re:
2399 """Wrapper namespace for re type aliases."""
2400
2401 __all__ = ['Pattern', 'Match']
2402 Pattern = Pattern
2403 Match = Match
2404
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002405
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002406re.__name__ = __name__ + '.re'
2407sys.modules[re.__name__] = re