blob: 4676d28c8e0eaa44aa37ad0a81ca869f754fecc6 [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
92class TypingMeta(type):
93 """Metaclass for every type defined below.
94
95 This overrides __new__() to require an extra keyword parameter
96 '_root', which serves as a guard against naive subclassing of the
97 typing classes. Any legitimate class defined using a metaclass
98 derived from TypingMeta (including internal subclasses created by
99 e.g. Union[X, Y]) must pass _root=True.
100
101 This also defines a dummy constructor (all the work is done in
102 __new__) and a nicer repr().
103 """
104
105 _is_protocol = False
106
107 def __new__(cls, name, bases, namespace, *, _root=False):
108 if not _root:
109 raise TypeError("Cannot subclass %s" %
110 (', '.join(map(_type_repr, bases)) or '()'))
111 return super().__new__(cls, name, bases, namespace)
112
113 def __init__(self, *args, **kwds):
114 pass
115
116 def _eval_type(self, globalns, localns):
117 """Override this in subclasses to interpret forward references.
118
119 For example, Union['C'] is internally stored as
120 Union[_ForwardRef('C')], which should evaluate to _Union[C],
121 where C is an object found in globalns or localns (searching
122 localns first, of course).
123 """
124 return self
125
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700126 def _get_type_vars(self, tvars):
127 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700128
129 def __repr__(self):
130 return '%s.%s' % (self.__module__, _qualname(self))
131
132
133class Final:
134 """Mix-in class to prevent instantiation."""
135
Guido van Rossumd70fe632015-08-05 12:11:06 +0200136 __slots__ = ()
137
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700138 def __new__(self, *args, **kwds):
139 raise TypeError("Cannot instantiate %r" % self.__class__)
140
141
142class _ForwardRef(TypingMeta):
143 """Wrapper to hold a forward reference."""
144
145 def __new__(cls, arg):
146 if not isinstance(arg, str):
147 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
148 try:
149 code = compile(arg, '<string>', 'eval')
150 except SyntaxError:
151 raise SyntaxError('ForwardRef must be an expression -- got %r' %
152 (arg,))
153 self = super().__new__(cls, arg, (), {}, _root=True)
154 self.__forward_arg__ = arg
155 self.__forward_code__ = code
156 self.__forward_evaluated__ = False
157 self.__forward_value__ = None
158 typing_globals = globals()
159 frame = sys._getframe(1)
160 while frame is not None and frame.f_globals is typing_globals:
161 frame = frame.f_back
162 assert frame is not None
163 self.__forward_frame__ = frame
164 return self
165
166 def _eval_type(self, globalns, localns):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700167 if not self.__forward_evaluated__:
168 if globalns is None and localns is None:
169 globalns = localns = {}
170 elif globalns is None:
171 globalns = localns
172 elif localns is None:
173 localns = globalns
174 self.__forward_value__ = _type_check(
175 eval(self.__forward_code__, globalns, localns),
176 "Forward references must evaluate to types.")
177 self.__forward_evaluated__ = True
178 return self.__forward_value__
179
Guido van Rossumd70fe632015-08-05 12:11:06 +0200180 def __instancecheck__(self, obj):
181 raise TypeError("Forward references cannot be used with isinstance().")
182
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700183 def __subclasscheck__(self, cls):
184 if not self.__forward_evaluated__:
185 globalns = self.__forward_frame__.f_globals
186 localns = self.__forward_frame__.f_locals
187 try:
188 self._eval_type(globalns, localns)
189 except NameError:
190 return False # Too early.
191 return issubclass(cls, self.__forward_value__)
192
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700193 def __repr__(self):
194 return '_ForwardRef(%r)' % (self.__forward_arg__,)
195
196
197class _TypeAlias:
198 """Internal helper class for defining generic variants of concrete types.
199
200 Note that this is not a type; let's call it a pseudo-type. It can
201 be used in instance and subclass checks, e.g. isinstance(m, Match)
202 or issubclass(type(m), Match). However, it cannot be itself the
203 target of an issubclass() call; e.g. issubclass(Match, C) (for
204 some arbitrary class C) raises TypeError rather than returning
205 False.
206 """
207
Guido van Rossumd70fe632015-08-05 12:11:06 +0200208 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
209
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700210 def __new__(cls, *args, **kwds):
211 """Constructor.
212
213 This only exists to give a better error message in case
214 someone tries to subclass a type alias (not a good idea).
215 """
216 if (len(args) == 3 and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700217 isinstance(args[0], str) and
218 isinstance(args[1], tuple)):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700219 # Close enough.
220 raise TypeError("A type alias cannot be subclassed")
221 return object.__new__(cls)
222
223 def __init__(self, name, type_var, impl_type, type_checker):
224 """Initializer.
225
226 Args:
227 name: The name, e.g. 'Pattern'.
228 type_var: The type parameter, e.g. AnyStr, or the
229 specific type, e.g. str.
230 impl_type: The implementation type.
231 type_checker: Function that takes an impl_type instance.
232 and returns a value that should be a type_var instance.
233 """
234 assert isinstance(name, str), repr(name)
235 assert isinstance(type_var, type), repr(type_var)
236 assert isinstance(impl_type, type), repr(impl_type)
237 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
238 self.name = name
239 self.type_var = type_var
240 self.impl_type = impl_type
241 self.type_checker = type_checker
242
243 def __repr__(self):
244 return "%s[%s]" % (self.name, _type_repr(self.type_var))
245
246 def __getitem__(self, parameter):
247 assert isinstance(parameter, type), repr(parameter)
248 if not isinstance(self.type_var, TypeVar):
249 raise TypeError("%s cannot be further parameterized." % self)
250 if self.type_var.__constraints__:
251 if not issubclass(parameter, Union[self.type_var.__constraints__]):
252 raise TypeError("%s is not a valid substitution for %s." %
253 (parameter, self.type_var))
254 return self.__class__(self.name, parameter,
255 self.impl_type, self.type_checker)
256
257 def __instancecheck__(self, obj):
Guido van Rossumd70fe632015-08-05 12:11:06 +0200258 raise TypeError("Type aliases cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700259
260 def __subclasscheck__(self, cls):
261 if cls is Any:
262 return True
263 if isinstance(cls, _TypeAlias):
264 # Covariance. For now, we compare by name.
265 return (cls.name == self.name and
266 issubclass(cls.type_var, self.type_var))
267 else:
268 # Note that this is too lenient, because the
269 # implementation type doesn't carry information about
270 # whether it is about bytes or str (for example).
271 return issubclass(cls, self.impl_type)
272
273
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700274def _get_type_vars(types, tvars):
275 for t in types:
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700276 if isinstance(t, TypingMeta) or isinstance(t, _ClassVar):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700277 t._get_type_vars(tvars)
278
279
280def _type_vars(types):
281 tvars = []
282 _get_type_vars(types, tvars)
283 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700284
285
286def _eval_type(t, globalns, localns):
Guido van Rossum0a6976d2016-09-11 15:34:56 -0700287 if isinstance(t, TypingMeta) or isinstance(t, _ClassVar):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700288 return t._eval_type(globalns, localns)
289 else:
290 return t
291
292
293def _type_check(arg, msg):
294 """Check that the argument is a type, and return it.
295
296 As a special case, accept None and return type(None) instead.
297 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
298
299 The msg argument is a human-readable error message, e.g.
300
301 "Union[arg, ...]: arg should be a type."
302
303 We append the repr() of the actual value (truncated to 100 chars).
304 """
305 if arg is None:
306 return type(None)
307 if isinstance(arg, str):
308 arg = _ForwardRef(arg)
Guido van Rossum91185fe2016-06-08 11:19:11 -0700309 if not isinstance(arg, (type, _TypeAlias)) and not callable(arg):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700310 raise TypeError(msg + " Got %.100r." % (arg,))
311 return arg
312
313
314def _type_repr(obj):
315 """Return the repr() of an object, special-casing types.
316
317 If obj is a type, we return a shorter version than the default
318 type.__repr__, based on the module and qualified name, which is
319 typically enough to uniquely identify a type. For everything
320 else, we fall back on repr(obj).
321 """
322 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
323 if obj.__module__ == 'builtins':
324 return _qualname(obj)
325 else:
326 return '%s.%s' % (obj.__module__, _qualname(obj))
327 else:
328 return repr(obj)
329
330
331class AnyMeta(TypingMeta):
332 """Metaclass for Any."""
333
334 def __new__(cls, name, bases, namespace, _root=False):
335 self = super().__new__(cls, name, bases, namespace, _root=_root)
336 return self
337
Guido van Rossumd70fe632015-08-05 12:11:06 +0200338 def __instancecheck__(self, obj):
339 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700340
341 def __subclasscheck__(self, cls):
342 if not isinstance(cls, type):
343 return super().__subclasscheck__(cls) # To TypeError.
344 return True
345
346
347class Any(Final, metaclass=AnyMeta, _root=True):
348 """Special type indicating an unconstrained type.
349
350 - Any object is an instance of Any.
351 - Any class is a subclass of Any.
352 - As a special case, Any and object are subclasses of each other.
353 """
354
Guido van Rossumd70fe632015-08-05 12:11:06 +0200355 __slots__ = ()
356
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700357
358class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
359 """Type variable.
360
361 Usage::
362
363 T = TypeVar('T') # Can be anything
364 A = TypeVar('A', str, bytes) # Must be str or bytes
365
366 Type variables exist primarily for the benefit of static type
367 checkers. They serve as the parameters for generic types as well
368 as for generic function definitions. See class Generic for more
369 information on generic types. Generic functions work as follows:
370
371 def repeat(x: T, n: int) -> Sequence[T]:
372 '''Return a list containing n references to x.'''
373 return [x]*n
374
375 def longest(x: A, y: A) -> A:
376 '''Return the longest of two strings.'''
377 return x if len(x) >= len(y) else y
378
379 The latter example's signature is essentially the overloading
380 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
381 that if the arguments are instances of some subclass of str,
382 the return type is still plain str.
383
384 At runtime, isinstance(x, T) will raise TypeError. However,
385 issubclass(C, T) is true for any class C, and issubclass(str, A)
386 and issubclass(bytes, A) are true, and issubclass(int, A) is
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700387 false. (TODO: Why is this needed? This may change. See #136.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700388
Guido van Rossumefa798d2016-08-23 11:01:50 -0700389 Type variables defined with covariant=True or contravariant=True
390 can be used do declare covariant or contravariant generic types.
391 See PEP 484 for more details. By default generic types are invariant
392 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700393
394 Type variables can be introspected. e.g.:
395
396 T.__name__ == 'T'
397 T.__constraints__ == ()
398 T.__covariant__ == False
399 T.__contravariant__ = False
400 A.__constraints__ == (str, bytes)
401 """
402
403 def __new__(cls, name, *constraints, bound=None,
404 covariant=False, contravariant=False):
405 self = super().__new__(cls, name, (Final,), {}, _root=True)
406 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700407 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700408 self.__covariant__ = bool(covariant)
409 self.__contravariant__ = bool(contravariant)
410 if constraints and bound is not None:
411 raise TypeError("Constraints cannot be combined with bound=...")
412 if constraints and len(constraints) == 1:
413 raise TypeError("A single constraint is not allowed")
414 msg = "TypeVar(name, constraint, ...): constraints must be types."
415 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
416 if bound:
417 self.__bound__ = _type_check(bound, "Bound must be a type.")
418 else:
419 self.__bound__ = None
420 return self
421
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700422 def _get_type_vars(self, tvars):
423 if self not in tvars:
424 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700425
426 def __repr__(self):
427 if self.__covariant__:
428 prefix = '+'
429 elif self.__contravariant__:
430 prefix = '-'
431 else:
432 prefix = '~'
433 return prefix + self.__name__
434
435 def __instancecheck__(self, instance):
436 raise TypeError("Type variables cannot be used with isinstance().")
437
438 def __subclasscheck__(self, cls):
439 # TODO: Make this raise TypeError too?
440 if cls is self:
441 return True
442 if cls is Any:
443 return True
444 if self.__bound__ is not None:
445 return issubclass(cls, self.__bound__)
446 if self.__constraints__:
447 return any(issubclass(cls, c) for c in self.__constraints__)
448 return True
449
450
451# Some unconstrained type variables. These are used by the container types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700452# (These are not for export.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700453T = TypeVar('T') # Any type.
454KT = TypeVar('KT') # Key type.
455VT = TypeVar('VT') # Value type.
456T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
457V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700458VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
459T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
460
461# A useful type variable with constraints. This represents string types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700462# (This one *is* for export!)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700463AnyStr = TypeVar('AnyStr', bytes, str)
464
465
466class UnionMeta(TypingMeta):
467 """Metaclass for Union."""
468
469 def __new__(cls, name, bases, namespace, parameters=None, _root=False):
470 if parameters is None:
471 return super().__new__(cls, name, bases, namespace, _root=_root)
472 if not isinstance(parameters, tuple):
473 raise TypeError("Expected parameters=<tuple>")
474 # Flatten out Union[Union[...], ...] and type-check non-Union args.
475 params = []
476 msg = "Union[arg, ...]: each arg must be a type."
477 for p in parameters:
478 if isinstance(p, UnionMeta):
479 params.extend(p.__union_params__)
480 else:
481 params.append(_type_check(p, msg))
482 # Weed out strict duplicates, preserving the first of each occurrence.
483 all_params = set(params)
484 if len(all_params) < len(params):
485 new_params = []
486 for t in params:
487 if t in all_params:
488 new_params.append(t)
489 all_params.remove(t)
490 params = new_params
491 assert not all_params, all_params
492 # Weed out subclasses.
493 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
494 # If Any or object is present it will be the sole survivor.
495 # If both Any and object are present, Any wins.
496 # Never discard type variables, except against Any.
497 # (In particular, Union[str, AnyStr] != AnyStr.)
498 all_params = set(params)
499 for t1 in params:
500 if t1 is Any:
501 return Any
502 if isinstance(t1, TypeVar):
503 continue
Guido van Rossumca636ea2015-10-19 14:55:47 -0700504 if isinstance(t1, _TypeAlias):
505 # _TypeAlias is not a real class.
506 continue
Guido van Rossum91185fe2016-06-08 11:19:11 -0700507 if not isinstance(t1, type):
508 assert callable(t1) # A callable might sneak through.
509 continue
510 if any(isinstance(t2, type) and issubclass(t1, t2)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700511 for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
512 all_params.remove(t1)
513 # It's not a union if there's only one type left.
514 if len(all_params) == 1:
515 return all_params.pop()
516 # Create a new class with these params.
517 self = super().__new__(cls, name, bases, {}, _root=True)
518 self.__union_params__ = tuple(t for t in params if t in all_params)
519 self.__union_set_params__ = frozenset(self.__union_params__)
520 return self
521
522 def _eval_type(self, globalns, localns):
523 p = tuple(_eval_type(t, globalns, localns)
524 for t in self.__union_params__)
525 if p == self.__union_params__:
526 return self
527 else:
528 return self.__class__(self.__name__, self.__bases__, {},
529 p, _root=True)
530
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700531 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700532 if self.__union_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700533 _get_type_vars(self.__union_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700534
535 def __repr__(self):
536 r = super().__repr__()
537 if self.__union_params__:
538 r += '[%s]' % (', '.join(_type_repr(t)
539 for t in self.__union_params__))
540 return r
541
542 def __getitem__(self, parameters):
543 if self.__union_params__ is not None:
544 raise TypeError(
545 "Cannot subscript an existing Union. Use Union[u, t] instead.")
546 if parameters == ():
547 raise TypeError("Cannot take a Union of no types.")
548 if not isinstance(parameters, tuple):
549 parameters = (parameters,)
550 return self.__class__(self.__name__, self.__bases__,
551 dict(self.__dict__), parameters, _root=True)
552
553 def __eq__(self, other):
554 if not isinstance(other, UnionMeta):
555 return NotImplemented
556 return self.__union_set_params__ == other.__union_set_params__
557
558 def __hash__(self):
559 return hash(self.__union_set_params__)
560
Guido van Rossumd70fe632015-08-05 12:11:06 +0200561 def __instancecheck__(self, obj):
562 raise TypeError("Unions cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700563
564 def __subclasscheck__(self, cls):
565 if cls is Any:
566 return True
567 if self.__union_params__ is None:
568 return isinstance(cls, UnionMeta)
569 elif isinstance(cls, UnionMeta):
570 if cls.__union_params__ is None:
571 return False
572 return all(issubclass(c, self) for c in (cls.__union_params__))
573 elif isinstance(cls, TypeVar):
574 if cls in self.__union_params__:
575 return True
576 if cls.__constraints__:
577 return issubclass(Union[cls.__constraints__], self)
578 return False
579 else:
580 return any(issubclass(cls, t) for t in self.__union_params__)
581
582
583class Union(Final, metaclass=UnionMeta, _root=True):
584 """Union type; Union[X, Y] means either X or Y.
585
586 To define a union, use e.g. Union[int, str]. Details:
587
588 - The arguments must be types and there must be at least one.
589
590 - None as an argument is a special case and is replaced by
591 type(None).
592
593 - Unions of unions are flattened, e.g.::
594
595 Union[Union[int, str], float] == Union[int, str, float]
596
597 - Unions of a single argument vanish, e.g.::
598
599 Union[int] == int # The constructor actually returns int
600
601 - Redundant arguments are skipped, e.g.::
602
603 Union[int, str, int] == Union[int, str]
604
605 - When comparing unions, the argument order is ignored, e.g.::
606
607 Union[int, str] == Union[str, int]
608
609 - When two arguments have a subclass relationship, the least
610 derived argument is kept, e.g.::
611
612 class Employee: pass
613 class Manager(Employee): pass
614 Union[int, Employee, Manager] == Union[int, Employee]
615 Union[Manager, int, Employee] == Union[int, Employee]
616 Union[Employee, Manager] == Employee
617
618 - Corollary: if Any is present it is the sole survivor, e.g.::
619
620 Union[int, Any] == Any
621
622 - Similar for object::
623
624 Union[int, object] == object
625
626 - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
627
628 - You cannot subclass or instantiate a union.
629
630 - You cannot write Union[X][Y] (what would it mean?).
631
632 - You can use Optional[X] as a shorthand for Union[X, None].
633 """
634
635 # Unsubscripted Union type has params set to None.
636 __union_params__ = None
637 __union_set_params__ = None
638
639
640class OptionalMeta(TypingMeta):
641 """Metaclass for Optional."""
642
643 def __new__(cls, name, bases, namespace, _root=False):
644 return super().__new__(cls, name, bases, namespace, _root=_root)
645
646 def __getitem__(self, arg):
647 arg = _type_check(arg, "Optional[t] requires a single type.")
648 return Union[arg, type(None)]
649
650
651class Optional(Final, metaclass=OptionalMeta, _root=True):
652 """Optional type.
653
654 Optional[X] is equivalent to Union[X, type(None)].
655 """
656
Guido van Rossumd70fe632015-08-05 12:11:06 +0200657 __slots__ = ()
658
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700659
660class TupleMeta(TypingMeta):
661 """Metaclass for Tuple."""
662
663 def __new__(cls, name, bases, namespace, parameters=None,
664 use_ellipsis=False, _root=False):
665 self = super().__new__(cls, name, bases, namespace, _root=_root)
666 self.__tuple_params__ = parameters
667 self.__tuple_use_ellipsis__ = use_ellipsis
668 return self
669
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700670 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700671 if self.__tuple_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700672 _get_type_vars(self.__tuple_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700673
674 def _eval_type(self, globalns, localns):
675 tp = self.__tuple_params__
676 if tp is None:
677 return self
678 p = tuple(_eval_type(t, globalns, localns) for t in tp)
679 if p == self.__tuple_params__:
680 return self
681 else:
682 return self.__class__(self.__name__, self.__bases__, {},
683 p, _root=True)
684
685 def __repr__(self):
686 r = super().__repr__()
687 if self.__tuple_params__ is not None:
688 params = [_type_repr(p) for p in self.__tuple_params__]
689 if self.__tuple_use_ellipsis__:
690 params.append('...')
Guido van Rossum91185fe2016-06-08 11:19:11 -0700691 if not params:
692 params.append('()')
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700693 r += '[%s]' % (
694 ', '.join(params))
695 return r
696
697 def __getitem__(self, parameters):
698 if self.__tuple_params__ is not None:
699 raise TypeError("Cannot re-parameterize %r" % (self,))
700 if not isinstance(parameters, tuple):
701 parameters = (parameters,)
702 if len(parameters) == 2 and parameters[1] == Ellipsis:
703 parameters = parameters[:1]
704 use_ellipsis = True
705 msg = "Tuple[t, ...]: t must be a type."
706 else:
707 use_ellipsis = False
708 msg = "Tuple[t0, t1, ...]: each t must be a type."
709 parameters = tuple(_type_check(p, msg) for p in parameters)
710 return self.__class__(self.__name__, self.__bases__,
711 dict(self.__dict__), parameters,
712 use_ellipsis=use_ellipsis, _root=True)
713
714 def __eq__(self, other):
715 if not isinstance(other, TupleMeta):
716 return NotImplemented
Guido van Rossum5abcbb32016-04-18 07:37:41 -0700717 return (self.__tuple_params__ == other.__tuple_params__ and
718 self.__tuple_use_ellipsis__ == other.__tuple_use_ellipsis__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700719
720 def __hash__(self):
721 return hash(self.__tuple_params__)
722
Guido van Rossumd70fe632015-08-05 12:11:06 +0200723 def __instancecheck__(self, obj):
724 raise TypeError("Tuples cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700725
726 def __subclasscheck__(self, cls):
727 if cls is Any:
728 return True
729 if not isinstance(cls, type):
730 return super().__subclasscheck__(cls) # To TypeError.
731 if issubclass(cls, tuple):
732 return True # Special case.
733 if not isinstance(cls, TupleMeta):
734 return super().__subclasscheck__(cls) # False.
735 if self.__tuple_params__ is None:
736 return True
737 if cls.__tuple_params__ is None:
738 return False # ???
739 if cls.__tuple_use_ellipsis__ != self.__tuple_use_ellipsis__:
740 return False
741 # Covariance.
742 return (len(self.__tuple_params__) == len(cls.__tuple_params__) and
743 all(issubclass(x, p)
744 for x, p in zip(cls.__tuple_params__,
745 self.__tuple_params__)))
746
747
748class Tuple(Final, metaclass=TupleMeta, _root=True):
749 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
750
751 Example: Tuple[T1, T2] is a tuple of two elements corresponding
752 to type variables T1 and T2. Tuple[int, float, str] is a tuple
753 of an int, a float and a string.
754
755 To specify a variable-length tuple of homogeneous type, use Sequence[T].
756 """
757
Guido van Rossumd70fe632015-08-05 12:11:06 +0200758 __slots__ = ()
759
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700760
761class CallableMeta(TypingMeta):
762 """Metaclass for Callable."""
763
764 def __new__(cls, name, bases, namespace, _root=False,
765 args=None, result=None):
766 if args is None and result is None:
767 pass # Must be 'class Callable'.
768 else:
769 if args is not Ellipsis:
770 if not isinstance(args, list):
771 raise TypeError("Callable[args, result]: "
772 "args must be a list."
773 " Got %.100r." % (args,))
774 msg = "Callable[[arg, ...], result]: each arg must be a type."
775 args = tuple(_type_check(arg, msg) for arg in args)
776 msg = "Callable[args, result]: result must be a type."
777 result = _type_check(result, msg)
778 self = super().__new__(cls, name, bases, namespace, _root=_root)
779 self.__args__ = args
780 self.__result__ = result
781 return self
782
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700783 def _get_type_vars(self, tvars):
Guido van Rossumefa798d2016-08-23 11:01:50 -0700784 if self.__args__ and self.__args__ is not Ellipsis:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700785 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700786
787 def _eval_type(self, globalns, localns):
788 if self.__args__ is None and self.__result__ is None:
789 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200790 if self.__args__ is Ellipsis:
791 args = self.__args__
792 else:
793 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700794 result = _eval_type(self.__result__, globalns, localns)
795 if args == self.__args__ and result == self.__result__:
796 return self
797 else:
798 return self.__class__(self.__name__, self.__bases__, {},
799 args=args, result=result, _root=True)
800
801 def __repr__(self):
802 r = super().__repr__()
803 if self.__args__ is not None or self.__result__ is not None:
804 if self.__args__ is Ellipsis:
805 args_r = '...'
806 else:
807 args_r = '[%s]' % ', '.join(_type_repr(t)
808 for t in self.__args__)
809 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
810 return r
811
812 def __getitem__(self, parameters):
813 if self.__args__ is not None or self.__result__ is not None:
814 raise TypeError("This Callable type is already parameterized.")
815 if not isinstance(parameters, tuple) or len(parameters) != 2:
816 raise TypeError(
817 "Callable must be used as Callable[[arg, ...], result].")
818 args, result = parameters
819 return self.__class__(self.__name__, self.__bases__,
820 dict(self.__dict__), _root=True,
821 args=args, result=result)
822
823 def __eq__(self, other):
824 if not isinstance(other, CallableMeta):
825 return NotImplemented
826 return (self.__args__ == other.__args__ and
827 self.__result__ == other.__result__)
828
829 def __hash__(self):
830 return hash(self.__args__) ^ hash(self.__result__)
831
Guido van Rossumd70fe632015-08-05 12:11:06 +0200832 def __instancecheck__(self, obj):
833 # For unparametrized Callable we allow this, because
834 # typing.Callable should be equivalent to
835 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700836 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200837 return isinstance(obj, collections_abc.Callable)
838 else:
839 raise TypeError("Callable[] cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700840
841 def __subclasscheck__(self, cls):
842 if cls is Any:
843 return True
844 if not isinstance(cls, CallableMeta):
845 return super().__subclasscheck__(cls)
846 if self.__args__ is None and self.__result__ is None:
847 return True
848 # We're not doing covariance or contravariance -- this is *invariance*.
849 return self == cls
850
851
852class Callable(Final, metaclass=CallableMeta, _root=True):
853 """Callable type; Callable[[int], str] is a function of (int) -> str.
854
855 The subscription syntax must always be used with exactly two
856 values: the argument list and the return type. The argument list
857 must be a list of types; the return type must be a single type.
858
859 There is no syntax to indicate optional or keyword arguments,
860 such function types are rarely used as callback types.
861 """
862
Guido van Rossumd70fe632015-08-05 12:11:06 +0200863 __slots__ = ()
864
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700865
866def _gorg(a):
867 """Return the farthest origin of a generic class."""
868 assert isinstance(a, GenericMeta)
869 while a.__origin__ is not None:
870 a = a.__origin__
871 return a
872
873
874def _geqv(a, b):
875 """Return whether two generic classes are equivalent.
876
877 The intention is to consider generic class X and any of its
878 parameterized forms (X[T], X[int], etc.) as equivalent.
879
880 However, X is not equivalent to a subclass of X.
881
882 The relation is reflexive, symmetric and transitive.
883 """
884 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
885 # Reduce each to its origin.
886 return _gorg(a) is _gorg(b)
887
888
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700889def _next_in_mro(cls):
890 """Helper for Generic.__new__.
891
892 Returns the class after the last occurrence of Generic or
893 Generic[...] in cls.__mro__.
894 """
895 next_in_mro = object
896 # Look for the last occurrence of Generic or Generic[...].
897 for i, c in enumerate(cls.__mro__[:-1]):
898 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
899 next_in_mro = cls.__mro__[i+1]
900 return next_in_mro
901
902
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700903class GenericMeta(TypingMeta, abc.ABCMeta):
904 """Metaclass for generic types."""
905
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700906 def __new__(cls, name, bases, namespace,
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700907 tvars=None, args=None, origin=None, extra=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700908 self = super().__new__(cls, name, bases, namespace, _root=True)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700909
910 if tvars is not None:
911 # Called from __getitem__() below.
912 assert origin is not None
913 assert all(isinstance(t, TypeVar) for t in tvars), tvars
914 else:
915 # Called from class statement.
916 assert tvars is None, tvars
917 assert args is None, args
918 assert origin is None, origin
919
920 # Get the full set of tvars from the bases.
921 tvars = _type_vars(bases)
922 # Look for Generic[T1, ..., Tn].
923 # If found, tvars must be a subset of it.
924 # If not found, tvars is it.
925 # Also check for and reject plain Generic,
926 # and reject multiple Generic[...].
927 gvars = None
928 for base in bases:
929 if base is Generic:
930 raise TypeError("Cannot inherit from plain Generic")
931 if (isinstance(base, GenericMeta) and
932 base.__origin__ is Generic):
933 if gvars is not None:
934 raise TypeError(
935 "Cannot inherit from Generic[...] multiple types.")
936 gvars = base.__parameters__
937 if gvars is None:
938 gvars = tvars
939 else:
940 tvarset = set(tvars)
941 gvarset = set(gvars)
942 if not tvarset <= gvarset:
943 raise TypeError(
944 "Some type variables (%s) "
945 "are not listed in Generic[%s]" %
946 (", ".join(str(t) for t in tvars if t not in gvarset),
947 ", ".join(str(g) for g in gvars)))
948 tvars = gvars
949
950 self.__parameters__ = tvars
951 self.__args__ = args
952 self.__origin__ = origin
Guido van Rossum1cea70f2016-05-18 08:35:00 -0700953 self.__extra__ = extra
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700954 # Speed hack (https://github.com/python/typing/issues/196).
955 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700956 return self
957
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700958 def _get_type_vars(self, tvars):
959 if self.__origin__ and self.__parameters__:
960 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700961
962 def __repr__(self):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700963 if self.__origin__ is not None:
964 r = repr(self.__origin__)
965 else:
966 r = super().__repr__()
967 if self.__args__:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700968 r += '[%s]' % (
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700969 ', '.join(_type_repr(p) for p in self.__args__))
970 if self.__parameters__:
971 r += '<%s>' % (
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700972 ', '.join(_type_repr(p) for p in self.__parameters__))
973 return r
974
975 def __eq__(self, other):
976 if not isinstance(other, GenericMeta):
977 return NotImplemented
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700978 if self.__origin__ is not None:
979 return (self.__origin__ is other.__origin__ and
980 self.__args__ == other.__args__ and
981 self.__parameters__ == other.__parameters__)
982 else:
983 return self is other
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700984
985 def __hash__(self):
986 return hash((self.__name__, self.__parameters__))
987
988 def __getitem__(self, params):
989 if not isinstance(params, tuple):
990 params = (params,)
991 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700992 raise TypeError(
993 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700994 msg = "Parameters to generic types must be types."
995 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700996 if self is Generic:
997 # Generic can only be subscripted with unique type variables.
998 if not all(isinstance(p, TypeVar) for p in params):
999 raise TypeError(
1000 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +02001001 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -07001002 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001003 "Parameters to Generic[...] must all be unique")
1004 tvars = params
1005 args = None
1006 elif self is _Protocol:
1007 # _Protocol is internal, don't check anything.
1008 tvars = params
1009 args = None
1010 elif self.__origin__ in (Generic, _Protocol):
1011 # Can't subscript Generic[...] or _Protocol[...].
1012 raise TypeError("Cannot subscript already-subscripted %s" %
1013 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001014 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001015 # Subscripting a regular Generic subclass.
1016 if not self.__parameters__:
1017 raise TypeError("%s is not a generic class" % repr(self))
1018 alen = len(params)
1019 elen = len(self.__parameters__)
1020 if alen != elen:
1021 raise TypeError(
1022 "Too %s parameters for %s; actual %s, expected %s" %
1023 ("many" if alen > elen else "few", repr(self), alen, elen))
1024 tvars = _type_vars(params)
1025 args = params
1026 return self.__class__(self.__name__,
1027 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001028 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001029 tvars=tvars,
1030 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001031 origin=self,
1032 extra=self.__extra__)
1033
Guido van Rossum1b669102015-09-04 12:15:54 -07001034 def __instancecheck__(self, instance):
1035 # Since we extend ABC.__subclasscheck__ and
1036 # ABC.__instancecheck__ inlines the cache checking done by the
1037 # latter, we must extend __instancecheck__ too. For simplicity
1038 # we just skip the cache check -- instance checks for generic
1039 # classes are supposed to be rare anyways.
1040 return self.__subclasscheck__(instance.__class__)
1041
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001042 def __subclasscheck__(self, cls):
1043 if cls is Any:
1044 return True
1045 if isinstance(cls, GenericMeta):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001046 # For a covariant class C(Generic[T]),
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001047 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
1048 origin = self.__origin__
1049 if origin is not None and origin is cls.__origin__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001050 assert len(self.__args__) == len(origin.__parameters__)
1051 assert len(cls.__args__) == len(origin.__parameters__)
1052 for p_self, p_cls, p_origin in zip(self.__args__,
1053 cls.__args__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001054 origin.__parameters__):
1055 if isinstance(p_origin, TypeVar):
1056 if p_origin.__covariant__:
1057 # Covariant -- p_cls must be a subclass of p_self.
1058 if not issubclass(p_cls, p_self):
1059 break
1060 elif p_origin.__contravariant__:
1061 # Contravariant. I think it's the opposite. :-)
1062 if not issubclass(p_self, p_cls):
1063 break
1064 else:
1065 # Invariant -- p_cls and p_self must equal.
1066 if p_self != p_cls:
1067 break
1068 else:
1069 # If the origin's parameter is not a typevar,
1070 # insist on invariance.
1071 if p_self != p_cls:
1072 break
1073 else:
1074 return True
1075 # If we break out of the loop, the superclass gets a chance.
1076 if super().__subclasscheck__(cls):
1077 return True
1078 if self.__extra__ is None or isinstance(cls, GenericMeta):
1079 return False
1080 return issubclass(cls, self.__extra__)
1081
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001082
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001083# Prevent checks for Generic to crash when defining Generic.
1084Generic = None
1085
1086
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001087class Generic(metaclass=GenericMeta):
1088 """Abstract base class for generic types.
1089
1090 A generic type is typically declared by inheriting from an
1091 instantiation of this class with one or more type variables.
1092 For example, a generic mapping type might be defined as::
1093
1094 class Mapping(Generic[KT, VT]):
1095 def __getitem__(self, key: KT) -> VT:
1096 ...
1097 # Etc.
1098
1099 This class can then be used as follows::
1100
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001101 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001102 try:
1103 return mapping[key]
1104 except KeyError:
1105 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001106 """
1107
Guido van Rossumd70fe632015-08-05 12:11:06 +02001108 __slots__ = ()
1109
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001110 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001111 if cls.__origin__ is None:
1112 return cls.__next_in_mro__.__new__(cls)
1113 else:
1114 origin = _gorg(cls)
1115 obj = cls.__next_in_mro__.__new__(origin)
1116 obj.__init__(*args, **kwds)
1117 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001118
1119
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001120class _ClassVar(metaclass=TypingMeta, _root=True):
1121 """Special type construct to mark class variables.
1122
1123 An annotation wrapped in ClassVar indicates that a given
1124 attribute is intended to be used as a class variable and
1125 should not be set on instances of that class. Usage::
1126
1127 class Starship:
1128 stats: ClassVar[Dict[str, int]] = {} # class variable
1129 damage: int = 10 # instance variable
1130
1131 ClassVar accepts only types and cannot be further subscribed.
1132
1133 Note that ClassVar is not a class itself, and should not
1134 be used with isinstance() or issubclass().
1135 """
1136
1137 def __init__(self, tp=None, _root=False):
1138 cls = type(self)
1139 if _root:
1140 self.__type__ = tp
1141 else:
1142 raise TypeError('Cannot initialize {}'.format(cls.__name__[1:]))
1143
1144 def __getitem__(self, item):
1145 cls = type(self)
1146 if self.__type__ is None:
1147 return cls(_type_check(item,
1148 '{} accepts only types.'.format(cls.__name__[1:])),
1149 _root=True)
1150 raise TypeError('{} cannot be further subscripted'
1151 .format(cls.__name__[1:]))
1152
1153 def _eval_type(self, globalns, localns):
1154 return type(self)(_eval_type(self.__type__, globalns, localns),
1155 _root=True)
1156
1157 def _get_type_vars(self, tvars):
1158 if self.__type__:
1159 _get_type_vars(self.__type__, tvars)
1160
1161 def __repr__(self):
1162 cls = type(self)
1163 if not self.__type__:
1164 return '{}.{}'.format(cls.__module__, cls.__name__[1:])
1165 return '{}.{}[{}]'.format(cls.__module__, cls.__name__[1:],
1166 _type_repr(self.__type__))
1167
1168 def __hash__(self):
1169 return hash((type(self).__name__, self.__type__))
1170
1171 def __eq__(self, other):
1172 if not isinstance(other, _ClassVar):
1173 return NotImplemented
1174 if self.__type__ is not None:
1175 return self.__type__ == other.__type__
1176 return self is other
1177
1178ClassVar = _ClassVar(_root=True)
1179
1180
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001181def cast(typ, val):
1182 """Cast a value to a type.
1183
1184 This returns the value unchanged. To the type checker this
1185 signals that the return value has the designated type, but at
1186 runtime we intentionally don't check anything (we want this
1187 to be as fast as possible).
1188 """
1189 return val
1190
1191
1192def _get_defaults(func):
1193 """Internal helper to extract the default arguments, by name."""
1194 code = func.__code__
1195 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001196 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001197 arg_names = arg_names[:pos_count]
1198 defaults = func.__defaults__ or ()
1199 kwdefaults = func.__kwdefaults__
1200 res = dict(kwdefaults) if kwdefaults else {}
1201 pos_offset = pos_count - len(defaults)
1202 for name, value in zip(arg_names[pos_offset:], defaults):
1203 assert name not in res
1204 res[name] = value
1205 return res
1206
1207
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001208if sys.version_info[:2] >= (3, 3):
1209 def get_type_hints(obj, globalns=None, localns=None):
1210 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001211
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001212 This is often the same as obj.__annotations__, but it handles
1213 forward references encoded as string literals, and if necessary
1214 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001215
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001216 The argument may be a module, class, method, or function. The annotations
1217 are returned as a dictionary, or in the case of a class, a ChainMap of
1218 dictionaries.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001219
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001220 TypeError is raised if the argument is not of a type that can contain
1221 annotations, and an empty dictionary is returned if no annotations are
1222 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001223
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001224 BEWARE -- the behavior of globalns and localns is counterintuitive
1225 (unless you are familiar with how eval() and exec() work). The
1226 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001227
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001228 - If no dict arguments are passed, an attempt is made to use the
1229 globals from obj, and these are also used as the locals. If the
1230 object does not appear to have globals, an exception is raised.
1231
1232 - If one dict argument is passed, it is used for both globals and
1233 locals.
1234
1235 - If two dict arguments are passed, they specify globals and
1236 locals, respectively.
1237 """
1238
1239 if getattr(obj, '__no_type_check__', None):
1240 return {}
1241 if globalns is None:
1242 globalns = getattr(obj, '__globals__', {})
1243 if localns is None:
1244 localns = globalns
1245 elif localns is None:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001246 localns = globalns
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001247
1248 if (isinstance(obj, types.FunctionType) or
1249 isinstance(obj, types.BuiltinFunctionType) or
1250 isinstance(obj, types.MethodType)):
1251 defaults = _get_defaults(obj)
1252 hints = obj.__annotations__
1253 for name, value in hints.items():
1254 if value is None:
1255 value = type(None)
1256 if isinstance(value, str):
1257 value = _ForwardRef(value)
1258 value = _eval_type(value, globalns, localns)
1259 if name in defaults and defaults[name] is None:
1260 value = Optional[value]
1261 hints[name] = value
1262 return hints
1263
1264 if isinstance(obj, types.ModuleType):
1265 try:
1266 hints = obj.__annotations__
1267 except AttributeError:
1268 return {}
1269 # we keep only those annotations that can be accessed on module
1270 members = obj.__dict__
1271 hints = {name: value for name, value in hints.items()
1272 if name in members}
1273 for name, value in hints.items():
1274 if value is None:
1275 value = type(None)
1276 if isinstance(value, str):
1277 value = _ForwardRef(value)
1278 value = _eval_type(value, globalns, localns)
1279 hints[name] = value
1280 return hints
1281
1282 if isinstance(object, type):
1283 cmap = None
1284 for base in reversed(obj.__mro__):
1285 new_map = collections.ChainMap if cmap is None else cmap.new_child
1286 try:
1287 hints = base.__dict__['__annotations__']
1288 except KeyError:
1289 cmap = new_map()
1290 else:
1291 for name, value in hints.items():
1292 if value is None:
1293 value = type(None)
1294 if isinstance(value, str):
1295 value = _ForwardRef(value)
1296 value = _eval_type(value, globalns, localns)
1297 hints[name] = value
1298 cmap = new_map(hints)
1299 return cmap
1300
1301 raise TypeError('{!r} is not a module, class, method, '
1302 'or function.'.format(obj))
1303
1304else:
1305 def get_type_hints(obj, globalns=None, localns=None):
1306 """Return type hints for a function or method object.
1307
1308 This is often the same as obj.__annotations__, but it handles
1309 forward references encoded as string literals, and if necessary
1310 adds Optional[t] if a default value equal to None is set.
1311
1312 BEWARE -- the behavior of globalns and localns is counterintuitive
1313 (unless you are familiar with how eval() and exec() work). The
1314 search order is locals first, then globals.
1315
1316 - If no dict arguments are passed, an attempt is made to use the
1317 globals from obj, and these are also used as the locals. If the
1318 object does not appear to have globals, an exception is raised.
1319
1320 - If one dict argument is passed, it is used for both globals and
1321 locals.
1322
1323 - If two dict arguments are passed, they specify globals and
1324 locals, respectively.
1325 """
1326 if getattr(obj, '__no_type_check__', None):
1327 return {}
1328 if globalns is None:
1329 globalns = getattr(obj, '__globals__', {})
1330 if localns is None:
1331 localns = globalns
1332 elif localns is None:
1333 localns = globalns
1334 defaults = _get_defaults(obj)
1335 hints = dict(obj.__annotations__)
1336 for name, value in hints.items():
1337 if isinstance(value, str):
1338 value = _ForwardRef(value)
1339 value = _eval_type(value, globalns, localns)
1340 if name in defaults and defaults[name] is None:
1341 value = Optional[value]
1342 hints[name] = value
1343 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001344
1345
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001346def no_type_check(arg):
1347 """Decorator to indicate that annotations are not type hints.
1348
1349 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001350 applies recursively to all methods and classes defined in that class
1351 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001352
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001353 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001354 """
1355 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001356 arg_attrs = arg.__dict__.copy()
1357 for attr, val in arg.__dict__.items():
1358 if val in arg.__bases__:
1359 arg_attrs.pop(attr)
1360 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001361 if isinstance(obj, types.FunctionType):
1362 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001363 if isinstance(obj, type):
1364 no_type_check(obj)
1365 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001366 arg.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001367 except TypeError: # built-in classes
1368 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001369 return arg
1370
1371
1372def no_type_check_decorator(decorator):
1373 """Decorator to give another decorator the @no_type_check effect.
1374
1375 This wraps the decorator with something that wraps the decorated
1376 function in @no_type_check.
1377 """
1378
1379 @functools.wraps(decorator)
1380 def wrapped_decorator(*args, **kwds):
1381 func = decorator(*args, **kwds)
1382 func = no_type_check(func)
1383 return func
1384
1385 return wrapped_decorator
1386
1387
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001388def _overload_dummy(*args, **kwds):
1389 """Helper for @overload to raise when called."""
1390 raise NotImplementedError(
1391 "You should not call an overloaded function. "
1392 "A series of @overload-decorated functions "
1393 "outside a stub module should always be followed "
1394 "by an implementation that is not @overload-ed.")
1395
1396
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001397def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001398 """Decorator for overloaded functions/methods.
1399
1400 In a stub file, place two or more stub definitions for the same
1401 function in a row, each decorated with @overload. For example:
1402
1403 @overload
1404 def utf8(value: None) -> None: ...
1405 @overload
1406 def utf8(value: bytes) -> bytes: ...
1407 @overload
1408 def utf8(value: str) -> bytes: ...
1409
1410 In a non-stub file (i.e. a regular .py file), do the same but
1411 follow it with an implementation. The implementation should *not*
1412 be decorated with @overload. For example:
1413
1414 @overload
1415 def utf8(value: None) -> None: ...
1416 @overload
1417 def utf8(value: bytes) -> bytes: ...
1418 @overload
1419 def utf8(value: str) -> bytes: ...
1420 def utf8(value):
1421 # implementation goes here
1422 """
1423 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001424
1425
1426class _ProtocolMeta(GenericMeta):
1427 """Internal metaclass for _Protocol.
1428
1429 This exists so _Protocol classes can be generic without deriving
1430 from Generic.
1431 """
1432
Guido van Rossumd70fe632015-08-05 12:11:06 +02001433 def __instancecheck__(self, obj):
1434 raise TypeError("Protocols cannot be used with isinstance().")
1435
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001436 def __subclasscheck__(self, cls):
1437 if not self._is_protocol:
1438 # No structural checks since this isn't a protocol.
1439 return NotImplemented
1440
1441 if self is _Protocol:
1442 # Every class is a subclass of the empty protocol.
1443 return True
1444
1445 # Find all attributes defined in the protocol.
1446 attrs = self._get_protocol_attrs()
1447
1448 for attr in attrs:
1449 if not any(attr in d.__dict__ for d in cls.__mro__):
1450 return False
1451 return True
1452
1453 def _get_protocol_attrs(self):
1454 # Get all Protocol base classes.
1455 protocol_bases = []
1456 for c in self.__mro__:
1457 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1458 protocol_bases.append(c)
1459
1460 # Get attributes included in protocol.
1461 attrs = set()
1462 for base in protocol_bases:
1463 for attr in base.__dict__.keys():
1464 # Include attributes not defined in any non-protocol bases.
1465 for c in self.__mro__:
1466 if (c is not base and attr in c.__dict__ and
1467 not getattr(c, '_is_protocol', False)):
1468 break
1469 else:
1470 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001471 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001472 attr != '__annotations__' and
1473 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001474 attr != '_is_protocol' and
1475 attr != '__dict__' and
1476 attr != '__args__' and
1477 attr != '__slots__' and
1478 attr != '_get_protocol_attrs' and
1479 attr != '__next_in_mro__' and
1480 attr != '__parameters__' and
1481 attr != '__origin__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001482 attr != '__extra__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001483 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001484 attrs.add(attr)
1485
1486 return attrs
1487
1488
1489class _Protocol(metaclass=_ProtocolMeta):
1490 """Internal base class for protocol classes.
1491
1492 This implements a simple-minded structural isinstance check
1493 (similar but more general than the one-offs in collections.abc
1494 such as Hashable).
1495 """
1496
Guido van Rossumd70fe632015-08-05 12:11:06 +02001497 __slots__ = ()
1498
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001499 _is_protocol = True
1500
1501
1502# Various ABCs mimicking those in collections.abc.
1503# A few are simply re-exported for completeness.
1504
1505Hashable = collections_abc.Hashable # Not generic.
1506
1507
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001508if hasattr(collections_abc, 'Awaitable'):
1509 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1510 __slots__ = ()
1511else:
1512 Awaitable = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001513
1514
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001515if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001516
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001517 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1518 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001519
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001520 class AsyncIterator(AsyncIterable[T_co],
1521 extra=collections_abc.AsyncIterator):
1522 __slots__ = ()
1523
1524else:
1525 AsyncIterable = None
1526 AsyncIterator = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001527
1528
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001529class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001530 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001531
1532
1533class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001534 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001535
1536
1537class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001538 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001539
1540 @abstractmethod
1541 def __int__(self) -> int:
1542 pass
1543
1544
1545class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001546 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001547
1548 @abstractmethod
1549 def __float__(self) -> float:
1550 pass
1551
1552
1553class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001554 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001555
1556 @abstractmethod
1557 def __complex__(self) -> complex:
1558 pass
1559
1560
1561class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001562 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001563
1564 @abstractmethod
1565 def __bytes__(self) -> bytes:
1566 pass
1567
1568
Guido van Rossumd70fe632015-08-05 12:11:06 +02001569class SupportsAbs(_Protocol[T_co]):
1570 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001571
1572 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001573 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001574 pass
1575
1576
Guido van Rossumd70fe632015-08-05 12:11:06 +02001577class SupportsRound(_Protocol[T_co]):
1578 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001579
1580 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001581 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001582 pass
1583
1584
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001585if hasattr(collections_abc, 'Reversible'):
1586 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1587 __slots__ = ()
1588else:
1589 class Reversible(_Protocol[T_co]):
1590 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001591
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001592 @abstractmethod
1593 def __reversed__(self) -> 'Iterator[T_co]':
1594 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001595
1596
1597Sized = collections_abc.Sized # Not generic.
1598
1599
1600class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001601 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001602
1603
Guido van Rossumefa798d2016-08-23 11:01:50 -07001604if hasattr(collections_abc, 'Collection'):
1605 class Collection(Sized, Iterable[T_co], Container[T_co],
1606 extra=collections_abc.Collection):
1607 __slots__ = ()
1608
1609 __all__.append('Collection')
1610
1611
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001612# Callable was defined earlier.
1613
Guido van Rossumefa798d2016-08-23 11:01:50 -07001614if hasattr(collections_abc, 'Collection'):
1615 class AbstractSet(Collection[T_co],
1616 extra=collections_abc.Set):
1617 pass
1618else:
1619 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1620 extra=collections_abc.Set):
1621 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001622
1623
1624class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
1625 pass
1626
1627
Guido van Rossumefa798d2016-08-23 11:01:50 -07001628# NOTE: It is only covariant in the value type.
1629if hasattr(collections_abc, 'Collection'):
1630 class Mapping(Collection[KT], Generic[KT, VT_co],
1631 extra=collections_abc.Mapping):
1632 pass
1633else:
1634 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1635 extra=collections_abc.Mapping):
1636 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001637
1638
1639class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
1640 pass
1641
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001642if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001643 if hasattr(collections_abc, 'Collection'):
1644 class Sequence(Reversible[T_co], Collection[T_co],
1645 extra=collections_abc.Sequence):
1646 pass
1647 else:
1648 class Sequence(Sized, Reversible[T_co], Container[T_co],
1649 extra=collections_abc.Sequence):
1650 pass
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001651else:
1652 class Sequence(Sized, Iterable[T_co], Container[T_co],
1653 extra=collections_abc.Sequence):
1654 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001655
1656
1657class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
1658 pass
1659
1660
1661class ByteString(Sequence[int], extra=collections_abc.ByteString):
1662 pass
1663
1664
1665ByteString.register(type(memoryview(b'')))
1666
1667
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001668class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001669
1670 def __new__(cls, *args, **kwds):
1671 if _geqv(cls, List):
1672 raise TypeError("Type List cannot be instantiated; "
1673 "use list() instead")
1674 return list.__new__(cls, *args, **kwds)
1675
1676
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001677class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001678
1679 def __new__(cls, *args, **kwds):
1680 if _geqv(cls, Set):
1681 raise TypeError("Type Set cannot be instantiated; "
1682 "use set() instead")
1683 return set.__new__(cls, *args, **kwds)
1684
1685
Guido van Rossumd70fe632015-08-05 12:11:06 +02001686class _FrozenSetMeta(GenericMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001687 """This metaclass ensures set is not a subclass of FrozenSet.
1688
1689 Without this metaclass, set would be considered a subclass of
1690 FrozenSet, because FrozenSet.__extra__ is collections.abc.Set, and
1691 set is a subclass of that.
1692 """
1693
1694 def __subclasscheck__(self, cls):
1695 if issubclass(cls, Set):
1696 return False
1697 return super().__subclasscheck__(cls)
1698
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001699
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001700class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta,
1701 extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001702 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001703
1704 def __new__(cls, *args, **kwds):
1705 if _geqv(cls, FrozenSet):
1706 raise TypeError("Type FrozenSet cannot be instantiated; "
1707 "use frozenset() instead")
1708 return frozenset.__new__(cls, *args, **kwds)
1709
1710
1711class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
1712 pass
1713
1714
Guido van Rossumd70fe632015-08-05 12:11:06 +02001715class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001716 extra=collections_abc.KeysView):
1717 pass
1718
1719
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001720class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001721 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001722 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001723 extra=collections_abc.ItemsView):
1724 pass
1725
1726
1727class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
1728 pass
1729
1730
Brett Cannonf3ad0422016-04-15 10:51:30 -07001731if hasattr(contextlib, 'AbstractContextManager'):
1732 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1733 __slots__ = ()
1734 __all__.append('ContextManager')
1735
1736
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001737class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001738
1739 def __new__(cls, *args, **kwds):
1740 if _geqv(cls, Dict):
1741 raise TypeError("Type Dict cannot be instantiated; "
1742 "use dict() instead")
1743 return dict.__new__(cls, *args, **kwds)
1744
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001745class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
1746 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001747
1748 def __new__(cls, *args, **kwds):
1749 if _geqv(cls, DefaultDict):
1750 raise TypeError("Type DefaultDict cannot be instantiated; "
1751 "use collections.defaultdict() instead")
1752 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001753
1754# Determine what base class to use for Generator.
1755if hasattr(collections_abc, 'Generator'):
1756 # Sufficiently recent versions of 3.5 have a Generator ABC.
1757 _G_base = collections_abc.Generator
1758else:
1759 # Fall back on the exact type.
1760 _G_base = types.GeneratorType
1761
1762
1763class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1764 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001765 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001766
1767 def __new__(cls, *args, **kwds):
1768 if _geqv(cls, Generator):
1769 raise TypeError("Type Generator cannot be instantiated; "
1770 "create a subclass instead")
1771 return super().__new__(cls, *args, **kwds)
1772
1773
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001774# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07001775CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001776
1777
Guido van Rossumb22c7082016-05-26 09:56:19 -07001778# This is not a real generic class. Don't use outside annotations.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001779class Type(Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07001780 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001781
1782 For example, suppose we have the following classes::
1783
1784 class User: ... # Abstract base for User classes
1785 class BasicUser(User): ...
1786 class ProUser(User): ...
1787 class TeamUser(User): ...
1788
1789 And a function that takes a class argument that's a subclass of
1790 User and returns an instance of the corresponding class::
1791
1792 U = TypeVar('U', bound=User)
1793 def new_user(user_class: Type[U]) -> U:
1794 user = user_class()
1795 # (Here we could write the user object to a database)
1796 return user
1797
1798 joe = new_user(BasicUser)
1799
1800 At this point the type checker knows that joe has type BasicUser.
1801 """
1802
1803
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001804def _make_nmtuple(name, types):
1805 nm_tpl = collections.namedtuple(name, [n for n, t in types])
1806 nm_tpl._field_types = dict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001807 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001808 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001809 except (AttributeError, ValueError):
1810 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001811 return nm_tpl
1812
1813
1814if sys.version_info[:2] >= (3, 6):
1815 class NamedTupleMeta(type):
1816
1817 def __new__(cls, typename, bases, ns, *, _root=False):
1818 if _root:
1819 return super().__new__(cls, typename, bases, ns)
1820 types = ns.get('__annotations__', {})
1821 return _make_nmtuple(typename, types.items())
1822
1823 class NamedTuple(metaclass=NamedTupleMeta, _root=True):
1824 """Typed version of namedtuple.
1825
1826 Usage::
1827
1828 class Employee(NamedTuple):
1829 name: str
1830 id: int
1831
1832 This is equivalent to::
1833
1834 Employee = collections.namedtuple('Employee', ['name', 'id'])
1835
1836 The resulting class has one extra attribute: _field_types,
1837 giving a dict mapping field names to types. (The field names
1838 are in the _fields attribute, which is part of the namedtuple
1839 API.) Backward-compatible usage::
1840
1841 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1842 """
1843
1844 def __new__(self, typename, fields):
1845 return _make_nmtuple(typename, fields)
1846else:
1847 def NamedTuple(typename, fields):
1848 """Typed version of namedtuple.
1849
1850 Usage::
1851
1852 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1853
1854 This is equivalent to::
1855
1856 Employee = collections.namedtuple('Employee', ['name', 'id'])
1857
1858 The resulting class has one extra attribute: _field_types,
1859 giving a dict mapping field names to types. (The field names
1860 are in the _fields attribute, which is part of the namedtuple
1861 API.)
1862 """
1863 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001864
1865
Guido van Rossum91185fe2016-06-08 11:19:11 -07001866def NewType(name, tp):
1867 """NewType creates simple unique types with almost zero
1868 runtime overhead. NewType(name, tp) is considered a subtype of tp
1869 by static type checkers. At runtime, NewType(name, tp) returns
1870 a dummy function that simply returns its argument. Usage::
1871
1872 UserId = NewType('UserId', int)
1873
1874 def name_by_id(user_id: UserId) -> str:
1875 ...
1876
1877 UserId('user') # Fails type check
1878
1879 name_by_id(42) # Fails type check
1880 name_by_id(UserId(42)) # OK
1881
1882 num = UserId(5) + 1 # type: int
1883 """
1884
1885 def new_type(x):
1886 return x
1887
1888 new_type.__name__ = name
1889 new_type.__supertype__ = tp
1890 return new_type
1891
1892
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001893# Python-version-specific alias (Python 2: unicode; Python 3: str)
1894Text = str
1895
1896
Guido van Rossum91185fe2016-06-08 11:19:11 -07001897# Constant that's True when type checking, but False here.
1898TYPE_CHECKING = False
1899
1900
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001901class IO(Generic[AnyStr]):
1902 """Generic base class for TextIO and BinaryIO.
1903
1904 This is an abstract, generic version of the return of open().
1905
1906 NOTE: This does not distinguish between the different possible
1907 classes (text vs. binary, read vs. write vs. read/write,
1908 append-only, unbuffered). The TextIO and BinaryIO subclasses
1909 below capture the distinctions between text vs. binary, which is
1910 pervasive in the interface; however we currently do not offer a
1911 way to track the other distinctions in the type system.
1912 """
1913
Guido van Rossumd70fe632015-08-05 12:11:06 +02001914 __slots__ = ()
1915
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001916 @abstractproperty
1917 def mode(self) -> str:
1918 pass
1919
1920 @abstractproperty
1921 def name(self) -> str:
1922 pass
1923
1924 @abstractmethod
1925 def close(self) -> None:
1926 pass
1927
1928 @abstractmethod
1929 def closed(self) -> bool:
1930 pass
1931
1932 @abstractmethod
1933 def fileno(self) -> int:
1934 pass
1935
1936 @abstractmethod
1937 def flush(self) -> None:
1938 pass
1939
1940 @abstractmethod
1941 def isatty(self) -> bool:
1942 pass
1943
1944 @abstractmethod
1945 def read(self, n: int = -1) -> AnyStr:
1946 pass
1947
1948 @abstractmethod
1949 def readable(self) -> bool:
1950 pass
1951
1952 @abstractmethod
1953 def readline(self, limit: int = -1) -> AnyStr:
1954 pass
1955
1956 @abstractmethod
1957 def readlines(self, hint: int = -1) -> List[AnyStr]:
1958 pass
1959
1960 @abstractmethod
1961 def seek(self, offset: int, whence: int = 0) -> int:
1962 pass
1963
1964 @abstractmethod
1965 def seekable(self) -> bool:
1966 pass
1967
1968 @abstractmethod
1969 def tell(self) -> int:
1970 pass
1971
1972 @abstractmethod
1973 def truncate(self, size: int = None) -> int:
1974 pass
1975
1976 @abstractmethod
1977 def writable(self) -> bool:
1978 pass
1979
1980 @abstractmethod
1981 def write(self, s: AnyStr) -> int:
1982 pass
1983
1984 @abstractmethod
1985 def writelines(self, lines: List[AnyStr]) -> None:
1986 pass
1987
1988 @abstractmethod
1989 def __enter__(self) -> 'IO[AnyStr]':
1990 pass
1991
1992 @abstractmethod
1993 def __exit__(self, type, value, traceback) -> None:
1994 pass
1995
1996
1997class BinaryIO(IO[bytes]):
1998 """Typed version of the return of open() in binary mode."""
1999
Guido van Rossumd70fe632015-08-05 12:11:06 +02002000 __slots__ = ()
2001
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002002 @abstractmethod
2003 def write(self, s: Union[bytes, bytearray]) -> int:
2004 pass
2005
2006 @abstractmethod
2007 def __enter__(self) -> 'BinaryIO':
2008 pass
2009
2010
2011class TextIO(IO[str]):
2012 """Typed version of the return of open() in text mode."""
2013
Guido van Rossumd70fe632015-08-05 12:11:06 +02002014 __slots__ = ()
2015
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002016 @abstractproperty
2017 def buffer(self) -> BinaryIO:
2018 pass
2019
2020 @abstractproperty
2021 def encoding(self) -> str:
2022 pass
2023
2024 @abstractproperty
2025 def errors(self) -> str:
2026 pass
2027
2028 @abstractproperty
2029 def line_buffering(self) -> bool:
2030 pass
2031
2032 @abstractproperty
2033 def newlines(self) -> Any:
2034 pass
2035
2036 @abstractmethod
2037 def __enter__(self) -> 'TextIO':
2038 pass
2039
2040
2041class io:
2042 """Wrapper namespace for IO generic classes."""
2043
2044 __all__ = ['IO', 'TextIO', 'BinaryIO']
2045 IO = IO
2046 TextIO = TextIO
2047 BinaryIO = BinaryIO
2048
2049io.__name__ = __name__ + '.io'
2050sys.modules[io.__name__] = io
2051
2052
2053Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
2054 lambda p: p.pattern)
2055Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
2056 lambda m: m.re.pattern)
2057
2058
2059class re:
2060 """Wrapper namespace for re type aliases."""
2061
2062 __all__ = ['Pattern', 'Match']
2063 Pattern = Pattern
2064 Match = Match
2065
2066re.__name__ = __name__ + '.re'
2067sys.modules[re.__name__] = re