blob: 925d9e42d3c5a2602f8721926ab76f6caba7c880 [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.
Guido van Rossum0a6976d2016-09-11 15:34:56 -070013if sys.version_info[:2] >= (3, 3):
14 from collections import ChainMap
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070015
16
17# Please keep __all__ alphabetized within each category.
18__all__ = [
19 # Super-special typing primitives.
20 'Any',
21 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070022 'ClassVar',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070023 'Generic',
24 'Optional',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070025 'Tuple',
26 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070027 'TypeVar',
28 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070029
30 # ABCs (from collections.abc).
31 'AbstractSet', # collections.abc.Set.
Guido van Rossumf17c2002015-12-03 17:31:24 -080032 'Awaitable',
33 'AsyncIterator',
34 'AsyncIterable',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070035 'ByteString',
36 'Container',
37 'Hashable',
38 'ItemsView',
39 'Iterable',
40 'Iterator',
41 'KeysView',
42 'Mapping',
43 'MappingView',
44 'MutableMapping',
45 'MutableSequence',
46 'MutableSet',
47 'Sequence',
48 'Sized',
49 'ValuesView',
50
51 # Structural checks, a.k.a. protocols.
52 'Reversible',
53 'SupportsAbs',
54 'SupportsFloat',
55 'SupportsInt',
56 'SupportsRound',
57
58 # Concrete collection types.
59 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070060 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070061 'List',
62 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070063 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070064 'NamedTuple', # Not really a type.
65 'Generator',
66
67 # One-off things.
68 'AnyStr',
69 'cast',
70 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -070071 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070072 'no_type_check',
73 'no_type_check_decorator',
74 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -070075 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -070076 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070077]
78
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070079# The pseudo-submodules 're' and 'io' are part of the public
80# namespace, but excluded from __all__ because they might stomp on
81# legitimate imports of those modules.
82
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070083
84def _qualname(x):
85 if sys.version_info[:2] >= (3, 3):
86 return x.__qualname__
87 else:
88 # Fall back to just name.
89 return x.__name__
90
91
Guido van Rossum4cefe742016-09-27 15:20:12 -070092def _trim_name(nm):
93 if nm.startswith('_') and nm not in ('_TypeAlias',
94 '_ForwardRef', '_TypingBase', '_FinalTypingBase'):
95 nm = nm[1:]
96 return nm
97
98
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070099class TypingMeta(type):
100 """Metaclass for every type defined below.
101
102 This overrides __new__() to require an extra keyword parameter
103 '_root', which serves as a guard against naive subclassing of the
104 typing classes. Any legitimate class defined using a metaclass
105 derived from TypingMeta (including internal subclasses created by
106 e.g. Union[X, Y]) must pass _root=True.
107
108 This also defines a dummy constructor (all the work is done in
109 __new__) and a nicer repr().
110 """
111
112 _is_protocol = False
113
114 def __new__(cls, name, bases, namespace, *, _root=False):
115 if not _root:
116 raise TypeError("Cannot subclass %s" %
117 (', '.join(map(_type_repr, bases)) or '()'))
118 return super().__new__(cls, name, bases, namespace)
119
120 def __init__(self, *args, **kwds):
121 pass
122
123 def _eval_type(self, globalns, localns):
124 """Override this in subclasses to interpret forward references.
125
126 For example, Union['C'] is internally stored as
127 Union[_ForwardRef('C')], which should evaluate to _Union[C],
128 where C is an object found in globalns or localns (searching
129 localns first, of course).
130 """
131 return self
132
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700133 def _get_type_vars(self, tvars):
134 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700135
136 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700137 qname = _trim_name(_qualname(self))
138 return '%s.%s' % (self.__module__, qname)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700139
140
Guido van Rossum4cefe742016-09-27 15:20:12 -0700141class _TypingBase(metaclass=TypingMeta, _root=True):
142 """Indicator of special typing constructs."""
143
144 __slots__ = ()
145
146
147 def __init__(self, *args, **kwds):
148 pass
149
150 def __new__(cls, *args, **kwds):
151 """Constructor.
152
153 This only exists to give a better error message in case
154 someone tries to subclass a special typing object (not a good idea).
155 """
156 if (len(args) == 3 and
157 isinstance(args[0], str) and
158 isinstance(args[1], tuple)):
159 # Close enough.
160 raise TypeError("Cannot subclass %r" % cls)
161 return object.__new__(cls)
162
163 # Things that are not classes also need these.
164 def _eval_type(self, globalns, localns):
165 return self
166
167 def _get_type_vars(self, tvars):
168 pass
169
170 def __repr__(self):
171 cls = type(self)
172 qname = _trim_name(_qualname(cls))
173 return '%s.%s' % (cls.__module__, qname)
174
175 def __call__(self, *args, **kwds):
176 raise TypeError("Cannot instantiate %r" % type(self))
177
178
179class _FinalTypingBase(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700180 """Mix-in class to prevent instantiation."""
181
Guido van Rossumd70fe632015-08-05 12:11:06 +0200182 __slots__ = ()
183
Guido van Rossum4cefe742016-09-27 15:20:12 -0700184 def __new__(cls, *args, _root=False, **kwds):
185 self = super().__new__(cls, *args, **kwds)
186 if _root is True:
187 return self
188 raise TypeError("Cannot instantiate %r" % cls)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700189
190
Guido van Rossum4cefe742016-09-27 15:20:12 -0700191class _ForwardRef(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700192 """Wrapper to hold a forward reference."""
193
Guido van Rossum4cefe742016-09-27 15:20:12 -0700194 __slots__ = ('__forward_arg__', '__forward_code__',
195 '__forward_evaluated__', '__forward_value__',
196 '__forward_frame__')
197
198 def __init__(self, arg):
199 super().__init__(arg)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700200 if not isinstance(arg, str):
201 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
202 try:
203 code = compile(arg, '<string>', 'eval')
204 except SyntaxError:
205 raise SyntaxError('ForwardRef must be an expression -- got %r' %
206 (arg,))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700207 self.__forward_arg__ = arg
208 self.__forward_code__ = code
209 self.__forward_evaluated__ = False
210 self.__forward_value__ = None
211 typing_globals = globals()
212 frame = sys._getframe(1)
213 while frame is not None and frame.f_globals is typing_globals:
214 frame = frame.f_back
215 assert frame is not None
216 self.__forward_frame__ = frame
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700217
218 def _eval_type(self, globalns, localns):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700219 if not self.__forward_evaluated__:
220 if globalns is None and localns is None:
221 globalns = localns = {}
222 elif globalns is None:
223 globalns = localns
224 elif localns is None:
225 localns = globalns
226 self.__forward_value__ = _type_check(
227 eval(self.__forward_code__, globalns, localns),
228 "Forward references must evaluate to types.")
229 self.__forward_evaluated__ = True
230 return self.__forward_value__
231
Guido van Rossum4cefe742016-09-27 15:20:12 -0700232 def __eq__(self, other):
233 if not isinstance(other, _ForwardRef):
234 return NotImplemented
235 return (self.__forward_arg__ == other.__forward_arg__ and
236 self.__forward_frame__ == other.__forward_frame__)
237
238 def __hash__(self):
239 return hash((self.__forward_arg__, self.__forward_frame__))
240
Guido van Rossumd70fe632015-08-05 12:11:06 +0200241 def __instancecheck__(self, obj):
242 raise TypeError("Forward references cannot be used with isinstance().")
243
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700244 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700245 raise TypeError("Forward references cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700246
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700247 def __repr__(self):
248 return '_ForwardRef(%r)' % (self.__forward_arg__,)
249
250
Guido van Rossum4cefe742016-09-27 15:20:12 -0700251class _TypeAlias(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700252 """Internal helper class for defining generic variants of concrete types.
253
Guido van Rossum4cefe742016-09-27 15:20:12 -0700254 Note that this is not a type; let's call it a pseudo-type. It cannot
255 be used in instance and subclass checks in parameterized form, i.e.
256 ``isinstance(42, Match[str])`` raises ``TypeError`` instead of returning
257 ``False``.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700258 """
259
Guido van Rossumd70fe632015-08-05 12:11:06 +0200260 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
261
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700262 def __init__(self, name, type_var, impl_type, type_checker):
263 """Initializer.
264
265 Args:
266 name: The name, e.g. 'Pattern'.
267 type_var: The type parameter, e.g. AnyStr, or the
268 specific type, e.g. str.
269 impl_type: The implementation type.
270 type_checker: Function that takes an impl_type instance.
271 and returns a value that should be a type_var instance.
272 """
273 assert isinstance(name, str), repr(name)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700274 assert isinstance(impl_type, type), repr(impl_type)
275 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700276 assert isinstance(type_var, (type, _TypingBase))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700277 self.name = name
278 self.type_var = type_var
279 self.impl_type = impl_type
280 self.type_checker = type_checker
281
282 def __repr__(self):
283 return "%s[%s]" % (self.name, _type_repr(self.type_var))
284
285 def __getitem__(self, parameter):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700286 if not isinstance(self.type_var, TypeVar):
287 raise TypeError("%s cannot be further parameterized." % self)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700288 if self.type_var.__constraints__ and isinstance(parameter, type):
289 if not issubclass(parameter, self.type_var.__constraints__):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700290 raise TypeError("%s is not a valid substitution for %s." %
291 (parameter, self.type_var))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700292 if isinstance(parameter, TypeVar):
293 raise TypeError("%s cannot be re-parameterized." % self.type_var)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700294 return self.__class__(self.name, parameter,
295 self.impl_type, self.type_checker)
296
Guido van Rossum4cefe742016-09-27 15:20:12 -0700297 def __eq__(self, other):
298 if not isinstance(other, _TypeAlias):
299 return NotImplemented
300 return self.name == other.name and self.type_var == other.type_var
301
302 def __hash__(self):
303 return hash((self.name, self.type_var))
304
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700305 def __instancecheck__(self, obj):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700306 if not isinstance(self.type_var, TypeVar):
307 raise TypeError("Parameterized type aliases cannot be used "
308 "with isinstance().")
309 return isinstance(obj, self.impl_type)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700310
311 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700312 if not isinstance(self.type_var, TypeVar):
313 raise TypeError("Parameterized type aliases cannot be used "
314 "with issubclass().")
315 return issubclass(cls, self.impl_type)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700316
317
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700318def _get_type_vars(types, tvars):
319 for t in types:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700320 if isinstance(t, TypingMeta) or isinstance(t, _TypingBase):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700321 t._get_type_vars(tvars)
322
323
324def _type_vars(types):
325 tvars = []
326 _get_type_vars(types, tvars)
327 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700328
329
330def _eval_type(t, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700331 if isinstance(t, TypingMeta) or isinstance(t, _TypingBase):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700332 return t._eval_type(globalns, localns)
333 else:
334 return t
335
336
337def _type_check(arg, msg):
338 """Check that the argument is a type, and return it.
339
340 As a special case, accept None and return type(None) instead.
341 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
342
343 The msg argument is a human-readable error message, e.g.
344
345 "Union[arg, ...]: arg should be a type."
346
347 We append the repr() of the actual value (truncated to 100 chars).
348 """
349 if arg is None:
350 return type(None)
351 if isinstance(arg, str):
352 arg = _ForwardRef(arg)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700353 if not isinstance(arg, (type, _TypingBase)) and not callable(arg):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700354 raise TypeError(msg + " Got %.100r." % (arg,))
355 return arg
356
357
358def _type_repr(obj):
359 """Return the repr() of an object, special-casing types.
360
361 If obj is a type, we return a shorter version than the default
362 type.__repr__, based on the module and qualified name, which is
363 typically enough to uniquely identify a type. For everything
364 else, we fall back on repr(obj).
365 """
366 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
367 if obj.__module__ == 'builtins':
368 return _qualname(obj)
369 else:
370 return '%s.%s' % (obj.__module__, _qualname(obj))
371 else:
372 return repr(obj)
373
374
Guido van Rossum4cefe742016-09-27 15:20:12 -0700375class _Any(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700376 """Special type indicating an unconstrained type.
377
378 - Any object is an instance of Any.
379 - Any class is a subclass of Any.
380 - As a special case, Any and object are subclasses of each other.
381 """
382
Guido van Rossumd70fe632015-08-05 12:11:06 +0200383 __slots__ = ()
384
Guido van Rossum4cefe742016-09-27 15:20:12 -0700385 def __instancecheck__(self, obj):
386 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700387
Guido van Rossum4cefe742016-09-27 15:20:12 -0700388 def __subclasscheck__(self, cls):
389 raise TypeError("Any cannot be used with issubclass().")
390
391
392Any = _Any(_root=True)
393
394
395class TypeVar(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700396 """Type variable.
397
398 Usage::
399
400 T = TypeVar('T') # Can be anything
401 A = TypeVar('A', str, bytes) # Must be str or bytes
402
403 Type variables exist primarily for the benefit of static type
404 checkers. They serve as the parameters for generic types as well
405 as for generic function definitions. See class Generic for more
406 information on generic types. Generic functions work as follows:
407
408 def repeat(x: T, n: int) -> Sequence[T]:
409 '''Return a list containing n references to x.'''
410 return [x]*n
411
412 def longest(x: A, y: A) -> A:
413 '''Return the longest of two strings.'''
414 return x if len(x) >= len(y) else y
415
416 The latter example's signature is essentially the overloading
417 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
418 that if the arguments are instances of some subclass of str,
419 the return type is still plain str.
420
421 At runtime, isinstance(x, T) will raise TypeError. However,
422 issubclass(C, T) is true for any class C, and issubclass(str, A)
423 and issubclass(bytes, A) are true, and issubclass(int, A) is
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700424 false. (TODO: Why is this needed? This may change. See #136.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700425
Guido van Rossumefa798d2016-08-23 11:01:50 -0700426 Type variables defined with covariant=True or contravariant=True
427 can be used do declare covariant or contravariant generic types.
428 See PEP 484 for more details. By default generic types are invariant
429 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700430
431 Type variables can be introspected. e.g.:
432
433 T.__name__ == 'T'
434 T.__constraints__ == ()
435 T.__covariant__ == False
436 T.__contravariant__ = False
437 A.__constraints__ == (str, bytes)
438 """
439
Guido van Rossum4cefe742016-09-27 15:20:12 -0700440 __slots__ = ('__name__', '__bound__', '__constraints__',
441 '__covariant__', '__contravariant__')
442
443 def __init__(self, name, *constraints, bound=None,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700444 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700445 super().__init__(name, *constraints, bound=bound,
446 covariant=covariant, contravariant=contravariant)
447 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700448 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700449 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700450 self.__covariant__ = bool(covariant)
451 self.__contravariant__ = bool(contravariant)
452 if constraints and bound is not None:
453 raise TypeError("Constraints cannot be combined with bound=...")
454 if constraints and len(constraints) == 1:
455 raise TypeError("A single constraint is not allowed")
456 msg = "TypeVar(name, constraint, ...): constraints must be types."
457 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
458 if bound:
459 self.__bound__ = _type_check(bound, "Bound must be a type.")
460 else:
461 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700462
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700463 def _get_type_vars(self, tvars):
464 if self not in tvars:
465 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700466
467 def __repr__(self):
468 if self.__covariant__:
469 prefix = '+'
470 elif self.__contravariant__:
471 prefix = '-'
472 else:
473 prefix = '~'
474 return prefix + self.__name__
475
476 def __instancecheck__(self, instance):
477 raise TypeError("Type variables cannot be used with isinstance().")
478
479 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700480 raise TypeError("Type variables cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700481
482
483# Some unconstrained type variables. These are used by the container types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700484# (These are not for export.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700485T = TypeVar('T') # Any type.
486KT = TypeVar('KT') # Key type.
487VT = TypeVar('VT') # Value type.
488T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
489V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700490VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
491T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
492
493# A useful type variable with constraints. This represents string types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700494# (This one *is* for export!)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700495AnyStr = TypeVar('AnyStr', bytes, str)
496
497
Guido van Rossum4cefe742016-09-27 15:20:12 -0700498def _tp_cache(func):
499 cached = functools.lru_cache()(func)
500 @functools.wraps(func)
501 def inner(*args, **kwds):
502 try:
503 return cached(*args, **kwds)
504 except TypeError:
505 pass # Do not duplicate real errors.
506 return func(*args, **kwds)
507 return inner
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700508
509
Guido van Rossum4cefe742016-09-27 15:20:12 -0700510class _Union(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700511 """Union type; Union[X, Y] means either X or Y.
512
513 To define a union, use e.g. Union[int, str]. Details:
514
515 - The arguments must be types and there must be at least one.
516
517 - None as an argument is a special case and is replaced by
518 type(None).
519
520 - Unions of unions are flattened, e.g.::
521
522 Union[Union[int, str], float] == Union[int, str, float]
523
524 - Unions of a single argument vanish, e.g.::
525
526 Union[int] == int # The constructor actually returns int
527
528 - Redundant arguments are skipped, e.g.::
529
530 Union[int, str, int] == Union[int, str]
531
532 - When comparing unions, the argument order is ignored, e.g.::
533
534 Union[int, str] == Union[str, int]
535
536 - When two arguments have a subclass relationship, the least
537 derived argument is kept, e.g.::
538
539 class Employee: pass
540 class Manager(Employee): pass
541 Union[int, Employee, Manager] == Union[int, Employee]
542 Union[Manager, int, Employee] == Union[int, Employee]
543 Union[Employee, Manager] == Employee
544
545 - Corollary: if Any is present it is the sole survivor, e.g.::
546
547 Union[int, Any] == Any
548
549 - Similar for object::
550
551 Union[int, object] == object
552
553 - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
554
555 - You cannot subclass or instantiate a union.
556
557 - You cannot write Union[X][Y] (what would it mean?).
558
559 - You can use Optional[X] as a shorthand for Union[X, None].
560 """
561
Guido van Rossum4cefe742016-09-27 15:20:12 -0700562 __slots__ = ('__union_params__', '__union_set_params__')
563
564 def __new__(cls, parameters=None, *args, _root=False):
565 self = super().__new__(cls, parameters, *args, _root=_root)
566 if parameters is None:
567 self.__union_params__ = None
568 self.__union_set_params__ = None
569 return self
570 if not isinstance(parameters, tuple):
571 raise TypeError("Expected parameters=<tuple>")
572 # Flatten out Union[Union[...], ...] and type-check non-Union args.
573 params = []
574 msg = "Union[arg, ...]: each arg must be a type."
575 for p in parameters:
576 if isinstance(p, _Union):
577 params.extend(p.__union_params__)
578 else:
579 params.append(_type_check(p, msg))
580 # Weed out strict duplicates, preserving the first of each occurrence.
581 all_params = set(params)
582 if len(all_params) < len(params):
583 new_params = []
584 for t in params:
585 if t in all_params:
586 new_params.append(t)
587 all_params.remove(t)
588 params = new_params
589 assert not all_params, all_params
590 # Weed out subclasses.
591 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
592 # If Any or object is present it will be the sole survivor.
593 # If both Any and object are present, Any wins.
594 # Never discard type variables, except against Any.
595 # (In particular, Union[str, AnyStr] != AnyStr.)
596 all_params = set(params)
597 for t1 in params:
598 if t1 is Any:
599 return Any
600 if not isinstance(t1, type):
601 continue
602 if any(isinstance(t2, type) and issubclass(t1, t2)
603 for t2 in all_params - {t1}
604 if not (isinstance(t2, GenericMeta) and
605 t2.__origin__ is not None)):
606 all_params.remove(t1)
607 # It's not a union if there's only one type left.
608 if len(all_params) == 1:
609 return all_params.pop()
610 self.__union_params__ = tuple(t for t in params if t in all_params)
611 self.__union_set_params__ = frozenset(self.__union_params__)
612 return self
613
614 def _eval_type(self, globalns, localns):
615 p = tuple(_eval_type(t, globalns, localns)
616 for t in self.__union_params__)
617 if p == self.__union_params__:
618 return self
619 else:
620 return self.__class__(p, _root=True)
621
622 def _get_type_vars(self, tvars):
623 if self.__union_params__:
624 _get_type_vars(self.__union_params__, tvars)
625
626 def __repr__(self):
627 r = super().__repr__()
628 if self.__union_params__:
629 r += '[%s]' % (', '.join(_type_repr(t)
630 for t in self.__union_params__))
631 return r
632
633 @_tp_cache
634 def __getitem__(self, parameters):
635 if self.__union_params__ is not None:
636 raise TypeError(
637 "Cannot subscript an existing Union. Use Union[u, t] instead.")
638 if parameters == ():
639 raise TypeError("Cannot take a Union of no types.")
640 if not isinstance(parameters, tuple):
641 parameters = (parameters,)
642 return self.__class__(parameters, _root=True)
643
644 def __eq__(self, other):
645 if not isinstance(other, _Union):
646 return NotImplemented
647 return self.__union_set_params__ == other.__union_set_params__
648
649 def __hash__(self):
650 return hash(self.__union_set_params__)
651
652 def __instancecheck__(self, obj):
653 raise TypeError("Unions cannot be used with isinstance().")
654
655 def __subclasscheck__(self, cls):
656 raise TypeError("Unions cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700657
658
Guido van Rossum4cefe742016-09-27 15:20:12 -0700659Union = _Union(_root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700660
661
Guido van Rossum4cefe742016-09-27 15:20:12 -0700662class _Optional(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700663 """Optional type.
664
665 Optional[X] is equivalent to Union[X, type(None)].
666 """
667
Guido van Rossumd70fe632015-08-05 12:11:06 +0200668 __slots__ = ()
669
Guido van Rossum4cefe742016-09-27 15:20:12 -0700670 @_tp_cache
671 def __getitem__(self, arg):
672 arg = _type_check(arg, "Optional[t] requires a single type.")
673 return Union[arg, type(None)]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700674
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700675
Guido van Rossum4cefe742016-09-27 15:20:12 -0700676Optional = _Optional(_root=True)
677
678
679class _Tuple(_FinalTypingBase, _root=True):
680 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
681
682 Example: Tuple[T1, T2] is a tuple of two elements corresponding
683 to type variables T1 and T2. Tuple[int, float, str] is a tuple
684 of an int, a float and a string.
685
686 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
687 """
688
689 __slots__ = ('__tuple_params__', '__tuple_use_ellipsis__')
690
691 def __init__(self, parameters=None,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700692 use_ellipsis=False, _root=False):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700693 self.__tuple_params__ = parameters
694 self.__tuple_use_ellipsis__ = use_ellipsis
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700695
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700696 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700697 if self.__tuple_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700698 _get_type_vars(self.__tuple_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700699
700 def _eval_type(self, globalns, localns):
701 tp = self.__tuple_params__
702 if tp is None:
703 return self
704 p = tuple(_eval_type(t, globalns, localns) for t in tp)
705 if p == self.__tuple_params__:
706 return self
707 else:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700708 return self.__class__(p, _root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700709
710 def __repr__(self):
711 r = super().__repr__()
712 if self.__tuple_params__ is not None:
713 params = [_type_repr(p) for p in self.__tuple_params__]
714 if self.__tuple_use_ellipsis__:
715 params.append('...')
Guido van Rossum91185fe2016-06-08 11:19:11 -0700716 if not params:
717 params.append('()')
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700718 r += '[%s]' % (
719 ', '.join(params))
720 return r
721
Guido van Rossum4cefe742016-09-27 15:20:12 -0700722 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700723 def __getitem__(self, parameters):
724 if self.__tuple_params__ is not None:
725 raise TypeError("Cannot re-parameterize %r" % (self,))
726 if not isinstance(parameters, tuple):
727 parameters = (parameters,)
728 if len(parameters) == 2 and parameters[1] == Ellipsis:
729 parameters = parameters[:1]
730 use_ellipsis = True
731 msg = "Tuple[t, ...]: t must be a type."
732 else:
733 use_ellipsis = False
734 msg = "Tuple[t0, t1, ...]: each t must be a type."
735 parameters = tuple(_type_check(p, msg) for p in parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700736 return self.__class__(parameters,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700737 use_ellipsis=use_ellipsis, _root=True)
738
739 def __eq__(self, other):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700740 if not isinstance(other, _Tuple):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700741 return NotImplemented
Guido van Rossum5abcbb32016-04-18 07:37:41 -0700742 return (self.__tuple_params__ == other.__tuple_params__ and
743 self.__tuple_use_ellipsis__ == other.__tuple_use_ellipsis__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700744
745 def __hash__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700746 return hash((self.__tuple_params__, self.__tuple_use_ellipsis__))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700747
Guido van Rossumd70fe632015-08-05 12:11:06 +0200748 def __instancecheck__(self, obj):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700749 if self.__tuple_params__ == None:
750 return isinstance(obj, tuple)
751 raise TypeError("Parameterized Tuple cannot be used "
752 "with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700753
754 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700755 if self.__tuple_params__ == None:
756 return issubclass(cls, tuple)
757 raise TypeError("Parameterized Tuple cannot be used "
758 "with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700759
760
Guido van Rossum4cefe742016-09-27 15:20:12 -0700761Tuple = _Tuple(_root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700762
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700763
Guido van Rossum4cefe742016-09-27 15:20:12 -0700764class _Callable(_FinalTypingBase, _root=True):
765 """Callable type; Callable[[int], str] is a function of (int) -> str.
766
767 The subscription syntax must always be used with exactly two
768 values: the argument list and the return type. The argument list
769 must be a list of types; the return type must be a single type.
770
771 There is no syntax to indicate optional or keyword arguments,
772 such function types are rarely used as callback types.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700773 """
774
Guido van Rossum4cefe742016-09-27 15:20:12 -0700775 __slots__ = ('__args__', '__result__')
Guido van Rossumd70fe632015-08-05 12:11:06 +0200776
Guido van Rossum4cefe742016-09-27 15:20:12 -0700777 def __init__(self, args=None, result=None, _root=False):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700778 if args is None and result is None:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700779 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700780 else:
781 if args is not Ellipsis:
782 if not isinstance(args, list):
783 raise TypeError("Callable[args, result]: "
784 "args must be a list."
785 " Got %.100r." % (args,))
786 msg = "Callable[[arg, ...], result]: each arg must be a type."
787 args = tuple(_type_check(arg, msg) for arg in args)
788 msg = "Callable[args, result]: result must be a type."
789 result = _type_check(result, msg)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700790 self.__args__ = args
791 self.__result__ = result
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700792
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700793 def _get_type_vars(self, tvars):
Guido van Rossumefa798d2016-08-23 11:01:50 -0700794 if self.__args__ and self.__args__ is not Ellipsis:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700795 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700796
797 def _eval_type(self, globalns, localns):
798 if self.__args__ is None and self.__result__ is None:
799 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200800 if self.__args__ is Ellipsis:
801 args = self.__args__
802 else:
803 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700804 result = _eval_type(self.__result__, globalns, localns)
805 if args == self.__args__ and result == self.__result__:
806 return self
807 else:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700808 return self.__class__(args, result, _root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700809
810 def __repr__(self):
811 r = super().__repr__()
812 if self.__args__ is not None or self.__result__ is not None:
813 if self.__args__ is Ellipsis:
814 args_r = '...'
815 else:
816 args_r = '[%s]' % ', '.join(_type_repr(t)
817 for t in self.__args__)
818 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
819 return r
820
821 def __getitem__(self, parameters):
822 if self.__args__ is not None or self.__result__ is not None:
823 raise TypeError("This Callable type is already parameterized.")
824 if not isinstance(parameters, tuple) or len(parameters) != 2:
825 raise TypeError(
826 "Callable must be used as Callable[[arg, ...], result].")
827 args, result = parameters
Guido van Rossum4cefe742016-09-27 15:20:12 -0700828 return self.__class__(args, result, _root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700829
830 def __eq__(self, other):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700831 if not isinstance(other, _Callable):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700832 return NotImplemented
833 return (self.__args__ == other.__args__ and
834 self.__result__ == other.__result__)
835
836 def __hash__(self):
837 return hash(self.__args__) ^ hash(self.__result__)
838
Guido van Rossumd70fe632015-08-05 12:11:06 +0200839 def __instancecheck__(self, obj):
840 # For unparametrized Callable we allow this, because
841 # typing.Callable should be equivalent to
842 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700843 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200844 return isinstance(obj, collections_abc.Callable)
845 else:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700846 raise TypeError("Parameterized Callable cannot be used "
847 "with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700848
849 def __subclasscheck__(self, cls):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700850 if self.__args__ is None and self.__result__ is None:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700851 return issubclass(cls, collections_abc.Callable)
852 else:
853 raise TypeError("Parameterized Callable cannot be used "
854 "with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700855
856
Guido van Rossum4cefe742016-09-27 15:20:12 -0700857Callable = _Callable(_root=True)
Guido van Rossumd70fe632015-08-05 12:11:06 +0200858
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700859
860def _gorg(a):
861 """Return the farthest origin of a generic class."""
862 assert isinstance(a, GenericMeta)
863 while a.__origin__ is not None:
864 a = a.__origin__
865 return a
866
867
868def _geqv(a, b):
869 """Return whether two generic classes are equivalent.
870
871 The intention is to consider generic class X and any of its
872 parameterized forms (X[T], X[int], etc.) as equivalent.
873
874 However, X is not equivalent to a subclass of X.
875
876 The relation is reflexive, symmetric and transitive.
877 """
878 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
879 # Reduce each to its origin.
880 return _gorg(a) is _gorg(b)
881
882
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700883def _next_in_mro(cls):
884 """Helper for Generic.__new__.
885
886 Returns the class after the last occurrence of Generic or
887 Generic[...] in cls.__mro__.
888 """
889 next_in_mro = object
890 # Look for the last occurrence of Generic or Generic[...].
891 for i, c in enumerate(cls.__mro__[:-1]):
892 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
893 next_in_mro = cls.__mro__[i+1]
894 return next_in_mro
895
896
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700897class GenericMeta(TypingMeta, abc.ABCMeta):
898 """Metaclass for generic types."""
899
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700900 def __new__(cls, name, bases, namespace,
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700901 tvars=None, args=None, origin=None, extra=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700902 self = super().__new__(cls, name, bases, namespace, _root=True)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700903
904 if tvars is not None:
905 # Called from __getitem__() below.
906 assert origin is not None
907 assert all(isinstance(t, TypeVar) for t in tvars), tvars
908 else:
909 # Called from class statement.
910 assert tvars is None, tvars
911 assert args is None, args
912 assert origin is None, origin
913
914 # Get the full set of tvars from the bases.
915 tvars = _type_vars(bases)
916 # Look for Generic[T1, ..., Tn].
917 # If found, tvars must be a subset of it.
918 # If not found, tvars is it.
919 # Also check for and reject plain Generic,
920 # and reject multiple Generic[...].
921 gvars = None
922 for base in bases:
923 if base is Generic:
924 raise TypeError("Cannot inherit from plain Generic")
925 if (isinstance(base, GenericMeta) and
926 base.__origin__ is Generic):
927 if gvars is not None:
928 raise TypeError(
929 "Cannot inherit from Generic[...] multiple types.")
930 gvars = base.__parameters__
931 if gvars is None:
932 gvars = tvars
933 else:
934 tvarset = set(tvars)
935 gvarset = set(gvars)
936 if not tvarset <= gvarset:
937 raise TypeError(
938 "Some type variables (%s) "
939 "are not listed in Generic[%s]" %
940 (", ".join(str(t) for t in tvars if t not in gvarset),
941 ", ".join(str(g) for g in gvars)))
942 tvars = gvars
943
944 self.__parameters__ = tvars
945 self.__args__ = args
946 self.__origin__ = origin
Guido van Rossum1cea70f2016-05-18 08:35:00 -0700947 self.__extra__ = extra
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700948 # Speed hack (https://github.com/python/typing/issues/196).
949 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700950 return self
951
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700952 def _get_type_vars(self, tvars):
953 if self.__origin__ and self.__parameters__:
954 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700955
956 def __repr__(self):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700957 if self.__origin__ is not None:
958 r = repr(self.__origin__)
959 else:
960 r = super().__repr__()
961 if self.__args__:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700962 r += '[%s]' % (
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700963 ', '.join(_type_repr(p) for p in self.__args__))
964 if self.__parameters__:
965 r += '<%s>' % (
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700966 ', '.join(_type_repr(p) for p in self.__parameters__))
967 return r
968
969 def __eq__(self, other):
970 if not isinstance(other, GenericMeta):
971 return NotImplemented
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700972 if self.__origin__ is not None:
973 return (self.__origin__ is other.__origin__ and
974 self.__args__ == other.__args__ and
975 self.__parameters__ == other.__parameters__)
976 else:
977 return self is other
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700978
979 def __hash__(self):
980 return hash((self.__name__, self.__parameters__))
981
Guido van Rossum4cefe742016-09-27 15:20:12 -0700982 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700983 def __getitem__(self, params):
984 if not isinstance(params, tuple):
985 params = (params,)
986 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700987 raise TypeError(
988 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700989 msg = "Parameters to generic types must be types."
990 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700991 if self is Generic:
992 # Generic can only be subscripted with unique type variables.
993 if not all(isinstance(p, TypeVar) for p in params):
994 raise TypeError(
995 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200996 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -0700997 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700998 "Parameters to Generic[...] must all be unique")
999 tvars = params
1000 args = None
1001 elif self is _Protocol:
1002 # _Protocol is internal, don't check anything.
1003 tvars = params
1004 args = None
1005 elif self.__origin__ in (Generic, _Protocol):
1006 # Can't subscript Generic[...] or _Protocol[...].
1007 raise TypeError("Cannot subscript already-subscripted %s" %
1008 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001009 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001010 # Subscripting a regular Generic subclass.
1011 if not self.__parameters__:
1012 raise TypeError("%s is not a generic class" % repr(self))
1013 alen = len(params)
1014 elen = len(self.__parameters__)
1015 if alen != elen:
1016 raise TypeError(
1017 "Too %s parameters for %s; actual %s, expected %s" %
1018 ("many" if alen > elen else "few", repr(self), alen, elen))
1019 tvars = _type_vars(params)
1020 args = params
1021 return self.__class__(self.__name__,
1022 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001023 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001024 tvars=tvars,
1025 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001026 origin=self,
1027 extra=self.__extra__)
1028
Guido van Rossum1b669102015-09-04 12:15:54 -07001029 def __instancecheck__(self, instance):
1030 # Since we extend ABC.__subclasscheck__ and
1031 # ABC.__instancecheck__ inlines the cache checking done by the
1032 # latter, we must extend __instancecheck__ too. For simplicity
1033 # we just skip the cache check -- instance checks for generic
1034 # classes are supposed to be rare anyways.
1035 return self.__subclasscheck__(instance.__class__)
1036
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001037 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001038 if self is Generic:
1039 raise TypeError("Class %r cannot be used with class "
1040 "or instance checks" % self)
1041 if (self.__origin__ is not None and
1042 sys._getframe(1).f_globals['__name__'] != 'abc'):
1043 raise TypeError("Parameterized generics cannot be used with class "
1044 "or instance checks")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001045 if super().__subclasscheck__(cls):
1046 return True
Guido van Rossum4cefe742016-09-27 15:20:12 -07001047 if self.__extra__ is not None:
1048 return issubclass(cls, self.__extra__)
1049 return False
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001050
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001051
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001052# Prevent checks for Generic to crash when defining Generic.
1053Generic = None
1054
1055
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001056class Generic(metaclass=GenericMeta):
1057 """Abstract base class for generic types.
1058
1059 A generic type is typically declared by inheriting from an
1060 instantiation of this class with one or more type variables.
1061 For example, a generic mapping type might be defined as::
1062
1063 class Mapping(Generic[KT, VT]):
1064 def __getitem__(self, key: KT) -> VT:
1065 ...
1066 # Etc.
1067
1068 This class can then be used as follows::
1069
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001070 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001071 try:
1072 return mapping[key]
1073 except KeyError:
1074 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001075 """
1076
Guido van Rossumd70fe632015-08-05 12:11:06 +02001077 __slots__ = ()
1078
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001079 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001080 if cls.__origin__ is None:
1081 return cls.__next_in_mro__.__new__(cls)
1082 else:
1083 origin = _gorg(cls)
1084 obj = cls.__next_in_mro__.__new__(origin)
1085 obj.__init__(*args, **kwds)
1086 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001087
1088
Guido van Rossum4cefe742016-09-27 15:20:12 -07001089class _ClassVar(_FinalTypingBase, _root=True):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001090 """Special type construct to mark class variables.
1091
1092 An annotation wrapped in ClassVar indicates that a given
1093 attribute is intended to be used as a class variable and
1094 should not be set on instances of that class. Usage::
1095
1096 class Starship:
1097 stats: ClassVar[Dict[str, int]] = {} # class variable
1098 damage: int = 10 # instance variable
1099
1100 ClassVar accepts only types and cannot be further subscribed.
1101
1102 Note that ClassVar is not a class itself, and should not
1103 be used with isinstance() or issubclass().
1104 """
1105
Guido van Rossum4cefe742016-09-27 15:20:12 -07001106 __slots__ = ('__type__',)
1107
1108 def __init__(self, tp=None, **kwds):
1109 self.__type__ = tp
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001110
1111 def __getitem__(self, item):
1112 cls = type(self)
1113 if self.__type__ is None:
1114 return cls(_type_check(item,
Guido van Rossum4cefe742016-09-27 15:20:12 -07001115 '{} accepts only single type.'.format(cls.__name__[1:])),
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001116 _root=True)
1117 raise TypeError('{} cannot be further subscripted'
1118 .format(cls.__name__[1:]))
1119
1120 def _eval_type(self, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001121 new_tp = _eval_type(self.__type__, globalns, localns)
1122 if new_tp == self.__type__:
1123 return self
1124 return type(self)(new_tp, _root=True)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001125
1126 def _get_type_vars(self, tvars):
1127 if self.__type__:
1128 _get_type_vars(self.__type__, tvars)
1129
1130 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001131 r = super().__repr__()
1132 if self.__type__ is not None:
1133 r += '[{}]'.format(_type_repr(self.__type__))
1134 return r
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001135
1136 def __hash__(self):
1137 return hash((type(self).__name__, self.__type__))
1138
1139 def __eq__(self, other):
1140 if not isinstance(other, _ClassVar):
1141 return NotImplemented
1142 if self.__type__ is not None:
1143 return self.__type__ == other.__type__
1144 return self is other
1145
1146ClassVar = _ClassVar(_root=True)
1147
1148
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001149def cast(typ, val):
1150 """Cast a value to a type.
1151
1152 This returns the value unchanged. To the type checker this
1153 signals that the return value has the designated type, but at
1154 runtime we intentionally don't check anything (we want this
1155 to be as fast as possible).
1156 """
1157 return val
1158
1159
1160def _get_defaults(func):
1161 """Internal helper to extract the default arguments, by name."""
1162 code = func.__code__
1163 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001164 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001165 arg_names = arg_names[:pos_count]
1166 defaults = func.__defaults__ or ()
1167 kwdefaults = func.__kwdefaults__
1168 res = dict(kwdefaults) if kwdefaults else {}
1169 pos_offset = pos_count - len(defaults)
1170 for name, value in zip(arg_names[pos_offset:], defaults):
1171 assert name not in res
1172 res[name] = value
1173 return res
1174
1175
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001176if sys.version_info[:2] >= (3, 3):
1177 def get_type_hints(obj, globalns=None, localns=None):
1178 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001179
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001180 This is often the same as obj.__annotations__, but it handles
1181 forward references encoded as string literals, and if necessary
1182 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001183
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001184 The argument may be a module, class, method, or function. The annotations
1185 are returned as a dictionary, or in the case of a class, a ChainMap of
1186 dictionaries.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001187
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001188 TypeError is raised if the argument is not of a type that can contain
1189 annotations, and an empty dictionary is returned if no annotations are
1190 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001191
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001192 BEWARE -- the behavior of globalns and localns is counterintuitive
1193 (unless you are familiar with how eval() and exec() work). The
1194 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001195
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001196 - If no dict arguments are passed, an attempt is made to use the
1197 globals from obj, and these are also used as the locals. If the
1198 object does not appear to have globals, an exception is raised.
1199
1200 - If one dict argument is passed, it is used for both globals and
1201 locals.
1202
1203 - If two dict arguments are passed, they specify globals and
1204 locals, respectively.
1205 """
1206
1207 if getattr(obj, '__no_type_check__', None):
1208 return {}
1209 if globalns is None:
1210 globalns = getattr(obj, '__globals__', {})
1211 if localns is None:
1212 localns = globalns
1213 elif localns is None:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001214 localns = globalns
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001215
1216 if (isinstance(obj, types.FunctionType) or
1217 isinstance(obj, types.BuiltinFunctionType) or
1218 isinstance(obj, types.MethodType)):
1219 defaults = _get_defaults(obj)
1220 hints = obj.__annotations__
1221 for name, value in hints.items():
1222 if value is None:
1223 value = type(None)
1224 if isinstance(value, str):
1225 value = _ForwardRef(value)
1226 value = _eval_type(value, globalns, localns)
1227 if name in defaults and defaults[name] is None:
1228 value = Optional[value]
1229 hints[name] = value
1230 return hints
1231
1232 if isinstance(obj, types.ModuleType):
1233 try:
1234 hints = obj.__annotations__
1235 except AttributeError:
1236 return {}
1237 # we keep only those annotations that can be accessed on module
1238 members = obj.__dict__
1239 hints = {name: value for name, value in hints.items()
1240 if name in members}
1241 for name, value in hints.items():
1242 if value is None:
1243 value = type(None)
1244 if isinstance(value, str):
1245 value = _ForwardRef(value)
1246 value = _eval_type(value, globalns, localns)
1247 hints[name] = value
1248 return hints
1249
1250 if isinstance(object, type):
1251 cmap = None
1252 for base in reversed(obj.__mro__):
1253 new_map = collections.ChainMap if cmap is None else cmap.new_child
1254 try:
1255 hints = base.__dict__['__annotations__']
1256 except KeyError:
1257 cmap = new_map()
1258 else:
1259 for name, value in hints.items():
1260 if value is None:
1261 value = type(None)
1262 if isinstance(value, str):
1263 value = _ForwardRef(value)
1264 value = _eval_type(value, globalns, localns)
1265 hints[name] = value
1266 cmap = new_map(hints)
1267 return cmap
1268
1269 raise TypeError('{!r} is not a module, class, method, '
1270 'or function.'.format(obj))
1271
1272else:
1273 def get_type_hints(obj, globalns=None, localns=None):
1274 """Return type hints for a function or method object.
1275
1276 This is often the same as obj.__annotations__, but it handles
1277 forward references encoded as string literals, and if necessary
1278 adds Optional[t] if a default value equal to None is set.
1279
1280 BEWARE -- the behavior of globalns and localns is counterintuitive
1281 (unless you are familiar with how eval() and exec() work). The
1282 search order is locals first, then globals.
1283
1284 - If no dict arguments are passed, an attempt is made to use the
1285 globals from obj, and these are also used as the locals. If the
1286 object does not appear to have globals, an exception is raised.
1287
1288 - If one dict argument is passed, it is used for both globals and
1289 locals.
1290
1291 - If two dict arguments are passed, they specify globals and
1292 locals, respectively.
1293 """
1294 if getattr(obj, '__no_type_check__', None):
1295 return {}
1296 if globalns is None:
1297 globalns = getattr(obj, '__globals__', {})
1298 if localns is None:
1299 localns = globalns
1300 elif localns is None:
1301 localns = globalns
1302 defaults = _get_defaults(obj)
1303 hints = dict(obj.__annotations__)
1304 for name, value in hints.items():
1305 if isinstance(value, str):
1306 value = _ForwardRef(value)
1307 value = _eval_type(value, globalns, localns)
1308 if name in defaults and defaults[name] is None:
1309 value = Optional[value]
1310 hints[name] = value
1311 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001312
1313
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001314def no_type_check(arg):
1315 """Decorator to indicate that annotations are not type hints.
1316
1317 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001318 applies recursively to all methods and classes defined in that class
1319 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001320
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001321 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001322 """
1323 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001324 arg_attrs = arg.__dict__.copy()
1325 for attr, val in arg.__dict__.items():
1326 if val in arg.__bases__:
1327 arg_attrs.pop(attr)
1328 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001329 if isinstance(obj, types.FunctionType):
1330 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001331 if isinstance(obj, type):
1332 no_type_check(obj)
1333 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001334 arg.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001335 except TypeError: # built-in classes
1336 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001337 return arg
1338
1339
1340def no_type_check_decorator(decorator):
1341 """Decorator to give another decorator the @no_type_check effect.
1342
1343 This wraps the decorator with something that wraps the decorated
1344 function in @no_type_check.
1345 """
1346
1347 @functools.wraps(decorator)
1348 def wrapped_decorator(*args, **kwds):
1349 func = decorator(*args, **kwds)
1350 func = no_type_check(func)
1351 return func
1352
1353 return wrapped_decorator
1354
1355
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001356def _overload_dummy(*args, **kwds):
1357 """Helper for @overload to raise when called."""
1358 raise NotImplementedError(
1359 "You should not call an overloaded function. "
1360 "A series of @overload-decorated functions "
1361 "outside a stub module should always be followed "
1362 "by an implementation that is not @overload-ed.")
1363
1364
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001365def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001366 """Decorator for overloaded functions/methods.
1367
1368 In a stub file, place two or more stub definitions for the same
1369 function in a row, each decorated with @overload. For example:
1370
1371 @overload
1372 def utf8(value: None) -> None: ...
1373 @overload
1374 def utf8(value: bytes) -> bytes: ...
1375 @overload
1376 def utf8(value: str) -> bytes: ...
1377
1378 In a non-stub file (i.e. a regular .py file), do the same but
1379 follow it with an implementation. The implementation should *not*
1380 be decorated with @overload. For example:
1381
1382 @overload
1383 def utf8(value: None) -> None: ...
1384 @overload
1385 def utf8(value: bytes) -> bytes: ...
1386 @overload
1387 def utf8(value: str) -> bytes: ...
1388 def utf8(value):
1389 # implementation goes here
1390 """
1391 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001392
1393
1394class _ProtocolMeta(GenericMeta):
1395 """Internal metaclass for _Protocol.
1396
1397 This exists so _Protocol classes can be generic without deriving
1398 from Generic.
1399 """
1400
Guido van Rossumd70fe632015-08-05 12:11:06 +02001401 def __instancecheck__(self, obj):
1402 raise TypeError("Protocols cannot be used with isinstance().")
1403
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001404 def __subclasscheck__(self, cls):
1405 if not self._is_protocol:
1406 # No structural checks since this isn't a protocol.
1407 return NotImplemented
1408
1409 if self is _Protocol:
1410 # Every class is a subclass of the empty protocol.
1411 return True
1412
1413 # Find all attributes defined in the protocol.
1414 attrs = self._get_protocol_attrs()
1415
1416 for attr in attrs:
1417 if not any(attr in d.__dict__ for d in cls.__mro__):
1418 return False
1419 return True
1420
1421 def _get_protocol_attrs(self):
1422 # Get all Protocol base classes.
1423 protocol_bases = []
1424 for c in self.__mro__:
1425 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1426 protocol_bases.append(c)
1427
1428 # Get attributes included in protocol.
1429 attrs = set()
1430 for base in protocol_bases:
1431 for attr in base.__dict__.keys():
1432 # Include attributes not defined in any non-protocol bases.
1433 for c in self.__mro__:
1434 if (c is not base and attr in c.__dict__ and
1435 not getattr(c, '_is_protocol', False)):
1436 break
1437 else:
1438 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001439 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001440 attr != '__annotations__' and
1441 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001442 attr != '_is_protocol' and
1443 attr != '__dict__' and
1444 attr != '__args__' and
1445 attr != '__slots__' and
1446 attr != '_get_protocol_attrs' and
1447 attr != '__next_in_mro__' and
1448 attr != '__parameters__' and
1449 attr != '__origin__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001450 attr != '__extra__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001451 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001452 attrs.add(attr)
1453
1454 return attrs
1455
1456
1457class _Protocol(metaclass=_ProtocolMeta):
1458 """Internal base class for protocol classes.
1459
1460 This implements a simple-minded structural isinstance check
1461 (similar but more general than the one-offs in collections.abc
1462 such as Hashable).
1463 """
1464
Guido van Rossumd70fe632015-08-05 12:11:06 +02001465 __slots__ = ()
1466
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001467 _is_protocol = True
1468
1469
1470# Various ABCs mimicking those in collections.abc.
1471# A few are simply re-exported for completeness.
1472
1473Hashable = collections_abc.Hashable # Not generic.
1474
1475
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001476if hasattr(collections_abc, 'Awaitable'):
1477 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1478 __slots__ = ()
1479else:
1480 Awaitable = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001481
1482
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001483if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001484
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001485 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1486 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001487
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001488 class AsyncIterator(AsyncIterable[T_co],
1489 extra=collections_abc.AsyncIterator):
1490 __slots__ = ()
1491
1492else:
1493 AsyncIterable = None
1494 AsyncIterator = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001495
1496
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001497class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001498 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001499
1500
1501class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001502 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001503
1504
1505class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001506 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001507
1508 @abstractmethod
1509 def __int__(self) -> int:
1510 pass
1511
1512
1513class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001514 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001515
1516 @abstractmethod
1517 def __float__(self) -> float:
1518 pass
1519
1520
1521class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001522 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001523
1524 @abstractmethod
1525 def __complex__(self) -> complex:
1526 pass
1527
1528
1529class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001530 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001531
1532 @abstractmethod
1533 def __bytes__(self) -> bytes:
1534 pass
1535
1536
Guido van Rossumd70fe632015-08-05 12:11:06 +02001537class SupportsAbs(_Protocol[T_co]):
1538 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001539
1540 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001541 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001542 pass
1543
1544
Guido van Rossumd70fe632015-08-05 12:11:06 +02001545class SupportsRound(_Protocol[T_co]):
1546 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001547
1548 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001549 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001550 pass
1551
1552
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001553if hasattr(collections_abc, 'Reversible'):
1554 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1555 __slots__ = ()
1556else:
1557 class Reversible(_Protocol[T_co]):
1558 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001559
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001560 @abstractmethod
1561 def __reversed__(self) -> 'Iterator[T_co]':
1562 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001563
1564
1565Sized = collections_abc.Sized # Not generic.
1566
1567
1568class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001569 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001570
1571
Guido van Rossumefa798d2016-08-23 11:01:50 -07001572if hasattr(collections_abc, 'Collection'):
1573 class Collection(Sized, Iterable[T_co], Container[T_co],
1574 extra=collections_abc.Collection):
1575 __slots__ = ()
1576
1577 __all__.append('Collection')
1578
1579
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001580# Callable was defined earlier.
1581
Guido van Rossumefa798d2016-08-23 11:01:50 -07001582if hasattr(collections_abc, 'Collection'):
1583 class AbstractSet(Collection[T_co],
1584 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001585 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001586else:
1587 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1588 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001589 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001590
1591
1592class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001593 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001594
1595
Guido van Rossumefa798d2016-08-23 11:01:50 -07001596# NOTE: It is only covariant in the value type.
1597if hasattr(collections_abc, 'Collection'):
1598 class Mapping(Collection[KT], Generic[KT, VT_co],
1599 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001600 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001601else:
1602 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1603 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001604 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001605
1606
1607class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001608 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001609
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001610if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001611 if hasattr(collections_abc, 'Collection'):
1612 class Sequence(Reversible[T_co], Collection[T_co],
1613 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001614 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001615 else:
1616 class Sequence(Sized, Reversible[T_co], Container[T_co],
1617 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001618 __slots__ = ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001619else:
1620 class Sequence(Sized, Iterable[T_co], Container[T_co],
1621 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001622 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001623
1624
1625class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001626 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001627
1628
1629class ByteString(Sequence[int], extra=collections_abc.ByteString):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001630 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001631
1632
1633ByteString.register(type(memoryview(b'')))
1634
1635
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001636class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001637
Guido van Rossum4cefe742016-09-27 15:20:12 -07001638 __slots__ = ()
1639
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001640 def __new__(cls, *args, **kwds):
1641 if _geqv(cls, List):
1642 raise TypeError("Type List cannot be instantiated; "
1643 "use list() instead")
1644 return list.__new__(cls, *args, **kwds)
1645
1646
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001647class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001648
Guido van Rossum4cefe742016-09-27 15:20:12 -07001649 __slots__ = ()
1650
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001651 def __new__(cls, *args, **kwds):
1652 if _geqv(cls, Set):
1653 raise TypeError("Type Set cannot be instantiated; "
1654 "use set() instead")
1655 return set.__new__(cls, *args, **kwds)
1656
1657
Guido van Rossum4cefe742016-09-27 15:20:12 -07001658class FrozenSet(frozenset, AbstractSet[T_co], extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001659 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001660
1661 def __new__(cls, *args, **kwds):
1662 if _geqv(cls, FrozenSet):
1663 raise TypeError("Type FrozenSet cannot be instantiated; "
1664 "use frozenset() instead")
1665 return frozenset.__new__(cls, *args, **kwds)
1666
1667
1668class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001669 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001670
1671
Guido van Rossumd70fe632015-08-05 12:11:06 +02001672class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001673 extra=collections_abc.KeysView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001674 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001675
1676
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001677class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001678 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001679 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001680 extra=collections_abc.ItemsView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001681 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001682
1683
1684class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001685 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001686
1687
Brett Cannonf3ad0422016-04-15 10:51:30 -07001688if hasattr(contextlib, 'AbstractContextManager'):
1689 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1690 __slots__ = ()
1691 __all__.append('ContextManager')
1692
1693
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001694class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001695
Guido van Rossum4cefe742016-09-27 15:20:12 -07001696 __slots__ = ()
1697
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001698 def __new__(cls, *args, **kwds):
1699 if _geqv(cls, Dict):
1700 raise TypeError("Type Dict cannot be instantiated; "
1701 "use dict() instead")
1702 return dict.__new__(cls, *args, **kwds)
1703
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001704class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
1705 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001706
Guido van Rossum4cefe742016-09-27 15:20:12 -07001707 __slots__ = ()
1708
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001709 def __new__(cls, *args, **kwds):
1710 if _geqv(cls, DefaultDict):
1711 raise TypeError("Type DefaultDict cannot be instantiated; "
1712 "use collections.defaultdict() instead")
1713 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001714
1715# Determine what base class to use for Generator.
1716if hasattr(collections_abc, 'Generator'):
1717 # Sufficiently recent versions of 3.5 have a Generator ABC.
1718 _G_base = collections_abc.Generator
1719else:
1720 # Fall back on the exact type.
1721 _G_base = types.GeneratorType
1722
1723
1724class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1725 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001726 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001727
1728 def __new__(cls, *args, **kwds):
1729 if _geqv(cls, Generator):
1730 raise TypeError("Type Generator cannot be instantiated; "
1731 "create a subclass instead")
1732 return super().__new__(cls, *args, **kwds)
1733
1734
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001735# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07001736CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001737
1738
Guido van Rossumb22c7082016-05-26 09:56:19 -07001739# This is not a real generic class. Don't use outside annotations.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001740class Type(Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07001741 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001742
1743 For example, suppose we have the following classes::
1744
1745 class User: ... # Abstract base for User classes
1746 class BasicUser(User): ...
1747 class ProUser(User): ...
1748 class TeamUser(User): ...
1749
1750 And a function that takes a class argument that's a subclass of
1751 User and returns an instance of the corresponding class::
1752
1753 U = TypeVar('U', bound=User)
1754 def new_user(user_class: Type[U]) -> U:
1755 user = user_class()
1756 # (Here we could write the user object to a database)
1757 return user
1758
1759 joe = new_user(BasicUser)
1760
1761 At this point the type checker knows that joe has type BasicUser.
1762 """
1763
Guido van Rossum4cefe742016-09-27 15:20:12 -07001764 __slots__ = ()
1765
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001766
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001767def _make_nmtuple(name, types):
1768 nm_tpl = collections.namedtuple(name, [n for n, t in types])
1769 nm_tpl._field_types = dict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001770 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001771 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001772 except (AttributeError, ValueError):
1773 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001774 return nm_tpl
1775
1776
1777if sys.version_info[:2] >= (3, 6):
1778 class NamedTupleMeta(type):
1779
1780 def __new__(cls, typename, bases, ns, *, _root=False):
1781 if _root:
1782 return super().__new__(cls, typename, bases, ns)
1783 types = ns.get('__annotations__', {})
1784 return _make_nmtuple(typename, types.items())
1785
1786 class NamedTuple(metaclass=NamedTupleMeta, _root=True):
1787 """Typed version of namedtuple.
1788
1789 Usage::
1790
1791 class Employee(NamedTuple):
1792 name: str
1793 id: int
1794
1795 This is equivalent to::
1796
1797 Employee = collections.namedtuple('Employee', ['name', 'id'])
1798
1799 The resulting class has one extra attribute: _field_types,
1800 giving a dict mapping field names to types. (The field names
1801 are in the _fields attribute, which is part of the namedtuple
1802 API.) Backward-compatible usage::
1803
1804 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1805 """
1806
1807 def __new__(self, typename, fields):
1808 return _make_nmtuple(typename, fields)
1809else:
1810 def NamedTuple(typename, fields):
1811 """Typed version of namedtuple.
1812
1813 Usage::
1814
1815 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1816
1817 This is equivalent to::
1818
1819 Employee = collections.namedtuple('Employee', ['name', 'id'])
1820
1821 The resulting class has one extra attribute: _field_types,
1822 giving a dict mapping field names to types. (The field names
1823 are in the _fields attribute, which is part of the namedtuple
1824 API.)
1825 """
1826 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001827
1828
Guido van Rossum91185fe2016-06-08 11:19:11 -07001829def NewType(name, tp):
1830 """NewType creates simple unique types with almost zero
1831 runtime overhead. NewType(name, tp) is considered a subtype of tp
1832 by static type checkers. At runtime, NewType(name, tp) returns
1833 a dummy function that simply returns its argument. Usage::
1834
1835 UserId = NewType('UserId', int)
1836
1837 def name_by_id(user_id: UserId) -> str:
1838 ...
1839
1840 UserId('user') # Fails type check
1841
1842 name_by_id(42) # Fails type check
1843 name_by_id(UserId(42)) # OK
1844
1845 num = UserId(5) + 1 # type: int
1846 """
1847
1848 def new_type(x):
1849 return x
1850
1851 new_type.__name__ = name
1852 new_type.__supertype__ = tp
1853 return new_type
1854
1855
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001856# Python-version-specific alias (Python 2: unicode; Python 3: str)
1857Text = str
1858
1859
Guido van Rossum91185fe2016-06-08 11:19:11 -07001860# Constant that's True when type checking, but False here.
1861TYPE_CHECKING = False
1862
1863
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001864class IO(Generic[AnyStr]):
1865 """Generic base class for TextIO and BinaryIO.
1866
1867 This is an abstract, generic version of the return of open().
1868
1869 NOTE: This does not distinguish between the different possible
1870 classes (text vs. binary, read vs. write vs. read/write,
1871 append-only, unbuffered). The TextIO and BinaryIO subclasses
1872 below capture the distinctions between text vs. binary, which is
1873 pervasive in the interface; however we currently do not offer a
1874 way to track the other distinctions in the type system.
1875 """
1876
Guido van Rossumd70fe632015-08-05 12:11:06 +02001877 __slots__ = ()
1878
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001879 @abstractproperty
1880 def mode(self) -> str:
1881 pass
1882
1883 @abstractproperty
1884 def name(self) -> str:
1885 pass
1886
1887 @abstractmethod
1888 def close(self) -> None:
1889 pass
1890
1891 @abstractmethod
1892 def closed(self) -> bool:
1893 pass
1894
1895 @abstractmethod
1896 def fileno(self) -> int:
1897 pass
1898
1899 @abstractmethod
1900 def flush(self) -> None:
1901 pass
1902
1903 @abstractmethod
1904 def isatty(self) -> bool:
1905 pass
1906
1907 @abstractmethod
1908 def read(self, n: int = -1) -> AnyStr:
1909 pass
1910
1911 @abstractmethod
1912 def readable(self) -> bool:
1913 pass
1914
1915 @abstractmethod
1916 def readline(self, limit: int = -1) -> AnyStr:
1917 pass
1918
1919 @abstractmethod
1920 def readlines(self, hint: int = -1) -> List[AnyStr]:
1921 pass
1922
1923 @abstractmethod
1924 def seek(self, offset: int, whence: int = 0) -> int:
1925 pass
1926
1927 @abstractmethod
1928 def seekable(self) -> bool:
1929 pass
1930
1931 @abstractmethod
1932 def tell(self) -> int:
1933 pass
1934
1935 @abstractmethod
1936 def truncate(self, size: int = None) -> int:
1937 pass
1938
1939 @abstractmethod
1940 def writable(self) -> bool:
1941 pass
1942
1943 @abstractmethod
1944 def write(self, s: AnyStr) -> int:
1945 pass
1946
1947 @abstractmethod
1948 def writelines(self, lines: List[AnyStr]) -> None:
1949 pass
1950
1951 @abstractmethod
1952 def __enter__(self) -> 'IO[AnyStr]':
1953 pass
1954
1955 @abstractmethod
1956 def __exit__(self, type, value, traceback) -> None:
1957 pass
1958
1959
1960class BinaryIO(IO[bytes]):
1961 """Typed version of the return of open() in binary mode."""
1962
Guido van Rossumd70fe632015-08-05 12:11:06 +02001963 __slots__ = ()
1964
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001965 @abstractmethod
1966 def write(self, s: Union[bytes, bytearray]) -> int:
1967 pass
1968
1969 @abstractmethod
1970 def __enter__(self) -> 'BinaryIO':
1971 pass
1972
1973
1974class TextIO(IO[str]):
1975 """Typed version of the return of open() in text mode."""
1976
Guido van Rossumd70fe632015-08-05 12:11:06 +02001977 __slots__ = ()
1978
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001979 @abstractproperty
1980 def buffer(self) -> BinaryIO:
1981 pass
1982
1983 @abstractproperty
1984 def encoding(self) -> str:
1985 pass
1986
1987 @abstractproperty
1988 def errors(self) -> str:
1989 pass
1990
1991 @abstractproperty
1992 def line_buffering(self) -> bool:
1993 pass
1994
1995 @abstractproperty
1996 def newlines(self) -> Any:
1997 pass
1998
1999 @abstractmethod
2000 def __enter__(self) -> 'TextIO':
2001 pass
2002
2003
2004class io:
2005 """Wrapper namespace for IO generic classes."""
2006
2007 __all__ = ['IO', 'TextIO', 'BinaryIO']
2008 IO = IO
2009 TextIO = TextIO
2010 BinaryIO = BinaryIO
2011
2012io.__name__ = __name__ + '.io'
2013sys.modules[io.__name__] = io
2014
2015
2016Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
2017 lambda p: p.pattern)
2018Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
2019 lambda m: m.re.pattern)
2020
2021
2022class re:
2023 """Wrapper namespace for re type aliases."""
2024
2025 __all__ = ['Pattern', 'Match']
2026 Pattern = Pattern
2027 Match = Match
2028
2029re.__name__ = __name__ + '.re'
2030sys.modules[re.__name__] = re