blob: 4bd2135436273baad554583ec02b8d5976d467e5 [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.
13
14
15# Please keep __all__ alphabetized within each category.
16__all__ = [
17 # Super-special typing primitives.
18 'Any',
19 'Callable',
20 'Generic',
21 'Optional',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070022 'Tuple',
23 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070024 'TypeVar',
25 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026
27 # ABCs (from collections.abc).
28 'AbstractSet', # collections.abc.Set.
Guido van Rossumf17c2002015-12-03 17:31:24 -080029 'Awaitable',
30 'AsyncIterator',
31 'AsyncIterable',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070032 'ByteString',
33 'Container',
34 'Hashable',
35 'ItemsView',
36 'Iterable',
37 'Iterator',
38 'KeysView',
39 'Mapping',
40 'MappingView',
41 'MutableMapping',
42 'MutableSequence',
43 'MutableSet',
44 'Sequence',
45 'Sized',
46 'ValuesView',
47
48 # Structural checks, a.k.a. protocols.
49 'Reversible',
50 'SupportsAbs',
51 'SupportsFloat',
52 'SupportsInt',
53 'SupportsRound',
54
55 # Concrete collection types.
56 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070057 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070058 'List',
59 'Set',
60 'NamedTuple', # Not really a type.
61 'Generator',
62
63 # One-off things.
64 'AnyStr',
65 'cast',
66 'get_type_hints',
67 'no_type_check',
68 'no_type_check_decorator',
69 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -070070 'Text',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070071]
72
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070073# The pseudo-submodules 're' and 'io' are part of the public
74# namespace, but excluded from __all__ because they might stomp on
75# legitimate imports of those modules.
76
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070077
78def _qualname(x):
79 if sys.version_info[:2] >= (3, 3):
80 return x.__qualname__
81 else:
82 # Fall back to just name.
83 return x.__name__
84
85
86class TypingMeta(type):
87 """Metaclass for every type defined below.
88
89 This overrides __new__() to require an extra keyword parameter
90 '_root', which serves as a guard against naive subclassing of the
91 typing classes. Any legitimate class defined using a metaclass
92 derived from TypingMeta (including internal subclasses created by
93 e.g. Union[X, Y]) must pass _root=True.
94
95 This also defines a dummy constructor (all the work is done in
96 __new__) and a nicer repr().
97 """
98
99 _is_protocol = False
100
101 def __new__(cls, name, bases, namespace, *, _root=False):
102 if not _root:
103 raise TypeError("Cannot subclass %s" %
104 (', '.join(map(_type_repr, bases)) or '()'))
105 return super().__new__(cls, name, bases, namespace)
106
107 def __init__(self, *args, **kwds):
108 pass
109
110 def _eval_type(self, globalns, localns):
111 """Override this in subclasses to interpret forward references.
112
113 For example, Union['C'] is internally stored as
114 Union[_ForwardRef('C')], which should evaluate to _Union[C],
115 where C is an object found in globalns or localns (searching
116 localns first, of course).
117 """
118 return self
119
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700120 def _get_type_vars(self, tvars):
121 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700122
123 def __repr__(self):
124 return '%s.%s' % (self.__module__, _qualname(self))
125
126
127class Final:
128 """Mix-in class to prevent instantiation."""
129
Guido van Rossumd70fe632015-08-05 12:11:06 +0200130 __slots__ = ()
131
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700132 def __new__(self, *args, **kwds):
133 raise TypeError("Cannot instantiate %r" % self.__class__)
134
135
136class _ForwardRef(TypingMeta):
137 """Wrapper to hold a forward reference."""
138
139 def __new__(cls, arg):
140 if not isinstance(arg, str):
141 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
142 try:
143 code = compile(arg, '<string>', 'eval')
144 except SyntaxError:
145 raise SyntaxError('ForwardRef must be an expression -- got %r' %
146 (arg,))
147 self = super().__new__(cls, arg, (), {}, _root=True)
148 self.__forward_arg__ = arg
149 self.__forward_code__ = code
150 self.__forward_evaluated__ = False
151 self.__forward_value__ = None
152 typing_globals = globals()
153 frame = sys._getframe(1)
154 while frame is not None and frame.f_globals is typing_globals:
155 frame = frame.f_back
156 assert frame is not None
157 self.__forward_frame__ = frame
158 return self
159
160 def _eval_type(self, globalns, localns):
161 if not isinstance(localns, dict):
162 raise TypeError('ForwardRef localns must be a dict -- got %r' %
163 (localns,))
164 if not isinstance(globalns, dict):
165 raise TypeError('ForwardRef globalns must be a dict -- got %r' %
166 (globalns,))
167 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:
276 if isinstance(t, TypingMeta):
277 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):
287 if isinstance(t, TypingMeta):
288 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)
309 if not isinstance(arg, (type, _TypeAlias)):
310 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
389 Type variables may be marked covariant or contravariant by passing
390 covariant=True or contravariant=True. See PEP 484 for more
391 details. By default type variables are invariant.
392
393 Type variables can be introspected. e.g.:
394
395 T.__name__ == 'T'
396 T.__constraints__ == ()
397 T.__covariant__ == False
398 T.__contravariant__ = False
399 A.__constraints__ == (str, bytes)
400 """
401
402 def __new__(cls, name, *constraints, bound=None,
403 covariant=False, contravariant=False):
404 self = super().__new__(cls, name, (Final,), {}, _root=True)
405 if covariant and contravariant:
406 raise ValueError("Bivariant type variables are not supported.")
407 self.__covariant__ = bool(covariant)
408 self.__contravariant__ = bool(contravariant)
409 if constraints and bound is not None:
410 raise TypeError("Constraints cannot be combined with bound=...")
411 if constraints and len(constraints) == 1:
412 raise TypeError("A single constraint is not allowed")
413 msg = "TypeVar(name, constraint, ...): constraints must be types."
414 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
415 if bound:
416 self.__bound__ = _type_check(bound, "Bound must be a type.")
417 else:
418 self.__bound__ = None
419 return self
420
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700421 def _get_type_vars(self, tvars):
422 if self not in tvars:
423 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700424
425 def __repr__(self):
426 if self.__covariant__:
427 prefix = '+'
428 elif self.__contravariant__:
429 prefix = '-'
430 else:
431 prefix = '~'
432 return prefix + self.__name__
433
434 def __instancecheck__(self, instance):
435 raise TypeError("Type variables cannot be used with isinstance().")
436
437 def __subclasscheck__(self, cls):
438 # TODO: Make this raise TypeError too?
439 if cls is self:
440 return True
441 if cls is Any:
442 return True
443 if self.__bound__ is not None:
444 return issubclass(cls, self.__bound__)
445 if self.__constraints__:
446 return any(issubclass(cls, c) for c in self.__constraints__)
447 return True
448
449
450# Some unconstrained type variables. These are used by the container types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700451# (These are not for export.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700452T = TypeVar('T') # Any type.
453KT = TypeVar('KT') # Key type.
454VT = TypeVar('VT') # Value type.
455T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
456V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700457VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
458T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
459
460# A useful type variable with constraints. This represents string types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700461# (This one *is* for export!)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700462AnyStr = TypeVar('AnyStr', bytes, str)
463
464
465class UnionMeta(TypingMeta):
466 """Metaclass for Union."""
467
468 def __new__(cls, name, bases, namespace, parameters=None, _root=False):
469 if parameters is None:
470 return super().__new__(cls, name, bases, namespace, _root=_root)
471 if not isinstance(parameters, tuple):
472 raise TypeError("Expected parameters=<tuple>")
473 # Flatten out Union[Union[...], ...] and type-check non-Union args.
474 params = []
475 msg = "Union[arg, ...]: each arg must be a type."
476 for p in parameters:
477 if isinstance(p, UnionMeta):
478 params.extend(p.__union_params__)
479 else:
480 params.append(_type_check(p, msg))
481 # Weed out strict duplicates, preserving the first of each occurrence.
482 all_params = set(params)
483 if len(all_params) < len(params):
484 new_params = []
485 for t in params:
486 if t in all_params:
487 new_params.append(t)
488 all_params.remove(t)
489 params = new_params
490 assert not all_params, all_params
491 # Weed out subclasses.
492 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
493 # If Any or object is present it will be the sole survivor.
494 # If both Any and object are present, Any wins.
495 # Never discard type variables, except against Any.
496 # (In particular, Union[str, AnyStr] != AnyStr.)
497 all_params = set(params)
498 for t1 in params:
499 if t1 is Any:
500 return Any
501 if isinstance(t1, TypeVar):
502 continue
Guido van Rossumca636ea2015-10-19 14:55:47 -0700503 if isinstance(t1, _TypeAlias):
504 # _TypeAlias is not a real class.
505 continue
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700506 if any(issubclass(t1, t2)
507 for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
508 all_params.remove(t1)
509 # It's not a union if there's only one type left.
510 if len(all_params) == 1:
511 return all_params.pop()
512 # Create a new class with these params.
513 self = super().__new__(cls, name, bases, {}, _root=True)
514 self.__union_params__ = tuple(t for t in params if t in all_params)
515 self.__union_set_params__ = frozenset(self.__union_params__)
516 return self
517
518 def _eval_type(self, globalns, localns):
519 p = tuple(_eval_type(t, globalns, localns)
520 for t in self.__union_params__)
521 if p == self.__union_params__:
522 return self
523 else:
524 return self.__class__(self.__name__, self.__bases__, {},
525 p, _root=True)
526
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700527 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700528 if self.__union_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700529 _get_type_vars(self.__union_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700530
531 def __repr__(self):
532 r = super().__repr__()
533 if self.__union_params__:
534 r += '[%s]' % (', '.join(_type_repr(t)
535 for t in self.__union_params__))
536 return r
537
538 def __getitem__(self, parameters):
539 if self.__union_params__ is not None:
540 raise TypeError(
541 "Cannot subscript an existing Union. Use Union[u, t] instead.")
542 if parameters == ():
543 raise TypeError("Cannot take a Union of no types.")
544 if not isinstance(parameters, tuple):
545 parameters = (parameters,)
546 return self.__class__(self.__name__, self.__bases__,
547 dict(self.__dict__), parameters, _root=True)
548
549 def __eq__(self, other):
550 if not isinstance(other, UnionMeta):
551 return NotImplemented
552 return self.__union_set_params__ == other.__union_set_params__
553
554 def __hash__(self):
555 return hash(self.__union_set_params__)
556
Guido van Rossumd70fe632015-08-05 12:11:06 +0200557 def __instancecheck__(self, obj):
558 raise TypeError("Unions cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700559
560 def __subclasscheck__(self, cls):
561 if cls is Any:
562 return True
563 if self.__union_params__ is None:
564 return isinstance(cls, UnionMeta)
565 elif isinstance(cls, UnionMeta):
566 if cls.__union_params__ is None:
567 return False
568 return all(issubclass(c, self) for c in (cls.__union_params__))
569 elif isinstance(cls, TypeVar):
570 if cls in self.__union_params__:
571 return True
572 if cls.__constraints__:
573 return issubclass(Union[cls.__constraints__], self)
574 return False
575 else:
576 return any(issubclass(cls, t) for t in self.__union_params__)
577
578
579class Union(Final, metaclass=UnionMeta, _root=True):
580 """Union type; Union[X, Y] means either X or Y.
581
582 To define a union, use e.g. Union[int, str]. Details:
583
584 - The arguments must be types and there must be at least one.
585
586 - None as an argument is a special case and is replaced by
587 type(None).
588
589 - Unions of unions are flattened, e.g.::
590
591 Union[Union[int, str], float] == Union[int, str, float]
592
593 - Unions of a single argument vanish, e.g.::
594
595 Union[int] == int # The constructor actually returns int
596
597 - Redundant arguments are skipped, e.g.::
598
599 Union[int, str, int] == Union[int, str]
600
601 - When comparing unions, the argument order is ignored, e.g.::
602
603 Union[int, str] == Union[str, int]
604
605 - When two arguments have a subclass relationship, the least
606 derived argument is kept, e.g.::
607
608 class Employee: pass
609 class Manager(Employee): pass
610 Union[int, Employee, Manager] == Union[int, Employee]
611 Union[Manager, int, Employee] == Union[int, Employee]
612 Union[Employee, Manager] == Employee
613
614 - Corollary: if Any is present it is the sole survivor, e.g.::
615
616 Union[int, Any] == Any
617
618 - Similar for object::
619
620 Union[int, object] == object
621
622 - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
623
624 - You cannot subclass or instantiate a union.
625
626 - You cannot write Union[X][Y] (what would it mean?).
627
628 - You can use Optional[X] as a shorthand for Union[X, None].
629 """
630
631 # Unsubscripted Union type has params set to None.
632 __union_params__ = None
633 __union_set_params__ = None
634
635
636class OptionalMeta(TypingMeta):
637 """Metaclass for Optional."""
638
639 def __new__(cls, name, bases, namespace, _root=False):
640 return super().__new__(cls, name, bases, namespace, _root=_root)
641
642 def __getitem__(self, arg):
643 arg = _type_check(arg, "Optional[t] requires a single type.")
644 return Union[arg, type(None)]
645
646
647class Optional(Final, metaclass=OptionalMeta, _root=True):
648 """Optional type.
649
650 Optional[X] is equivalent to Union[X, type(None)].
651 """
652
Guido van Rossumd70fe632015-08-05 12:11:06 +0200653 __slots__ = ()
654
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700655
656class TupleMeta(TypingMeta):
657 """Metaclass for Tuple."""
658
659 def __new__(cls, name, bases, namespace, parameters=None,
660 use_ellipsis=False, _root=False):
661 self = super().__new__(cls, name, bases, namespace, _root=_root)
662 self.__tuple_params__ = parameters
663 self.__tuple_use_ellipsis__ = use_ellipsis
664 return self
665
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700666 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700667 if self.__tuple_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700668 _get_type_vars(self.__tuple_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700669
670 def _eval_type(self, globalns, localns):
671 tp = self.__tuple_params__
672 if tp is None:
673 return self
674 p = tuple(_eval_type(t, globalns, localns) for t in tp)
675 if p == self.__tuple_params__:
676 return self
677 else:
678 return self.__class__(self.__name__, self.__bases__, {},
679 p, _root=True)
680
681 def __repr__(self):
682 r = super().__repr__()
683 if self.__tuple_params__ is not None:
684 params = [_type_repr(p) for p in self.__tuple_params__]
685 if self.__tuple_use_ellipsis__:
686 params.append('...')
687 r += '[%s]' % (
688 ', '.join(params))
689 return r
690
691 def __getitem__(self, parameters):
692 if self.__tuple_params__ is not None:
693 raise TypeError("Cannot re-parameterize %r" % (self,))
694 if not isinstance(parameters, tuple):
695 parameters = (parameters,)
696 if len(parameters) == 2 and parameters[1] == Ellipsis:
697 parameters = parameters[:1]
698 use_ellipsis = True
699 msg = "Tuple[t, ...]: t must be a type."
700 else:
701 use_ellipsis = False
702 msg = "Tuple[t0, t1, ...]: each t must be a type."
703 parameters = tuple(_type_check(p, msg) for p in parameters)
704 return self.__class__(self.__name__, self.__bases__,
705 dict(self.__dict__), parameters,
706 use_ellipsis=use_ellipsis, _root=True)
707
708 def __eq__(self, other):
709 if not isinstance(other, TupleMeta):
710 return NotImplemented
Guido van Rossum5abcbb32016-04-18 07:37:41 -0700711 return (self.__tuple_params__ == other.__tuple_params__ and
712 self.__tuple_use_ellipsis__ == other.__tuple_use_ellipsis__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700713
714 def __hash__(self):
715 return hash(self.__tuple_params__)
716
Guido van Rossumd70fe632015-08-05 12:11:06 +0200717 def __instancecheck__(self, obj):
718 raise TypeError("Tuples cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700719
720 def __subclasscheck__(self, cls):
721 if cls is Any:
722 return True
723 if not isinstance(cls, type):
724 return super().__subclasscheck__(cls) # To TypeError.
725 if issubclass(cls, tuple):
726 return True # Special case.
727 if not isinstance(cls, TupleMeta):
728 return super().__subclasscheck__(cls) # False.
729 if self.__tuple_params__ is None:
730 return True
731 if cls.__tuple_params__ is None:
732 return False # ???
733 if cls.__tuple_use_ellipsis__ != self.__tuple_use_ellipsis__:
734 return False
735 # Covariance.
736 return (len(self.__tuple_params__) == len(cls.__tuple_params__) and
737 all(issubclass(x, p)
738 for x, p in zip(cls.__tuple_params__,
739 self.__tuple_params__)))
740
741
742class Tuple(Final, metaclass=TupleMeta, _root=True):
743 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
744
745 Example: Tuple[T1, T2] is a tuple of two elements corresponding
746 to type variables T1 and T2. Tuple[int, float, str] is a tuple
747 of an int, a float and a string.
748
749 To specify a variable-length tuple of homogeneous type, use Sequence[T].
750 """
751
Guido van Rossumd70fe632015-08-05 12:11:06 +0200752 __slots__ = ()
753
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700754
755class CallableMeta(TypingMeta):
756 """Metaclass for Callable."""
757
758 def __new__(cls, name, bases, namespace, _root=False,
759 args=None, result=None):
760 if args is None and result is None:
761 pass # Must be 'class Callable'.
762 else:
763 if args is not Ellipsis:
764 if not isinstance(args, list):
765 raise TypeError("Callable[args, result]: "
766 "args must be a list."
767 " Got %.100r." % (args,))
768 msg = "Callable[[arg, ...], result]: each arg must be a type."
769 args = tuple(_type_check(arg, msg) for arg in args)
770 msg = "Callable[args, result]: result must be a type."
771 result = _type_check(result, msg)
772 self = super().__new__(cls, name, bases, namespace, _root=_root)
773 self.__args__ = args
774 self.__result__ = result
775 return self
776
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700777 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700778 if self.__args__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700779 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700780
781 def _eval_type(self, globalns, localns):
782 if self.__args__ is None and self.__result__ is None:
783 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200784 if self.__args__ is Ellipsis:
785 args = self.__args__
786 else:
787 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700788 result = _eval_type(self.__result__, globalns, localns)
789 if args == self.__args__ and result == self.__result__:
790 return self
791 else:
792 return self.__class__(self.__name__, self.__bases__, {},
793 args=args, result=result, _root=True)
794
795 def __repr__(self):
796 r = super().__repr__()
797 if self.__args__ is not None or self.__result__ is not None:
798 if self.__args__ is Ellipsis:
799 args_r = '...'
800 else:
801 args_r = '[%s]' % ', '.join(_type_repr(t)
802 for t in self.__args__)
803 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
804 return r
805
806 def __getitem__(self, parameters):
807 if self.__args__ is not None or self.__result__ is not None:
808 raise TypeError("This Callable type is already parameterized.")
809 if not isinstance(parameters, tuple) or len(parameters) != 2:
810 raise TypeError(
811 "Callable must be used as Callable[[arg, ...], result].")
812 args, result = parameters
813 return self.__class__(self.__name__, self.__bases__,
814 dict(self.__dict__), _root=True,
815 args=args, result=result)
816
817 def __eq__(self, other):
818 if not isinstance(other, CallableMeta):
819 return NotImplemented
820 return (self.__args__ == other.__args__ and
821 self.__result__ == other.__result__)
822
823 def __hash__(self):
824 return hash(self.__args__) ^ hash(self.__result__)
825
Guido van Rossumd70fe632015-08-05 12:11:06 +0200826 def __instancecheck__(self, obj):
827 # For unparametrized Callable we allow this, because
828 # typing.Callable should be equivalent to
829 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700830 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200831 return isinstance(obj, collections_abc.Callable)
832 else:
833 raise TypeError("Callable[] cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700834
835 def __subclasscheck__(self, cls):
836 if cls is Any:
837 return True
838 if not isinstance(cls, CallableMeta):
839 return super().__subclasscheck__(cls)
840 if self.__args__ is None and self.__result__ is None:
841 return True
842 # We're not doing covariance or contravariance -- this is *invariance*.
843 return self == cls
844
845
846class Callable(Final, metaclass=CallableMeta, _root=True):
847 """Callable type; Callable[[int], str] is a function of (int) -> str.
848
849 The subscription syntax must always be used with exactly two
850 values: the argument list and the return type. The argument list
851 must be a list of types; the return type must be a single type.
852
853 There is no syntax to indicate optional or keyword arguments,
854 such function types are rarely used as callback types.
855 """
856
Guido van Rossumd70fe632015-08-05 12:11:06 +0200857 __slots__ = ()
858
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
982 def __getitem__(self, params):
983 if not isinstance(params, tuple):
984 params = (params,)
985 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700986 raise TypeError(
987 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700988 msg = "Parameters to generic types must be types."
989 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700990 if self is Generic:
991 # Generic can only be subscripted with unique type variables.
992 if not all(isinstance(p, TypeVar) for p in params):
993 raise TypeError(
994 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200995 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -0700996 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700997 "Parameters to Generic[...] must all be unique")
998 tvars = params
999 args = None
1000 elif self is _Protocol:
1001 # _Protocol is internal, don't check anything.
1002 tvars = params
1003 args = None
1004 elif self.__origin__ in (Generic, _Protocol):
1005 # Can't subscript Generic[...] or _Protocol[...].
1006 raise TypeError("Cannot subscript already-subscripted %s" %
1007 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001008 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001009 # Subscripting a regular Generic subclass.
1010 if not self.__parameters__:
1011 raise TypeError("%s is not a generic class" % repr(self))
1012 alen = len(params)
1013 elen = len(self.__parameters__)
1014 if alen != elen:
1015 raise TypeError(
1016 "Too %s parameters for %s; actual %s, expected %s" %
1017 ("many" if alen > elen else "few", repr(self), alen, elen))
1018 tvars = _type_vars(params)
1019 args = params
1020 return self.__class__(self.__name__,
1021 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001022 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001023 tvars=tvars,
1024 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001025 origin=self,
1026 extra=self.__extra__)
1027
Guido van Rossum1b669102015-09-04 12:15:54 -07001028 def __instancecheck__(self, instance):
1029 # Since we extend ABC.__subclasscheck__ and
1030 # ABC.__instancecheck__ inlines the cache checking done by the
1031 # latter, we must extend __instancecheck__ too. For simplicity
1032 # we just skip the cache check -- instance checks for generic
1033 # classes are supposed to be rare anyways.
1034 return self.__subclasscheck__(instance.__class__)
1035
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001036 def __subclasscheck__(self, cls):
1037 if cls is Any:
1038 return True
1039 if isinstance(cls, GenericMeta):
1040 # For a class C(Generic[T]) where T is co-variant,
1041 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
1042 origin = self.__origin__
1043 if origin is not None and origin is cls.__origin__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001044 assert len(self.__args__) == len(origin.__parameters__)
1045 assert len(cls.__args__) == len(origin.__parameters__)
1046 for p_self, p_cls, p_origin in zip(self.__args__,
1047 cls.__args__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001048 origin.__parameters__):
1049 if isinstance(p_origin, TypeVar):
1050 if p_origin.__covariant__:
1051 # Covariant -- p_cls must be a subclass of p_self.
1052 if not issubclass(p_cls, p_self):
1053 break
1054 elif p_origin.__contravariant__:
1055 # Contravariant. I think it's the opposite. :-)
1056 if not issubclass(p_self, p_cls):
1057 break
1058 else:
1059 # Invariant -- p_cls and p_self must equal.
1060 if p_self != p_cls:
1061 break
1062 else:
1063 # If the origin's parameter is not a typevar,
1064 # insist on invariance.
1065 if p_self != p_cls:
1066 break
1067 else:
1068 return True
1069 # If we break out of the loop, the superclass gets a chance.
1070 if super().__subclasscheck__(cls):
1071 return True
1072 if self.__extra__ is None or isinstance(cls, GenericMeta):
1073 return False
1074 return issubclass(cls, self.__extra__)
1075
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001076
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001077# Prevent checks for Generic to crash when defining Generic.
1078Generic = None
1079
1080
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001081class Generic(metaclass=GenericMeta):
1082 """Abstract base class for generic types.
1083
1084 A generic type is typically declared by inheriting from an
1085 instantiation of this class with one or more type variables.
1086 For example, a generic mapping type might be defined as::
1087
1088 class Mapping(Generic[KT, VT]):
1089 def __getitem__(self, key: KT) -> VT:
1090 ...
1091 # Etc.
1092
1093 This class can then be used as follows::
1094
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001095 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001096 try:
1097 return mapping[key]
1098 except KeyError:
1099 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001100 """
1101
Guido van Rossumd70fe632015-08-05 12:11:06 +02001102 __slots__ = ()
1103
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001104 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001105 if cls.__origin__ is None:
1106 return cls.__next_in_mro__.__new__(cls)
1107 else:
1108 origin = _gorg(cls)
1109 obj = cls.__next_in_mro__.__new__(origin)
1110 obj.__init__(*args, **kwds)
1111 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001112
1113
1114def cast(typ, val):
1115 """Cast a value to a type.
1116
1117 This returns the value unchanged. To the type checker this
1118 signals that the return value has the designated type, but at
1119 runtime we intentionally don't check anything (we want this
1120 to be as fast as possible).
1121 """
1122 return val
1123
1124
1125def _get_defaults(func):
1126 """Internal helper to extract the default arguments, by name."""
1127 code = func.__code__
1128 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001129 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001130 arg_names = arg_names[:pos_count]
1131 defaults = func.__defaults__ or ()
1132 kwdefaults = func.__kwdefaults__
1133 res = dict(kwdefaults) if kwdefaults else {}
1134 pos_offset = pos_count - len(defaults)
1135 for name, value in zip(arg_names[pos_offset:], defaults):
1136 assert name not in res
1137 res[name] = value
1138 return res
1139
1140
1141def get_type_hints(obj, globalns=None, localns=None):
1142 """Return type hints for a function or method object.
1143
1144 This is often the same as obj.__annotations__, but it handles
1145 forward references encoded as string literals, and if necessary
1146 adds Optional[t] if a default value equal to None is set.
1147
1148 BEWARE -- the behavior of globalns and localns is counterintuitive
1149 (unless you are familiar with how eval() and exec() work). The
1150 search order is locals first, then globals.
1151
1152 - If no dict arguments are passed, an attempt is made to use the
1153 globals from obj, and these are also used as the locals. If the
1154 object does not appear to have globals, an exception is raised.
1155
1156 - If one dict argument is passed, it is used for both globals and
1157 locals.
1158
1159 - If two dict arguments are passed, they specify globals and
1160 locals, respectively.
1161 """
1162 if getattr(obj, '__no_type_check__', None):
1163 return {}
1164 if globalns is None:
1165 globalns = getattr(obj, '__globals__', {})
1166 if localns is None:
1167 localns = globalns
1168 elif localns is None:
1169 localns = globalns
1170 defaults = _get_defaults(obj)
1171 hints = dict(obj.__annotations__)
1172 for name, value in hints.items():
1173 if isinstance(value, str):
1174 value = _ForwardRef(value)
1175 value = _eval_type(value, globalns, localns)
1176 if name in defaults and defaults[name] is None:
1177 value = Optional[value]
1178 hints[name] = value
1179 return hints
1180
1181
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001182def no_type_check(arg):
1183 """Decorator to indicate that annotations are not type hints.
1184
1185 The argument must be a class or function; if it is a class, it
1186 applies recursively to all methods defined in that class (but not
1187 to methods defined in its superclasses or subclasses).
1188
1189 This mutates the function(s) in place.
1190 """
1191 if isinstance(arg, type):
1192 for obj in arg.__dict__.values():
1193 if isinstance(obj, types.FunctionType):
1194 obj.__no_type_check__ = True
1195 else:
1196 arg.__no_type_check__ = True
1197 return arg
1198
1199
1200def no_type_check_decorator(decorator):
1201 """Decorator to give another decorator the @no_type_check effect.
1202
1203 This wraps the decorator with something that wraps the decorated
1204 function in @no_type_check.
1205 """
1206
1207 @functools.wraps(decorator)
1208 def wrapped_decorator(*args, **kwds):
1209 func = decorator(*args, **kwds)
1210 func = no_type_check(func)
1211 return func
1212
1213 return wrapped_decorator
1214
1215
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001216def _overload_dummy(*args, **kwds):
1217 """Helper for @overload to raise when called."""
1218 raise NotImplementedError(
1219 "You should not call an overloaded function. "
1220 "A series of @overload-decorated functions "
1221 "outside a stub module should always be followed "
1222 "by an implementation that is not @overload-ed.")
1223
1224
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001225def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001226 """Decorator for overloaded functions/methods.
1227
1228 In a stub file, place two or more stub definitions for the same
1229 function in a row, each decorated with @overload. For example:
1230
1231 @overload
1232 def utf8(value: None) -> None: ...
1233 @overload
1234 def utf8(value: bytes) -> bytes: ...
1235 @overload
1236 def utf8(value: str) -> bytes: ...
1237
1238 In a non-stub file (i.e. a regular .py file), do the same but
1239 follow it with an implementation. The implementation should *not*
1240 be decorated with @overload. For example:
1241
1242 @overload
1243 def utf8(value: None) -> None: ...
1244 @overload
1245 def utf8(value: bytes) -> bytes: ...
1246 @overload
1247 def utf8(value: str) -> bytes: ...
1248 def utf8(value):
1249 # implementation goes here
1250 """
1251 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001252
1253
1254class _ProtocolMeta(GenericMeta):
1255 """Internal metaclass for _Protocol.
1256
1257 This exists so _Protocol classes can be generic without deriving
1258 from Generic.
1259 """
1260
Guido van Rossumd70fe632015-08-05 12:11:06 +02001261 def __instancecheck__(self, obj):
1262 raise TypeError("Protocols cannot be used with isinstance().")
1263
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001264 def __subclasscheck__(self, cls):
1265 if not self._is_protocol:
1266 # No structural checks since this isn't a protocol.
1267 return NotImplemented
1268
1269 if self is _Protocol:
1270 # Every class is a subclass of the empty protocol.
1271 return True
1272
1273 # Find all attributes defined in the protocol.
1274 attrs = self._get_protocol_attrs()
1275
1276 for attr in attrs:
1277 if not any(attr in d.__dict__ for d in cls.__mro__):
1278 return False
1279 return True
1280
1281 def _get_protocol_attrs(self):
1282 # Get all Protocol base classes.
1283 protocol_bases = []
1284 for c in self.__mro__:
1285 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1286 protocol_bases.append(c)
1287
1288 # Get attributes included in protocol.
1289 attrs = set()
1290 for base in protocol_bases:
1291 for attr in base.__dict__.keys():
1292 # Include attributes not defined in any non-protocol bases.
1293 for c in self.__mro__:
1294 if (c is not base and attr in c.__dict__ and
1295 not getattr(c, '_is_protocol', False)):
1296 break
1297 else:
1298 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001299 attr != '__abstractmethods__' and
1300 attr != '_is_protocol' and
1301 attr != '__dict__' and
1302 attr != '__args__' and
1303 attr != '__slots__' and
1304 attr != '_get_protocol_attrs' and
1305 attr != '__next_in_mro__' and
1306 attr != '__parameters__' and
1307 attr != '__origin__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001308 attr != '__extra__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001309 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001310 attrs.add(attr)
1311
1312 return attrs
1313
1314
1315class _Protocol(metaclass=_ProtocolMeta):
1316 """Internal base class for protocol classes.
1317
1318 This implements a simple-minded structural isinstance check
1319 (similar but more general than the one-offs in collections.abc
1320 such as Hashable).
1321 """
1322
Guido van Rossumd70fe632015-08-05 12:11:06 +02001323 __slots__ = ()
1324
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001325 _is_protocol = True
1326
1327
1328# Various ABCs mimicking those in collections.abc.
1329# A few are simply re-exported for completeness.
1330
1331Hashable = collections_abc.Hashable # Not generic.
1332
1333
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001334if hasattr(collections_abc, 'Awaitable'):
1335 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1336 __slots__ = ()
1337else:
1338 Awaitable = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001339
1340
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001341if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001342
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001343 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1344 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001345
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001346 class AsyncIterator(AsyncIterable[T_co],
1347 extra=collections_abc.AsyncIterator):
1348 __slots__ = ()
1349
1350else:
1351 AsyncIterable = None
1352 AsyncIterator = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001353
1354
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001355class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001356 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001357
1358
1359class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001360 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001361
1362
1363class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001364 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001365
1366 @abstractmethod
1367 def __int__(self) -> int:
1368 pass
1369
1370
1371class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001372 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001373
1374 @abstractmethod
1375 def __float__(self) -> float:
1376 pass
1377
1378
1379class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001380 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001381
1382 @abstractmethod
1383 def __complex__(self) -> complex:
1384 pass
1385
1386
1387class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001388 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001389
1390 @abstractmethod
1391 def __bytes__(self) -> bytes:
1392 pass
1393
1394
Guido van Rossumd70fe632015-08-05 12:11:06 +02001395class SupportsAbs(_Protocol[T_co]):
1396 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001397
1398 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001399 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001400 pass
1401
1402
Guido van Rossumd70fe632015-08-05 12:11:06 +02001403class SupportsRound(_Protocol[T_co]):
1404 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001405
1406 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001407 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001408 pass
1409
1410
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001411if hasattr(collections_abc, 'Reversible'):
1412 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1413 __slots__ = ()
1414else:
1415 class Reversible(_Protocol[T_co]):
1416 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001417
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001418 @abstractmethod
1419 def __reversed__(self) -> 'Iterator[T_co]':
1420 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001421
1422
1423Sized = collections_abc.Sized # Not generic.
1424
1425
1426class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001427 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001428
1429
1430# Callable was defined earlier.
1431
1432
1433class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1434 extra=collections_abc.Set):
1435 pass
1436
1437
1438class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
1439 pass
1440
1441
Guido van Rossumd70fe632015-08-05 12:11:06 +02001442# NOTE: Only the value type is covariant.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001443class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001444 extra=collections_abc.Mapping):
1445 pass
1446
1447
1448class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
1449 pass
1450
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001451if hasattr(collections_abc, 'Reversible'):
1452 class Sequence(Sized, Reversible[T_co], Container[T_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001453 extra=collections_abc.Sequence):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001454 pass
1455else:
1456 class Sequence(Sized, Iterable[T_co], Container[T_co],
1457 extra=collections_abc.Sequence):
1458 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001459
1460
1461class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
1462 pass
1463
1464
1465class ByteString(Sequence[int], extra=collections_abc.ByteString):
1466 pass
1467
1468
1469ByteString.register(type(memoryview(b'')))
1470
1471
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001472class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001473
1474 def __new__(cls, *args, **kwds):
1475 if _geqv(cls, List):
1476 raise TypeError("Type List cannot be instantiated; "
1477 "use list() instead")
1478 return list.__new__(cls, *args, **kwds)
1479
1480
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001481class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001482
1483 def __new__(cls, *args, **kwds):
1484 if _geqv(cls, Set):
1485 raise TypeError("Type Set cannot be instantiated; "
1486 "use set() instead")
1487 return set.__new__(cls, *args, **kwds)
1488
1489
Guido van Rossumd70fe632015-08-05 12:11:06 +02001490class _FrozenSetMeta(GenericMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001491 """This metaclass ensures set is not a subclass of FrozenSet.
1492
1493 Without this metaclass, set would be considered a subclass of
1494 FrozenSet, because FrozenSet.__extra__ is collections.abc.Set, and
1495 set is a subclass of that.
1496 """
1497
1498 def __subclasscheck__(self, cls):
1499 if issubclass(cls, Set):
1500 return False
1501 return super().__subclasscheck__(cls)
1502
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001503
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001504class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta,
1505 extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001506 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001507
1508 def __new__(cls, *args, **kwds):
1509 if _geqv(cls, FrozenSet):
1510 raise TypeError("Type FrozenSet cannot be instantiated; "
1511 "use frozenset() instead")
1512 return frozenset.__new__(cls, *args, **kwds)
1513
1514
1515class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
1516 pass
1517
1518
Guido van Rossumd70fe632015-08-05 12:11:06 +02001519class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001520 extra=collections_abc.KeysView):
1521 pass
1522
1523
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001524class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001525 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001526 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001527 extra=collections_abc.ItemsView):
1528 pass
1529
1530
1531class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
1532 pass
1533
1534
Brett Cannonf3ad0422016-04-15 10:51:30 -07001535if hasattr(contextlib, 'AbstractContextManager'):
1536 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1537 __slots__ = ()
1538 __all__.append('ContextManager')
1539
1540
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001541class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001542
1543 def __new__(cls, *args, **kwds):
1544 if _geqv(cls, Dict):
1545 raise TypeError("Type Dict cannot be instantiated; "
1546 "use dict() instead")
1547 return dict.__new__(cls, *args, **kwds)
1548
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001549class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
1550 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001551
1552 def __new__(cls, *args, **kwds):
1553 if _geqv(cls, DefaultDict):
1554 raise TypeError("Type DefaultDict cannot be instantiated; "
1555 "use collections.defaultdict() instead")
1556 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001557
1558# Determine what base class to use for Generator.
1559if hasattr(collections_abc, 'Generator'):
1560 # Sufficiently recent versions of 3.5 have a Generator ABC.
1561 _G_base = collections_abc.Generator
1562else:
1563 # Fall back on the exact type.
1564 _G_base = types.GeneratorType
1565
1566
1567class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1568 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001569 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001570
1571 def __new__(cls, *args, **kwds):
1572 if _geqv(cls, Generator):
1573 raise TypeError("Type Generator cannot be instantiated; "
1574 "create a subclass instead")
1575 return super().__new__(cls, *args, **kwds)
1576
1577
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001578# Internal type variable used for Type[].
1579CT = TypeVar('CT', covariant=True, bound=type)
1580
1581
1582class Type(type, Generic[CT], extra=type):
1583 """A generic type usable to annotate class objects.
1584
1585 For example, suppose we have the following classes::
1586
1587 class User: ... # Abstract base for User classes
1588 class BasicUser(User): ...
1589 class ProUser(User): ...
1590 class TeamUser(User): ...
1591
1592 And a function that takes a class argument that's a subclass of
1593 User and returns an instance of the corresponding class::
1594
1595 U = TypeVar('U', bound=User)
1596 def new_user(user_class: Type[U]) -> U:
1597 user = user_class()
1598 # (Here we could write the user object to a database)
1599 return user
1600
1601 joe = new_user(BasicUser)
1602
1603 At this point the type checker knows that joe has type BasicUser.
1604 """
1605
1606
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001607def NamedTuple(typename, fields):
1608 """Typed version of namedtuple.
1609
1610 Usage::
1611
1612 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1613
1614 This is equivalent to::
1615
1616 Employee = collections.namedtuple('Employee', ['name', 'id'])
1617
1618 The resulting class has one extra attribute: _field_types,
1619 giving a dict mapping field names to types. (The field names
1620 are in the _fields attribute, which is part of the namedtuple
1621 API.)
1622 """
1623 fields = [(n, t) for n, t in fields]
1624 cls = collections.namedtuple(typename, [n for n, t in fields])
1625 cls._field_types = dict(fields)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001626 # Set the module to the caller's module (otherwise it'd be 'typing').
1627 try:
1628 cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
1629 except (AttributeError, ValueError):
1630 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001631 return cls
1632
1633
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001634# Python-version-specific alias (Python 2: unicode; Python 3: str)
1635Text = str
1636
1637
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001638class IO(Generic[AnyStr]):
1639 """Generic base class for TextIO and BinaryIO.
1640
1641 This is an abstract, generic version of the return of open().
1642
1643 NOTE: This does not distinguish between the different possible
1644 classes (text vs. binary, read vs. write vs. read/write,
1645 append-only, unbuffered). The TextIO and BinaryIO subclasses
1646 below capture the distinctions between text vs. binary, which is
1647 pervasive in the interface; however we currently do not offer a
1648 way to track the other distinctions in the type system.
1649 """
1650
Guido van Rossumd70fe632015-08-05 12:11:06 +02001651 __slots__ = ()
1652
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001653 @abstractproperty
1654 def mode(self) -> str:
1655 pass
1656
1657 @abstractproperty
1658 def name(self) -> str:
1659 pass
1660
1661 @abstractmethod
1662 def close(self) -> None:
1663 pass
1664
1665 @abstractmethod
1666 def closed(self) -> bool:
1667 pass
1668
1669 @abstractmethod
1670 def fileno(self) -> int:
1671 pass
1672
1673 @abstractmethod
1674 def flush(self) -> None:
1675 pass
1676
1677 @abstractmethod
1678 def isatty(self) -> bool:
1679 pass
1680
1681 @abstractmethod
1682 def read(self, n: int = -1) -> AnyStr:
1683 pass
1684
1685 @abstractmethod
1686 def readable(self) -> bool:
1687 pass
1688
1689 @abstractmethod
1690 def readline(self, limit: int = -1) -> AnyStr:
1691 pass
1692
1693 @abstractmethod
1694 def readlines(self, hint: int = -1) -> List[AnyStr]:
1695 pass
1696
1697 @abstractmethod
1698 def seek(self, offset: int, whence: int = 0) -> int:
1699 pass
1700
1701 @abstractmethod
1702 def seekable(self) -> bool:
1703 pass
1704
1705 @abstractmethod
1706 def tell(self) -> int:
1707 pass
1708
1709 @abstractmethod
1710 def truncate(self, size: int = None) -> int:
1711 pass
1712
1713 @abstractmethod
1714 def writable(self) -> bool:
1715 pass
1716
1717 @abstractmethod
1718 def write(self, s: AnyStr) -> int:
1719 pass
1720
1721 @abstractmethod
1722 def writelines(self, lines: List[AnyStr]) -> None:
1723 pass
1724
1725 @abstractmethod
1726 def __enter__(self) -> 'IO[AnyStr]':
1727 pass
1728
1729 @abstractmethod
1730 def __exit__(self, type, value, traceback) -> None:
1731 pass
1732
1733
1734class BinaryIO(IO[bytes]):
1735 """Typed version of the return of open() in binary mode."""
1736
Guido van Rossumd70fe632015-08-05 12:11:06 +02001737 __slots__ = ()
1738
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001739 @abstractmethod
1740 def write(self, s: Union[bytes, bytearray]) -> int:
1741 pass
1742
1743 @abstractmethod
1744 def __enter__(self) -> 'BinaryIO':
1745 pass
1746
1747
1748class TextIO(IO[str]):
1749 """Typed version of the return of open() in text mode."""
1750
Guido van Rossumd70fe632015-08-05 12:11:06 +02001751 __slots__ = ()
1752
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001753 @abstractproperty
1754 def buffer(self) -> BinaryIO:
1755 pass
1756
1757 @abstractproperty
1758 def encoding(self) -> str:
1759 pass
1760
1761 @abstractproperty
1762 def errors(self) -> str:
1763 pass
1764
1765 @abstractproperty
1766 def line_buffering(self) -> bool:
1767 pass
1768
1769 @abstractproperty
1770 def newlines(self) -> Any:
1771 pass
1772
1773 @abstractmethod
1774 def __enter__(self) -> 'TextIO':
1775 pass
1776
1777
1778class io:
1779 """Wrapper namespace for IO generic classes."""
1780
1781 __all__ = ['IO', 'TextIO', 'BinaryIO']
1782 IO = IO
1783 TextIO = TextIO
1784 BinaryIO = BinaryIO
1785
1786io.__name__ = __name__ + '.io'
1787sys.modules[io.__name__] = io
1788
1789
1790Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
1791 lambda p: p.pattern)
1792Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
1793 lambda m: m.re.pattern)
1794
1795
1796class re:
1797 """Wrapper namespace for re type aliases."""
1798
1799 __all__ = ['Pattern', 'Match']
1800 Pattern = Pattern
1801 Match = Match
1802
1803re.__name__ = __name__ + '.re'
1804sys.modules[re.__name__] = re