blob: 42a9ea3d660c82bc7a4e338b2cf9bb3234fd3572 [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
708 return self.__tuple_params__ == other.__tuple_params__
709
710 def __hash__(self):
711 return hash(self.__tuple_params__)
712
Guido van Rossumd70fe632015-08-05 12:11:06 +0200713 def __instancecheck__(self, obj):
714 raise TypeError("Tuples cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700715
716 def __subclasscheck__(self, cls):
717 if cls is Any:
718 return True
719 if not isinstance(cls, type):
720 return super().__subclasscheck__(cls) # To TypeError.
721 if issubclass(cls, tuple):
722 return True # Special case.
723 if not isinstance(cls, TupleMeta):
724 return super().__subclasscheck__(cls) # False.
725 if self.__tuple_params__ is None:
726 return True
727 if cls.__tuple_params__ is None:
728 return False # ???
729 if cls.__tuple_use_ellipsis__ != self.__tuple_use_ellipsis__:
730 return False
731 # Covariance.
732 return (len(self.__tuple_params__) == len(cls.__tuple_params__) and
733 all(issubclass(x, p)
734 for x, p in zip(cls.__tuple_params__,
735 self.__tuple_params__)))
736
737
738class Tuple(Final, metaclass=TupleMeta, _root=True):
739 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
740
741 Example: Tuple[T1, T2] is a tuple of two elements corresponding
742 to type variables T1 and T2. Tuple[int, float, str] is a tuple
743 of an int, a float and a string.
744
745 To specify a variable-length tuple of homogeneous type, use Sequence[T].
746 """
747
Guido van Rossumd70fe632015-08-05 12:11:06 +0200748 __slots__ = ()
749
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700750
751class CallableMeta(TypingMeta):
752 """Metaclass for Callable."""
753
754 def __new__(cls, name, bases, namespace, _root=False,
755 args=None, result=None):
756 if args is None and result is None:
757 pass # Must be 'class Callable'.
758 else:
759 if args is not Ellipsis:
760 if not isinstance(args, list):
761 raise TypeError("Callable[args, result]: "
762 "args must be a list."
763 " Got %.100r." % (args,))
764 msg = "Callable[[arg, ...], result]: each arg must be a type."
765 args = tuple(_type_check(arg, msg) for arg in args)
766 msg = "Callable[args, result]: result must be a type."
767 result = _type_check(result, msg)
768 self = super().__new__(cls, name, bases, namespace, _root=_root)
769 self.__args__ = args
770 self.__result__ = result
771 return self
772
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700773 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700774 if self.__args__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700775 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700776
777 def _eval_type(self, globalns, localns):
778 if self.__args__ is None and self.__result__ is None:
779 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200780 if self.__args__ is Ellipsis:
781 args = self.__args__
782 else:
783 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700784 result = _eval_type(self.__result__, globalns, localns)
785 if args == self.__args__ and result == self.__result__:
786 return self
787 else:
788 return self.__class__(self.__name__, self.__bases__, {},
789 args=args, result=result, _root=True)
790
791 def __repr__(self):
792 r = super().__repr__()
793 if self.__args__ is not None or self.__result__ is not None:
794 if self.__args__ is Ellipsis:
795 args_r = '...'
796 else:
797 args_r = '[%s]' % ', '.join(_type_repr(t)
798 for t in self.__args__)
799 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
800 return r
801
802 def __getitem__(self, parameters):
803 if self.__args__ is not None or self.__result__ is not None:
804 raise TypeError("This Callable type is already parameterized.")
805 if not isinstance(parameters, tuple) or len(parameters) != 2:
806 raise TypeError(
807 "Callable must be used as Callable[[arg, ...], result].")
808 args, result = parameters
809 return self.__class__(self.__name__, self.__bases__,
810 dict(self.__dict__), _root=True,
811 args=args, result=result)
812
813 def __eq__(self, other):
814 if not isinstance(other, CallableMeta):
815 return NotImplemented
816 return (self.__args__ == other.__args__ and
817 self.__result__ == other.__result__)
818
819 def __hash__(self):
820 return hash(self.__args__) ^ hash(self.__result__)
821
Guido van Rossumd70fe632015-08-05 12:11:06 +0200822 def __instancecheck__(self, obj):
823 # For unparametrized Callable we allow this, because
824 # typing.Callable should be equivalent to
825 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700826 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200827 return isinstance(obj, collections_abc.Callable)
828 else:
829 raise TypeError("Callable[] cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700830
831 def __subclasscheck__(self, cls):
832 if cls is Any:
833 return True
834 if not isinstance(cls, CallableMeta):
835 return super().__subclasscheck__(cls)
836 if self.__args__ is None and self.__result__ is None:
837 return True
838 # We're not doing covariance or contravariance -- this is *invariance*.
839 return self == cls
840
841
842class Callable(Final, metaclass=CallableMeta, _root=True):
843 """Callable type; Callable[[int], str] is a function of (int) -> str.
844
845 The subscription syntax must always be used with exactly two
846 values: the argument list and the return type. The argument list
847 must be a list of types; the return type must be a single type.
848
849 There is no syntax to indicate optional or keyword arguments,
850 such function types are rarely used as callback types.
851 """
852
Guido van Rossumd70fe632015-08-05 12:11:06 +0200853 __slots__ = ()
854
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700855
856def _gorg(a):
857 """Return the farthest origin of a generic class."""
858 assert isinstance(a, GenericMeta)
859 while a.__origin__ is not None:
860 a = a.__origin__
861 return a
862
863
864def _geqv(a, b):
865 """Return whether two generic classes are equivalent.
866
867 The intention is to consider generic class X and any of its
868 parameterized forms (X[T], X[int], etc.) as equivalent.
869
870 However, X is not equivalent to a subclass of X.
871
872 The relation is reflexive, symmetric and transitive.
873 """
874 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
875 # Reduce each to its origin.
876 return _gorg(a) is _gorg(b)
877
878
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700879def _next_in_mro(cls):
880 """Helper for Generic.__new__.
881
882 Returns the class after the last occurrence of Generic or
883 Generic[...] in cls.__mro__.
884 """
885 next_in_mro = object
886 # Look for the last occurrence of Generic or Generic[...].
887 for i, c in enumerate(cls.__mro__[:-1]):
888 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
889 next_in_mro = cls.__mro__[i+1]
890 return next_in_mro
891
892
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700893class GenericMeta(TypingMeta, abc.ABCMeta):
894 """Metaclass for generic types."""
895
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700896 __extra__ = None
897
898 def __new__(cls, name, bases, namespace,
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700899 tvars=None, args=None, origin=None, extra=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700900 self = super().__new__(cls, name, bases, namespace, _root=True)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700901
902 if tvars is not None:
903 # Called from __getitem__() below.
904 assert origin is not None
905 assert all(isinstance(t, TypeVar) for t in tvars), tvars
906 else:
907 # Called from class statement.
908 assert tvars is None, tvars
909 assert args is None, args
910 assert origin is None, origin
911
912 # Get the full set of tvars from the bases.
913 tvars = _type_vars(bases)
914 # Look for Generic[T1, ..., Tn].
915 # If found, tvars must be a subset of it.
916 # If not found, tvars is it.
917 # Also check for and reject plain Generic,
918 # and reject multiple Generic[...].
919 gvars = None
920 for base in bases:
921 if base is Generic:
922 raise TypeError("Cannot inherit from plain Generic")
923 if (isinstance(base, GenericMeta) and
924 base.__origin__ is Generic):
925 if gvars is not None:
926 raise TypeError(
927 "Cannot inherit from Generic[...] multiple types.")
928 gvars = base.__parameters__
929 if gvars is None:
930 gvars = tvars
931 else:
932 tvarset = set(tvars)
933 gvarset = set(gvars)
934 if not tvarset <= gvarset:
935 raise TypeError(
936 "Some type variables (%s) "
937 "are not listed in Generic[%s]" %
938 (", ".join(str(t) for t in tvars if t not in gvarset),
939 ", ".join(str(g) for g in gvars)))
940 tvars = gvars
941
942 self.__parameters__ = tvars
943 self.__args__ = args
944 self.__origin__ = origin
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700945 if extra is not None:
946 self.__extra__ = extra
947 # Else __extra__ is inherited, eventually from the
948 # (meta-)class default above.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700949 # Speed hack (https://github.com/python/typing/issues/196).
950 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700951 return self
952
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700953 def _get_type_vars(self, tvars):
954 if self.__origin__ and self.__parameters__:
955 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700956
957 def __repr__(self):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700958 if self.__origin__ is not None:
959 r = repr(self.__origin__)
960 else:
961 r = super().__repr__()
962 if self.__args__:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700963 r += '[%s]' % (
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700964 ', '.join(_type_repr(p) for p in self.__args__))
965 if self.__parameters__:
966 r += '<%s>' % (
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700967 ', '.join(_type_repr(p) for p in self.__parameters__))
968 return r
969
970 def __eq__(self, other):
971 if not isinstance(other, GenericMeta):
972 return NotImplemented
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700973 if self.__origin__ is not None:
974 return (self.__origin__ is other.__origin__ and
975 self.__args__ == other.__args__ and
976 self.__parameters__ == other.__parameters__)
977 else:
978 return self is other
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700979
980 def __hash__(self):
981 return hash((self.__name__, self.__parameters__))
982
983 def __getitem__(self, params):
984 if not isinstance(params, tuple):
985 params = (params,)
986 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700987 raise TypeError(
988 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700989 msg = "Parameters to generic types must be types."
990 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700991 if self is Generic:
992 # Generic can only be subscripted with unique type variables.
993 if not all(isinstance(p, TypeVar) for p in params):
994 raise TypeError(
995 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200996 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -0700997 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700998 "Parameters to Generic[...] must all be unique")
999 tvars = params
1000 args = None
1001 elif self is _Protocol:
1002 # _Protocol is internal, don't check anything.
1003 tvars = params
1004 args = None
1005 elif self.__origin__ in (Generic, _Protocol):
1006 # Can't subscript Generic[...] or _Protocol[...].
1007 raise TypeError("Cannot subscript already-subscripted %s" %
1008 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001009 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001010 # Subscripting a regular Generic subclass.
1011 if not self.__parameters__:
1012 raise TypeError("%s is not a generic class" % repr(self))
1013 alen = len(params)
1014 elen = len(self.__parameters__)
1015 if alen != elen:
1016 raise TypeError(
1017 "Too %s parameters for %s; actual %s, expected %s" %
1018 ("many" if alen > elen else "few", repr(self), alen, elen))
1019 tvars = _type_vars(params)
1020 args = params
1021 return self.__class__(self.__name__,
1022 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001023 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001024 tvars=tvars,
1025 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001026 origin=self,
1027 extra=self.__extra__)
1028
Guido van Rossum1b669102015-09-04 12:15:54 -07001029 def __instancecheck__(self, instance):
1030 # Since we extend ABC.__subclasscheck__ and
1031 # ABC.__instancecheck__ inlines the cache checking done by the
1032 # latter, we must extend __instancecheck__ too. For simplicity
1033 # we just skip the cache check -- instance checks for generic
1034 # classes are supposed to be rare anyways.
1035 return self.__subclasscheck__(instance.__class__)
1036
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001037 def __subclasscheck__(self, cls):
1038 if cls is Any:
1039 return True
1040 if isinstance(cls, GenericMeta):
1041 # For a class C(Generic[T]) where T is co-variant,
1042 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
1043 origin = self.__origin__
1044 if origin is not None and origin is cls.__origin__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001045 assert len(self.__args__) == len(origin.__parameters__)
1046 assert len(cls.__args__) == len(origin.__parameters__)
1047 for p_self, p_cls, p_origin in zip(self.__args__,
1048 cls.__args__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001049 origin.__parameters__):
1050 if isinstance(p_origin, TypeVar):
1051 if p_origin.__covariant__:
1052 # Covariant -- p_cls must be a subclass of p_self.
1053 if not issubclass(p_cls, p_self):
1054 break
1055 elif p_origin.__contravariant__:
1056 # Contravariant. I think it's the opposite. :-)
1057 if not issubclass(p_self, p_cls):
1058 break
1059 else:
1060 # Invariant -- p_cls and p_self must equal.
1061 if p_self != p_cls:
1062 break
1063 else:
1064 # If the origin's parameter is not a typevar,
1065 # insist on invariance.
1066 if p_self != p_cls:
1067 break
1068 else:
1069 return True
1070 # If we break out of the loop, the superclass gets a chance.
1071 if super().__subclasscheck__(cls):
1072 return True
1073 if self.__extra__ is None or isinstance(cls, GenericMeta):
1074 return False
1075 return issubclass(cls, self.__extra__)
1076
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001077
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001078# Prevent checks for Generic to crash when defining Generic.
1079Generic = None
1080
1081
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001082class Generic(metaclass=GenericMeta):
1083 """Abstract base class for generic types.
1084
1085 A generic type is typically declared by inheriting from an
1086 instantiation of this class with one or more type variables.
1087 For example, a generic mapping type might be defined as::
1088
1089 class Mapping(Generic[KT, VT]):
1090 def __getitem__(self, key: KT) -> VT:
1091 ...
1092 # Etc.
1093
1094 This class can then be used as follows::
1095
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001096 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001097 try:
1098 return mapping[key]
1099 except KeyError:
1100 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001101 """
1102
Guido van Rossumd70fe632015-08-05 12:11:06 +02001103 __slots__ = ()
1104
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001105 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001106 if cls.__origin__ is None:
1107 return cls.__next_in_mro__.__new__(cls)
1108 else:
1109 origin = _gorg(cls)
1110 obj = cls.__next_in_mro__.__new__(origin)
1111 obj.__init__(*args, **kwds)
1112 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001113
1114
1115def cast(typ, val):
1116 """Cast a value to a type.
1117
1118 This returns the value unchanged. To the type checker this
1119 signals that the return value has the designated type, but at
1120 runtime we intentionally don't check anything (we want this
1121 to be as fast as possible).
1122 """
1123 return val
1124
1125
1126def _get_defaults(func):
1127 """Internal helper to extract the default arguments, by name."""
1128 code = func.__code__
1129 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001130 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001131 arg_names = arg_names[:pos_count]
1132 defaults = func.__defaults__ or ()
1133 kwdefaults = func.__kwdefaults__
1134 res = dict(kwdefaults) if kwdefaults else {}
1135 pos_offset = pos_count - len(defaults)
1136 for name, value in zip(arg_names[pos_offset:], defaults):
1137 assert name not in res
1138 res[name] = value
1139 return res
1140
1141
1142def get_type_hints(obj, globalns=None, localns=None):
1143 """Return type hints for a function or method object.
1144
1145 This is often the same as obj.__annotations__, but it handles
1146 forward references encoded as string literals, and if necessary
1147 adds Optional[t] if a default value equal to None is set.
1148
1149 BEWARE -- the behavior of globalns and localns is counterintuitive
1150 (unless you are familiar with how eval() and exec() work). The
1151 search order is locals first, then globals.
1152
1153 - If no dict arguments are passed, an attempt is made to use the
1154 globals from obj, and these are also used as the locals. If the
1155 object does not appear to have globals, an exception is raised.
1156
1157 - If one dict argument is passed, it is used for both globals and
1158 locals.
1159
1160 - If two dict arguments are passed, they specify globals and
1161 locals, respectively.
1162 """
1163 if getattr(obj, '__no_type_check__', None):
1164 return {}
1165 if globalns is None:
1166 globalns = getattr(obj, '__globals__', {})
1167 if localns is None:
1168 localns = globalns
1169 elif localns is None:
1170 localns = globalns
1171 defaults = _get_defaults(obj)
1172 hints = dict(obj.__annotations__)
1173 for name, value in hints.items():
1174 if isinstance(value, str):
1175 value = _ForwardRef(value)
1176 value = _eval_type(value, globalns, localns)
1177 if name in defaults and defaults[name] is None:
1178 value = Optional[value]
1179 hints[name] = value
1180 return hints
1181
1182
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001183def no_type_check(arg):
1184 """Decorator to indicate that annotations are not type hints.
1185
1186 The argument must be a class or function; if it is a class, it
1187 applies recursively to all methods defined in that class (but not
1188 to methods defined in its superclasses or subclasses).
1189
1190 This mutates the function(s) in place.
1191 """
1192 if isinstance(arg, type):
1193 for obj in arg.__dict__.values():
1194 if isinstance(obj, types.FunctionType):
1195 obj.__no_type_check__ = True
1196 else:
1197 arg.__no_type_check__ = True
1198 return arg
1199
1200
1201def no_type_check_decorator(decorator):
1202 """Decorator to give another decorator the @no_type_check effect.
1203
1204 This wraps the decorator with something that wraps the decorated
1205 function in @no_type_check.
1206 """
1207
1208 @functools.wraps(decorator)
1209 def wrapped_decorator(*args, **kwds):
1210 func = decorator(*args, **kwds)
1211 func = no_type_check(func)
1212 return func
1213
1214 return wrapped_decorator
1215
1216
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001217def _overload_dummy(*args, **kwds):
1218 """Helper for @overload to raise when called."""
1219 raise NotImplementedError(
1220 "You should not call an overloaded function. "
1221 "A series of @overload-decorated functions "
1222 "outside a stub module should always be followed "
1223 "by an implementation that is not @overload-ed.")
1224
1225
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001226def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001227 """Decorator for overloaded functions/methods.
1228
1229 In a stub file, place two or more stub definitions for the same
1230 function in a row, each decorated with @overload. For example:
1231
1232 @overload
1233 def utf8(value: None) -> None: ...
1234 @overload
1235 def utf8(value: bytes) -> bytes: ...
1236 @overload
1237 def utf8(value: str) -> bytes: ...
1238
1239 In a non-stub file (i.e. a regular .py file), do the same but
1240 follow it with an implementation. The implementation should *not*
1241 be decorated with @overload. For example:
1242
1243 @overload
1244 def utf8(value: None) -> None: ...
1245 @overload
1246 def utf8(value: bytes) -> bytes: ...
1247 @overload
1248 def utf8(value: str) -> bytes: ...
1249 def utf8(value):
1250 # implementation goes here
1251 """
1252 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001253
1254
1255class _ProtocolMeta(GenericMeta):
1256 """Internal metaclass for _Protocol.
1257
1258 This exists so _Protocol classes can be generic without deriving
1259 from Generic.
1260 """
1261
Guido van Rossumd70fe632015-08-05 12:11:06 +02001262 def __instancecheck__(self, obj):
1263 raise TypeError("Protocols cannot be used with isinstance().")
1264
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001265 def __subclasscheck__(self, cls):
1266 if not self._is_protocol:
1267 # No structural checks since this isn't a protocol.
1268 return NotImplemented
1269
1270 if self is _Protocol:
1271 # Every class is a subclass of the empty protocol.
1272 return True
1273
1274 # Find all attributes defined in the protocol.
1275 attrs = self._get_protocol_attrs()
1276
1277 for attr in attrs:
1278 if not any(attr in d.__dict__ for d in cls.__mro__):
1279 return False
1280 return True
1281
1282 def _get_protocol_attrs(self):
1283 # Get all Protocol base classes.
1284 protocol_bases = []
1285 for c in self.__mro__:
1286 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1287 protocol_bases.append(c)
1288
1289 # Get attributes included in protocol.
1290 attrs = set()
1291 for base in protocol_bases:
1292 for attr in base.__dict__.keys():
1293 # Include attributes not defined in any non-protocol bases.
1294 for c in self.__mro__:
1295 if (c is not base and attr in c.__dict__ and
1296 not getattr(c, '_is_protocol', False)):
1297 break
1298 else:
1299 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001300 attr != '__abstractmethods__' and
1301 attr != '_is_protocol' and
1302 attr != '__dict__' and
1303 attr != '__args__' and
1304 attr != '__slots__' and
1305 attr != '_get_protocol_attrs' and
1306 attr != '__next_in_mro__' and
1307 attr != '__parameters__' and
1308 attr != '__origin__' and
1309 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 Rossumd70fe632015-08-05 12:11:06 +02001472class List(list, MutableSequence[T]):
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 Rossumd70fe632015-08-05 12:11:06 +02001481class Set(set, MutableSet[T]):
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
1504class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001505 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001506
1507 def __new__(cls, *args, **kwds):
1508 if _geqv(cls, FrozenSet):
1509 raise TypeError("Type FrozenSet cannot be instantiated; "
1510 "use frozenset() instead")
1511 return frozenset.__new__(cls, *args, **kwds)
1512
1513
1514class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
1515 pass
1516
1517
Guido van Rossumd70fe632015-08-05 12:11:06 +02001518class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001519 extra=collections_abc.KeysView):
1520 pass
1521
1522
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001523class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001524 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001525 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001526 extra=collections_abc.ItemsView):
1527 pass
1528
1529
1530class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
1531 pass
1532
1533
Brett Cannonf3ad0422016-04-15 10:51:30 -07001534if hasattr(contextlib, 'AbstractContextManager'):
1535 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1536 __slots__ = ()
1537 __all__.append('ContextManager')
1538
1539
Guido van Rossumd70fe632015-08-05 12:11:06 +02001540class Dict(dict, MutableMapping[KT, VT]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001541
1542 def __new__(cls, *args, **kwds):
1543 if _geqv(cls, Dict):
1544 raise TypeError("Type Dict cannot be instantiated; "
1545 "use dict() instead")
1546 return dict.__new__(cls, *args, **kwds)
1547
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001548class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
1549
1550 def __new__(cls, *args, **kwds):
1551 if _geqv(cls, DefaultDict):
1552 raise TypeError("Type DefaultDict cannot be instantiated; "
1553 "use collections.defaultdict() instead")
1554 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001555
1556# Determine what base class to use for Generator.
1557if hasattr(collections_abc, 'Generator'):
1558 # Sufficiently recent versions of 3.5 have a Generator ABC.
1559 _G_base = collections_abc.Generator
1560else:
1561 # Fall back on the exact type.
1562 _G_base = types.GeneratorType
1563
1564
1565class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1566 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001567 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001568
1569 def __new__(cls, *args, **kwds):
1570 if _geqv(cls, Generator):
1571 raise TypeError("Type Generator cannot be instantiated; "
1572 "create a subclass instead")
1573 return super().__new__(cls, *args, **kwds)
1574
1575
1576def NamedTuple(typename, fields):
1577 """Typed version of namedtuple.
1578
1579 Usage::
1580
1581 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1582
1583 This is equivalent to::
1584
1585 Employee = collections.namedtuple('Employee', ['name', 'id'])
1586
1587 The resulting class has one extra attribute: _field_types,
1588 giving a dict mapping field names to types. (The field names
1589 are in the _fields attribute, which is part of the namedtuple
1590 API.)
1591 """
1592 fields = [(n, t) for n, t in fields]
1593 cls = collections.namedtuple(typename, [n for n, t in fields])
1594 cls._field_types = dict(fields)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001595 # Set the module to the caller's module (otherwise it'd be 'typing').
1596 try:
1597 cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
1598 except (AttributeError, ValueError):
1599 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001600 return cls
1601
1602
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001603# Python-version-specific alias (Python 2: unicode; Python 3: str)
1604Text = str
1605
1606
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001607class IO(Generic[AnyStr]):
1608 """Generic base class for TextIO and BinaryIO.
1609
1610 This is an abstract, generic version of the return of open().
1611
1612 NOTE: This does not distinguish between the different possible
1613 classes (text vs. binary, read vs. write vs. read/write,
1614 append-only, unbuffered). The TextIO and BinaryIO subclasses
1615 below capture the distinctions between text vs. binary, which is
1616 pervasive in the interface; however we currently do not offer a
1617 way to track the other distinctions in the type system.
1618 """
1619
Guido van Rossumd70fe632015-08-05 12:11:06 +02001620 __slots__ = ()
1621
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001622 @abstractproperty
1623 def mode(self) -> str:
1624 pass
1625
1626 @abstractproperty
1627 def name(self) -> str:
1628 pass
1629
1630 @abstractmethod
1631 def close(self) -> None:
1632 pass
1633
1634 @abstractmethod
1635 def closed(self) -> bool:
1636 pass
1637
1638 @abstractmethod
1639 def fileno(self) -> int:
1640 pass
1641
1642 @abstractmethod
1643 def flush(self) -> None:
1644 pass
1645
1646 @abstractmethod
1647 def isatty(self) -> bool:
1648 pass
1649
1650 @abstractmethod
1651 def read(self, n: int = -1) -> AnyStr:
1652 pass
1653
1654 @abstractmethod
1655 def readable(self) -> bool:
1656 pass
1657
1658 @abstractmethod
1659 def readline(self, limit: int = -1) -> AnyStr:
1660 pass
1661
1662 @abstractmethod
1663 def readlines(self, hint: int = -1) -> List[AnyStr]:
1664 pass
1665
1666 @abstractmethod
1667 def seek(self, offset: int, whence: int = 0) -> int:
1668 pass
1669
1670 @abstractmethod
1671 def seekable(self) -> bool:
1672 pass
1673
1674 @abstractmethod
1675 def tell(self) -> int:
1676 pass
1677
1678 @abstractmethod
1679 def truncate(self, size: int = None) -> int:
1680 pass
1681
1682 @abstractmethod
1683 def writable(self) -> bool:
1684 pass
1685
1686 @abstractmethod
1687 def write(self, s: AnyStr) -> int:
1688 pass
1689
1690 @abstractmethod
1691 def writelines(self, lines: List[AnyStr]) -> None:
1692 pass
1693
1694 @abstractmethod
1695 def __enter__(self) -> 'IO[AnyStr]':
1696 pass
1697
1698 @abstractmethod
1699 def __exit__(self, type, value, traceback) -> None:
1700 pass
1701
1702
1703class BinaryIO(IO[bytes]):
1704 """Typed version of the return of open() in binary mode."""
1705
Guido van Rossumd70fe632015-08-05 12:11:06 +02001706 __slots__ = ()
1707
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001708 @abstractmethod
1709 def write(self, s: Union[bytes, bytearray]) -> int:
1710 pass
1711
1712 @abstractmethod
1713 def __enter__(self) -> 'BinaryIO':
1714 pass
1715
1716
1717class TextIO(IO[str]):
1718 """Typed version of the return of open() in text mode."""
1719
Guido van Rossumd70fe632015-08-05 12:11:06 +02001720 __slots__ = ()
1721
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001722 @abstractproperty
1723 def buffer(self) -> BinaryIO:
1724 pass
1725
1726 @abstractproperty
1727 def encoding(self) -> str:
1728 pass
1729
1730 @abstractproperty
1731 def errors(self) -> str:
1732 pass
1733
1734 @abstractproperty
1735 def line_buffering(self) -> bool:
1736 pass
1737
1738 @abstractproperty
1739 def newlines(self) -> Any:
1740 pass
1741
1742 @abstractmethod
1743 def __enter__(self) -> 'TextIO':
1744 pass
1745
1746
1747class io:
1748 """Wrapper namespace for IO generic classes."""
1749
1750 __all__ = ['IO', 'TextIO', 'BinaryIO']
1751 IO = IO
1752 TextIO = TextIO
1753 BinaryIO = BinaryIO
1754
1755io.__name__ = __name__ + '.io'
1756sys.modules[io.__name__] = io
1757
1758
1759Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
1760 lambda p: p.pattern)
1761Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
1762 lambda m: m.re.pattern)
1763
1764
1765class re:
1766 """Wrapper namespace for re type aliases."""
1767
1768 __all__ = ['Pattern', 'Match']
1769 Pattern = Pattern
1770 Match = Match
1771
1772re.__name__ = __name__ + '.re'
1773sys.modules[re.__name__] = re