blob: d2750111d4502e159108767206e1796675033392 [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',
22 'TypeVar',
23 'Union',
24 'Tuple',
25
26 # ABCs (from collections.abc).
27 'AbstractSet', # collections.abc.Set.
Guido van Rossumf17c2002015-12-03 17:31:24 -080028 'Awaitable',
29 'AsyncIterator',
30 'AsyncIterable',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070031 'ByteString',
32 'Container',
33 'Hashable',
34 'ItemsView',
35 'Iterable',
36 'Iterator',
37 'KeysView',
38 'Mapping',
39 'MappingView',
40 'MutableMapping',
41 'MutableSequence',
42 'MutableSet',
43 'Sequence',
44 'Sized',
45 'ValuesView',
46
47 # Structural checks, a.k.a. protocols.
48 'Reversible',
49 'SupportsAbs',
50 'SupportsFloat',
51 'SupportsInt',
52 'SupportsRound',
53
54 # Concrete collection types.
55 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070056 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070057 'List',
58 'Set',
59 'NamedTuple', # Not really a type.
60 'Generator',
61
62 # One-off things.
63 'AnyStr',
64 'cast',
65 'get_type_hints',
66 'no_type_check',
67 'no_type_check_decorator',
68 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -070069 'Text',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070070]
71
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070072# The pseudo-submodules 're' and 'io' are part of the public
73# namespace, but excluded from __all__ because they might stomp on
74# legitimate imports of those modules.
75
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070076
77def _qualname(x):
78 if sys.version_info[:2] >= (3, 3):
79 return x.__qualname__
80 else:
81 # Fall back to just name.
82 return x.__name__
83
84
85class TypingMeta(type):
86 """Metaclass for every type defined below.
87
88 This overrides __new__() to require an extra keyword parameter
89 '_root', which serves as a guard against naive subclassing of the
90 typing classes. Any legitimate class defined using a metaclass
91 derived from TypingMeta (including internal subclasses created by
92 e.g. Union[X, Y]) must pass _root=True.
93
94 This also defines a dummy constructor (all the work is done in
95 __new__) and a nicer repr().
96 """
97
98 _is_protocol = False
99
100 def __new__(cls, name, bases, namespace, *, _root=False):
101 if not _root:
102 raise TypeError("Cannot subclass %s" %
103 (', '.join(map(_type_repr, bases)) or '()'))
104 return super().__new__(cls, name, bases, namespace)
105
106 def __init__(self, *args, **kwds):
107 pass
108
109 def _eval_type(self, globalns, localns):
110 """Override this in subclasses to interpret forward references.
111
112 For example, Union['C'] is internally stored as
113 Union[_ForwardRef('C')], which should evaluate to _Union[C],
114 where C is an object found in globalns or localns (searching
115 localns first, of course).
116 """
117 return self
118
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700119 def _get_type_vars(self, tvars):
120 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700121
122 def __repr__(self):
123 return '%s.%s' % (self.__module__, _qualname(self))
124
125
126class Final:
127 """Mix-in class to prevent instantiation."""
128
Guido van Rossumd70fe632015-08-05 12:11:06 +0200129 __slots__ = ()
130
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700131 def __new__(self, *args, **kwds):
132 raise TypeError("Cannot instantiate %r" % self.__class__)
133
134
135class _ForwardRef(TypingMeta):
136 """Wrapper to hold a forward reference."""
137
138 def __new__(cls, arg):
139 if not isinstance(arg, str):
140 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
141 try:
142 code = compile(arg, '<string>', 'eval')
143 except SyntaxError:
144 raise SyntaxError('ForwardRef must be an expression -- got %r' %
145 (arg,))
146 self = super().__new__(cls, arg, (), {}, _root=True)
147 self.__forward_arg__ = arg
148 self.__forward_code__ = code
149 self.__forward_evaluated__ = False
150 self.__forward_value__ = None
151 typing_globals = globals()
152 frame = sys._getframe(1)
153 while frame is not None and frame.f_globals is typing_globals:
154 frame = frame.f_back
155 assert frame is not None
156 self.__forward_frame__ = frame
157 return self
158
159 def _eval_type(self, globalns, localns):
160 if not isinstance(localns, dict):
161 raise TypeError('ForwardRef localns must be a dict -- got %r' %
162 (localns,))
163 if not isinstance(globalns, dict):
164 raise TypeError('ForwardRef globalns must be a dict -- got %r' %
165 (globalns,))
166 if not self.__forward_evaluated__:
167 if globalns is None and localns is None:
168 globalns = localns = {}
169 elif globalns is None:
170 globalns = localns
171 elif localns is None:
172 localns = globalns
173 self.__forward_value__ = _type_check(
174 eval(self.__forward_code__, globalns, localns),
175 "Forward references must evaluate to types.")
176 self.__forward_evaluated__ = True
177 return self.__forward_value__
178
Guido van Rossumd70fe632015-08-05 12:11:06 +0200179 def __instancecheck__(self, obj):
180 raise TypeError("Forward references cannot be used with isinstance().")
181
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700182 def __subclasscheck__(self, cls):
183 if not self.__forward_evaluated__:
184 globalns = self.__forward_frame__.f_globals
185 localns = self.__forward_frame__.f_locals
186 try:
187 self._eval_type(globalns, localns)
188 except NameError:
189 return False # Too early.
190 return issubclass(cls, self.__forward_value__)
191
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700192 def __repr__(self):
193 return '_ForwardRef(%r)' % (self.__forward_arg__,)
194
195
196class _TypeAlias:
197 """Internal helper class for defining generic variants of concrete types.
198
199 Note that this is not a type; let's call it a pseudo-type. It can
200 be used in instance and subclass checks, e.g. isinstance(m, Match)
201 or issubclass(type(m), Match). However, it cannot be itself the
202 target of an issubclass() call; e.g. issubclass(Match, C) (for
203 some arbitrary class C) raises TypeError rather than returning
204 False.
205 """
206
Guido van Rossumd70fe632015-08-05 12:11:06 +0200207 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
208
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700209 def __new__(cls, *args, **kwds):
210 """Constructor.
211
212 This only exists to give a better error message in case
213 someone tries to subclass a type alias (not a good idea).
214 """
215 if (len(args) == 3 and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700216 isinstance(args[0], str) and
217 isinstance(args[1], tuple)):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700218 # Close enough.
219 raise TypeError("A type alias cannot be subclassed")
220 return object.__new__(cls)
221
222 def __init__(self, name, type_var, impl_type, type_checker):
223 """Initializer.
224
225 Args:
226 name: The name, e.g. 'Pattern'.
227 type_var: The type parameter, e.g. AnyStr, or the
228 specific type, e.g. str.
229 impl_type: The implementation type.
230 type_checker: Function that takes an impl_type instance.
231 and returns a value that should be a type_var instance.
232 """
233 assert isinstance(name, str), repr(name)
234 assert isinstance(type_var, type), repr(type_var)
235 assert isinstance(impl_type, type), repr(impl_type)
236 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
237 self.name = name
238 self.type_var = type_var
239 self.impl_type = impl_type
240 self.type_checker = type_checker
241
242 def __repr__(self):
243 return "%s[%s]" % (self.name, _type_repr(self.type_var))
244
245 def __getitem__(self, parameter):
246 assert isinstance(parameter, type), repr(parameter)
247 if not isinstance(self.type_var, TypeVar):
248 raise TypeError("%s cannot be further parameterized." % self)
249 if self.type_var.__constraints__:
250 if not issubclass(parameter, Union[self.type_var.__constraints__]):
251 raise TypeError("%s is not a valid substitution for %s." %
252 (parameter, self.type_var))
253 return self.__class__(self.name, parameter,
254 self.impl_type, self.type_checker)
255
256 def __instancecheck__(self, obj):
Guido van Rossumd70fe632015-08-05 12:11:06 +0200257 raise TypeError("Type aliases cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700258
259 def __subclasscheck__(self, cls):
260 if cls is Any:
261 return True
262 if isinstance(cls, _TypeAlias):
263 # Covariance. For now, we compare by name.
264 return (cls.name == self.name and
265 issubclass(cls.type_var, self.type_var))
266 else:
267 # Note that this is too lenient, because the
268 # implementation type doesn't carry information about
269 # whether it is about bytes or str (for example).
270 return issubclass(cls, self.impl_type)
271
272
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700273def _get_type_vars(types, tvars):
274 for t in types:
275 if isinstance(t, TypingMeta):
276 t._get_type_vars(tvars)
277
278
279def _type_vars(types):
280 tvars = []
281 _get_type_vars(types, tvars)
282 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700283
284
285def _eval_type(t, globalns, localns):
286 if isinstance(t, TypingMeta):
287 return t._eval_type(globalns, localns)
288 else:
289 return t
290
291
292def _type_check(arg, msg):
293 """Check that the argument is a type, and return it.
294
295 As a special case, accept None and return type(None) instead.
296 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
297
298 The msg argument is a human-readable error message, e.g.
299
300 "Union[arg, ...]: arg should be a type."
301
302 We append the repr() of the actual value (truncated to 100 chars).
303 """
304 if arg is None:
305 return type(None)
306 if isinstance(arg, str):
307 arg = _ForwardRef(arg)
308 if not isinstance(arg, (type, _TypeAlias)):
309 raise TypeError(msg + " Got %.100r." % (arg,))
310 return arg
311
312
313def _type_repr(obj):
314 """Return the repr() of an object, special-casing types.
315
316 If obj is a type, we return a shorter version than the default
317 type.__repr__, based on the module and qualified name, which is
318 typically enough to uniquely identify a type. For everything
319 else, we fall back on repr(obj).
320 """
321 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
322 if obj.__module__ == 'builtins':
323 return _qualname(obj)
324 else:
325 return '%s.%s' % (obj.__module__, _qualname(obj))
326 else:
327 return repr(obj)
328
329
330class AnyMeta(TypingMeta):
331 """Metaclass for Any."""
332
333 def __new__(cls, name, bases, namespace, _root=False):
334 self = super().__new__(cls, name, bases, namespace, _root=_root)
335 return self
336
Guido van Rossumd70fe632015-08-05 12:11:06 +0200337 def __instancecheck__(self, obj):
338 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700339
340 def __subclasscheck__(self, cls):
341 if not isinstance(cls, type):
342 return super().__subclasscheck__(cls) # To TypeError.
343 return True
344
345
346class Any(Final, metaclass=AnyMeta, _root=True):
347 """Special type indicating an unconstrained type.
348
349 - Any object is an instance of Any.
350 - Any class is a subclass of Any.
351 - As a special case, Any and object are subclasses of each other.
352 """
353
Guido van Rossumd70fe632015-08-05 12:11:06 +0200354 __slots__ = ()
355
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700356
357class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
358 """Type variable.
359
360 Usage::
361
362 T = TypeVar('T') # Can be anything
363 A = TypeVar('A', str, bytes) # Must be str or bytes
364
365 Type variables exist primarily for the benefit of static type
366 checkers. They serve as the parameters for generic types as well
367 as for generic function definitions. See class Generic for more
368 information on generic types. Generic functions work as follows:
369
370 def repeat(x: T, n: int) -> Sequence[T]:
371 '''Return a list containing n references to x.'''
372 return [x]*n
373
374 def longest(x: A, y: A) -> A:
375 '''Return the longest of two strings.'''
376 return x if len(x) >= len(y) else y
377
378 The latter example's signature is essentially the overloading
379 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
380 that if the arguments are instances of some subclass of str,
381 the return type is still plain str.
382
383 At runtime, isinstance(x, T) will raise TypeError. However,
384 issubclass(C, T) is true for any class C, and issubclass(str, A)
385 and issubclass(bytes, A) are true, and issubclass(int, A) is
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700386 false. (TODO: Why is this needed? This may change. See #136.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700387
388 Type variables may be marked covariant or contravariant by passing
389 covariant=True or contravariant=True. See PEP 484 for more
390 details. By default type variables are invariant.
391
392 Type variables can be introspected. e.g.:
393
394 T.__name__ == 'T'
395 T.__constraints__ == ()
396 T.__covariant__ == False
397 T.__contravariant__ = False
398 A.__constraints__ == (str, bytes)
399 """
400
401 def __new__(cls, name, *constraints, bound=None,
402 covariant=False, contravariant=False):
403 self = super().__new__(cls, name, (Final,), {}, _root=True)
404 if covariant and contravariant:
405 raise ValueError("Bivariant type variables are not supported.")
406 self.__covariant__ = bool(covariant)
407 self.__contravariant__ = bool(contravariant)
408 if constraints and bound is not None:
409 raise TypeError("Constraints cannot be combined with bound=...")
410 if constraints and len(constraints) == 1:
411 raise TypeError("A single constraint is not allowed")
412 msg = "TypeVar(name, constraint, ...): constraints must be types."
413 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
414 if bound:
415 self.__bound__ = _type_check(bound, "Bound must be a type.")
416 else:
417 self.__bound__ = None
418 return self
419
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700420 def _get_type_vars(self, tvars):
421 if self not in tvars:
422 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700423
424 def __repr__(self):
425 if self.__covariant__:
426 prefix = '+'
427 elif self.__contravariant__:
428 prefix = '-'
429 else:
430 prefix = '~'
431 return prefix + self.__name__
432
433 def __instancecheck__(self, instance):
434 raise TypeError("Type variables cannot be used with isinstance().")
435
436 def __subclasscheck__(self, cls):
437 # TODO: Make this raise TypeError too?
438 if cls is self:
439 return True
440 if cls is Any:
441 return True
442 if self.__bound__ is not None:
443 return issubclass(cls, self.__bound__)
444 if self.__constraints__:
445 return any(issubclass(cls, c) for c in self.__constraints__)
446 return True
447
448
449# Some unconstrained type variables. These are used by the container types.
450T = TypeVar('T') # Any type.
451KT = TypeVar('KT') # Key type.
452VT = TypeVar('VT') # Value type.
453T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
454V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700455VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
456T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
457
458# A useful type variable with constraints. This represents string types.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700459AnyStr = TypeVar('AnyStr', bytes, str)
460
461
462class UnionMeta(TypingMeta):
463 """Metaclass for Union."""
464
465 def __new__(cls, name, bases, namespace, parameters=None, _root=False):
466 if parameters is None:
467 return super().__new__(cls, name, bases, namespace, _root=_root)
468 if not isinstance(parameters, tuple):
469 raise TypeError("Expected parameters=<tuple>")
470 # Flatten out Union[Union[...], ...] and type-check non-Union args.
471 params = []
472 msg = "Union[arg, ...]: each arg must be a type."
473 for p in parameters:
474 if isinstance(p, UnionMeta):
475 params.extend(p.__union_params__)
476 else:
477 params.append(_type_check(p, msg))
478 # Weed out strict duplicates, preserving the first of each occurrence.
479 all_params = set(params)
480 if len(all_params) < len(params):
481 new_params = []
482 for t in params:
483 if t in all_params:
484 new_params.append(t)
485 all_params.remove(t)
486 params = new_params
487 assert not all_params, all_params
488 # Weed out subclasses.
489 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
490 # If Any or object is present it will be the sole survivor.
491 # If both Any and object are present, Any wins.
492 # Never discard type variables, except against Any.
493 # (In particular, Union[str, AnyStr] != AnyStr.)
494 all_params = set(params)
495 for t1 in params:
496 if t1 is Any:
497 return Any
498 if isinstance(t1, TypeVar):
499 continue
Guido van Rossumca636ea2015-10-19 14:55:47 -0700500 if isinstance(t1, _TypeAlias):
501 # _TypeAlias is not a real class.
502 continue
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700503 if any(issubclass(t1, t2)
504 for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
505 all_params.remove(t1)
506 # It's not a union if there's only one type left.
507 if len(all_params) == 1:
508 return all_params.pop()
509 # Create a new class with these params.
510 self = super().__new__(cls, name, bases, {}, _root=True)
511 self.__union_params__ = tuple(t for t in params if t in all_params)
512 self.__union_set_params__ = frozenset(self.__union_params__)
513 return self
514
515 def _eval_type(self, globalns, localns):
516 p = tuple(_eval_type(t, globalns, localns)
517 for t in self.__union_params__)
518 if p == self.__union_params__:
519 return self
520 else:
521 return self.__class__(self.__name__, self.__bases__, {},
522 p, _root=True)
523
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700524 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700525 if self.__union_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700526 _get_type_vars(self.__union_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700527
528 def __repr__(self):
529 r = super().__repr__()
530 if self.__union_params__:
531 r += '[%s]' % (', '.join(_type_repr(t)
532 for t in self.__union_params__))
533 return r
534
535 def __getitem__(self, parameters):
536 if self.__union_params__ is not None:
537 raise TypeError(
538 "Cannot subscript an existing Union. Use Union[u, t] instead.")
539 if parameters == ():
540 raise TypeError("Cannot take a Union of no types.")
541 if not isinstance(parameters, tuple):
542 parameters = (parameters,)
543 return self.__class__(self.__name__, self.__bases__,
544 dict(self.__dict__), parameters, _root=True)
545
546 def __eq__(self, other):
547 if not isinstance(other, UnionMeta):
548 return NotImplemented
549 return self.__union_set_params__ == other.__union_set_params__
550
551 def __hash__(self):
552 return hash(self.__union_set_params__)
553
Guido van Rossumd70fe632015-08-05 12:11:06 +0200554 def __instancecheck__(self, obj):
555 raise TypeError("Unions cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700556
557 def __subclasscheck__(self, cls):
558 if cls is Any:
559 return True
560 if self.__union_params__ is None:
561 return isinstance(cls, UnionMeta)
562 elif isinstance(cls, UnionMeta):
563 if cls.__union_params__ is None:
564 return False
565 return all(issubclass(c, self) for c in (cls.__union_params__))
566 elif isinstance(cls, TypeVar):
567 if cls in self.__union_params__:
568 return True
569 if cls.__constraints__:
570 return issubclass(Union[cls.__constraints__], self)
571 return False
572 else:
573 return any(issubclass(cls, t) for t in self.__union_params__)
574
575
576class Union(Final, metaclass=UnionMeta, _root=True):
577 """Union type; Union[X, Y] means either X or Y.
578
579 To define a union, use e.g. Union[int, str]. Details:
580
581 - The arguments must be types and there must be at least one.
582
583 - None as an argument is a special case and is replaced by
584 type(None).
585
586 - Unions of unions are flattened, e.g.::
587
588 Union[Union[int, str], float] == Union[int, str, float]
589
590 - Unions of a single argument vanish, e.g.::
591
592 Union[int] == int # The constructor actually returns int
593
594 - Redundant arguments are skipped, e.g.::
595
596 Union[int, str, int] == Union[int, str]
597
598 - When comparing unions, the argument order is ignored, e.g.::
599
600 Union[int, str] == Union[str, int]
601
602 - When two arguments have a subclass relationship, the least
603 derived argument is kept, e.g.::
604
605 class Employee: pass
606 class Manager(Employee): pass
607 Union[int, Employee, Manager] == Union[int, Employee]
608 Union[Manager, int, Employee] == Union[int, Employee]
609 Union[Employee, Manager] == Employee
610
611 - Corollary: if Any is present it is the sole survivor, e.g.::
612
613 Union[int, Any] == Any
614
615 - Similar for object::
616
617 Union[int, object] == object
618
619 - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
620
621 - You cannot subclass or instantiate a union.
622
623 - You cannot write Union[X][Y] (what would it mean?).
624
625 - You can use Optional[X] as a shorthand for Union[X, None].
626 """
627
628 # Unsubscripted Union type has params set to None.
629 __union_params__ = None
630 __union_set_params__ = None
631
632
633class OptionalMeta(TypingMeta):
634 """Metaclass for Optional."""
635
636 def __new__(cls, name, bases, namespace, _root=False):
637 return super().__new__(cls, name, bases, namespace, _root=_root)
638
639 def __getitem__(self, arg):
640 arg = _type_check(arg, "Optional[t] requires a single type.")
641 return Union[arg, type(None)]
642
643
644class Optional(Final, metaclass=OptionalMeta, _root=True):
645 """Optional type.
646
647 Optional[X] is equivalent to Union[X, type(None)].
648 """
649
Guido van Rossumd70fe632015-08-05 12:11:06 +0200650 __slots__ = ()
651
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700652
653class TupleMeta(TypingMeta):
654 """Metaclass for Tuple."""
655
656 def __new__(cls, name, bases, namespace, parameters=None,
657 use_ellipsis=False, _root=False):
658 self = super().__new__(cls, name, bases, namespace, _root=_root)
659 self.__tuple_params__ = parameters
660 self.__tuple_use_ellipsis__ = use_ellipsis
661 return self
662
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700663 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700664 if self.__tuple_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700665 _get_type_vars(self.__tuple_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700666
667 def _eval_type(self, globalns, localns):
668 tp = self.__tuple_params__
669 if tp is None:
670 return self
671 p = tuple(_eval_type(t, globalns, localns) for t in tp)
672 if p == self.__tuple_params__:
673 return self
674 else:
675 return self.__class__(self.__name__, self.__bases__, {},
676 p, _root=True)
677
678 def __repr__(self):
679 r = super().__repr__()
680 if self.__tuple_params__ is not None:
681 params = [_type_repr(p) for p in self.__tuple_params__]
682 if self.__tuple_use_ellipsis__:
683 params.append('...')
684 r += '[%s]' % (
685 ', '.join(params))
686 return r
687
688 def __getitem__(self, parameters):
689 if self.__tuple_params__ is not None:
690 raise TypeError("Cannot re-parameterize %r" % (self,))
691 if not isinstance(parameters, tuple):
692 parameters = (parameters,)
693 if len(parameters) == 2 and parameters[1] == Ellipsis:
694 parameters = parameters[:1]
695 use_ellipsis = True
696 msg = "Tuple[t, ...]: t must be a type."
697 else:
698 use_ellipsis = False
699 msg = "Tuple[t0, t1, ...]: each t must be a type."
700 parameters = tuple(_type_check(p, msg) for p in parameters)
701 return self.__class__(self.__name__, self.__bases__,
702 dict(self.__dict__), parameters,
703 use_ellipsis=use_ellipsis, _root=True)
704
705 def __eq__(self, other):
706 if not isinstance(other, TupleMeta):
707 return NotImplemented
Guido van Rossum5abcbb32016-04-18 07:37:41 -0700708 return (self.__tuple_params__ == other.__tuple_params__ and
709 self.__tuple_use_ellipsis__ == other.__tuple_use_ellipsis__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700710
711 def __hash__(self):
712 return hash(self.__tuple_params__)
713
Guido van Rossumd70fe632015-08-05 12:11:06 +0200714 def __instancecheck__(self, obj):
715 raise TypeError("Tuples cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700716
717 def __subclasscheck__(self, cls):
718 if cls is Any:
719 return True
720 if not isinstance(cls, type):
721 return super().__subclasscheck__(cls) # To TypeError.
722 if issubclass(cls, tuple):
723 return True # Special case.
724 if not isinstance(cls, TupleMeta):
725 return super().__subclasscheck__(cls) # False.
726 if self.__tuple_params__ is None:
727 return True
728 if cls.__tuple_params__ is None:
729 return False # ???
730 if cls.__tuple_use_ellipsis__ != self.__tuple_use_ellipsis__:
731 return False
732 # Covariance.
733 return (len(self.__tuple_params__) == len(cls.__tuple_params__) and
734 all(issubclass(x, p)
735 for x, p in zip(cls.__tuple_params__,
736 self.__tuple_params__)))
737
738
739class Tuple(Final, metaclass=TupleMeta, _root=True):
740 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
741
742 Example: Tuple[T1, T2] is a tuple of two elements corresponding
743 to type variables T1 and T2. Tuple[int, float, str] is a tuple
744 of an int, a float and a string.
745
746 To specify a variable-length tuple of homogeneous type, use Sequence[T].
747 """
748
Guido van Rossumd70fe632015-08-05 12:11:06 +0200749 __slots__ = ()
750
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700751
752class CallableMeta(TypingMeta):
753 """Metaclass for Callable."""
754
755 def __new__(cls, name, bases, namespace, _root=False,
756 args=None, result=None):
757 if args is None and result is None:
758 pass # Must be 'class Callable'.
759 else:
760 if args is not Ellipsis:
761 if not isinstance(args, list):
762 raise TypeError("Callable[args, result]: "
763 "args must be a list."
764 " Got %.100r." % (args,))
765 msg = "Callable[[arg, ...], result]: each arg must be a type."
766 args = tuple(_type_check(arg, msg) for arg in args)
767 msg = "Callable[args, result]: result must be a type."
768 result = _type_check(result, msg)
769 self = super().__new__(cls, name, bases, namespace, _root=_root)
770 self.__args__ = args
771 self.__result__ = result
772 return self
773
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700774 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700775 if self.__args__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700776 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700777
778 def _eval_type(self, globalns, localns):
779 if self.__args__ is None and self.__result__ is None:
780 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200781 if self.__args__ is Ellipsis:
782 args = self.__args__
783 else:
784 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700785 result = _eval_type(self.__result__, globalns, localns)
786 if args == self.__args__ and result == self.__result__:
787 return self
788 else:
789 return self.__class__(self.__name__, self.__bases__, {},
790 args=args, result=result, _root=True)
791
792 def __repr__(self):
793 r = super().__repr__()
794 if self.__args__ is not None or self.__result__ is not None:
795 if self.__args__ is Ellipsis:
796 args_r = '...'
797 else:
798 args_r = '[%s]' % ', '.join(_type_repr(t)
799 for t in self.__args__)
800 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
801 return r
802
803 def __getitem__(self, parameters):
804 if self.__args__ is not None or self.__result__ is not None:
805 raise TypeError("This Callable type is already parameterized.")
806 if not isinstance(parameters, tuple) or len(parameters) != 2:
807 raise TypeError(
808 "Callable must be used as Callable[[arg, ...], result].")
809 args, result = parameters
810 return self.__class__(self.__name__, self.__bases__,
811 dict(self.__dict__), _root=True,
812 args=args, result=result)
813
814 def __eq__(self, other):
815 if not isinstance(other, CallableMeta):
816 return NotImplemented
817 return (self.__args__ == other.__args__ and
818 self.__result__ == other.__result__)
819
820 def __hash__(self):
821 return hash(self.__args__) ^ hash(self.__result__)
822
Guido van Rossumd70fe632015-08-05 12:11:06 +0200823 def __instancecheck__(self, obj):
824 # For unparametrized Callable we allow this, because
825 # typing.Callable should be equivalent to
826 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700827 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200828 return isinstance(obj, collections_abc.Callable)
829 else:
830 raise TypeError("Callable[] cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700831
832 def __subclasscheck__(self, cls):
833 if cls is Any:
834 return True
835 if not isinstance(cls, CallableMeta):
836 return super().__subclasscheck__(cls)
837 if self.__args__ is None and self.__result__ is None:
838 return True
839 # We're not doing covariance or contravariance -- this is *invariance*.
840 return self == cls
841
842
843class Callable(Final, metaclass=CallableMeta, _root=True):
844 """Callable type; Callable[[int], str] is a function of (int) -> str.
845
846 The subscription syntax must always be used with exactly two
847 values: the argument list and the return type. The argument list
848 must be a list of types; the return type must be a single type.
849
850 There is no syntax to indicate optional or keyword arguments,
851 such function types are rarely used as callback types.
852 """
853
Guido van Rossumd70fe632015-08-05 12:11:06 +0200854 __slots__ = ()
855
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700856
857def _gorg(a):
858 """Return the farthest origin of a generic class."""
859 assert isinstance(a, GenericMeta)
860 while a.__origin__ is not None:
861 a = a.__origin__
862 return a
863
864
865def _geqv(a, b):
866 """Return whether two generic classes are equivalent.
867
868 The intention is to consider generic class X and any of its
869 parameterized forms (X[T], X[int], etc.) as equivalent.
870
871 However, X is not equivalent to a subclass of X.
872
873 The relation is reflexive, symmetric and transitive.
874 """
875 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
876 # Reduce each to its origin.
877 return _gorg(a) is _gorg(b)
878
879
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700880def _next_in_mro(cls):
881 """Helper for Generic.__new__.
882
883 Returns the class after the last occurrence of Generic or
884 Generic[...] in cls.__mro__.
885 """
886 next_in_mro = object
887 # Look for the last occurrence of Generic or Generic[...].
888 for i, c in enumerate(cls.__mro__[:-1]):
889 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
890 next_in_mro = cls.__mro__[i+1]
891 return next_in_mro
892
893
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700894class GenericMeta(TypingMeta, abc.ABCMeta):
895 """Metaclass for generic types."""
896
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700897 __extra__ = None
898
899 def __new__(cls, name, bases, namespace,
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700900 tvars=None, args=None, origin=None, extra=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700901 self = super().__new__(cls, name, bases, namespace, _root=True)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700902
903 if tvars is not None:
904 # Called from __getitem__() below.
905 assert origin is not None
906 assert all(isinstance(t, TypeVar) for t in tvars), tvars
907 else:
908 # Called from class statement.
909 assert tvars is None, tvars
910 assert args is None, args
911 assert origin is None, origin
912
913 # Get the full set of tvars from the bases.
914 tvars = _type_vars(bases)
915 # Look for Generic[T1, ..., Tn].
916 # If found, tvars must be a subset of it.
917 # If not found, tvars is it.
918 # Also check for and reject plain Generic,
919 # and reject multiple Generic[...].
920 gvars = None
921 for base in bases:
922 if base is Generic:
923 raise TypeError("Cannot inherit from plain Generic")
924 if (isinstance(base, GenericMeta) and
925 base.__origin__ is Generic):
926 if gvars is not None:
927 raise TypeError(
928 "Cannot inherit from Generic[...] multiple types.")
929 gvars = base.__parameters__
930 if gvars is None:
931 gvars = tvars
932 else:
933 tvarset = set(tvars)
934 gvarset = set(gvars)
935 if not tvarset <= gvarset:
936 raise TypeError(
937 "Some type variables (%s) "
938 "are not listed in Generic[%s]" %
939 (", ".join(str(t) for t in tvars if t not in gvarset),
940 ", ".join(str(g) for g in gvars)))
941 tvars = gvars
942
943 self.__parameters__ = tvars
944 self.__args__ = args
945 self.__origin__ = origin
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700946 if extra is not None:
947 self.__extra__ = extra
948 # Else __extra__ is inherited, eventually from the
949 # (meta-)class default above.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700950 # Speed hack (https://github.com/python/typing/issues/196).
951 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700952 return self
953
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700954 def _get_type_vars(self, tvars):
955 if self.__origin__ and self.__parameters__:
956 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700957
958 def __repr__(self):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700959 if self.__origin__ is not None:
960 r = repr(self.__origin__)
961 else:
962 r = super().__repr__()
963 if self.__args__:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700964 r += '[%s]' % (
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700965 ', '.join(_type_repr(p) for p in self.__args__))
966 if self.__parameters__:
967 r += '<%s>' % (
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700968 ', '.join(_type_repr(p) for p in self.__parameters__))
969 return r
970
971 def __eq__(self, other):
972 if not isinstance(other, GenericMeta):
973 return NotImplemented
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700974 if self.__origin__ is not None:
975 return (self.__origin__ is other.__origin__ and
976 self.__args__ == other.__args__ and
977 self.__parameters__ == other.__parameters__)
978 else:
979 return self is other
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700980
981 def __hash__(self):
982 return hash((self.__name__, self.__parameters__))
983
984 def __getitem__(self, params):
985 if not isinstance(params, tuple):
986 params = (params,)
987 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700988 raise TypeError(
989 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700990 msg = "Parameters to generic types must be types."
991 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700992 if self is Generic:
993 # Generic can only be subscripted with unique type variables.
994 if not all(isinstance(p, TypeVar) for p in params):
995 raise TypeError(
996 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200997 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -0700998 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700999 "Parameters to Generic[...] must all be unique")
1000 tvars = params
1001 args = None
1002 elif self is _Protocol:
1003 # _Protocol is internal, don't check anything.
1004 tvars = params
1005 args = None
1006 elif self.__origin__ in (Generic, _Protocol):
1007 # Can't subscript Generic[...] or _Protocol[...].
1008 raise TypeError("Cannot subscript already-subscripted %s" %
1009 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001010 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001011 # Subscripting a regular Generic subclass.
1012 if not self.__parameters__:
1013 raise TypeError("%s is not a generic class" % repr(self))
1014 alen = len(params)
1015 elen = len(self.__parameters__)
1016 if alen != elen:
1017 raise TypeError(
1018 "Too %s parameters for %s; actual %s, expected %s" %
1019 ("many" if alen > elen else "few", repr(self), alen, elen))
1020 tvars = _type_vars(params)
1021 args = params
1022 return self.__class__(self.__name__,
1023 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001024 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001025 tvars=tvars,
1026 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001027 origin=self,
1028 extra=self.__extra__)
1029
Guido van Rossum1b669102015-09-04 12:15:54 -07001030 def __instancecheck__(self, instance):
1031 # Since we extend ABC.__subclasscheck__ and
1032 # ABC.__instancecheck__ inlines the cache checking done by the
1033 # latter, we must extend __instancecheck__ too. For simplicity
1034 # we just skip the cache check -- instance checks for generic
1035 # classes are supposed to be rare anyways.
1036 return self.__subclasscheck__(instance.__class__)
1037
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001038 def __subclasscheck__(self, cls):
1039 if cls is Any:
1040 return True
1041 if isinstance(cls, GenericMeta):
1042 # For a class C(Generic[T]) where T is co-variant,
1043 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
1044 origin = self.__origin__
1045 if origin is not None and origin is cls.__origin__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001046 assert len(self.__args__) == len(origin.__parameters__)
1047 assert len(cls.__args__) == len(origin.__parameters__)
1048 for p_self, p_cls, p_origin in zip(self.__args__,
1049 cls.__args__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001050 origin.__parameters__):
1051 if isinstance(p_origin, TypeVar):
1052 if p_origin.__covariant__:
1053 # Covariant -- p_cls must be a subclass of p_self.
1054 if not issubclass(p_cls, p_self):
1055 break
1056 elif p_origin.__contravariant__:
1057 # Contravariant. I think it's the opposite. :-)
1058 if not issubclass(p_self, p_cls):
1059 break
1060 else:
1061 # Invariant -- p_cls and p_self must equal.
1062 if p_self != p_cls:
1063 break
1064 else:
1065 # If the origin's parameter is not a typevar,
1066 # insist on invariance.
1067 if p_self != p_cls:
1068 break
1069 else:
1070 return True
1071 # If we break out of the loop, the superclass gets a chance.
1072 if super().__subclasscheck__(cls):
1073 return True
1074 if self.__extra__ is None or isinstance(cls, GenericMeta):
1075 return False
1076 return issubclass(cls, self.__extra__)
1077
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001078
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001079# Prevent checks for Generic to crash when defining Generic.
1080Generic = None
1081
1082
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001083class Generic(metaclass=GenericMeta):
1084 """Abstract base class for generic types.
1085
1086 A generic type is typically declared by inheriting from an
1087 instantiation of this class with one or more type variables.
1088 For example, a generic mapping type might be defined as::
1089
1090 class Mapping(Generic[KT, VT]):
1091 def __getitem__(self, key: KT) -> VT:
1092 ...
1093 # Etc.
1094
1095 This class can then be used as follows::
1096
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001097 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001098 try:
1099 return mapping[key]
1100 except KeyError:
1101 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001102 """
1103
Guido van Rossumd70fe632015-08-05 12:11:06 +02001104 __slots__ = ()
1105
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001106 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001107 if cls.__origin__ is None:
1108 return cls.__next_in_mro__.__new__(cls)
1109 else:
1110 origin = _gorg(cls)
1111 obj = cls.__next_in_mro__.__new__(origin)
1112 obj.__init__(*args, **kwds)
1113 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001114
1115
1116def cast(typ, val):
1117 """Cast a value to a type.
1118
1119 This returns the value unchanged. To the type checker this
1120 signals that the return value has the designated type, but at
1121 runtime we intentionally don't check anything (we want this
1122 to be as fast as possible).
1123 """
1124 return val
1125
1126
1127def _get_defaults(func):
1128 """Internal helper to extract the default arguments, by name."""
1129 code = func.__code__
1130 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001131 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001132 arg_names = arg_names[:pos_count]
1133 defaults = func.__defaults__ or ()
1134 kwdefaults = func.__kwdefaults__
1135 res = dict(kwdefaults) if kwdefaults else {}
1136 pos_offset = pos_count - len(defaults)
1137 for name, value in zip(arg_names[pos_offset:], defaults):
1138 assert name not in res
1139 res[name] = value
1140 return res
1141
1142
1143def get_type_hints(obj, globalns=None, localns=None):
1144 """Return type hints for a function or method object.
1145
1146 This is often the same as obj.__annotations__, but it handles
1147 forward references encoded as string literals, and if necessary
1148 adds Optional[t] if a default value equal to None is set.
1149
1150 BEWARE -- the behavior of globalns and localns is counterintuitive
1151 (unless you are familiar with how eval() and exec() work). The
1152 search order is locals first, then globals.
1153
1154 - If no dict arguments are passed, an attempt is made to use the
1155 globals from obj, and these are also used as the locals. If the
1156 object does not appear to have globals, an exception is raised.
1157
1158 - If one dict argument is passed, it is used for both globals and
1159 locals.
1160
1161 - If two dict arguments are passed, they specify globals and
1162 locals, respectively.
1163 """
1164 if getattr(obj, '__no_type_check__', None):
1165 return {}
1166 if globalns is None:
1167 globalns = getattr(obj, '__globals__', {})
1168 if localns is None:
1169 localns = globalns
1170 elif localns is None:
1171 localns = globalns
1172 defaults = _get_defaults(obj)
1173 hints = dict(obj.__annotations__)
1174 for name, value in hints.items():
1175 if isinstance(value, str):
1176 value = _ForwardRef(value)
1177 value = _eval_type(value, globalns, localns)
1178 if name in defaults and defaults[name] is None:
1179 value = Optional[value]
1180 hints[name] = value
1181 return hints
1182
1183
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001184def no_type_check(arg):
1185 """Decorator to indicate that annotations are not type hints.
1186
1187 The argument must be a class or function; if it is a class, it
1188 applies recursively to all methods defined in that class (but not
1189 to methods defined in its superclasses or subclasses).
1190
1191 This mutates the function(s) in place.
1192 """
1193 if isinstance(arg, type):
1194 for obj in arg.__dict__.values():
1195 if isinstance(obj, types.FunctionType):
1196 obj.__no_type_check__ = True
1197 else:
1198 arg.__no_type_check__ = True
1199 return arg
1200
1201
1202def no_type_check_decorator(decorator):
1203 """Decorator to give another decorator the @no_type_check effect.
1204
1205 This wraps the decorator with something that wraps the decorated
1206 function in @no_type_check.
1207 """
1208
1209 @functools.wraps(decorator)
1210 def wrapped_decorator(*args, **kwds):
1211 func = decorator(*args, **kwds)
1212 func = no_type_check(func)
1213 return func
1214
1215 return wrapped_decorator
1216
1217
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001218def _overload_dummy(*args, **kwds):
1219 """Helper for @overload to raise when called."""
1220 raise NotImplementedError(
1221 "You should not call an overloaded function. "
1222 "A series of @overload-decorated functions "
1223 "outside a stub module should always be followed "
1224 "by an implementation that is not @overload-ed.")
1225
1226
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001227def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001228 """Decorator for overloaded functions/methods.
1229
1230 In a stub file, place two or more stub definitions for the same
1231 function in a row, each decorated with @overload. For example:
1232
1233 @overload
1234 def utf8(value: None) -> None: ...
1235 @overload
1236 def utf8(value: bytes) -> bytes: ...
1237 @overload
1238 def utf8(value: str) -> bytes: ...
1239
1240 In a non-stub file (i.e. a regular .py file), do the same but
1241 follow it with an implementation. The implementation should *not*
1242 be decorated with @overload. For example:
1243
1244 @overload
1245 def utf8(value: None) -> None: ...
1246 @overload
1247 def utf8(value: bytes) -> bytes: ...
1248 @overload
1249 def utf8(value: str) -> bytes: ...
1250 def utf8(value):
1251 # implementation goes here
1252 """
1253 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001254
1255
1256class _ProtocolMeta(GenericMeta):
1257 """Internal metaclass for _Protocol.
1258
1259 This exists so _Protocol classes can be generic without deriving
1260 from Generic.
1261 """
1262
Guido van Rossumd70fe632015-08-05 12:11:06 +02001263 def __instancecheck__(self, obj):
1264 raise TypeError("Protocols cannot be used with isinstance().")
1265
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001266 def __subclasscheck__(self, cls):
1267 if not self._is_protocol:
1268 # No structural checks since this isn't a protocol.
1269 return NotImplemented
1270
1271 if self is _Protocol:
1272 # Every class is a subclass of the empty protocol.
1273 return True
1274
1275 # Find all attributes defined in the protocol.
1276 attrs = self._get_protocol_attrs()
1277
1278 for attr in attrs:
1279 if not any(attr in d.__dict__ for d in cls.__mro__):
1280 return False
1281 return True
1282
1283 def _get_protocol_attrs(self):
1284 # Get all Protocol base classes.
1285 protocol_bases = []
1286 for c in self.__mro__:
1287 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1288 protocol_bases.append(c)
1289
1290 # Get attributes included in protocol.
1291 attrs = set()
1292 for base in protocol_bases:
1293 for attr in base.__dict__.keys():
1294 # Include attributes not defined in any non-protocol bases.
1295 for c in self.__mro__:
1296 if (c is not base and attr in c.__dict__ and
1297 not getattr(c, '_is_protocol', False)):
1298 break
1299 else:
1300 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001301 attr != '__abstractmethods__' and
1302 attr != '_is_protocol' and
1303 attr != '__dict__' and
1304 attr != '__args__' and
1305 attr != '__slots__' and
1306 attr != '_get_protocol_attrs' and
1307 attr != '__next_in_mro__' and
1308 attr != '__parameters__' and
1309 attr != '__origin__' and
1310 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001311 attrs.add(attr)
1312
1313 return attrs
1314
1315
1316class _Protocol(metaclass=_ProtocolMeta):
1317 """Internal base class for protocol classes.
1318
1319 This implements a simple-minded structural isinstance check
1320 (similar but more general than the one-offs in collections.abc
1321 such as Hashable).
1322 """
1323
Guido van Rossumd70fe632015-08-05 12:11:06 +02001324 __slots__ = ()
1325
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001326 _is_protocol = True
1327
1328
1329# Various ABCs mimicking those in collections.abc.
1330# A few are simply re-exported for completeness.
1331
1332Hashable = collections_abc.Hashable # Not generic.
1333
1334
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001335if hasattr(collections_abc, 'Awaitable'):
1336 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1337 __slots__ = ()
1338else:
1339 Awaitable = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001340
1341
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001342if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001343
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001344 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1345 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001346
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001347 class AsyncIterator(AsyncIterable[T_co],
1348 extra=collections_abc.AsyncIterator):
1349 __slots__ = ()
1350
1351else:
1352 AsyncIterable = None
1353 AsyncIterator = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001354
1355
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001356class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001357 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001358
1359
1360class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001361 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001362
1363
1364class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001365 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001366
1367 @abstractmethod
1368 def __int__(self) -> int:
1369 pass
1370
1371
1372class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001373 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001374
1375 @abstractmethod
1376 def __float__(self) -> float:
1377 pass
1378
1379
1380class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001381 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001382
1383 @abstractmethod
1384 def __complex__(self) -> complex:
1385 pass
1386
1387
1388class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001389 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001390
1391 @abstractmethod
1392 def __bytes__(self) -> bytes:
1393 pass
1394
1395
Guido van Rossumd70fe632015-08-05 12:11:06 +02001396class SupportsAbs(_Protocol[T_co]):
1397 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001398
1399 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001400 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001401 pass
1402
1403
Guido van Rossumd70fe632015-08-05 12:11:06 +02001404class SupportsRound(_Protocol[T_co]):
1405 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001406
1407 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001408 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001409 pass
1410
1411
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001412if hasattr(collections_abc, 'Reversible'):
1413 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1414 __slots__ = ()
1415else:
1416 class Reversible(_Protocol[T_co]):
1417 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001418
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001419 @abstractmethod
1420 def __reversed__(self) -> 'Iterator[T_co]':
1421 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001422
1423
1424Sized = collections_abc.Sized # Not generic.
1425
1426
1427class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001428 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001429
1430
1431# Callable was defined earlier.
1432
1433
1434class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1435 extra=collections_abc.Set):
1436 pass
1437
1438
1439class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
1440 pass
1441
1442
Guido van Rossumd70fe632015-08-05 12:11:06 +02001443# NOTE: Only the value type is covariant.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001444class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001445 extra=collections_abc.Mapping):
1446 pass
1447
1448
1449class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
1450 pass
1451
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001452if hasattr(collections_abc, 'Reversible'):
1453 class Sequence(Sized, Reversible[T_co], Container[T_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001454 extra=collections_abc.Sequence):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001455 pass
1456else:
1457 class Sequence(Sized, Iterable[T_co], Container[T_co],
1458 extra=collections_abc.Sequence):
1459 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001460
1461
1462class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
1463 pass
1464
1465
1466class ByteString(Sequence[int], extra=collections_abc.ByteString):
1467 pass
1468
1469
1470ByteString.register(type(memoryview(b'')))
1471
1472
Guido van Rossumd70fe632015-08-05 12:11:06 +02001473class List(list, MutableSequence[T]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001474
1475 def __new__(cls, *args, **kwds):
1476 if _geqv(cls, List):
1477 raise TypeError("Type List cannot be instantiated; "
1478 "use list() instead")
1479 return list.__new__(cls, *args, **kwds)
1480
1481
Guido van Rossumd70fe632015-08-05 12:11:06 +02001482class Set(set, MutableSet[T]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001483
1484 def __new__(cls, *args, **kwds):
1485 if _geqv(cls, Set):
1486 raise TypeError("Type Set cannot be instantiated; "
1487 "use set() instead")
1488 return set.__new__(cls, *args, **kwds)
1489
1490
Guido van Rossumd70fe632015-08-05 12:11:06 +02001491class _FrozenSetMeta(GenericMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001492 """This metaclass ensures set is not a subclass of FrozenSet.
1493
1494 Without this metaclass, set would be considered a subclass of
1495 FrozenSet, because FrozenSet.__extra__ is collections.abc.Set, and
1496 set is a subclass of that.
1497 """
1498
1499 def __subclasscheck__(self, cls):
1500 if issubclass(cls, Set):
1501 return False
1502 return super().__subclasscheck__(cls)
1503
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001504
1505class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
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 Rossumd70fe632015-08-05 12:11:06 +02001541class Dict(dict, MutableMapping[KT, VT]):
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 Rossumbd5b9a02016-04-05 08:28:52 -07001549class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
1550
1551 def __new__(cls, *args, **kwds):
1552 if _geqv(cls, DefaultDict):
1553 raise TypeError("Type DefaultDict cannot be instantiated; "
1554 "use collections.defaultdict() instead")
1555 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001556
1557# Determine what base class to use for Generator.
1558if hasattr(collections_abc, 'Generator'):
1559 # Sufficiently recent versions of 3.5 have a Generator ABC.
1560 _G_base = collections_abc.Generator
1561else:
1562 # Fall back on the exact type.
1563 _G_base = types.GeneratorType
1564
1565
1566class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1567 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001568 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001569
1570 def __new__(cls, *args, **kwds):
1571 if _geqv(cls, Generator):
1572 raise TypeError("Type Generator cannot be instantiated; "
1573 "create a subclass instead")
1574 return super().__new__(cls, *args, **kwds)
1575
1576
1577def NamedTuple(typename, fields):
1578 """Typed version of namedtuple.
1579
1580 Usage::
1581
1582 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1583
1584 This is equivalent to::
1585
1586 Employee = collections.namedtuple('Employee', ['name', 'id'])
1587
1588 The resulting class has one extra attribute: _field_types,
1589 giving a dict mapping field names to types. (The field names
1590 are in the _fields attribute, which is part of the namedtuple
1591 API.)
1592 """
1593 fields = [(n, t) for n, t in fields]
1594 cls = collections.namedtuple(typename, [n for n, t in fields])
1595 cls._field_types = dict(fields)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001596 # Set the module to the caller's module (otherwise it'd be 'typing').
1597 try:
1598 cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
1599 except (AttributeError, ValueError):
1600 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001601 return cls
1602
1603
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001604# Python-version-specific alias (Python 2: unicode; Python 3: str)
1605Text = str
1606
1607
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001608class IO(Generic[AnyStr]):
1609 """Generic base class for TextIO and BinaryIO.
1610
1611 This is an abstract, generic version of the return of open().
1612
1613 NOTE: This does not distinguish between the different possible
1614 classes (text vs. binary, read vs. write vs. read/write,
1615 append-only, unbuffered). The TextIO and BinaryIO subclasses
1616 below capture the distinctions between text vs. binary, which is
1617 pervasive in the interface; however we currently do not offer a
1618 way to track the other distinctions in the type system.
1619 """
1620
Guido van Rossumd70fe632015-08-05 12:11:06 +02001621 __slots__ = ()
1622
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001623 @abstractproperty
1624 def mode(self) -> str:
1625 pass
1626
1627 @abstractproperty
1628 def name(self) -> str:
1629 pass
1630
1631 @abstractmethod
1632 def close(self) -> None:
1633 pass
1634
1635 @abstractmethod
1636 def closed(self) -> bool:
1637 pass
1638
1639 @abstractmethod
1640 def fileno(self) -> int:
1641 pass
1642
1643 @abstractmethod
1644 def flush(self) -> None:
1645 pass
1646
1647 @abstractmethod
1648 def isatty(self) -> bool:
1649 pass
1650
1651 @abstractmethod
1652 def read(self, n: int = -1) -> AnyStr:
1653 pass
1654
1655 @abstractmethod
1656 def readable(self) -> bool:
1657 pass
1658
1659 @abstractmethod
1660 def readline(self, limit: int = -1) -> AnyStr:
1661 pass
1662
1663 @abstractmethod
1664 def readlines(self, hint: int = -1) -> List[AnyStr]:
1665 pass
1666
1667 @abstractmethod
1668 def seek(self, offset: int, whence: int = 0) -> int:
1669 pass
1670
1671 @abstractmethod
1672 def seekable(self) -> bool:
1673 pass
1674
1675 @abstractmethod
1676 def tell(self) -> int:
1677 pass
1678
1679 @abstractmethod
1680 def truncate(self, size: int = None) -> int:
1681 pass
1682
1683 @abstractmethod
1684 def writable(self) -> bool:
1685 pass
1686
1687 @abstractmethod
1688 def write(self, s: AnyStr) -> int:
1689 pass
1690
1691 @abstractmethod
1692 def writelines(self, lines: List[AnyStr]) -> None:
1693 pass
1694
1695 @abstractmethod
1696 def __enter__(self) -> 'IO[AnyStr]':
1697 pass
1698
1699 @abstractmethod
1700 def __exit__(self, type, value, traceback) -> None:
1701 pass
1702
1703
1704class BinaryIO(IO[bytes]):
1705 """Typed version of the return of open() in binary mode."""
1706
Guido van Rossumd70fe632015-08-05 12:11:06 +02001707 __slots__ = ()
1708
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001709 @abstractmethod
1710 def write(self, s: Union[bytes, bytearray]) -> int:
1711 pass
1712
1713 @abstractmethod
1714 def __enter__(self) -> 'BinaryIO':
1715 pass
1716
1717
1718class TextIO(IO[str]):
1719 """Typed version of the return of open() in text mode."""
1720
Guido van Rossumd70fe632015-08-05 12:11:06 +02001721 __slots__ = ()
1722
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001723 @abstractproperty
1724 def buffer(self) -> BinaryIO:
1725 pass
1726
1727 @abstractproperty
1728 def encoding(self) -> str:
1729 pass
1730
1731 @abstractproperty
1732 def errors(self) -> str:
1733 pass
1734
1735 @abstractproperty
1736 def line_buffering(self) -> bool:
1737 pass
1738
1739 @abstractproperty
1740 def newlines(self) -> Any:
1741 pass
1742
1743 @abstractmethod
1744 def __enter__(self) -> 'TextIO':
1745 pass
1746
1747
1748class io:
1749 """Wrapper namespace for IO generic classes."""
1750
1751 __all__ = ['IO', 'TextIO', 'BinaryIO']
1752 IO = IO
1753 TextIO = TextIO
1754 BinaryIO = BinaryIO
1755
1756io.__name__ = __name__ + '.io'
1757sys.modules[io.__name__] = io
1758
1759
1760Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
1761 lambda p: p.pattern)
1762Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
1763 lambda m: m.re.pattern)
1764
1765
1766class re:
1767 """Wrapper namespace for re type aliases."""
1768
1769 __all__ = ['Pattern', 'Match']
1770 Pattern = Pattern
1771 Match = Match
1772
1773re.__name__ = __name__ + '.re'
1774sys.modules[re.__name__] = re