blob: 6ead3c41946dc5191d155f73c2ffad3a4d46ea71 [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 Rossum0e0563c2016-04-05 14:54:25 -070068 'Text',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070069]
70
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070071# The pseudo-submodules 're' and 'io' are part of the public
72# namespace, but excluded from __all__ because they might stomp on
73# legitimate imports of those modules.
74
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070075
76def _qualname(x):
77 if sys.version_info[:2] >= (3, 3):
78 return x.__qualname__
79 else:
80 # Fall back to just name.
81 return x.__name__
82
83
84class TypingMeta(type):
85 """Metaclass for every type defined below.
86
87 This overrides __new__() to require an extra keyword parameter
88 '_root', which serves as a guard against naive subclassing of the
89 typing classes. Any legitimate class defined using a metaclass
90 derived from TypingMeta (including internal subclasses created by
91 e.g. Union[X, Y]) must pass _root=True.
92
93 This also defines a dummy constructor (all the work is done in
94 __new__) and a nicer repr().
95 """
96
97 _is_protocol = False
98
99 def __new__(cls, name, bases, namespace, *, _root=False):
100 if not _root:
101 raise TypeError("Cannot subclass %s" %
102 (', '.join(map(_type_repr, bases)) or '()'))
103 return super().__new__(cls, name, bases, namespace)
104
105 def __init__(self, *args, **kwds):
106 pass
107
108 def _eval_type(self, globalns, localns):
109 """Override this in subclasses to interpret forward references.
110
111 For example, Union['C'] is internally stored as
112 Union[_ForwardRef('C')], which should evaluate to _Union[C],
113 where C is an object found in globalns or localns (searching
114 localns first, of course).
115 """
116 return self
117
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700118 def _get_type_vars(self, tvars):
119 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700120
121 def __repr__(self):
122 return '%s.%s' % (self.__module__, _qualname(self))
123
124
125class Final:
126 """Mix-in class to prevent instantiation."""
127
Guido van Rossumd70fe632015-08-05 12:11:06 +0200128 __slots__ = ()
129
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700130 def __new__(self, *args, **kwds):
131 raise TypeError("Cannot instantiate %r" % self.__class__)
132
133
134class _ForwardRef(TypingMeta):
135 """Wrapper to hold a forward reference."""
136
137 def __new__(cls, arg):
138 if not isinstance(arg, str):
139 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
140 try:
141 code = compile(arg, '<string>', 'eval')
142 except SyntaxError:
143 raise SyntaxError('ForwardRef must be an expression -- got %r' %
144 (arg,))
145 self = super().__new__(cls, arg, (), {}, _root=True)
146 self.__forward_arg__ = arg
147 self.__forward_code__ = code
148 self.__forward_evaluated__ = False
149 self.__forward_value__ = None
150 typing_globals = globals()
151 frame = sys._getframe(1)
152 while frame is not None and frame.f_globals is typing_globals:
153 frame = frame.f_back
154 assert frame is not None
155 self.__forward_frame__ = frame
156 return self
157
158 def _eval_type(self, globalns, localns):
159 if not isinstance(localns, dict):
160 raise TypeError('ForwardRef localns must be a dict -- got %r' %
161 (localns,))
162 if not isinstance(globalns, dict):
163 raise TypeError('ForwardRef globalns must be a dict -- got %r' %
164 (globalns,))
165 if not self.__forward_evaluated__:
166 if globalns is None and localns is None:
167 globalns = localns = {}
168 elif globalns is None:
169 globalns = localns
170 elif localns is None:
171 localns = globalns
172 self.__forward_value__ = _type_check(
173 eval(self.__forward_code__, globalns, localns),
174 "Forward references must evaluate to types.")
175 self.__forward_evaluated__ = True
176 return self.__forward_value__
177
Guido van Rossumd70fe632015-08-05 12:11:06 +0200178 def __instancecheck__(self, obj):
179 raise TypeError("Forward references cannot be used with isinstance().")
180
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700181 def __subclasscheck__(self, cls):
182 if not self.__forward_evaluated__:
183 globalns = self.__forward_frame__.f_globals
184 localns = self.__forward_frame__.f_locals
185 try:
186 self._eval_type(globalns, localns)
187 except NameError:
188 return False # Too early.
189 return issubclass(cls, self.__forward_value__)
190
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700191 def __repr__(self):
192 return '_ForwardRef(%r)' % (self.__forward_arg__,)
193
194
195class _TypeAlias:
196 """Internal helper class for defining generic variants of concrete types.
197
198 Note that this is not a type; let's call it a pseudo-type. It can
199 be used in instance and subclass checks, e.g. isinstance(m, Match)
200 or issubclass(type(m), Match). However, it cannot be itself the
201 target of an issubclass() call; e.g. issubclass(Match, C) (for
202 some arbitrary class C) raises TypeError rather than returning
203 False.
204 """
205
Guido van Rossumd70fe632015-08-05 12:11:06 +0200206 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
207
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700208 def __new__(cls, *args, **kwds):
209 """Constructor.
210
211 This only exists to give a better error message in case
212 someone tries to subclass a type alias (not a good idea).
213 """
214 if (len(args) == 3 and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700215 isinstance(args[0], str) and
216 isinstance(args[1], tuple)):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700217 # Close enough.
218 raise TypeError("A type alias cannot be subclassed")
219 return object.__new__(cls)
220
221 def __init__(self, name, type_var, impl_type, type_checker):
222 """Initializer.
223
224 Args:
225 name: The name, e.g. 'Pattern'.
226 type_var: The type parameter, e.g. AnyStr, or the
227 specific type, e.g. str.
228 impl_type: The implementation type.
229 type_checker: Function that takes an impl_type instance.
230 and returns a value that should be a type_var instance.
231 """
232 assert isinstance(name, str), repr(name)
233 assert isinstance(type_var, type), repr(type_var)
234 assert isinstance(impl_type, type), repr(impl_type)
235 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
236 self.name = name
237 self.type_var = type_var
238 self.impl_type = impl_type
239 self.type_checker = type_checker
240
241 def __repr__(self):
242 return "%s[%s]" % (self.name, _type_repr(self.type_var))
243
244 def __getitem__(self, parameter):
245 assert isinstance(parameter, type), repr(parameter)
246 if not isinstance(self.type_var, TypeVar):
247 raise TypeError("%s cannot be further parameterized." % self)
248 if self.type_var.__constraints__:
249 if not issubclass(parameter, Union[self.type_var.__constraints__]):
250 raise TypeError("%s is not a valid substitution for %s." %
251 (parameter, self.type_var))
252 return self.__class__(self.name, parameter,
253 self.impl_type, self.type_checker)
254
255 def __instancecheck__(self, obj):
Guido van Rossumd70fe632015-08-05 12:11:06 +0200256 raise TypeError("Type aliases cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700257
258 def __subclasscheck__(self, cls):
259 if cls is Any:
260 return True
261 if isinstance(cls, _TypeAlias):
262 # Covariance. For now, we compare by name.
263 return (cls.name == self.name and
264 issubclass(cls.type_var, self.type_var))
265 else:
266 # Note that this is too lenient, because the
267 # implementation type doesn't carry information about
268 # whether it is about bytes or str (for example).
269 return issubclass(cls, self.impl_type)
270
271
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700272def _get_type_vars(types, tvars):
273 for t in types:
274 if isinstance(t, TypingMeta):
275 t._get_type_vars(tvars)
276
277
278def _type_vars(types):
279 tvars = []
280 _get_type_vars(types, tvars)
281 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700282
283
284def _eval_type(t, globalns, localns):
285 if isinstance(t, TypingMeta):
286 return t._eval_type(globalns, localns)
287 else:
288 return t
289
290
291def _type_check(arg, msg):
292 """Check that the argument is a type, and return it.
293
294 As a special case, accept None and return type(None) instead.
295 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
296
297 The msg argument is a human-readable error message, e.g.
298
299 "Union[arg, ...]: arg should be a type."
300
301 We append the repr() of the actual value (truncated to 100 chars).
302 """
303 if arg is None:
304 return type(None)
305 if isinstance(arg, str):
306 arg = _ForwardRef(arg)
307 if not isinstance(arg, (type, _TypeAlias)):
308 raise TypeError(msg + " Got %.100r." % (arg,))
309 return arg
310
311
312def _type_repr(obj):
313 """Return the repr() of an object, special-casing types.
314
315 If obj is a type, we return a shorter version than the default
316 type.__repr__, based on the module and qualified name, which is
317 typically enough to uniquely identify a type. For everything
318 else, we fall back on repr(obj).
319 """
320 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
321 if obj.__module__ == 'builtins':
322 return _qualname(obj)
323 else:
324 return '%s.%s' % (obj.__module__, _qualname(obj))
325 else:
326 return repr(obj)
327
328
329class AnyMeta(TypingMeta):
330 """Metaclass for Any."""
331
332 def __new__(cls, name, bases, namespace, _root=False):
333 self = super().__new__(cls, name, bases, namespace, _root=_root)
334 return self
335
Guido van Rossumd70fe632015-08-05 12:11:06 +0200336 def __instancecheck__(self, obj):
337 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700338
339 def __subclasscheck__(self, cls):
340 if not isinstance(cls, type):
341 return super().__subclasscheck__(cls) # To TypeError.
342 return True
343
344
345class Any(Final, metaclass=AnyMeta, _root=True):
346 """Special type indicating an unconstrained type.
347
348 - Any object is an instance of Any.
349 - Any class is a subclass of Any.
350 - As a special case, Any and object are subclasses of each other.
351 """
352
Guido van Rossumd70fe632015-08-05 12:11:06 +0200353 __slots__ = ()
354
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700355
356class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
357 """Type variable.
358
359 Usage::
360
361 T = TypeVar('T') # Can be anything
362 A = TypeVar('A', str, bytes) # Must be str or bytes
363
364 Type variables exist primarily for the benefit of static type
365 checkers. They serve as the parameters for generic types as well
366 as for generic function definitions. See class Generic for more
367 information on generic types. Generic functions work as follows:
368
369 def repeat(x: T, n: int) -> Sequence[T]:
370 '''Return a list containing n references to x.'''
371 return [x]*n
372
373 def longest(x: A, y: A) -> A:
374 '''Return the longest of two strings.'''
375 return x if len(x) >= len(y) else y
376
377 The latter example's signature is essentially the overloading
378 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
379 that if the arguments are instances of some subclass of str,
380 the return type is still plain str.
381
382 At runtime, isinstance(x, T) will raise TypeError. However,
383 issubclass(C, T) is true for any class C, and issubclass(str, A)
384 and issubclass(bytes, A) are true, and issubclass(int, A) is
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700385 false. (TODO: Why is this needed? This may change. See #136.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700386
387 Type variables may be marked covariant or contravariant by passing
388 covariant=True or contravariant=True. See PEP 484 for more
389 details. By default type variables are invariant.
390
391 Type variables can be introspected. e.g.:
392
393 T.__name__ == 'T'
394 T.__constraints__ == ()
395 T.__covariant__ == False
396 T.__contravariant__ = False
397 A.__constraints__ == (str, bytes)
398 """
399
400 def __new__(cls, name, *constraints, bound=None,
401 covariant=False, contravariant=False):
402 self = super().__new__(cls, name, (Final,), {}, _root=True)
403 if covariant and contravariant:
404 raise ValueError("Bivariant type variables are not supported.")
405 self.__covariant__ = bool(covariant)
406 self.__contravariant__ = bool(contravariant)
407 if constraints and bound is not None:
408 raise TypeError("Constraints cannot be combined with bound=...")
409 if constraints and len(constraints) == 1:
410 raise TypeError("A single constraint is not allowed")
411 msg = "TypeVar(name, constraint, ...): constraints must be types."
412 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
413 if bound:
414 self.__bound__ = _type_check(bound, "Bound must be a type.")
415 else:
416 self.__bound__ = None
417 return self
418
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700419 def _get_type_vars(self, tvars):
420 if self not in tvars:
421 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700422
423 def __repr__(self):
424 if self.__covariant__:
425 prefix = '+'
426 elif self.__contravariant__:
427 prefix = '-'
428 else:
429 prefix = '~'
430 return prefix + self.__name__
431
432 def __instancecheck__(self, instance):
433 raise TypeError("Type variables cannot be used with isinstance().")
434
435 def __subclasscheck__(self, cls):
436 # TODO: Make this raise TypeError too?
437 if cls is self:
438 return True
439 if cls is Any:
440 return True
441 if self.__bound__ is not None:
442 return issubclass(cls, self.__bound__)
443 if self.__constraints__:
444 return any(issubclass(cls, c) for c in self.__constraints__)
445 return True
446
447
448# Some unconstrained type variables. These are used by the container types.
449T = TypeVar('T') # Any type.
450KT = TypeVar('KT') # Key type.
451VT = TypeVar('VT') # Value type.
452T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
453V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700454VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
455T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
456
457# A useful type variable with constraints. This represents string types.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700458AnyStr = TypeVar('AnyStr', bytes, str)
459
460
461class UnionMeta(TypingMeta):
462 """Metaclass for Union."""
463
464 def __new__(cls, name, bases, namespace, parameters=None, _root=False):
465 if parameters is None:
466 return super().__new__(cls, name, bases, namespace, _root=_root)
467 if not isinstance(parameters, tuple):
468 raise TypeError("Expected parameters=<tuple>")
469 # Flatten out Union[Union[...], ...] and type-check non-Union args.
470 params = []
471 msg = "Union[arg, ...]: each arg must be a type."
472 for p in parameters:
473 if isinstance(p, UnionMeta):
474 params.extend(p.__union_params__)
475 else:
476 params.append(_type_check(p, msg))
477 # Weed out strict duplicates, preserving the first of each occurrence.
478 all_params = set(params)
479 if len(all_params) < len(params):
480 new_params = []
481 for t in params:
482 if t in all_params:
483 new_params.append(t)
484 all_params.remove(t)
485 params = new_params
486 assert not all_params, all_params
487 # Weed out subclasses.
488 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
489 # If Any or object is present it will be the sole survivor.
490 # If both Any and object are present, Any wins.
491 # Never discard type variables, except against Any.
492 # (In particular, Union[str, AnyStr] != AnyStr.)
493 all_params = set(params)
494 for t1 in params:
495 if t1 is Any:
496 return Any
497 if isinstance(t1, TypeVar):
498 continue
Guido van Rossumca636ea2015-10-19 14:55:47 -0700499 if isinstance(t1, _TypeAlias):
500 # _TypeAlias is not a real class.
501 continue
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700502 if any(issubclass(t1, t2)
503 for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
504 all_params.remove(t1)
505 # It's not a union if there's only one type left.
506 if len(all_params) == 1:
507 return all_params.pop()
508 # Create a new class with these params.
509 self = super().__new__(cls, name, bases, {}, _root=True)
510 self.__union_params__ = tuple(t for t in params if t in all_params)
511 self.__union_set_params__ = frozenset(self.__union_params__)
512 return self
513
514 def _eval_type(self, globalns, localns):
515 p = tuple(_eval_type(t, globalns, localns)
516 for t in self.__union_params__)
517 if p == self.__union_params__:
518 return self
519 else:
520 return self.__class__(self.__name__, self.__bases__, {},
521 p, _root=True)
522
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700523 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700524 if self.__union_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700525 _get_type_vars(self.__union_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700526
527 def __repr__(self):
528 r = super().__repr__()
529 if self.__union_params__:
530 r += '[%s]' % (', '.join(_type_repr(t)
531 for t in self.__union_params__))
532 return r
533
534 def __getitem__(self, parameters):
535 if self.__union_params__ is not None:
536 raise TypeError(
537 "Cannot subscript an existing Union. Use Union[u, t] instead.")
538 if parameters == ():
539 raise TypeError("Cannot take a Union of no types.")
540 if not isinstance(parameters, tuple):
541 parameters = (parameters,)
542 return self.__class__(self.__name__, self.__bases__,
543 dict(self.__dict__), parameters, _root=True)
544
545 def __eq__(self, other):
546 if not isinstance(other, UnionMeta):
547 return NotImplemented
548 return self.__union_set_params__ == other.__union_set_params__
549
550 def __hash__(self):
551 return hash(self.__union_set_params__)
552
Guido van Rossumd70fe632015-08-05 12:11:06 +0200553 def __instancecheck__(self, obj):
554 raise TypeError("Unions cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700555
556 def __subclasscheck__(self, cls):
557 if cls is Any:
558 return True
559 if self.__union_params__ is None:
560 return isinstance(cls, UnionMeta)
561 elif isinstance(cls, UnionMeta):
562 if cls.__union_params__ is None:
563 return False
564 return all(issubclass(c, self) for c in (cls.__union_params__))
565 elif isinstance(cls, TypeVar):
566 if cls in self.__union_params__:
567 return True
568 if cls.__constraints__:
569 return issubclass(Union[cls.__constraints__], self)
570 return False
571 else:
572 return any(issubclass(cls, t) for t in self.__union_params__)
573
574
575class Union(Final, metaclass=UnionMeta, _root=True):
576 """Union type; Union[X, Y] means either X or Y.
577
578 To define a union, use e.g. Union[int, str]. Details:
579
580 - The arguments must be types and there must be at least one.
581
582 - None as an argument is a special case and is replaced by
583 type(None).
584
585 - Unions of unions are flattened, e.g.::
586
587 Union[Union[int, str], float] == Union[int, str, float]
588
589 - Unions of a single argument vanish, e.g.::
590
591 Union[int] == int # The constructor actually returns int
592
593 - Redundant arguments are skipped, e.g.::
594
595 Union[int, str, int] == Union[int, str]
596
597 - When comparing unions, the argument order is ignored, e.g.::
598
599 Union[int, str] == Union[str, int]
600
601 - When two arguments have a subclass relationship, the least
602 derived argument is kept, e.g.::
603
604 class Employee: pass
605 class Manager(Employee): pass
606 Union[int, Employee, Manager] == Union[int, Employee]
607 Union[Manager, int, Employee] == Union[int, Employee]
608 Union[Employee, Manager] == Employee
609
610 - Corollary: if Any is present it is the sole survivor, e.g.::
611
612 Union[int, Any] == Any
613
614 - Similar for object::
615
616 Union[int, object] == object
617
618 - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
619
620 - You cannot subclass or instantiate a union.
621
622 - You cannot write Union[X][Y] (what would it mean?).
623
624 - You can use Optional[X] as a shorthand for Union[X, None].
625 """
626
627 # Unsubscripted Union type has params set to None.
628 __union_params__ = None
629 __union_set_params__ = None
630
631
632class OptionalMeta(TypingMeta):
633 """Metaclass for Optional."""
634
635 def __new__(cls, name, bases, namespace, _root=False):
636 return super().__new__(cls, name, bases, namespace, _root=_root)
637
638 def __getitem__(self, arg):
639 arg = _type_check(arg, "Optional[t] requires a single type.")
640 return Union[arg, type(None)]
641
642
643class Optional(Final, metaclass=OptionalMeta, _root=True):
644 """Optional type.
645
646 Optional[X] is equivalent to Union[X, type(None)].
647 """
648
Guido van Rossumd70fe632015-08-05 12:11:06 +0200649 __slots__ = ()
650
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700651
652class TupleMeta(TypingMeta):
653 """Metaclass for Tuple."""
654
655 def __new__(cls, name, bases, namespace, parameters=None,
656 use_ellipsis=False, _root=False):
657 self = super().__new__(cls, name, bases, namespace, _root=_root)
658 self.__tuple_params__ = parameters
659 self.__tuple_use_ellipsis__ = use_ellipsis
660 return self
661
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700662 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700663 if self.__tuple_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700664 _get_type_vars(self.__tuple_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700665
666 def _eval_type(self, globalns, localns):
667 tp = self.__tuple_params__
668 if tp is None:
669 return self
670 p = tuple(_eval_type(t, globalns, localns) for t in tp)
671 if p == self.__tuple_params__:
672 return self
673 else:
674 return self.__class__(self.__name__, self.__bases__, {},
675 p, _root=True)
676
677 def __repr__(self):
678 r = super().__repr__()
679 if self.__tuple_params__ is not None:
680 params = [_type_repr(p) for p in self.__tuple_params__]
681 if self.__tuple_use_ellipsis__:
682 params.append('...')
683 r += '[%s]' % (
684 ', '.join(params))
685 return r
686
687 def __getitem__(self, parameters):
688 if self.__tuple_params__ is not None:
689 raise TypeError("Cannot re-parameterize %r" % (self,))
690 if not isinstance(parameters, tuple):
691 parameters = (parameters,)
692 if len(parameters) == 2 and parameters[1] == Ellipsis:
693 parameters = parameters[:1]
694 use_ellipsis = True
695 msg = "Tuple[t, ...]: t must be a type."
696 else:
697 use_ellipsis = False
698 msg = "Tuple[t0, t1, ...]: each t must be a type."
699 parameters = tuple(_type_check(p, msg) for p in parameters)
700 return self.__class__(self.__name__, self.__bases__,
701 dict(self.__dict__), parameters,
702 use_ellipsis=use_ellipsis, _root=True)
703
704 def __eq__(self, other):
705 if not isinstance(other, TupleMeta):
706 return NotImplemented
707 return self.__tuple_params__ == other.__tuple_params__
708
709 def __hash__(self):
710 return hash(self.__tuple_params__)
711
Guido van Rossumd70fe632015-08-05 12:11:06 +0200712 def __instancecheck__(self, obj):
713 raise TypeError("Tuples cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700714
715 def __subclasscheck__(self, cls):
716 if cls is Any:
717 return True
718 if not isinstance(cls, type):
719 return super().__subclasscheck__(cls) # To TypeError.
720 if issubclass(cls, tuple):
721 return True # Special case.
722 if not isinstance(cls, TupleMeta):
723 return super().__subclasscheck__(cls) # False.
724 if self.__tuple_params__ is None:
725 return True
726 if cls.__tuple_params__ is None:
727 return False # ???
728 if cls.__tuple_use_ellipsis__ != self.__tuple_use_ellipsis__:
729 return False
730 # Covariance.
731 return (len(self.__tuple_params__) == len(cls.__tuple_params__) and
732 all(issubclass(x, p)
733 for x, p in zip(cls.__tuple_params__,
734 self.__tuple_params__)))
735
736
737class Tuple(Final, metaclass=TupleMeta, _root=True):
738 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
739
740 Example: Tuple[T1, T2] is a tuple of two elements corresponding
741 to type variables T1 and T2. Tuple[int, float, str] is a tuple
742 of an int, a float and a string.
743
744 To specify a variable-length tuple of homogeneous type, use Sequence[T].
745 """
746
Guido van Rossumd70fe632015-08-05 12:11:06 +0200747 __slots__ = ()
748
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700749
750class CallableMeta(TypingMeta):
751 """Metaclass for Callable."""
752
753 def __new__(cls, name, bases, namespace, _root=False,
754 args=None, result=None):
755 if args is None and result is None:
756 pass # Must be 'class Callable'.
757 else:
758 if args is not Ellipsis:
759 if not isinstance(args, list):
760 raise TypeError("Callable[args, result]: "
761 "args must be a list."
762 " Got %.100r." % (args,))
763 msg = "Callable[[arg, ...], result]: each arg must be a type."
764 args = tuple(_type_check(arg, msg) for arg in args)
765 msg = "Callable[args, result]: result must be a type."
766 result = _type_check(result, msg)
767 self = super().__new__(cls, name, bases, namespace, _root=_root)
768 self.__args__ = args
769 self.__result__ = result
770 return self
771
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700772 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700773 if self.__args__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700774 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700775
776 def _eval_type(self, globalns, localns):
777 if self.__args__ is None and self.__result__ is None:
778 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200779 if self.__args__ is Ellipsis:
780 args = self.__args__
781 else:
782 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700783 result = _eval_type(self.__result__, globalns, localns)
784 if args == self.__args__ and result == self.__result__:
785 return self
786 else:
787 return self.__class__(self.__name__, self.__bases__, {},
788 args=args, result=result, _root=True)
789
790 def __repr__(self):
791 r = super().__repr__()
792 if self.__args__ is not None or self.__result__ is not None:
793 if self.__args__ is Ellipsis:
794 args_r = '...'
795 else:
796 args_r = '[%s]' % ', '.join(_type_repr(t)
797 for t in self.__args__)
798 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
799 return r
800
801 def __getitem__(self, parameters):
802 if self.__args__ is not None or self.__result__ is not None:
803 raise TypeError("This Callable type is already parameterized.")
804 if not isinstance(parameters, tuple) or len(parameters) != 2:
805 raise TypeError(
806 "Callable must be used as Callable[[arg, ...], result].")
807 args, result = parameters
808 return self.__class__(self.__name__, self.__bases__,
809 dict(self.__dict__), _root=True,
810 args=args, result=result)
811
812 def __eq__(self, other):
813 if not isinstance(other, CallableMeta):
814 return NotImplemented
815 return (self.__args__ == other.__args__ and
816 self.__result__ == other.__result__)
817
818 def __hash__(self):
819 return hash(self.__args__) ^ hash(self.__result__)
820
Guido van Rossumd70fe632015-08-05 12:11:06 +0200821 def __instancecheck__(self, obj):
822 # For unparametrized Callable we allow this, because
823 # typing.Callable should be equivalent to
824 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700825 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200826 return isinstance(obj, collections_abc.Callable)
827 else:
828 raise TypeError("Callable[] cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700829
830 def __subclasscheck__(self, cls):
831 if cls is Any:
832 return True
833 if not isinstance(cls, CallableMeta):
834 return super().__subclasscheck__(cls)
835 if self.__args__ is None and self.__result__ is None:
836 return True
837 # We're not doing covariance or contravariance -- this is *invariance*.
838 return self == cls
839
840
841class Callable(Final, metaclass=CallableMeta, _root=True):
842 """Callable type; Callable[[int], str] is a function of (int) -> str.
843
844 The subscription syntax must always be used with exactly two
845 values: the argument list and the return type. The argument list
846 must be a list of types; the return type must be a single type.
847
848 There is no syntax to indicate optional or keyword arguments,
849 such function types are rarely used as callback types.
850 """
851
Guido van Rossumd70fe632015-08-05 12:11:06 +0200852 __slots__ = ()
853
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700854
855def _gorg(a):
856 """Return the farthest origin of a generic class."""
857 assert isinstance(a, GenericMeta)
858 while a.__origin__ is not None:
859 a = a.__origin__
860 return a
861
862
863def _geqv(a, b):
864 """Return whether two generic classes are equivalent.
865
866 The intention is to consider generic class X and any of its
867 parameterized forms (X[T], X[int], etc.) as equivalent.
868
869 However, X is not equivalent to a subclass of X.
870
871 The relation is reflexive, symmetric and transitive.
872 """
873 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
874 # Reduce each to its origin.
875 return _gorg(a) is _gorg(b)
876
877
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700878def _next_in_mro(cls):
879 """Helper for Generic.__new__.
880
881 Returns the class after the last occurrence of Generic or
882 Generic[...] in cls.__mro__.
883 """
884 next_in_mro = object
885 # Look for the last occurrence of Generic or Generic[...].
886 for i, c in enumerate(cls.__mro__[:-1]):
887 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
888 next_in_mro = cls.__mro__[i+1]
889 return next_in_mro
890
891
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700892class GenericMeta(TypingMeta, abc.ABCMeta):
893 """Metaclass for generic types."""
894
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700895 __extra__ = None
896
897 def __new__(cls, name, bases, namespace,
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700898 tvars=None, args=None, origin=None, extra=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700899 self = super().__new__(cls, name, bases, namespace, _root=True)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700900
901 if tvars is not None:
902 # Called from __getitem__() below.
903 assert origin is not None
904 assert all(isinstance(t, TypeVar) for t in tvars), tvars
905 else:
906 # Called from class statement.
907 assert tvars is None, tvars
908 assert args is None, args
909 assert origin is None, origin
910
911 # Get the full set of tvars from the bases.
912 tvars = _type_vars(bases)
913 # Look for Generic[T1, ..., Tn].
914 # If found, tvars must be a subset of it.
915 # If not found, tvars is it.
916 # Also check for and reject plain Generic,
917 # and reject multiple Generic[...].
918 gvars = None
919 for base in bases:
920 if base is Generic:
921 raise TypeError("Cannot inherit from plain Generic")
922 if (isinstance(base, GenericMeta) and
923 base.__origin__ is Generic):
924 if gvars is not None:
925 raise TypeError(
926 "Cannot inherit from Generic[...] multiple types.")
927 gvars = base.__parameters__
928 if gvars is None:
929 gvars = tvars
930 else:
931 tvarset = set(tvars)
932 gvarset = set(gvars)
933 if not tvarset <= gvarset:
934 raise TypeError(
935 "Some type variables (%s) "
936 "are not listed in Generic[%s]" %
937 (", ".join(str(t) for t in tvars if t not in gvarset),
938 ", ".join(str(g) for g in gvars)))
939 tvars = gvars
940
941 self.__parameters__ = tvars
942 self.__args__ = args
943 self.__origin__ = origin
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700944 if extra is not None:
945 self.__extra__ = extra
946 # Else __extra__ is inherited, eventually from the
947 # (meta-)class default above.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700948 # Speed hack (https://github.com/python/typing/issues/196).
949 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700950 return self
951
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700952 def _get_type_vars(self, tvars):
953 if self.__origin__ and self.__parameters__:
954 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700955
956 def __repr__(self):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700957 if self.__origin__ is not None:
958 r = repr(self.__origin__)
959 else:
960 r = super().__repr__()
961 if self.__args__:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700962 r += '[%s]' % (
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700963 ', '.join(_type_repr(p) for p in self.__args__))
964 if self.__parameters__:
965 r += '<%s>' % (
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700966 ', '.join(_type_repr(p) for p in self.__parameters__))
967 return r
968
969 def __eq__(self, other):
970 if not isinstance(other, GenericMeta):
971 return NotImplemented
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700972 if self.__origin__ is not None:
973 return (self.__origin__ is other.__origin__ and
974 self.__args__ == other.__args__ and
975 self.__parameters__ == other.__parameters__)
976 else:
977 return self is other
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700978
979 def __hash__(self):
980 return hash((self.__name__, self.__parameters__))
981
982 def __getitem__(self, params):
983 if not isinstance(params, tuple):
984 params = (params,)
985 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700986 raise TypeError(
987 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700988 msg = "Parameters to generic types must be types."
989 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700990 if self is Generic:
991 # Generic can only be subscripted with unique type variables.
992 if not all(isinstance(p, TypeVar) for p in params):
993 raise TypeError(
994 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200995 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -0700996 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700997 "Parameters to Generic[...] must all be unique")
998 tvars = params
999 args = None
1000 elif self is _Protocol:
1001 # _Protocol is internal, don't check anything.
1002 tvars = params
1003 args = None
1004 elif self.__origin__ in (Generic, _Protocol):
1005 # Can't subscript Generic[...] or _Protocol[...].
1006 raise TypeError("Cannot subscript already-subscripted %s" %
1007 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001008 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001009 # Subscripting a regular Generic subclass.
1010 if not self.__parameters__:
1011 raise TypeError("%s is not a generic class" % repr(self))
1012 alen = len(params)
1013 elen = len(self.__parameters__)
1014 if alen != elen:
1015 raise TypeError(
1016 "Too %s parameters for %s; actual %s, expected %s" %
1017 ("many" if alen > elen else "few", repr(self), alen, elen))
1018 tvars = _type_vars(params)
1019 args = params
1020 return self.__class__(self.__name__,
1021 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001022 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001023 tvars=tvars,
1024 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001025 origin=self,
1026 extra=self.__extra__)
1027
Guido van Rossum1b669102015-09-04 12:15:54 -07001028 def __instancecheck__(self, instance):
1029 # Since we extend ABC.__subclasscheck__ and
1030 # ABC.__instancecheck__ inlines the cache checking done by the
1031 # latter, we must extend __instancecheck__ too. For simplicity
1032 # we just skip the cache check -- instance checks for generic
1033 # classes are supposed to be rare anyways.
1034 return self.__subclasscheck__(instance.__class__)
1035
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001036 def __subclasscheck__(self, cls):
1037 if cls is Any:
1038 return True
1039 if isinstance(cls, GenericMeta):
1040 # For a class C(Generic[T]) where T is co-variant,
1041 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
1042 origin = self.__origin__
1043 if origin is not None and origin is cls.__origin__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001044 assert len(self.__args__) == len(origin.__parameters__)
1045 assert len(cls.__args__) == len(origin.__parameters__)
1046 for p_self, p_cls, p_origin in zip(self.__args__,
1047 cls.__args__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001048 origin.__parameters__):
1049 if isinstance(p_origin, TypeVar):
1050 if p_origin.__covariant__:
1051 # Covariant -- p_cls must be a subclass of p_self.
1052 if not issubclass(p_cls, p_self):
1053 break
1054 elif p_origin.__contravariant__:
1055 # Contravariant. I think it's the opposite. :-)
1056 if not issubclass(p_self, p_cls):
1057 break
1058 else:
1059 # Invariant -- p_cls and p_self must equal.
1060 if p_self != p_cls:
1061 break
1062 else:
1063 # If the origin's parameter is not a typevar,
1064 # insist on invariance.
1065 if p_self != p_cls:
1066 break
1067 else:
1068 return True
1069 # If we break out of the loop, the superclass gets a chance.
1070 if super().__subclasscheck__(cls):
1071 return True
1072 if self.__extra__ is None or isinstance(cls, GenericMeta):
1073 return False
1074 return issubclass(cls, self.__extra__)
1075
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001076
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001077# Prevent checks for Generic to crash when defining Generic.
1078Generic = None
1079
1080
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001081class Generic(metaclass=GenericMeta):
1082 """Abstract base class for generic types.
1083
1084 A generic type is typically declared by inheriting from an
1085 instantiation of this class with one or more type variables.
1086 For example, a generic mapping type might be defined as::
1087
1088 class Mapping(Generic[KT, VT]):
1089 def __getitem__(self, key: KT) -> VT:
1090 ...
1091 # Etc.
1092
1093 This class can then be used as follows::
1094
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001095 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001096 try:
1097 return mapping[key]
1098 except KeyError:
1099 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001100 """
1101
Guido van Rossumd70fe632015-08-05 12:11:06 +02001102 __slots__ = ()
1103
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001104 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001105 if cls.__origin__ is None:
1106 return cls.__next_in_mro__.__new__(cls)
1107 else:
1108 origin = _gorg(cls)
1109 obj = cls.__next_in_mro__.__new__(origin)
1110 obj.__init__(*args, **kwds)
1111 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001112
1113
1114def cast(typ, val):
1115 """Cast a value to a type.
1116
1117 This returns the value unchanged. To the type checker this
1118 signals that the return value has the designated type, but at
1119 runtime we intentionally don't check anything (we want this
1120 to be as fast as possible).
1121 """
1122 return val
1123
1124
1125def _get_defaults(func):
1126 """Internal helper to extract the default arguments, by name."""
1127 code = func.__code__
1128 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001129 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001130 arg_names = arg_names[:pos_count]
1131 defaults = func.__defaults__ or ()
1132 kwdefaults = func.__kwdefaults__
1133 res = dict(kwdefaults) if kwdefaults else {}
1134 pos_offset = pos_count - len(defaults)
1135 for name, value in zip(arg_names[pos_offset:], defaults):
1136 assert name not in res
1137 res[name] = value
1138 return res
1139
1140
1141def get_type_hints(obj, globalns=None, localns=None):
1142 """Return type hints for a function or method object.
1143
1144 This is often the same as obj.__annotations__, but it handles
1145 forward references encoded as string literals, and if necessary
1146 adds Optional[t] if a default value equal to None is set.
1147
1148 BEWARE -- the behavior of globalns and localns is counterintuitive
1149 (unless you are familiar with how eval() and exec() work). The
1150 search order is locals first, then globals.
1151
1152 - If no dict arguments are passed, an attempt is made to use the
1153 globals from obj, and these are also used as the locals. If the
1154 object does not appear to have globals, an exception is raised.
1155
1156 - If one dict argument is passed, it is used for both globals and
1157 locals.
1158
1159 - If two dict arguments are passed, they specify globals and
1160 locals, respectively.
1161 """
1162 if getattr(obj, '__no_type_check__', None):
1163 return {}
1164 if globalns is None:
1165 globalns = getattr(obj, '__globals__', {})
1166 if localns is None:
1167 localns = globalns
1168 elif localns is None:
1169 localns = globalns
1170 defaults = _get_defaults(obj)
1171 hints = dict(obj.__annotations__)
1172 for name, value in hints.items():
1173 if isinstance(value, str):
1174 value = _ForwardRef(value)
1175 value = _eval_type(value, globalns, localns)
1176 if name in defaults and defaults[name] is None:
1177 value = Optional[value]
1178 hints[name] = value
1179 return hints
1180
1181
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001182def no_type_check(arg):
1183 """Decorator to indicate that annotations are not type hints.
1184
1185 The argument must be a class or function; if it is a class, it
1186 applies recursively to all methods defined in that class (but not
1187 to methods defined in its superclasses or subclasses).
1188
1189 This mutates the function(s) in place.
1190 """
1191 if isinstance(arg, type):
1192 for obj in arg.__dict__.values():
1193 if isinstance(obj, types.FunctionType):
1194 obj.__no_type_check__ = True
1195 else:
1196 arg.__no_type_check__ = True
1197 return arg
1198
1199
1200def no_type_check_decorator(decorator):
1201 """Decorator to give another decorator the @no_type_check effect.
1202
1203 This wraps the decorator with something that wraps the decorated
1204 function in @no_type_check.
1205 """
1206
1207 @functools.wraps(decorator)
1208 def wrapped_decorator(*args, **kwds):
1209 func = decorator(*args, **kwds)
1210 func = no_type_check(func)
1211 return func
1212
1213 return wrapped_decorator
1214
1215
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001216def _overload_dummy(*args, **kwds):
1217 """Helper for @overload to raise when called."""
1218 raise NotImplementedError(
1219 "You should not call an overloaded function. "
1220 "A series of @overload-decorated functions "
1221 "outside a stub module should always be followed "
1222 "by an implementation that is not @overload-ed.")
1223
1224
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001225def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001226 """Decorator for overloaded functions/methods.
1227
1228 In a stub file, place two or more stub definitions for the same
1229 function in a row, each decorated with @overload. For example:
1230
1231 @overload
1232 def utf8(value: None) -> None: ...
1233 @overload
1234 def utf8(value: bytes) -> bytes: ...
1235 @overload
1236 def utf8(value: str) -> bytes: ...
1237
1238 In a non-stub file (i.e. a regular .py file), do the same but
1239 follow it with an implementation. The implementation should *not*
1240 be decorated with @overload. For example:
1241
1242 @overload
1243 def utf8(value: None) -> None: ...
1244 @overload
1245 def utf8(value: bytes) -> bytes: ...
1246 @overload
1247 def utf8(value: str) -> bytes: ...
1248 def utf8(value):
1249 # implementation goes here
1250 """
1251 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001252
1253
1254class _ProtocolMeta(GenericMeta):
1255 """Internal metaclass for _Protocol.
1256
1257 This exists so _Protocol classes can be generic without deriving
1258 from Generic.
1259 """
1260
Guido van Rossumd70fe632015-08-05 12:11:06 +02001261 def __instancecheck__(self, obj):
1262 raise TypeError("Protocols cannot be used with isinstance().")
1263
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001264 def __subclasscheck__(self, cls):
1265 if not self._is_protocol:
1266 # No structural checks since this isn't a protocol.
1267 return NotImplemented
1268
1269 if self is _Protocol:
1270 # Every class is a subclass of the empty protocol.
1271 return True
1272
1273 # Find all attributes defined in the protocol.
1274 attrs = self._get_protocol_attrs()
1275
1276 for attr in attrs:
1277 if not any(attr in d.__dict__ for d in cls.__mro__):
1278 return False
1279 return True
1280
1281 def _get_protocol_attrs(self):
1282 # Get all Protocol base classes.
1283 protocol_bases = []
1284 for c in self.__mro__:
1285 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1286 protocol_bases.append(c)
1287
1288 # Get attributes included in protocol.
1289 attrs = set()
1290 for base in protocol_bases:
1291 for attr in base.__dict__.keys():
1292 # Include attributes not defined in any non-protocol bases.
1293 for c in self.__mro__:
1294 if (c is not base and attr in c.__dict__ and
1295 not getattr(c, '_is_protocol', False)):
1296 break
1297 else:
1298 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001299 attr != '__abstractmethods__' and
1300 attr != '_is_protocol' and
1301 attr != '__dict__' and
1302 attr != '__args__' and
1303 attr != '__slots__' and
1304 attr != '_get_protocol_attrs' and
1305 attr != '__next_in_mro__' and
1306 attr != '__parameters__' and
1307 attr != '__origin__' and
1308 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001309 attrs.add(attr)
1310
1311 return attrs
1312
1313
1314class _Protocol(metaclass=_ProtocolMeta):
1315 """Internal base class for protocol classes.
1316
1317 This implements a simple-minded structural isinstance check
1318 (similar but more general than the one-offs in collections.abc
1319 such as Hashable).
1320 """
1321
Guido van Rossumd70fe632015-08-05 12:11:06 +02001322 __slots__ = ()
1323
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001324 _is_protocol = True
1325
1326
1327# Various ABCs mimicking those in collections.abc.
1328# A few are simply re-exported for completeness.
1329
1330Hashable = collections_abc.Hashable # Not generic.
1331
1332
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001333if hasattr(collections_abc, 'Awaitable'):
1334 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1335 __slots__ = ()
1336else:
1337 Awaitable = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001338
1339
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001340if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001341
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001342 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1343 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001344
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001345 class AsyncIterator(AsyncIterable[T_co],
1346 extra=collections_abc.AsyncIterator):
1347 __slots__ = ()
1348
1349else:
1350 AsyncIterable = None
1351 AsyncIterator = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001352
1353
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001354class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001355 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001356
1357
1358class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001359 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001360
1361
1362class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001363 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001364
1365 @abstractmethod
1366 def __int__(self) -> int:
1367 pass
1368
1369
1370class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001371 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001372
1373 @abstractmethod
1374 def __float__(self) -> float:
1375 pass
1376
1377
1378class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001379 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001380
1381 @abstractmethod
1382 def __complex__(self) -> complex:
1383 pass
1384
1385
1386class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001387 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001388
1389 @abstractmethod
1390 def __bytes__(self) -> bytes:
1391 pass
1392
1393
Guido van Rossumd70fe632015-08-05 12:11:06 +02001394class SupportsAbs(_Protocol[T_co]):
1395 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001396
1397 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001398 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001399 pass
1400
1401
Guido van Rossumd70fe632015-08-05 12:11:06 +02001402class SupportsRound(_Protocol[T_co]):
1403 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001404
1405 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001406 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001407 pass
1408
1409
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001410if hasattr(collections_abc, 'Reversible'):
1411 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1412 __slots__ = ()
1413else:
1414 class Reversible(_Protocol[T_co]):
1415 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001416
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001417 @abstractmethod
1418 def __reversed__(self) -> 'Iterator[T_co]':
1419 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001420
1421
1422Sized = collections_abc.Sized # Not generic.
1423
1424
1425class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001426 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001427
1428
1429# Callable was defined earlier.
1430
1431
1432class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1433 extra=collections_abc.Set):
1434 pass
1435
1436
1437class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
1438 pass
1439
1440
Guido van Rossumd70fe632015-08-05 12:11:06 +02001441# NOTE: Only the value type is covariant.
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001442class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001443 extra=collections_abc.Mapping):
1444 pass
1445
1446
1447class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
1448 pass
1449
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001450if hasattr(collections_abc, 'Reversible'):
1451 class Sequence(Sized, Reversible[T_co], Container[T_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001452 extra=collections_abc.Sequence):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001453 pass
1454else:
1455 class Sequence(Sized, Iterable[T_co], Container[T_co],
1456 extra=collections_abc.Sequence):
1457 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001458
1459
1460class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
1461 pass
1462
1463
1464class ByteString(Sequence[int], extra=collections_abc.ByteString):
1465 pass
1466
1467
1468ByteString.register(type(memoryview(b'')))
1469
1470
Guido van Rossumd70fe632015-08-05 12:11:06 +02001471class List(list, MutableSequence[T]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001472
1473 def __new__(cls, *args, **kwds):
1474 if _geqv(cls, List):
1475 raise TypeError("Type List cannot be instantiated; "
1476 "use list() instead")
1477 return list.__new__(cls, *args, **kwds)
1478
1479
Guido van Rossumd70fe632015-08-05 12:11:06 +02001480class Set(set, MutableSet[T]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001481
1482 def __new__(cls, *args, **kwds):
1483 if _geqv(cls, Set):
1484 raise TypeError("Type Set cannot be instantiated; "
1485 "use set() instead")
1486 return set.__new__(cls, *args, **kwds)
1487
1488
Guido van Rossumd70fe632015-08-05 12:11:06 +02001489class _FrozenSetMeta(GenericMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001490 """This metaclass ensures set is not a subclass of FrozenSet.
1491
1492 Without this metaclass, set would be considered a subclass of
1493 FrozenSet, because FrozenSet.__extra__ is collections.abc.Set, and
1494 set is a subclass of that.
1495 """
1496
1497 def __subclasscheck__(self, cls):
1498 if issubclass(cls, Set):
1499 return False
1500 return super().__subclasscheck__(cls)
1501
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001502
1503class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001504 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001505
1506 def __new__(cls, *args, **kwds):
1507 if _geqv(cls, FrozenSet):
1508 raise TypeError("Type FrozenSet cannot be instantiated; "
1509 "use frozenset() instead")
1510 return frozenset.__new__(cls, *args, **kwds)
1511
1512
1513class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
1514 pass
1515
1516
Guido van Rossumd70fe632015-08-05 12:11:06 +02001517class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001518 extra=collections_abc.KeysView):
1519 pass
1520
1521
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001522class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001523 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001524 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001525 extra=collections_abc.ItemsView):
1526 pass
1527
1528
1529class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
1530 pass
1531
1532
Guido van Rossumd70fe632015-08-05 12:11:06 +02001533class Dict(dict, MutableMapping[KT, VT]):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001534
1535 def __new__(cls, *args, **kwds):
1536 if _geqv(cls, Dict):
1537 raise TypeError("Type Dict cannot be instantiated; "
1538 "use dict() instead")
1539 return dict.__new__(cls, *args, **kwds)
1540
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001541class DefaultDict(collections.defaultdict, MutableMapping[KT, VT]):
1542
1543 def __new__(cls, *args, **kwds):
1544 if _geqv(cls, DefaultDict):
1545 raise TypeError("Type DefaultDict cannot be instantiated; "
1546 "use collections.defaultdict() instead")
1547 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001548
1549# Determine what base class to use for Generator.
1550if hasattr(collections_abc, 'Generator'):
1551 # Sufficiently recent versions of 3.5 have a Generator ABC.
1552 _G_base = collections_abc.Generator
1553else:
1554 # Fall back on the exact type.
1555 _G_base = types.GeneratorType
1556
1557
1558class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1559 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001560 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001561
1562 def __new__(cls, *args, **kwds):
1563 if _geqv(cls, Generator):
1564 raise TypeError("Type Generator cannot be instantiated; "
1565 "create a subclass instead")
1566 return super().__new__(cls, *args, **kwds)
1567
1568
1569def NamedTuple(typename, fields):
1570 """Typed version of namedtuple.
1571
1572 Usage::
1573
1574 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1575
1576 This is equivalent to::
1577
1578 Employee = collections.namedtuple('Employee', ['name', 'id'])
1579
1580 The resulting class has one extra attribute: _field_types,
1581 giving a dict mapping field names to types. (The field names
1582 are in the _fields attribute, which is part of the namedtuple
1583 API.)
1584 """
1585 fields = [(n, t) for n, t in fields]
1586 cls = collections.namedtuple(typename, [n for n, t in fields])
1587 cls._field_types = dict(fields)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001588 # Set the module to the caller's module (otherwise it'd be 'typing').
1589 try:
1590 cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
1591 except (AttributeError, ValueError):
1592 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001593 return cls
1594
1595
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001596# Python-version-specific alias (Python 2: unicode; Python 3: str)
1597Text = str
1598
1599
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001600class IO(Generic[AnyStr]):
1601 """Generic base class for TextIO and BinaryIO.
1602
1603 This is an abstract, generic version of the return of open().
1604
1605 NOTE: This does not distinguish between the different possible
1606 classes (text vs. binary, read vs. write vs. read/write,
1607 append-only, unbuffered). The TextIO and BinaryIO subclasses
1608 below capture the distinctions between text vs. binary, which is
1609 pervasive in the interface; however we currently do not offer a
1610 way to track the other distinctions in the type system.
1611 """
1612
Guido van Rossumd70fe632015-08-05 12:11:06 +02001613 __slots__ = ()
1614
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001615 @abstractproperty
1616 def mode(self) -> str:
1617 pass
1618
1619 @abstractproperty
1620 def name(self) -> str:
1621 pass
1622
1623 @abstractmethod
1624 def close(self) -> None:
1625 pass
1626
1627 @abstractmethod
1628 def closed(self) -> bool:
1629 pass
1630
1631 @abstractmethod
1632 def fileno(self) -> int:
1633 pass
1634
1635 @abstractmethod
1636 def flush(self) -> None:
1637 pass
1638
1639 @abstractmethod
1640 def isatty(self) -> bool:
1641 pass
1642
1643 @abstractmethod
1644 def read(self, n: int = -1) -> AnyStr:
1645 pass
1646
1647 @abstractmethod
1648 def readable(self) -> bool:
1649 pass
1650
1651 @abstractmethod
1652 def readline(self, limit: int = -1) -> AnyStr:
1653 pass
1654
1655 @abstractmethod
1656 def readlines(self, hint: int = -1) -> List[AnyStr]:
1657 pass
1658
1659 @abstractmethod
1660 def seek(self, offset: int, whence: int = 0) -> int:
1661 pass
1662
1663 @abstractmethod
1664 def seekable(self) -> bool:
1665 pass
1666
1667 @abstractmethod
1668 def tell(self) -> int:
1669 pass
1670
1671 @abstractmethod
1672 def truncate(self, size: int = None) -> int:
1673 pass
1674
1675 @abstractmethod
1676 def writable(self) -> bool:
1677 pass
1678
1679 @abstractmethod
1680 def write(self, s: AnyStr) -> int:
1681 pass
1682
1683 @abstractmethod
1684 def writelines(self, lines: List[AnyStr]) -> None:
1685 pass
1686
1687 @abstractmethod
1688 def __enter__(self) -> 'IO[AnyStr]':
1689 pass
1690
1691 @abstractmethod
1692 def __exit__(self, type, value, traceback) -> None:
1693 pass
1694
1695
1696class BinaryIO(IO[bytes]):
1697 """Typed version of the return of open() in binary mode."""
1698
Guido van Rossumd70fe632015-08-05 12:11:06 +02001699 __slots__ = ()
1700
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001701 @abstractmethod
1702 def write(self, s: Union[bytes, bytearray]) -> int:
1703 pass
1704
1705 @abstractmethod
1706 def __enter__(self) -> 'BinaryIO':
1707 pass
1708
1709
1710class TextIO(IO[str]):
1711 """Typed version of the return of open() in text mode."""
1712
Guido van Rossumd70fe632015-08-05 12:11:06 +02001713 __slots__ = ()
1714
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001715 @abstractproperty
1716 def buffer(self) -> BinaryIO:
1717 pass
1718
1719 @abstractproperty
1720 def encoding(self) -> str:
1721 pass
1722
1723 @abstractproperty
1724 def errors(self) -> str:
1725 pass
1726
1727 @abstractproperty
1728 def line_buffering(self) -> bool:
1729 pass
1730
1731 @abstractproperty
1732 def newlines(self) -> Any:
1733 pass
1734
1735 @abstractmethod
1736 def __enter__(self) -> 'TextIO':
1737 pass
1738
1739
1740class io:
1741 """Wrapper namespace for IO generic classes."""
1742
1743 __all__ = ['IO', 'TextIO', 'BinaryIO']
1744 IO = IO
1745 TextIO = TextIO
1746 BinaryIO = BinaryIO
1747
1748io.__name__ = __name__ + '.io'
1749sys.modules[io.__name__] = io
1750
1751
1752Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
1753 lambda p: p.pattern)
1754Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
1755 lambda m: m.re.pattern)
1756
1757
1758class re:
1759 """Wrapper namespace for re type aliases."""
1760
1761 __all__ = ['Pattern', 'Match']
1762 Pattern = Pattern
1763 Match = Match
1764
1765re.__name__ = __name__ + '.re'
1766sys.modules[re.__name__] = re