blob: de2a462e75948216fccbc27375433cf2ca087bfc [file] [log] [blame]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001import abc
2from abc import abstractmethod, abstractproperty
3import collections
4import functools
5import re as stdlib_re # Avoid confusion with the re we export.
6import sys
7import types
8try:
9 import collections.abc as collections_abc
10except ImportError:
11 import collections as collections_abc # Fallback for PY3.2.
12
13
14# Please keep __all__ alphabetized within each category.
15__all__ = [
16 # Super-special typing primitives.
17 'Any',
18 'Callable',
19 'Generic',
20 'Optional',
21 'TypeVar',
22 'Union',
23 'Tuple',
24
25 # ABCs (from collections.abc).
26 'AbstractSet', # collections.abc.Set.
Guido van Rossumf17c2002015-12-03 17:31:24 -080027 'Awaitable',
28 'AsyncIterator',
29 'AsyncIterable',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070030 'ByteString',
31 'Container',
32 'Hashable',
33 'ItemsView',
34 'Iterable',
35 'Iterator',
36 'KeysView',
37 'Mapping',
38 'MappingView',
39 'MutableMapping',
40 'MutableSequence',
41 'MutableSet',
42 'Sequence',
43 'Sized',
44 'ValuesView',
45
46 # Structural checks, a.k.a. protocols.
47 'Reversible',
48 'SupportsAbs',
49 'SupportsFloat',
50 'SupportsInt',
51 'SupportsRound',
52
53 # Concrete collection types.
54 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070055 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070056 'List',
57 'Set',
58 'NamedTuple', # Not really a type.
59 'Generator',
60
61 # One-off things.
62 'AnyStr',
63 'cast',
64 'get_type_hints',
65 'no_type_check',
66 'no_type_check_decorator',
67 'overload',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070068]
69
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070070# The pseudo-submodules 're' and 'io' are part of the public
71# namespace, but excluded from __all__ because they might stomp on
72# legitimate imports of those modules.
73
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070074
75def _qualname(x):
76 if sys.version_info[:2] >= (3, 3):
77 return x.__qualname__
78 else:
79 # Fall back to just name.
80 return x.__name__
81
82
83class TypingMeta(type):
84 """Metaclass for every type defined below.
85
86 This overrides __new__() to require an extra keyword parameter
87 '_root', which serves as a guard against naive subclassing of the
88 typing classes. Any legitimate class defined using a metaclass
89 derived from TypingMeta (including internal subclasses created by
90 e.g. Union[X, Y]) must pass _root=True.
91
92 This also defines a dummy constructor (all the work is done in
93 __new__) and a nicer repr().
94 """
95
96 _is_protocol = False
97
98 def __new__(cls, name, bases, namespace, *, _root=False):
99 if not _root:
100 raise TypeError("Cannot subclass %s" %
101 (', '.join(map(_type_repr, bases)) or '()'))
102 return super().__new__(cls, name, bases, namespace)
103
104 def __init__(self, *args, **kwds):
105 pass
106
107 def _eval_type(self, globalns, localns):
108 """Override this in subclasses to interpret forward references.
109
110 For example, Union['C'] is internally stored as
111 Union[_ForwardRef('C')], which should evaluate to _Union[C],
112 where C is an object found in globalns or localns (searching
113 localns first, of course).
114 """
115 return self
116
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700117 def _get_type_vars(self, tvars):
118 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700119
120 def __repr__(self):
121 return '%s.%s' % (self.__module__, _qualname(self))
122
123
124class Final:
125 """Mix-in class to prevent instantiation."""
126
Guido van Rossumd70fe632015-08-05 12:11:06 +0200127 __slots__ = ()
128
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700129 def __new__(self, *args, **kwds):
130 raise TypeError("Cannot instantiate %r" % self.__class__)
131
132
133class _ForwardRef(TypingMeta):
134 """Wrapper to hold a forward reference."""
135
136 def __new__(cls, arg):
137 if not isinstance(arg, str):
138 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
139 try:
140 code = compile(arg, '<string>', 'eval')
141 except SyntaxError:
142 raise SyntaxError('ForwardRef must be an expression -- got %r' %
143 (arg,))
144 self = super().__new__(cls, arg, (), {}, _root=True)
145 self.__forward_arg__ = arg
146 self.__forward_code__ = code
147 self.__forward_evaluated__ = False
148 self.__forward_value__ = None
149 typing_globals = globals()
150 frame = sys._getframe(1)
151 while frame is not None and frame.f_globals is typing_globals:
152 frame = frame.f_back
153 assert frame is not None
154 self.__forward_frame__ = frame
155 return self
156
157 def _eval_type(self, globalns, localns):
158 if not isinstance(localns, dict):
159 raise TypeError('ForwardRef localns must be a dict -- got %r' %
160 (localns,))
161 if not isinstance(globalns, dict):
162 raise TypeError('ForwardRef globalns must be a dict -- got %r' %
163 (globalns,))
164 if not self.__forward_evaluated__:
165 if globalns is None and localns is None:
166 globalns = localns = {}
167 elif globalns is None:
168 globalns = localns
169 elif localns is None:
170 localns = globalns
171 self.__forward_value__ = _type_check(
172 eval(self.__forward_code__, globalns, localns),
173 "Forward references must evaluate to types.")
174 self.__forward_evaluated__ = True
175 return self.__forward_value__
176
Guido van Rossumd70fe632015-08-05 12:11:06 +0200177 def __instancecheck__(self, obj):
178 raise TypeError("Forward references cannot be used with isinstance().")
179
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700180 def __subclasscheck__(self, cls):
181 if not self.__forward_evaluated__:
182 globalns = self.__forward_frame__.f_globals
183 localns = self.__forward_frame__.f_locals
184 try:
185 self._eval_type(globalns, localns)
186 except NameError:
187 return False # Too early.
188 return issubclass(cls, self.__forward_value__)
189
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700190 def __repr__(self):
191 return '_ForwardRef(%r)' % (self.__forward_arg__,)
192
193
194class _TypeAlias:
195 """Internal helper class for defining generic variants of concrete types.
196
197 Note that this is not a type; let's call it a pseudo-type. It can
198 be used in instance and subclass checks, e.g. isinstance(m, Match)
199 or issubclass(type(m), Match). However, it cannot be itself the
200 target of an issubclass() call; e.g. issubclass(Match, C) (for
201 some arbitrary class C) raises TypeError rather than returning
202 False.
203 """
204
Guido van Rossumd70fe632015-08-05 12:11:06 +0200205 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
206
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700207 def __new__(cls, *args, **kwds):
208 """Constructor.
209
210 This only exists to give a better error message in case
211 someone tries to subclass a type alias (not a good idea).
212 """
213 if (len(args) == 3 and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700214 isinstance(args[0], str) and
215 isinstance(args[1], tuple)):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700216 # Close enough.
217 raise TypeError("A type alias cannot be subclassed")
218 return object.__new__(cls)
219
220 def __init__(self, name, type_var, impl_type, type_checker):
221 """Initializer.
222
223 Args:
224 name: The name, e.g. 'Pattern'.
225 type_var: The type parameter, e.g. AnyStr, or the
226 specific type, e.g. str.
227 impl_type: The implementation type.
228 type_checker: Function that takes an impl_type instance.
229 and returns a value that should be a type_var instance.
230 """
231 assert isinstance(name, str), repr(name)
232 assert isinstance(type_var, type), repr(type_var)
233 assert isinstance(impl_type, type), repr(impl_type)
234 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
235 self.name = name
236 self.type_var = type_var
237 self.impl_type = impl_type
238 self.type_checker = type_checker
239
240 def __repr__(self):
241 return "%s[%s]" % (self.name, _type_repr(self.type_var))
242
243 def __getitem__(self, parameter):
244 assert isinstance(parameter, type), repr(parameter)
245 if not isinstance(self.type_var, TypeVar):
246 raise TypeError("%s cannot be further parameterized." % self)
247 if self.type_var.__constraints__:
248 if not issubclass(parameter, Union[self.type_var.__constraints__]):
249 raise TypeError("%s is not a valid substitution for %s." %
250 (parameter, self.type_var))
251 return self.__class__(self.name, parameter,
252 self.impl_type, self.type_checker)
253
254 def __instancecheck__(self, obj):
Guido van Rossumd70fe632015-08-05 12:11:06 +0200255 raise TypeError("Type aliases cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700256
257 def __subclasscheck__(self, cls):
258 if cls is Any:
259 return True
260 if isinstance(cls, _TypeAlias):
261 # Covariance. For now, we compare by name.
262 return (cls.name == self.name and
263 issubclass(cls.type_var, self.type_var))
264 else:
265 # Note that this is too lenient, because the
266 # implementation type doesn't carry information about
267 # whether it is about bytes or str (for example).
268 return issubclass(cls, self.impl_type)
269
270
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700271def _get_type_vars(types, tvars):
272 for t in types:
273 if isinstance(t, TypingMeta):
274 t._get_type_vars(tvars)
275
276
277def _type_vars(types):
278 tvars = []
279 _get_type_vars(types, tvars)
280 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700281
282
283def _eval_type(t, globalns, localns):
284 if isinstance(t, TypingMeta):
285 return t._eval_type(globalns, localns)
286 else:
287 return t
288
289
290def _type_check(arg, msg):
291 """Check that the argument is a type, and return it.
292
293 As a special case, accept None and return type(None) instead.
294 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
295
296 The msg argument is a human-readable error message, e.g.
297
298 "Union[arg, ...]: arg should be a type."
299
300 We append the repr() of the actual value (truncated to 100 chars).
301 """
302 if arg is None:
303 return type(None)
304 if isinstance(arg, str):
305 arg = _ForwardRef(arg)
306 if not isinstance(arg, (type, _TypeAlias)):
307 raise TypeError(msg + " Got %.100r." % (arg,))
308 return arg
309
310
311def _type_repr(obj):
312 """Return the repr() of an object, special-casing types.
313
314 If obj is a type, we return a shorter version than the default
315 type.__repr__, based on the module and qualified name, which is
316 typically enough to uniquely identify a type. For everything
317 else, we fall back on repr(obj).
318 """
319 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
320 if obj.__module__ == 'builtins':
321 return _qualname(obj)
322 else:
323 return '%s.%s' % (obj.__module__, _qualname(obj))
324 else:
325 return repr(obj)
326
327
328class AnyMeta(TypingMeta):
329 """Metaclass for Any."""
330
331 def __new__(cls, name, bases, namespace, _root=False):
332 self = super().__new__(cls, name, bases, namespace, _root=_root)
333 return self
334
Guido van Rossumd70fe632015-08-05 12:11:06 +0200335 def __instancecheck__(self, obj):
336 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700337
338 def __subclasscheck__(self, cls):
339 if not isinstance(cls, type):
340 return super().__subclasscheck__(cls) # To TypeError.
341 return True
342
343
344class Any(Final, metaclass=AnyMeta, _root=True):
345 """Special type indicating an unconstrained type.
346
347 - Any object is an instance of Any.
348 - Any class is a subclass of Any.
349 - As a special case, Any and object are subclasses of each other.
350 """
351
Guido van Rossumd70fe632015-08-05 12:11:06 +0200352 __slots__ = ()
353
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700354
355class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
356 """Type variable.
357
358 Usage::
359
360 T = TypeVar('T') # Can be anything
361 A = TypeVar('A', str, bytes) # Must be str or bytes
362
363 Type variables exist primarily for the benefit of static type
364 checkers. They serve as the parameters for generic types as well
365 as for generic function definitions. See class Generic for more
366 information on generic types. Generic functions work as follows:
367
368 def repeat(x: T, n: int) -> Sequence[T]:
369 '''Return a list containing n references to x.'''
370 return [x]*n
371
372 def longest(x: A, y: A) -> A:
373 '''Return the longest of two strings.'''
374 return x if len(x) >= len(y) else y
375
376 The latter example's signature is essentially the overloading
377 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
378 that if the arguments are instances of some subclass of str,
379 the return type is still plain str.
380
381 At runtime, isinstance(x, T) will raise TypeError. However,
382 issubclass(C, T) is true for any class C, and issubclass(str, A)
383 and issubclass(bytes, A) are true, and issubclass(int, A) is
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700384 false. (TODO: Why is this needed? This may change. See #136.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700385
386 Type variables may be marked covariant or contravariant by passing
387 covariant=True or contravariant=True. See PEP 484 for more
388 details. By default type variables are invariant.
389
390 Type variables can be introspected. e.g.:
391
392 T.__name__ == 'T'
393 T.__constraints__ == ()
394 T.__covariant__ == False
395 T.__contravariant__ = False
396 A.__constraints__ == (str, bytes)
397 """
398
399 def __new__(cls, name, *constraints, bound=None,
400 covariant=False, contravariant=False):
401 self = super().__new__(cls, name, (Final,), {}, _root=True)
402 if covariant and contravariant:
403 raise ValueError("Bivariant type variables are not supported.")
404 self.__covariant__ = bool(covariant)
405 self.__contravariant__ = bool(contravariant)
406 if constraints and bound is not None:
407 raise TypeError("Constraints cannot be combined with bound=...")
408 if constraints and len(constraints) == 1:
409 raise TypeError("A single constraint is not allowed")
410 msg = "TypeVar(name, constraint, ...): constraints must be types."
411 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
412 if bound:
413 self.__bound__ = _type_check(bound, "Bound must be a type.")
414 else:
415 self.__bound__ = None
416 return self
417
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700418 def _get_type_vars(self, tvars):
419 if self not in tvars:
420 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700421
422 def __repr__(self):
423 if self.__covariant__:
424 prefix = '+'
425 elif self.__contravariant__:
426 prefix = '-'
427 else:
428 prefix = '~'
429 return prefix + self.__name__
430
431 def __instancecheck__(self, instance):
432 raise TypeError("Type variables cannot be used with isinstance().")
433
434 def __subclasscheck__(self, cls):
435 # TODO: Make this raise TypeError too?
436 if cls is self:
437 return True
438 if cls is Any:
439 return True
440 if self.__bound__ is not None:
441 return issubclass(cls, self.__bound__)
442 if self.__constraints__:
443 return any(issubclass(cls, c) for c in self.__constraints__)
444 return True
445
446
447# Some unconstrained type variables. These are used by the container types.
448T = TypeVar('T') # Any type.
449KT = TypeVar('KT') # Key type.
450VT = TypeVar('VT') # Value type.
451T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
452V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700453VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
454T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
455
456# A useful type variable with constraints. This represents string types.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700457AnyStr = TypeVar('AnyStr', bytes, str)
458
459
460class UnionMeta(TypingMeta):
461 """Metaclass for Union."""
462
463 def __new__(cls, name, bases, namespace, parameters=None, _root=False):
464 if parameters is None:
465 return super().__new__(cls, name, bases, namespace, _root=_root)
466 if not isinstance(parameters, tuple):
467 raise TypeError("Expected parameters=<tuple>")
468 # Flatten out Union[Union[...], ...] and type-check non-Union args.
469 params = []
470 msg = "Union[arg, ...]: each arg must be a type."
471 for p in parameters:
472 if isinstance(p, UnionMeta):
473 params.extend(p.__union_params__)
474 else:
475 params.append(_type_check(p, msg))
476 # Weed out strict duplicates, preserving the first of each occurrence.
477 all_params = set(params)
478 if len(all_params) < len(params):
479 new_params = []
480 for t in params:
481 if t in all_params:
482 new_params.append(t)
483 all_params.remove(t)
484 params = new_params
485 assert not all_params, all_params
486 # Weed out subclasses.
487 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
488 # If Any or object is present it will be the sole survivor.
489 # If both Any and object are present, Any wins.
490 # Never discard type variables, except against Any.
491 # (In particular, Union[str, AnyStr] != AnyStr.)
492 all_params = set(params)
493 for t1 in params:
494 if t1 is Any:
495 return Any
496 if isinstance(t1, TypeVar):
497 continue
Guido van Rossumca636ea2015-10-19 14:55:47 -0700498 if isinstance(t1, _TypeAlias):
499 # _TypeAlias is not a real class.
500 continue
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700501 if any(issubclass(t1, t2)
502 for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
503 all_params.remove(t1)
504 # It's not a union if there's only one type left.
505 if len(all_params) == 1:
506 return all_params.pop()
507 # Create a new class with these params.
508 self = super().__new__(cls, name, bases, {}, _root=True)
509 self.__union_params__ = tuple(t for t in params if t in all_params)
510 self.__union_set_params__ = frozenset(self.__union_params__)
511 return self
512
513 def _eval_type(self, globalns, localns):
514 p = tuple(_eval_type(t, globalns, localns)
515 for t in self.__union_params__)
516 if p == self.__union_params__:
517 return self
518 else:
519 return self.__class__(self.__name__, self.__bases__, {},
520 p, _root=True)
521
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700522 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700523 if self.__union_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700524 _get_type_vars(self.__union_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700525
526 def __repr__(self):
527 r = super().__repr__()
528 if self.__union_params__:
529 r += '[%s]' % (', '.join(_type_repr(t)
530 for t in self.__union_params__))
531 return r
532
533 def __getitem__(self, parameters):
534 if self.__union_params__ is not None:
535 raise TypeError(
536 "Cannot subscript an existing Union. Use Union[u, t] instead.")
537 if parameters == ():
538 raise TypeError("Cannot take a Union of no types.")
539 if not isinstance(parameters, tuple):
540 parameters = (parameters,)
541 return self.__class__(self.__name__, self.__bases__,
542 dict(self.__dict__), parameters, _root=True)
543
544 def __eq__(self, other):
545 if not isinstance(other, UnionMeta):
546 return NotImplemented
547 return self.__union_set_params__ == other.__union_set_params__
548
549 def __hash__(self):
550 return hash(self.__union_set_params__)
551
Guido van Rossumd70fe632015-08-05 12:11:06 +0200552 def __instancecheck__(self, obj):
553 raise TypeError("Unions cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700554
555 def __subclasscheck__(self, cls):
556 if cls is Any:
557 return True
558 if self.__union_params__ is None:
559 return isinstance(cls, UnionMeta)
560 elif isinstance(cls, UnionMeta):
561 if cls.__union_params__ is None:
562 return False
563 return all(issubclass(c, self) for c in (cls.__union_params__))
564 elif isinstance(cls, TypeVar):
565 if cls in self.__union_params__:
566 return True
567 if cls.__constraints__:
568 return issubclass(Union[cls.__constraints__], self)
569 return False
570 else:
571 return any(issubclass(cls, t) for t in self.__union_params__)
572
573
574class Union(Final, metaclass=UnionMeta, _root=True):
575 """Union type; Union[X, Y] means either X or Y.
576
577 To define a union, use e.g. Union[int, str]. Details:
578
579 - The arguments must be types and there must be at least one.
580
581 - None as an argument is a special case and is replaced by
582 type(None).
583
584 - Unions of unions are flattened, e.g.::
585
586 Union[Union[int, str], float] == Union[int, str, float]
587
588 - Unions of a single argument vanish, e.g.::
589
590 Union[int] == int # The constructor actually returns int
591
592 - Redundant arguments are skipped, e.g.::
593
594 Union[int, str, int] == Union[int, str]
595
596 - When comparing unions, the argument order is ignored, e.g.::
597
598 Union[int, str] == Union[str, int]
599
600 - When two arguments have a subclass relationship, the least
601 derived argument is kept, e.g.::
602
603 class Employee: pass
604 class Manager(Employee): pass
605 Union[int, Employee, Manager] == Union[int, Employee]
606 Union[Manager, int, Employee] == Union[int, Employee]
607 Union[Employee, Manager] == Employee
608
609 - Corollary: if Any is present it is the sole survivor, e.g.::
610
611 Union[int, Any] == Any
612
613 - Similar for object::
614
615 Union[int, object] == object
616
617 - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
618
619 - You cannot subclass or instantiate a union.
620
621 - You cannot write Union[X][Y] (what would it mean?).
622
623 - You can use Optional[X] as a shorthand for Union[X, None].
624 """
625
626 # Unsubscripted Union type has params set to None.
627 __union_params__ = None
628 __union_set_params__ = None
629
630
631class OptionalMeta(TypingMeta):
632 """Metaclass for Optional."""
633
634 def __new__(cls, name, bases, namespace, _root=False):
635 return super().__new__(cls, name, bases, namespace, _root=_root)
636
637 def __getitem__(self, arg):
638 arg = _type_check(arg, "Optional[t] requires a single type.")
639 return Union[arg, type(None)]
640
641
642class Optional(Final, metaclass=OptionalMeta, _root=True):
643 """Optional type.
644
645 Optional[X] is equivalent to Union[X, type(None)].
646 """
647
Guido van Rossumd70fe632015-08-05 12:11:06 +0200648 __slots__ = ()
649
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700650
651class TupleMeta(TypingMeta):
652 """Metaclass for Tuple."""
653
654 def __new__(cls, name, bases, namespace, parameters=None,
655 use_ellipsis=False, _root=False):
656 self = super().__new__(cls, name, bases, namespace, _root=_root)
657 self.__tuple_params__ = parameters
658 self.__tuple_use_ellipsis__ = use_ellipsis
659 return self
660
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700661 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700662 if self.__tuple_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700663 _get_type_vars(self.__tuple_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700664
665 def _eval_type(self, globalns, localns):
666 tp = self.__tuple_params__
667 if tp is None:
668 return self
669 p = tuple(_eval_type(t, globalns, localns) for t in tp)
670 if p == self.__tuple_params__:
671 return self
672 else:
673 return self.__class__(self.__name__, self.__bases__, {},
674 p, _root=True)
675
676 def __repr__(self):
677 r = super().__repr__()
678 if self.__tuple_params__ is not None:
679 params = [_type_repr(p) for p in self.__tuple_params__]
680 if self.__tuple_use_ellipsis__:
681 params.append('...')
682 r += '[%s]' % (
683 ', '.join(params))
684 return r
685
686 def __getitem__(self, parameters):
687 if self.__tuple_params__ is not None:
688 raise TypeError("Cannot re-parameterize %r" % (self,))
689 if not isinstance(parameters, tuple):
690 parameters = (parameters,)
691 if len(parameters) == 2 and parameters[1] == Ellipsis:
692 parameters = parameters[:1]
693 use_ellipsis = True
694 msg = "Tuple[t, ...]: t must be a type."
695 else:
696 use_ellipsis = False
697 msg = "Tuple[t0, t1, ...]: each t must be a type."
698 parameters = tuple(_type_check(p, msg) for p in parameters)
699 return self.__class__(self.__name__, self.__bases__,
700 dict(self.__dict__), parameters,
701 use_ellipsis=use_ellipsis, _root=True)
702
703 def __eq__(self, other):
704 if not isinstance(other, TupleMeta):
705 return NotImplemented
706 return self.__tuple_params__ == other.__tuple_params__
707
708 def __hash__(self):
709 return hash(self.__tuple_params__)
710
Guido van Rossumd70fe632015-08-05 12:11:06 +0200711 def __instancecheck__(self, obj):
712 raise TypeError("Tuples cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700713
714 def __subclasscheck__(self, cls):
715 if cls is Any:
716 return True
717 if not isinstance(cls, type):
718 return super().__subclasscheck__(cls) # To TypeError.
719 if issubclass(cls, tuple):
720 return True # Special case.
721 if not isinstance(cls, TupleMeta):
722 return super().__subclasscheck__(cls) # False.
723 if self.__tuple_params__ is None:
724 return True
725 if cls.__tuple_params__ is None:
726 return False # ???
727 if cls.__tuple_use_ellipsis__ != self.__tuple_use_ellipsis__:
728 return False
729 # Covariance.
730 return (len(self.__tuple_params__) == len(cls.__tuple_params__) and
731 all(issubclass(x, p)
732 for x, p in zip(cls.__tuple_params__,
733 self.__tuple_params__)))
734
735
736class Tuple(Final, metaclass=TupleMeta, _root=True):
737 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
738
739 Example: Tuple[T1, T2] is a tuple of two elements corresponding
740 to type variables T1 and T2. Tuple[int, float, str] is a tuple
741 of an int, a float and a string.
742
743 To specify a variable-length tuple of homogeneous type, use Sequence[T].
744 """
745
Guido van Rossumd70fe632015-08-05 12:11:06 +0200746 __slots__ = ()
747
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700748
749class CallableMeta(TypingMeta):
750 """Metaclass for Callable."""
751
752 def __new__(cls, name, bases, namespace, _root=False,
753 args=None, result=None):
754 if args is None and result is None:
755 pass # Must be 'class Callable'.
756 else:
757 if args is not Ellipsis:
758 if not isinstance(args, list):
759 raise TypeError("Callable[args, result]: "
760 "args must be a list."
761 " Got %.100r." % (args,))
762 msg = "Callable[[arg, ...], result]: each arg must be a type."
763 args = tuple(_type_check(arg, msg) for arg in args)
764 msg = "Callable[args, result]: result must be a type."
765 result = _type_check(result, msg)
766 self = super().__new__(cls, name, bases, namespace, _root=_root)
767 self.__args__ = args
768 self.__result__ = result
769 return self
770
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700771 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700772 if self.__args__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700773 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700774
775 def _eval_type(self, globalns, localns):
776 if self.__args__ is None and self.__result__ is None:
777 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200778 if self.__args__ is Ellipsis:
779 args = self.__args__
780 else:
781 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700782 result = _eval_type(self.__result__, globalns, localns)
783 if args == self.__args__ and result == self.__result__:
784 return self
785 else:
786 return self.__class__(self.__name__, self.__bases__, {},
787 args=args, result=result, _root=True)
788
789 def __repr__(self):
790 r = super().__repr__()
791 if self.__args__ is not None or self.__result__ is not None:
792 if self.__args__ is Ellipsis:
793 args_r = '...'
794 else:
795 args_r = '[%s]' % ', '.join(_type_repr(t)
796 for t in self.__args__)
797 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
798 return r
799
800 def __getitem__(self, parameters):
801 if self.__args__ is not None or self.__result__ is not None:
802 raise TypeError("This Callable type is already parameterized.")
803 if not isinstance(parameters, tuple) or len(parameters) != 2:
804 raise TypeError(
805 "Callable must be used as Callable[[arg, ...], result].")
806 args, result = parameters
807 return self.__class__(self.__name__, self.__bases__,
808 dict(self.__dict__), _root=True,
809 args=args, result=result)
810
811 def __eq__(self, other):
812 if not isinstance(other, CallableMeta):
813 return NotImplemented
814 return (self.__args__ == other.__args__ and
815 self.__result__ == other.__result__)
816
817 def __hash__(self):
818 return hash(self.__args__) ^ hash(self.__result__)
819
Guido van Rossumd70fe632015-08-05 12:11:06 +0200820 def __instancecheck__(self, obj):
821 # For unparametrized Callable we allow this, because
822 # typing.Callable should be equivalent to
823 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700824 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200825 return isinstance(obj, collections_abc.Callable)
826 else:
827 raise TypeError("Callable[] cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700828
829 def __subclasscheck__(self, cls):
830 if cls is Any:
831 return True
832 if not isinstance(cls, CallableMeta):
833 return super().__subclasscheck__(cls)
834 if self.__args__ is None and self.__result__ is None:
835 return True
836 # We're not doing covariance or contravariance -- this is *invariance*.
837 return self == cls
838
839
840class Callable(Final, metaclass=CallableMeta, _root=True):
841 """Callable type; Callable[[int], str] is a function of (int) -> str.
842
843 The subscription syntax must always be used with exactly two
844 values: the argument list and the return type. The argument list
845 must be a list of types; the return type must be a single type.
846
847 There is no syntax to indicate optional or keyword arguments,
848 such function types are rarely used as callback types.
849 """
850
Guido van Rossumd70fe632015-08-05 12:11:06 +0200851 __slots__ = ()
852
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700853
854def _gorg(a):
855 """Return the farthest origin of a generic class."""
856 assert isinstance(a, GenericMeta)
857 while a.__origin__ is not None:
858 a = a.__origin__
859 return a
860
861
862def _geqv(a, b):
863 """Return whether two generic classes are equivalent.
864
865 The intention is to consider generic class X and any of its
866 parameterized forms (X[T], X[int], etc.) as equivalent.
867
868 However, X is not equivalent to a subclass of X.
869
870 The relation is reflexive, symmetric and transitive.
871 """
872 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
873 # Reduce each to its origin.
874 return _gorg(a) is _gorg(b)
875
876
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700877def _next_in_mro(cls):
878 """Helper for Generic.__new__.
879
880 Returns the class after the last occurrence of Generic or
881 Generic[...] in cls.__mro__.
882 """
883 next_in_mro = object
884 # Look for the last occurrence of Generic or Generic[...].
885 for i, c in enumerate(cls.__mro__[:-1]):
886 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
887 next_in_mro = cls.__mro__[i+1]
888 return next_in_mro
889
890
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700891class GenericMeta(TypingMeta, abc.ABCMeta):
892 """Metaclass for generic types."""
893
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700894 __extra__ = None
895
896 def __new__(cls, name, bases, namespace,
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700897 tvars=None, args=None, origin=None, extra=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700898 self = super().__new__(cls, name, bases, namespace, _root=True)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700899
900 if tvars is not None:
901 # Called from __getitem__() below.
902 assert origin is not None
903 assert all(isinstance(t, TypeVar) for t in tvars), tvars
904 else:
905 # Called from class statement.
906 assert tvars is None, tvars
907 assert args is None, args
908 assert origin is None, origin
909
910 # Get the full set of tvars from the bases.
911 tvars = _type_vars(bases)
912 # Look for Generic[T1, ..., Tn].
913 # If found, tvars must be a subset of it.
914 # If not found, tvars is it.
915 # Also check for and reject plain Generic,
916 # and reject multiple Generic[...].
917 gvars = None
918 for base in bases:
919 if base is Generic:
920 raise TypeError("Cannot inherit from plain Generic")
921 if (isinstance(base, GenericMeta) and
922 base.__origin__ is Generic):
923 if gvars is not None:
924 raise TypeError(
925 "Cannot inherit from Generic[...] multiple types.")
926 gvars = base.__parameters__
927 if gvars is None:
928 gvars = tvars
929 else:
930 tvarset = set(tvars)
931 gvarset = set(gvars)
932 if not tvarset <= gvarset:
933 raise TypeError(
934 "Some type variables (%s) "
935 "are not listed in Generic[%s]" %
936 (", ".join(str(t) for t in tvars if t not in gvarset),
937 ", ".join(str(g) for g in gvars)))
938 tvars = gvars
939
940 self.__parameters__ = tvars
941 self.__args__ = args
942 self.__origin__ = origin
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700943 if extra is not None:
944 self.__extra__ = extra
945 # Else __extra__ is inherited, eventually from the
946 # (meta-)class default above.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700947 # Speed hack (https://github.com/python/typing/issues/196).
948 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700949 return self
950
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700951 def _get_type_vars(self, tvars):
952 if self.__origin__ and self.__parameters__:
953 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700954
955 def __repr__(self):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700956 if self.__origin__ is not None:
957 r = repr(self.__origin__)
958 else:
959 r = super().__repr__()
960 if self.__args__:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700961 r += '[%s]' % (
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700962 ', '.join(_type_repr(p) for p in self.__args__))
963 if self.__parameters__:
964 r += '<%s>' % (
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700965 ', '.join(_type_repr(p) for p in self.__parameters__))
966 return r
967
968 def __eq__(self, other):
969 if not isinstance(other, GenericMeta):
970 return NotImplemented
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700971 if self.__origin__ is not None:
972 return (self.__origin__ is other.__origin__ and
973 self.__args__ == other.__args__ and
974 self.__parameters__ == other.__parameters__)
975 else:
976 return self is other
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700977
978 def __hash__(self):
979 return hash((self.__name__, self.__parameters__))
980
981 def __getitem__(self, params):
982 if not isinstance(params, tuple):
983 params = (params,)
984 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700985 raise TypeError(
986 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700987 msg = "Parameters to generic types must be types."
988 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700989 if self is Generic:
990 # Generic can only be subscripted with unique type variables.
991 if not all(isinstance(p, TypeVar) for p in params):
992 raise TypeError(
993 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200994 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -0700995 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700996 "Parameters to Generic[...] must all be unique")
997 tvars = params
998 args = None
999 elif self is _Protocol:
1000 # _Protocol is internal, don't check anything.
1001 tvars = params
1002 args = None
1003 elif self.__origin__ in (Generic, _Protocol):
1004 # Can't subscript Generic[...] or _Protocol[...].
1005 raise TypeError("Cannot subscript already-subscripted %s" %
1006 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001007 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001008 # Subscripting a regular Generic subclass.
1009 if not self.__parameters__:
1010 raise TypeError("%s is not a generic class" % repr(self))
1011 alen = len(params)
1012 elen = len(self.__parameters__)
1013 if alen != elen:
1014 raise TypeError(
1015 "Too %s parameters for %s; actual %s, expected %s" %
1016 ("many" if alen > elen else "few", repr(self), alen, elen))
1017 tvars = _type_vars(params)
1018 args = params
1019 return self.__class__(self.__name__,
1020 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001021 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001022 tvars=tvars,
1023 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001024 origin=self,
1025 extra=self.__extra__)
1026
Guido van Rossum1b669102015-09-04 12:15:54 -07001027 def __instancecheck__(self, instance):
1028 # Since we extend ABC.__subclasscheck__ and
1029 # ABC.__instancecheck__ inlines the cache checking done by the
1030 # latter, we must extend __instancecheck__ too. For simplicity
1031 # we just skip the cache check -- instance checks for generic
1032 # classes are supposed to be rare anyways.
1033 return self.__subclasscheck__(instance.__class__)
1034
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001035 def __subclasscheck__(self, cls):
1036 if cls is Any:
1037 return True
1038 if isinstance(cls, GenericMeta):
1039 # For a class C(Generic[T]) where T is co-variant,
1040 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
1041 origin = self.__origin__
1042 if origin is not None and origin is cls.__origin__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001043 assert len(self.__args__) == len(origin.__parameters__)
1044 assert len(cls.__args__) == len(origin.__parameters__)
1045 for p_self, p_cls, p_origin in zip(self.__args__,
1046 cls.__args__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001047 origin.__parameters__):
1048 if isinstance(p_origin, TypeVar):
1049 if p_origin.__covariant__:
1050 # Covariant -- p_cls must be a subclass of p_self.
1051 if not issubclass(p_cls, p_self):
1052 break
1053 elif p_origin.__contravariant__:
1054 # Contravariant. I think it's the opposite. :-)
1055 if not issubclass(p_self, p_cls):
1056 break
1057 else:
1058 # Invariant -- p_cls and p_self must equal.
1059 if p_self != p_cls:
1060 break
1061 else:
1062 # If the origin's parameter is not a typevar,
1063 # insist on invariance.
1064 if p_self != p_cls:
1065 break
1066 else:
1067 return True
1068 # If we break out of the loop, the superclass gets a chance.
1069 if super().__subclasscheck__(cls):
1070 return True
1071 if self.__extra__ is None or isinstance(cls, GenericMeta):
1072 return False
1073 return issubclass(cls, self.__extra__)
1074
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001075
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001076# Prevent checks for Generic to crash when defining Generic.
1077Generic = None
1078
1079
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001080class Generic(metaclass=GenericMeta):
1081 """Abstract base class for generic types.
1082
1083 A generic type is typically declared by inheriting from an
1084 instantiation of this class with one or more type variables.
1085 For example, a generic mapping type might be defined as::
1086
1087 class Mapping(Generic[KT, VT]):
1088 def __getitem__(self, key: KT) -> VT:
1089 ...
1090 # Etc.
1091
1092 This class can then be used as follows::
1093
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001094 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001095 try:
1096 return mapping[key]
1097 except KeyError:
1098 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001099 """
1100
Guido van Rossumd70fe632015-08-05 12:11:06 +02001101 __slots__ = ()
1102
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001103 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001104 if cls.__origin__ is None:
1105 return cls.__next_in_mro__.__new__(cls)
1106 else:
1107 origin = _gorg(cls)
1108 obj = cls.__next_in_mro__.__new__(origin)
1109 obj.__init__(*args, **kwds)
1110 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001111
1112
1113def cast(typ, val):
1114 """Cast a value to a type.
1115
1116 This returns the value unchanged. To the type checker this
1117 signals that the return value has the designated type, but at
1118 runtime we intentionally don't check anything (we want this
1119 to be as fast as possible).
1120 """
1121 return val
1122
1123
1124def _get_defaults(func):
1125 """Internal helper to extract the default arguments, by name."""
1126 code = func.__code__
1127 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001128 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001129 arg_names = arg_names[:pos_count]
1130 defaults = func.__defaults__ or ()
1131 kwdefaults = func.__kwdefaults__
1132 res = dict(kwdefaults) if kwdefaults else {}
1133 pos_offset = pos_count - len(defaults)
1134 for name, value in zip(arg_names[pos_offset:], defaults):
1135 assert name not in res
1136 res[name] = value
1137 return res
1138
1139
1140def get_type_hints(obj, globalns=None, localns=None):
1141 """Return type hints for a function or method object.
1142
1143 This is often the same as obj.__annotations__, but it handles
1144 forward references encoded as string literals, and if necessary
1145 adds Optional[t] if a default value equal to None is set.
1146
1147 BEWARE -- the behavior of globalns and localns is counterintuitive
1148 (unless you are familiar with how eval() and exec() work). The
1149 search order is locals first, then globals.
1150
1151 - If no dict arguments are passed, an attempt is made to use the
1152 globals from obj, and these are also used as the locals. If the
1153 object does not appear to have globals, an exception is raised.
1154
1155 - If one dict argument is passed, it is used for both globals and
1156 locals.
1157
1158 - If two dict arguments are passed, they specify globals and
1159 locals, respectively.
1160 """
1161 if getattr(obj, '__no_type_check__', None):
1162 return {}
1163 if globalns is None:
1164 globalns = getattr(obj, '__globals__', {})
1165 if localns is None:
1166 localns = globalns
1167 elif localns is None:
1168 localns = globalns
1169 defaults = _get_defaults(obj)
1170 hints = dict(obj.__annotations__)
1171 for name, value in hints.items():
1172 if isinstance(value, str):
1173 value = _ForwardRef(value)
1174 value = _eval_type(value, globalns, localns)
1175 if name in defaults and defaults[name] is None:
1176 value = Optional[value]
1177 hints[name] = value
1178 return hints
1179
1180
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001181def no_type_check(arg):
1182 """Decorator to indicate that annotations are not type hints.
1183
1184 The argument must be a class or function; if it is a class, it
1185 applies recursively to all methods defined in that class (but not
1186 to methods defined in its superclasses or subclasses).
1187
1188 This mutates the function(s) in place.
1189 """
1190 if isinstance(arg, type):
1191 for obj in arg.__dict__.values():
1192 if isinstance(obj, types.FunctionType):
1193 obj.__no_type_check__ = True
1194 else:
1195 arg.__no_type_check__ = True
1196 return arg
1197
1198
1199def no_type_check_decorator(decorator):
1200 """Decorator to give another decorator the @no_type_check effect.
1201
1202 This wraps the decorator with something that wraps the decorated
1203 function in @no_type_check.
1204 """
1205
1206 @functools.wraps(decorator)
1207 def wrapped_decorator(*args, **kwds):
1208 func = decorator(*args, **kwds)
1209 func = no_type_check(func)
1210 return func
1211
1212 return wrapped_decorator
1213
1214
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001215def _overload_dummy(*args, **kwds):
1216 """Helper for @overload to raise when called."""
1217 raise NotImplementedError(
1218 "You should not call an overloaded function. "
1219 "A series of @overload-decorated functions "
1220 "outside a stub module should always be followed "
1221 "by an implementation that is not @overload-ed.")
1222
1223
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001224def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001225 """Decorator for overloaded functions/methods.
1226
1227 In a stub file, place two or more stub definitions for the same
1228 function in a row, each decorated with @overload. For example:
1229
1230 @overload
1231 def utf8(value: None) -> None: ...
1232 @overload
1233 def utf8(value: bytes) -> bytes: ...
1234 @overload
1235 def utf8(value: str) -> bytes: ...
1236
1237 In a non-stub file (i.e. a regular .py file), do the same but
1238 follow it with an implementation. The implementation should *not*
1239 be decorated with @overload. For example:
1240
1241 @overload
1242 def utf8(value: None) -> None: ...
1243 @overload
1244 def utf8(value: bytes) -> bytes: ...
1245 @overload
1246 def utf8(value: str) -> bytes: ...
1247 def utf8(value):
1248 # implementation goes here
1249 """
1250 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001251
1252
1253class _ProtocolMeta(GenericMeta):
1254 """Internal metaclass for _Protocol.
1255
1256 This exists so _Protocol classes can be generic without deriving
1257 from Generic.
1258 """
1259
Guido van Rossumd70fe632015-08-05 12:11:06 +02001260 def __instancecheck__(self, obj):
1261 raise TypeError("Protocols cannot be used with isinstance().")
1262
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001263 def __subclasscheck__(self, cls):
1264 if not self._is_protocol:
1265 # No structural checks since this isn't a protocol.
1266 return NotImplemented
1267
1268 if self is _Protocol:
1269 # Every class is a subclass of the empty protocol.
1270 return True
1271
1272 # Find all attributes defined in the protocol.
1273 attrs = self._get_protocol_attrs()
1274
1275 for attr in attrs:
1276 if not any(attr in d.__dict__ for d in cls.__mro__):
1277 return False
1278 return True
1279
1280 def _get_protocol_attrs(self):
1281 # Get all Protocol base classes.
1282 protocol_bases = []
1283 for c in self.__mro__:
1284 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1285 protocol_bases.append(c)
1286
1287 # Get attributes included in protocol.
1288 attrs = set()
1289 for base in protocol_bases:
1290 for attr in base.__dict__.keys():
1291 # Include attributes not defined in any non-protocol bases.
1292 for c in self.__mro__:
1293 if (c is not base and attr in c.__dict__ and
1294 not getattr(c, '_is_protocol', False)):
1295 break
1296 else:
1297 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001298 attr != '__abstractmethods__' and
1299 attr != '_is_protocol' and
1300 attr != '__dict__' and
1301 attr != '__args__' and
1302 attr != '__slots__' and
1303 attr != '_get_protocol_attrs' and
1304 attr != '__next_in_mro__' and
1305 attr != '__parameters__' and
1306 attr != '__origin__' and
1307 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001308 attrs.add(attr)
1309
1310 return attrs
1311
1312
1313class _Protocol(metaclass=_ProtocolMeta):
1314 """Internal base class for protocol classes.
1315
1316 This implements a simple-minded structural isinstance check
1317 (similar but more general than the one-offs in collections.abc
1318 such as Hashable).
1319 """
1320
Guido van Rossumd70fe632015-08-05 12:11:06 +02001321 __slots__ = ()
1322
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001323 _is_protocol = True
1324
1325
1326# Various ABCs mimicking those in collections.abc.
1327# A few are simply re-exported for completeness.
1328
1329Hashable = collections_abc.Hashable # Not generic.
1330
1331
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001332if hasattr(collections_abc, 'Awaitable'):
1333 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1334 __slots__ = ()
1335else:
1336 Awaitable = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001337
1338
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001339if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001340
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001341 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1342 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001343
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001344 class AsyncIterator(AsyncIterable[T_co],
1345 extra=collections_abc.AsyncIterator):
1346 __slots__ = ()
1347
1348else:
1349 AsyncIterable = None
1350 AsyncIterator = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001351
1352
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001353class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001354 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001355
1356
1357class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001358 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001359
1360
1361class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001362 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001363
1364 @abstractmethod
1365 def __int__(self) -> int:
1366 pass
1367
1368
1369class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001370 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001371
1372 @abstractmethod
1373 def __float__(self) -> float:
1374 pass
1375
1376
1377class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001378 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001379
1380 @abstractmethod
1381 def __complex__(self) -> complex:
1382 pass
1383
1384
1385class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001386 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001387
1388 @abstractmethod
1389 def __bytes__(self) -> bytes:
1390 pass
1391
1392
Guido van Rossumd70fe632015-08-05 12:11:06 +02001393class SupportsAbs(_Protocol[T_co]):
1394 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001395
1396 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001397 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001398 pass
1399
1400
Guido van Rossumd70fe632015-08-05 12:11:06 +02001401class SupportsRound(_Protocol[T_co]):
1402 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001403
1404 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001405 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001406 pass
1407
1408
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001409if hasattr(collections_abc, 'Reversible'):
1410 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1411 __slots__ = ()
1412else:
1413 class Reversible(_Protocol[T_co]):
1414 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001415
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001416 @abstractmethod
1417 def __reversed__(self) -> 'Iterator[T_co]':
1418 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001419
1420
1421Sized = collections_abc.Sized # Not generic.
1422
1423
1424class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001425 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001426
1427
1428# Callable was defined earlier.
1429
1430
1431class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1432 extra=collections_abc.Set):
1433 pass
1434
1435
1436class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
1437 pass
1438
1439
Guido van Rossumd70fe632015-08-05 12:11:06 +02001440# NOTE: Only the value type is covariant.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001441class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001442 extra=collections_abc.Mapping):
1443 pass
1444
1445
1446class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
1447 pass
1448
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001449if hasattr(collections_abc, 'Reversible'):
1450 class Sequence(Sized, Reversible[T_co], Container[T_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001451 extra=collections_abc.Sequence):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001452 pass
1453else:
1454 class Sequence(Sized, Iterable[T_co], Container[T_co],
1455 extra=collections_abc.Sequence):
1456 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001457
1458
1459class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
1460 pass
1461
1462
1463class ByteString(Sequence[int], extra=collections_abc.ByteString):
1464 pass
1465
1466
1467ByteString.register(type(memoryview(b'')))
1468
1469
Guido van Rossumd70fe632015-08-05 12:11:06 +02001470class List(list, MutableSequence[T]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001471
1472 def __new__(cls, *args, **kwds):
1473 if _geqv(cls, List):
1474 raise TypeError("Type List cannot be instantiated; "
1475 "use list() instead")
1476 return list.__new__(cls, *args, **kwds)
1477
1478
Guido van Rossumd70fe632015-08-05 12:11:06 +02001479class Set(set, MutableSet[T]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001480
1481 def __new__(cls, *args, **kwds):
1482 if _geqv(cls, Set):
1483 raise TypeError("Type Set cannot be instantiated; "
1484 "use set() instead")
1485 return set.__new__(cls, *args, **kwds)
1486
1487
Guido van Rossumd70fe632015-08-05 12:11:06 +02001488class _FrozenSetMeta(GenericMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001489 """This metaclass ensures set is not a subclass of FrozenSet.
1490
1491 Without this metaclass, set would be considered a subclass of
1492 FrozenSet, because FrozenSet.__extra__ is collections.abc.Set, and
1493 set is a subclass of that.
1494 """
1495
1496 def __subclasscheck__(self, cls):
1497 if issubclass(cls, Set):
1498 return False
1499 return super().__subclasscheck__(cls)
1500
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001501
1502class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001503 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001504
1505 def __new__(cls, *args, **kwds):
1506 if _geqv(cls, FrozenSet):
1507 raise TypeError("Type FrozenSet cannot be instantiated; "
1508 "use frozenset() instead")
1509 return frozenset.__new__(cls, *args, **kwds)
1510
1511
1512class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
1513 pass
1514
1515
Guido van Rossumd70fe632015-08-05 12:11:06 +02001516class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001517 extra=collections_abc.KeysView):
1518 pass
1519
1520
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001521class ItemsView(MappingView[Tuple[KT, VT_co]],
1522 Set[Tuple[KT, VT_co]],
1523 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001524 extra=collections_abc.ItemsView):
1525 pass
1526
1527
1528class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
1529 pass
1530
1531
Guido van Rossumd70fe632015-08-05 12:11:06 +02001532class Dict(dict, MutableMapping[KT, VT]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001533
1534 def __new__(cls, *args, **kwds):
1535 if _geqv(cls, Dict):
1536 raise TypeError("Type Dict cannot be instantiated; "
1537 "use dict() instead")
1538 return dict.__new__(cls, *args, **kwds)
1539
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001540class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
1541
1542 def __new__(cls, *args, **kwds):
1543 if _geqv(cls, DefaultDict):
1544 raise TypeError("Type DefaultDict cannot be instantiated; "
1545 "use collections.defaultdict() instead")
1546 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001547
1548# Determine what base class to use for Generator.
1549if hasattr(collections_abc, 'Generator'):
1550 # Sufficiently recent versions of 3.5 have a Generator ABC.
1551 _G_base = collections_abc.Generator
1552else:
1553 # Fall back on the exact type.
1554 _G_base = types.GeneratorType
1555
1556
1557class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1558 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001559 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001560
1561 def __new__(cls, *args, **kwds):
1562 if _geqv(cls, Generator):
1563 raise TypeError("Type Generator cannot be instantiated; "
1564 "create a subclass instead")
1565 return super().__new__(cls, *args, **kwds)
1566
1567
1568def NamedTuple(typename, fields):
1569 """Typed version of namedtuple.
1570
1571 Usage::
1572
1573 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1574
1575 This is equivalent to::
1576
1577 Employee = collections.namedtuple('Employee', ['name', 'id'])
1578
1579 The resulting class has one extra attribute: _field_types,
1580 giving a dict mapping field names to types. (The field names
1581 are in the _fields attribute, which is part of the namedtuple
1582 API.)
1583 """
1584 fields = [(n, t) for n, t in fields]
1585 cls = collections.namedtuple(typename, [n for n, t in fields])
1586 cls._field_types = dict(fields)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001587 # Set the module to the caller's module (otherwise it'd be 'typing').
1588 try:
1589 cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
1590 except (AttributeError, ValueError):
1591 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001592 return cls
1593
1594
1595class IO(Generic[AnyStr]):
1596 """Generic base class for TextIO and BinaryIO.
1597
1598 This is an abstract, generic version of the return of open().
1599
1600 NOTE: This does not distinguish between the different possible
1601 classes (text vs. binary, read vs. write vs. read/write,
1602 append-only, unbuffered). The TextIO and BinaryIO subclasses
1603 below capture the distinctions between text vs. binary, which is
1604 pervasive in the interface; however we currently do not offer a
1605 way to track the other distinctions in the type system.
1606 """
1607
Guido van Rossumd70fe632015-08-05 12:11:06 +02001608 __slots__ = ()
1609
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001610 @abstractproperty
1611 def mode(self) -> str:
1612 pass
1613
1614 @abstractproperty
1615 def name(self) -> str:
1616 pass
1617
1618 @abstractmethod
1619 def close(self) -> None:
1620 pass
1621
1622 @abstractmethod
1623 def closed(self) -> bool:
1624 pass
1625
1626 @abstractmethod
1627 def fileno(self) -> int:
1628 pass
1629
1630 @abstractmethod
1631 def flush(self) -> None:
1632 pass
1633
1634 @abstractmethod
1635 def isatty(self) -> bool:
1636 pass
1637
1638 @abstractmethod
1639 def read(self, n: int = -1) -> AnyStr:
1640 pass
1641
1642 @abstractmethod
1643 def readable(self) -> bool:
1644 pass
1645
1646 @abstractmethod
1647 def readline(self, limit: int = -1) -> AnyStr:
1648 pass
1649
1650 @abstractmethod
1651 def readlines(self, hint: int = -1) -> List[AnyStr]:
1652 pass
1653
1654 @abstractmethod
1655 def seek(self, offset: int, whence: int = 0) -> int:
1656 pass
1657
1658 @abstractmethod
1659 def seekable(self) -> bool:
1660 pass
1661
1662 @abstractmethod
1663 def tell(self) -> int:
1664 pass
1665
1666 @abstractmethod
1667 def truncate(self, size: int = None) -> int:
1668 pass
1669
1670 @abstractmethod
1671 def writable(self) -> bool:
1672 pass
1673
1674 @abstractmethod
1675 def write(self, s: AnyStr) -> int:
1676 pass
1677
1678 @abstractmethod
1679 def writelines(self, lines: List[AnyStr]) -> None:
1680 pass
1681
1682 @abstractmethod
1683 def __enter__(self) -> 'IO[AnyStr]':
1684 pass
1685
1686 @abstractmethod
1687 def __exit__(self, type, value, traceback) -> None:
1688 pass
1689
1690
1691class BinaryIO(IO[bytes]):
1692 """Typed version of the return of open() in binary mode."""
1693
Guido van Rossumd70fe632015-08-05 12:11:06 +02001694 __slots__ = ()
1695
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001696 @abstractmethod
1697 def write(self, s: Union[bytes, bytearray]) -> int:
1698 pass
1699
1700 @abstractmethod
1701 def __enter__(self) -> 'BinaryIO':
1702 pass
1703
1704
1705class TextIO(IO[str]):
1706 """Typed version of the return of open() in text mode."""
1707
Guido van Rossumd70fe632015-08-05 12:11:06 +02001708 __slots__ = ()
1709
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001710 @abstractproperty
1711 def buffer(self) -> BinaryIO:
1712 pass
1713
1714 @abstractproperty
1715 def encoding(self) -> str:
1716 pass
1717
1718 @abstractproperty
1719 def errors(self) -> str:
1720 pass
1721
1722 @abstractproperty
1723 def line_buffering(self) -> bool:
1724 pass
1725
1726 @abstractproperty
1727 def newlines(self) -> Any:
1728 pass
1729
1730 @abstractmethod
1731 def __enter__(self) -> 'TextIO':
1732 pass
1733
1734
1735class io:
1736 """Wrapper namespace for IO generic classes."""
1737
1738 __all__ = ['IO', 'TextIO', 'BinaryIO']
1739 IO = IO
1740 TextIO = TextIO
1741 BinaryIO = BinaryIO
1742
1743io.__name__ = __name__ + '.io'
1744sys.modules[io.__name__] = io
1745
1746
1747Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
1748 lambda p: p.pattern)
1749Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
1750 lambda m: m.re.pattern)
1751
1752
1753class re:
1754 """Wrapper namespace for re type aliases."""
1755
1756 __all__ = ['Pattern', 'Match']
1757 Pattern = Pattern
1758 Match = Match
1759
1760re.__name__ = __name__ + '.re'
1761sys.modules[re.__name__] = re