blob: 6ce74fc9f113e1f8a1947eb86d8d3ca4e0d55629 [file] [log] [blame]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001import abc
2from abc import abstractmethod, abstractproperty
3import collections
Brett Cannonf3ad0422016-04-15 10:51:30 -07004import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07005import functools
6import re as stdlib_re # Avoid confusion with the re we export.
7import sys
8import types
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07009
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070010try:
11 import collections.abc as collections_abc
12except ImportError:
13 import collections as collections_abc # Fallback for PY3.2.
14
15
16# Please keep __all__ alphabetized within each category.
17__all__ = [
18 # Super-special typing primitives.
19 'Any',
20 'Callable',
Yury Selivanovf8cb8a12016-09-08 20:50:03 -070021 'ClassVar',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070022 'Generic',
23 'Optional',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070024 'Tuple',
25 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026 'TypeVar',
27 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070028
29 # ABCs (from collections.abc).
30 'AbstractSet', # collections.abc.Set.
Guido van Rossumf17c2002015-12-03 17:31:24 -080031 'Awaitable',
32 'AsyncIterator',
33 'AsyncIterable',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070034 'ByteString',
35 'Container',
36 'Hashable',
37 'ItemsView',
38 'Iterable',
39 'Iterator',
40 'KeysView',
41 'Mapping',
42 'MappingView',
43 'MutableMapping',
44 'MutableSequence',
45 'MutableSet',
46 'Sequence',
47 'Sized',
48 'ValuesView',
49
50 # Structural checks, a.k.a. protocols.
51 'Reversible',
52 'SupportsAbs',
53 'SupportsFloat',
54 'SupportsInt',
55 'SupportsRound',
56
57 # Concrete collection types.
58 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070059 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070060 'List',
61 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070062 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070063 'NamedTuple', # Not really a type.
64 'Generator',
65
66 # One-off things.
67 'AnyStr',
68 'cast',
69 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -070070 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070071 'no_type_check',
72 'no_type_check_decorator',
73 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -070074 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -070075 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070076]
77
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070078# The pseudo-submodules 're' and 'io' are part of the public
79# namespace, but excluded from __all__ because they might stomp on
80# legitimate imports of those modules.
81
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070082
83def _qualname(x):
84 if sys.version_info[:2] >= (3, 3):
85 return x.__qualname__
86 else:
87 # Fall back to just name.
88 return x.__name__
89
90
91class TypingMeta(type):
92 """Metaclass for every type defined below.
93
94 This overrides __new__() to require an extra keyword parameter
95 '_root', which serves as a guard against naive subclassing of the
96 typing classes. Any legitimate class defined using a metaclass
97 derived from TypingMeta (including internal subclasses created by
98 e.g. Union[X, Y]) must pass _root=True.
99
100 This also defines a dummy constructor (all the work is done in
101 __new__) and a nicer repr().
102 """
103
104 _is_protocol = False
105
106 def __new__(cls, name, bases, namespace, *, _root=False):
107 if not _root:
108 raise TypeError("Cannot subclass %s" %
109 (', '.join(map(_type_repr, bases)) or '()'))
110 return super().__new__(cls, name, bases, namespace)
111
112 def __init__(self, *args, **kwds):
113 pass
114
115 def _eval_type(self, globalns, localns):
116 """Override this in subclasses to interpret forward references.
117
118 For example, Union['C'] is internally stored as
119 Union[_ForwardRef('C')], which should evaluate to _Union[C],
120 where C is an object found in globalns or localns (searching
121 localns first, of course).
122 """
123 return self
124
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700125 def _get_type_vars(self, tvars):
126 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700127
128 def __repr__(self):
129 return '%s.%s' % (self.__module__, _qualname(self))
130
131
132class Final:
133 """Mix-in class to prevent instantiation."""
134
Guido van Rossumd70fe632015-08-05 12:11:06 +0200135 __slots__ = ()
136
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700137 def __new__(self, *args, **kwds):
138 raise TypeError("Cannot instantiate %r" % self.__class__)
139
140
141class _ForwardRef(TypingMeta):
142 """Wrapper to hold a forward reference."""
143
144 def __new__(cls, arg):
145 if not isinstance(arg, str):
146 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
147 try:
148 code = compile(arg, '<string>', 'eval')
149 except SyntaxError:
150 raise SyntaxError('ForwardRef must be an expression -- got %r' %
151 (arg,))
152 self = super().__new__(cls, arg, (), {}, _root=True)
153 self.__forward_arg__ = arg
154 self.__forward_code__ = code
155 self.__forward_evaluated__ = False
156 self.__forward_value__ = None
157 typing_globals = globals()
158 frame = sys._getframe(1)
159 while frame is not None and frame.f_globals is typing_globals:
160 frame = frame.f_back
161 assert frame is not None
162 self.__forward_frame__ = frame
163 return self
164
165 def _eval_type(self, globalns, localns):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700166 if not self.__forward_evaluated__:
167 if globalns is None and localns is None:
168 globalns = localns = {}
169 elif globalns is None:
170 globalns = localns
171 elif localns is None:
172 localns = globalns
173 self.__forward_value__ = _type_check(
174 eval(self.__forward_code__, globalns, localns),
175 "Forward references must evaluate to types.")
176 self.__forward_evaluated__ = True
177 return self.__forward_value__
178
Guido van Rossumd70fe632015-08-05 12:11:06 +0200179 def __instancecheck__(self, obj):
180 raise TypeError("Forward references cannot be used with isinstance().")
181
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700182 def __subclasscheck__(self, cls):
183 if not self.__forward_evaluated__:
184 globalns = self.__forward_frame__.f_globals
185 localns = self.__forward_frame__.f_locals
186 try:
187 self._eval_type(globalns, localns)
188 except NameError:
189 return False # Too early.
190 return issubclass(cls, self.__forward_value__)
191
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700192 def __repr__(self):
193 return '_ForwardRef(%r)' % (self.__forward_arg__,)
194
195
196class _TypeAlias:
197 """Internal helper class for defining generic variants of concrete types.
198
199 Note that this is not a type; let's call it a pseudo-type. It can
200 be used in instance and subclass checks, e.g. isinstance(m, Match)
201 or issubclass(type(m), Match). However, it cannot be itself the
202 target of an issubclass() call; e.g. issubclass(Match, C) (for
203 some arbitrary class C) raises TypeError rather than returning
204 False.
205 """
206
Guido van Rossumd70fe632015-08-05 12:11:06 +0200207 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
208
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700209 def __new__(cls, *args, **kwds):
210 """Constructor.
211
212 This only exists to give a better error message in case
213 someone tries to subclass a type alias (not a good idea).
214 """
215 if (len(args) == 3 and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700216 isinstance(args[0], str) and
217 isinstance(args[1], tuple)):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700218 # Close enough.
219 raise TypeError("A type alias cannot be subclassed")
220 return object.__new__(cls)
221
222 def __init__(self, name, type_var, impl_type, type_checker):
223 """Initializer.
224
225 Args:
226 name: The name, e.g. 'Pattern'.
227 type_var: The type parameter, e.g. AnyStr, or the
228 specific type, e.g. str.
229 impl_type: The implementation type.
230 type_checker: Function that takes an impl_type instance.
231 and returns a value that should be a type_var instance.
232 """
233 assert isinstance(name, str), repr(name)
234 assert isinstance(type_var, type), repr(type_var)
235 assert isinstance(impl_type, type), repr(impl_type)
236 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
237 self.name = name
238 self.type_var = type_var
239 self.impl_type = impl_type
240 self.type_checker = type_checker
241
242 def __repr__(self):
243 return "%s[%s]" % (self.name, _type_repr(self.type_var))
244
245 def __getitem__(self, parameter):
246 assert isinstance(parameter, type), repr(parameter)
247 if not isinstance(self.type_var, TypeVar):
248 raise TypeError("%s cannot be further parameterized." % self)
249 if self.type_var.__constraints__:
250 if not issubclass(parameter, Union[self.type_var.__constraints__]):
251 raise TypeError("%s is not a valid substitution for %s." %
252 (parameter, self.type_var))
253 return self.__class__(self.name, parameter,
254 self.impl_type, self.type_checker)
255
256 def __instancecheck__(self, obj):
Guido van Rossumd70fe632015-08-05 12:11:06 +0200257 raise TypeError("Type aliases cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700258
259 def __subclasscheck__(self, cls):
260 if cls is Any:
261 return True
262 if isinstance(cls, _TypeAlias):
263 # Covariance. For now, we compare by name.
264 return (cls.name == self.name and
265 issubclass(cls.type_var, self.type_var))
266 else:
267 # Note that this is too lenient, because the
268 # implementation type doesn't carry information about
269 # whether it is about bytes or str (for example).
270 return issubclass(cls, self.impl_type)
271
272
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700273def _get_type_vars(types, tvars):
274 for t in types:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700275 if isinstance(t, TypingMeta) or isinstance(t, _ClassVar):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700276 t._get_type_vars(tvars)
277
278
279def _type_vars(types):
280 tvars = []
281 _get_type_vars(types, tvars)
282 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700283
284
285def _eval_type(t, globalns, localns):
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700286 if isinstance(t, TypingMeta) or isinstance(t, _ClassVar):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700287 return t._eval_type(globalns, localns)
288 else:
289 return t
290
291
292def _type_check(arg, msg):
293 """Check that the argument is a type, and return it.
294
295 As a special case, accept None and return type(None) instead.
296 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
297
298 The msg argument is a human-readable error message, e.g.
299
300 "Union[arg, ...]: arg should be a type."
301
302 We append the repr() of the actual value (truncated to 100 chars).
303 """
304 if arg is None:
305 return type(None)
306 if isinstance(arg, str):
307 arg = _ForwardRef(arg)
Guido van Rossum91185fe2016-06-08 11:19:11 -0700308 if not isinstance(arg, (type, _TypeAlias)) and not callable(arg):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700309 raise TypeError(msg + " Got %.100r." % (arg,))
310 return arg
311
312
313def _type_repr(obj):
314 """Return the repr() of an object, special-casing types.
315
316 If obj is a type, we return a shorter version than the default
317 type.__repr__, based on the module and qualified name, which is
318 typically enough to uniquely identify a type. For everything
319 else, we fall back on repr(obj).
320 """
321 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
322 if obj.__module__ == 'builtins':
323 return _qualname(obj)
324 else:
325 return '%s.%s' % (obj.__module__, _qualname(obj))
326 else:
327 return repr(obj)
328
329
330class AnyMeta(TypingMeta):
331 """Metaclass for Any."""
332
333 def __new__(cls, name, bases, namespace, _root=False):
334 self = super().__new__(cls, name, bases, namespace, _root=_root)
335 return self
336
Guido van Rossumd70fe632015-08-05 12:11:06 +0200337 def __instancecheck__(self, obj):
338 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700339
340 def __subclasscheck__(self, cls):
341 if not isinstance(cls, type):
342 return super().__subclasscheck__(cls) # To TypeError.
343 return True
344
345
346class Any(Final, metaclass=AnyMeta, _root=True):
347 """Special type indicating an unconstrained type.
348
349 - Any object is an instance of Any.
350 - Any class is a subclass of Any.
351 - As a special case, Any and object are subclasses of each other.
352 """
353
Guido van Rossumd70fe632015-08-05 12:11:06 +0200354 __slots__ = ()
355
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700356
357class TypeVar(TypingMeta, metaclass=TypingMeta, _root=True):
358 """Type variable.
359
360 Usage::
361
362 T = TypeVar('T') # Can be anything
363 A = TypeVar('A', str, bytes) # Must be str or bytes
364
365 Type variables exist primarily for the benefit of static type
366 checkers. They serve as the parameters for generic types as well
367 as for generic function definitions. See class Generic for more
368 information on generic types. Generic functions work as follows:
369
370 def repeat(x: T, n: int) -> Sequence[T]:
371 '''Return a list containing n references to x.'''
372 return [x]*n
373
374 def longest(x: A, y: A) -> A:
375 '''Return the longest of two strings.'''
376 return x if len(x) >= len(y) else y
377
378 The latter example's signature is essentially the overloading
379 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
380 that if the arguments are instances of some subclass of str,
381 the return type is still plain str.
382
383 At runtime, isinstance(x, T) will raise TypeError. However,
384 issubclass(C, T) is true for any class C, and issubclass(str, A)
385 and issubclass(bytes, A) are true, and issubclass(int, A) is
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700386 false. (TODO: Why is this needed? This may change. See #136.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700387
Guido van Rossumefa798d2016-08-23 11:01:50 -0700388 Type variables defined with covariant=True or contravariant=True
389 can be used do declare covariant or contravariant generic types.
390 See PEP 484 for more details. By default generic types are invariant
391 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700392
393 Type variables can be introspected. e.g.:
394
395 T.__name__ == 'T'
396 T.__constraints__ == ()
397 T.__covariant__ == False
398 T.__contravariant__ = False
399 A.__constraints__ == (str, bytes)
400 """
401
402 def __new__(cls, name, *constraints, bound=None,
403 covariant=False, contravariant=False):
404 self = super().__new__(cls, name, (Final,), {}, _root=True)
405 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700406 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700407 self.__covariant__ = bool(covariant)
408 self.__contravariant__ = bool(contravariant)
409 if constraints and bound is not None:
410 raise TypeError("Constraints cannot be combined with bound=...")
411 if constraints and len(constraints) == 1:
412 raise TypeError("A single constraint is not allowed")
413 msg = "TypeVar(name, constraint, ...): constraints must be types."
414 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
415 if bound:
416 self.__bound__ = _type_check(bound, "Bound must be a type.")
417 else:
418 self.__bound__ = None
419 return self
420
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700421 def _get_type_vars(self, tvars):
422 if self not in tvars:
423 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700424
425 def __repr__(self):
426 if self.__covariant__:
427 prefix = '+'
428 elif self.__contravariant__:
429 prefix = '-'
430 else:
431 prefix = '~'
432 return prefix + self.__name__
433
434 def __instancecheck__(self, instance):
435 raise TypeError("Type variables cannot be used with isinstance().")
436
437 def __subclasscheck__(self, cls):
438 # TODO: Make this raise TypeError too?
439 if cls is self:
440 return True
441 if cls is Any:
442 return True
443 if self.__bound__ is not None:
444 return issubclass(cls, self.__bound__)
445 if self.__constraints__:
446 return any(issubclass(cls, c) for c in self.__constraints__)
447 return True
448
449
450# Some unconstrained type variables. These are used by the container types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700451# (These are not for export.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700452T = TypeVar('T') # Any type.
453KT = TypeVar('KT') # Key type.
454VT = TypeVar('VT') # Value type.
455T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
456V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700457VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
458T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
459
460# A useful type variable with constraints. This represents string types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700461# (This one *is* for export!)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700462AnyStr = TypeVar('AnyStr', bytes, str)
463
464
465class UnionMeta(TypingMeta):
466 """Metaclass for Union."""
467
468 def __new__(cls, name, bases, namespace, parameters=None, _root=False):
469 if parameters is None:
470 return super().__new__(cls, name, bases, namespace, _root=_root)
471 if not isinstance(parameters, tuple):
472 raise TypeError("Expected parameters=<tuple>")
473 # Flatten out Union[Union[...], ...] and type-check non-Union args.
474 params = []
475 msg = "Union[arg, ...]: each arg must be a type."
476 for p in parameters:
477 if isinstance(p, UnionMeta):
478 params.extend(p.__union_params__)
479 else:
480 params.append(_type_check(p, msg))
481 # Weed out strict duplicates, preserving the first of each occurrence.
482 all_params = set(params)
483 if len(all_params) < len(params):
484 new_params = []
485 for t in params:
486 if t in all_params:
487 new_params.append(t)
488 all_params.remove(t)
489 params = new_params
490 assert not all_params, all_params
491 # Weed out subclasses.
492 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
493 # If Any or object is present it will be the sole survivor.
494 # If both Any and object are present, Any wins.
495 # Never discard type variables, except against Any.
496 # (In particular, Union[str, AnyStr] != AnyStr.)
497 all_params = set(params)
498 for t1 in params:
499 if t1 is Any:
500 return Any
501 if isinstance(t1, TypeVar):
502 continue
Guido van Rossumca636ea2015-10-19 14:55:47 -0700503 if isinstance(t1, _TypeAlias):
504 # _TypeAlias is not a real class.
505 continue
Guido van Rossum91185fe2016-06-08 11:19:11 -0700506 if not isinstance(t1, type):
507 assert callable(t1) # A callable might sneak through.
508 continue
509 if any(isinstance(t2, type) and issubclass(t1, t2)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700510 for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
511 all_params.remove(t1)
512 # It's not a union if there's only one type left.
513 if len(all_params) == 1:
514 return all_params.pop()
515 # Create a new class with these params.
516 self = super().__new__(cls, name, bases, {}, _root=True)
517 self.__union_params__ = tuple(t for t in params if t in all_params)
518 self.__union_set_params__ = frozenset(self.__union_params__)
519 return self
520
521 def _eval_type(self, globalns, localns):
522 p = tuple(_eval_type(t, globalns, localns)
523 for t in self.__union_params__)
524 if p == self.__union_params__:
525 return self
526 else:
527 return self.__class__(self.__name__, self.__bases__, {},
528 p, _root=True)
529
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700530 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700531 if self.__union_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700532 _get_type_vars(self.__union_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700533
534 def __repr__(self):
535 r = super().__repr__()
536 if self.__union_params__:
537 r += '[%s]' % (', '.join(_type_repr(t)
538 for t in self.__union_params__))
539 return r
540
541 def __getitem__(self, parameters):
542 if self.__union_params__ is not None:
543 raise TypeError(
544 "Cannot subscript an existing Union. Use Union[u, t] instead.")
545 if parameters == ():
546 raise TypeError("Cannot take a Union of no types.")
547 if not isinstance(parameters, tuple):
548 parameters = (parameters,)
549 return self.__class__(self.__name__, self.__bases__,
550 dict(self.__dict__), parameters, _root=True)
551
552 def __eq__(self, other):
553 if not isinstance(other, UnionMeta):
554 return NotImplemented
555 return self.__union_set_params__ == other.__union_set_params__
556
557 def __hash__(self):
558 return hash(self.__union_set_params__)
559
Guido van Rossumd70fe632015-08-05 12:11:06 +0200560 def __instancecheck__(self, obj):
561 raise TypeError("Unions cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700562
563 def __subclasscheck__(self, cls):
564 if cls is Any:
565 return True
566 if self.__union_params__ is None:
567 return isinstance(cls, UnionMeta)
568 elif isinstance(cls, UnionMeta):
569 if cls.__union_params__ is None:
570 return False
571 return all(issubclass(c, self) for c in (cls.__union_params__))
572 elif isinstance(cls, TypeVar):
573 if cls in self.__union_params__:
574 return True
575 if cls.__constraints__:
576 return issubclass(Union[cls.__constraints__], self)
577 return False
578 else:
579 return any(issubclass(cls, t) for t in self.__union_params__)
580
581
582class Union(Final, metaclass=UnionMeta, _root=True):
583 """Union type; Union[X, Y] means either X or Y.
584
585 To define a union, use e.g. Union[int, str]. Details:
586
587 - The arguments must be types and there must be at least one.
588
589 - None as an argument is a special case and is replaced by
590 type(None).
591
592 - Unions of unions are flattened, e.g.::
593
594 Union[Union[int, str], float] == Union[int, str, float]
595
596 - Unions of a single argument vanish, e.g.::
597
598 Union[int] == int # The constructor actually returns int
599
600 - Redundant arguments are skipped, e.g.::
601
602 Union[int, str, int] == Union[int, str]
603
604 - When comparing unions, the argument order is ignored, e.g.::
605
606 Union[int, str] == Union[str, int]
607
608 - When two arguments have a subclass relationship, the least
609 derived argument is kept, e.g.::
610
611 class Employee: pass
612 class Manager(Employee): pass
613 Union[int, Employee, Manager] == Union[int, Employee]
614 Union[Manager, int, Employee] == Union[int, Employee]
615 Union[Employee, Manager] == Employee
616
617 - Corollary: if Any is present it is the sole survivor, e.g.::
618
619 Union[int, Any] == Any
620
621 - Similar for object::
622
623 Union[int, object] == object
624
625 - To cut a tie: Union[object, Any] == Union[Any, object] == Any.
626
627 - You cannot subclass or instantiate a union.
628
629 - You cannot write Union[X][Y] (what would it mean?).
630
631 - You can use Optional[X] as a shorthand for Union[X, None].
632 """
633
634 # Unsubscripted Union type has params set to None.
635 __union_params__ = None
636 __union_set_params__ = None
637
638
639class OptionalMeta(TypingMeta):
640 """Metaclass for Optional."""
641
642 def __new__(cls, name, bases, namespace, _root=False):
643 return super().__new__(cls, name, bases, namespace, _root=_root)
644
645 def __getitem__(self, arg):
646 arg = _type_check(arg, "Optional[t] requires a single type.")
647 return Union[arg, type(None)]
648
649
650class Optional(Final, metaclass=OptionalMeta, _root=True):
651 """Optional type.
652
653 Optional[X] is equivalent to Union[X, type(None)].
654 """
655
Guido van Rossumd70fe632015-08-05 12:11:06 +0200656 __slots__ = ()
657
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700658
659class TupleMeta(TypingMeta):
660 """Metaclass for Tuple."""
661
662 def __new__(cls, name, bases, namespace, parameters=None,
663 use_ellipsis=False, _root=False):
664 self = super().__new__(cls, name, bases, namespace, _root=_root)
665 self.__tuple_params__ = parameters
666 self.__tuple_use_ellipsis__ = use_ellipsis
667 return self
668
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700669 def _get_type_vars(self, tvars):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700670 if self.__tuple_params__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700671 _get_type_vars(self.__tuple_params__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700672
673 def _eval_type(self, globalns, localns):
674 tp = self.__tuple_params__
675 if tp is None:
676 return self
677 p = tuple(_eval_type(t, globalns, localns) for t in tp)
678 if p == self.__tuple_params__:
679 return self
680 else:
681 return self.__class__(self.__name__, self.__bases__, {},
682 p, _root=True)
683
684 def __repr__(self):
685 r = super().__repr__()
686 if self.__tuple_params__ is not None:
687 params = [_type_repr(p) for p in self.__tuple_params__]
688 if self.__tuple_use_ellipsis__:
689 params.append('...')
Guido van Rossum91185fe2016-06-08 11:19:11 -0700690 if not params:
691 params.append('()')
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700692 r += '[%s]' % (
693 ', '.join(params))
694 return r
695
696 def __getitem__(self, parameters):
697 if self.__tuple_params__ is not None:
698 raise TypeError("Cannot re-parameterize %r" % (self,))
699 if not isinstance(parameters, tuple):
700 parameters = (parameters,)
701 if len(parameters) == 2 and parameters[1] == Ellipsis:
702 parameters = parameters[:1]
703 use_ellipsis = True
704 msg = "Tuple[t, ...]: t must be a type."
705 else:
706 use_ellipsis = False
707 msg = "Tuple[t0, t1, ...]: each t must be a type."
708 parameters = tuple(_type_check(p, msg) for p in parameters)
709 return self.__class__(self.__name__, self.__bases__,
710 dict(self.__dict__), parameters,
711 use_ellipsis=use_ellipsis, _root=True)
712
713 def __eq__(self, other):
714 if not isinstance(other, TupleMeta):
715 return NotImplemented
Guido van Rossum5abcbb32016-04-18 07:37:41 -0700716 return (self.__tuple_params__ == other.__tuple_params__ and
717 self.__tuple_use_ellipsis__ == other.__tuple_use_ellipsis__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700718
719 def __hash__(self):
720 return hash(self.__tuple_params__)
721
Guido van Rossumd70fe632015-08-05 12:11:06 +0200722 def __instancecheck__(self, obj):
723 raise TypeError("Tuples cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700724
725 def __subclasscheck__(self, cls):
726 if cls is Any:
727 return True
728 if not isinstance(cls, type):
729 return super().__subclasscheck__(cls) # To TypeError.
730 if issubclass(cls, tuple):
731 return True # Special case.
732 if not isinstance(cls, TupleMeta):
733 return super().__subclasscheck__(cls) # False.
734 if self.__tuple_params__ is None:
735 return True
736 if cls.__tuple_params__ is None:
737 return False # ???
738 if cls.__tuple_use_ellipsis__ != self.__tuple_use_ellipsis__:
739 return False
740 # Covariance.
741 return (len(self.__tuple_params__) == len(cls.__tuple_params__) and
742 all(issubclass(x, p)
743 for x, p in zip(cls.__tuple_params__,
744 self.__tuple_params__)))
745
746
747class Tuple(Final, metaclass=TupleMeta, _root=True):
748 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
749
750 Example: Tuple[T1, T2] is a tuple of two elements corresponding
751 to type variables T1 and T2. Tuple[int, float, str] is a tuple
752 of an int, a float and a string.
753
754 To specify a variable-length tuple of homogeneous type, use Sequence[T].
755 """
756
Guido van Rossumd70fe632015-08-05 12:11:06 +0200757 __slots__ = ()
758
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700759
760class CallableMeta(TypingMeta):
761 """Metaclass for Callable."""
762
763 def __new__(cls, name, bases, namespace, _root=False,
764 args=None, result=None):
765 if args is None and result is None:
766 pass # Must be 'class Callable'.
767 else:
768 if args is not Ellipsis:
769 if not isinstance(args, list):
770 raise TypeError("Callable[args, result]: "
771 "args must be a list."
772 " Got %.100r." % (args,))
773 msg = "Callable[[arg, ...], result]: each arg must be a type."
774 args = tuple(_type_check(arg, msg) for arg in args)
775 msg = "Callable[args, result]: result must be a type."
776 result = _type_check(result, msg)
777 self = super().__new__(cls, name, bases, namespace, _root=_root)
778 self.__args__ = args
779 self.__result__ = result
780 return self
781
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700782 def _get_type_vars(self, tvars):
Guido van Rossumefa798d2016-08-23 11:01:50 -0700783 if self.__args__ and self.__args__ is not Ellipsis:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700784 _get_type_vars(self.__args__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700785
786 def _eval_type(self, globalns, localns):
787 if self.__args__ is None and self.__result__ is None:
788 return self
Guido van Rossumd70fe632015-08-05 12:11:06 +0200789 if self.__args__ is Ellipsis:
790 args = self.__args__
791 else:
792 args = [_eval_type(t, globalns, localns) for t in self.__args__]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700793 result = _eval_type(self.__result__, globalns, localns)
794 if args == self.__args__ and result == self.__result__:
795 return self
796 else:
797 return self.__class__(self.__name__, self.__bases__, {},
798 args=args, result=result, _root=True)
799
800 def __repr__(self):
801 r = super().__repr__()
802 if self.__args__ is not None or self.__result__ is not None:
803 if self.__args__ is Ellipsis:
804 args_r = '...'
805 else:
806 args_r = '[%s]' % ', '.join(_type_repr(t)
807 for t in self.__args__)
808 r += '[%s, %s]' % (args_r, _type_repr(self.__result__))
809 return r
810
811 def __getitem__(self, parameters):
812 if self.__args__ is not None or self.__result__ is not None:
813 raise TypeError("This Callable type is already parameterized.")
814 if not isinstance(parameters, tuple) or len(parameters) != 2:
815 raise TypeError(
816 "Callable must be used as Callable[[arg, ...], result].")
817 args, result = parameters
818 return self.__class__(self.__name__, self.__bases__,
819 dict(self.__dict__), _root=True,
820 args=args, result=result)
821
822 def __eq__(self, other):
823 if not isinstance(other, CallableMeta):
824 return NotImplemented
825 return (self.__args__ == other.__args__ and
826 self.__result__ == other.__result__)
827
828 def __hash__(self):
829 return hash(self.__args__) ^ hash(self.__result__)
830
Guido van Rossumd70fe632015-08-05 12:11:06 +0200831 def __instancecheck__(self, obj):
832 # For unparametrized Callable we allow this, because
833 # typing.Callable should be equivalent to
834 # collections.abc.Callable.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700835 if self.__args__ is None and self.__result__ is None:
Guido van Rossumd70fe632015-08-05 12:11:06 +0200836 return isinstance(obj, collections_abc.Callable)
837 else:
838 raise TypeError("Callable[] cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700839
840 def __subclasscheck__(self, cls):
841 if cls is Any:
842 return True
843 if not isinstance(cls, CallableMeta):
844 return super().__subclasscheck__(cls)
845 if self.__args__ is None and self.__result__ is None:
846 return True
847 # We're not doing covariance or contravariance -- this is *invariance*.
848 return self == cls
849
850
851class Callable(Final, metaclass=CallableMeta, _root=True):
852 """Callable type; Callable[[int], str] is a function of (int) -> str.
853
854 The subscription syntax must always be used with exactly two
855 values: the argument list and the return type. The argument list
856 must be a list of types; the return type must be a single type.
857
858 There is no syntax to indicate optional or keyword arguments,
859 such function types are rarely used as callback types.
860 """
861
Guido van Rossumd70fe632015-08-05 12:11:06 +0200862 __slots__ = ()
863
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700864
865def _gorg(a):
866 """Return the farthest origin of a generic class."""
867 assert isinstance(a, GenericMeta)
868 while a.__origin__ is not None:
869 a = a.__origin__
870 return a
871
872
873def _geqv(a, b):
874 """Return whether two generic classes are equivalent.
875
876 The intention is to consider generic class X and any of its
877 parameterized forms (X[T], X[int], etc.) as equivalent.
878
879 However, X is not equivalent to a subclass of X.
880
881 The relation is reflexive, symmetric and transitive.
882 """
883 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
884 # Reduce each to its origin.
885 return _gorg(a) is _gorg(b)
886
887
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700888def _next_in_mro(cls):
889 """Helper for Generic.__new__.
890
891 Returns the class after the last occurrence of Generic or
892 Generic[...] in cls.__mro__.
893 """
894 next_in_mro = object
895 # Look for the last occurrence of Generic or Generic[...].
896 for i, c in enumerate(cls.__mro__[:-1]):
897 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
898 next_in_mro = cls.__mro__[i+1]
899 return next_in_mro
900
901
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700902class GenericMeta(TypingMeta, abc.ABCMeta):
903 """Metaclass for generic types."""
904
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700905 def __new__(cls, name, bases, namespace,
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700906 tvars=None, args=None, origin=None, extra=None):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700907 self = super().__new__(cls, name, bases, namespace, _root=True)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700908
909 if tvars is not None:
910 # Called from __getitem__() below.
911 assert origin is not None
912 assert all(isinstance(t, TypeVar) for t in tvars), tvars
913 else:
914 # Called from class statement.
915 assert tvars is None, tvars
916 assert args is None, args
917 assert origin is None, origin
918
919 # Get the full set of tvars from the bases.
920 tvars = _type_vars(bases)
921 # Look for Generic[T1, ..., Tn].
922 # If found, tvars must be a subset of it.
923 # If not found, tvars is it.
924 # Also check for and reject plain Generic,
925 # and reject multiple Generic[...].
926 gvars = None
927 for base in bases:
928 if base is Generic:
929 raise TypeError("Cannot inherit from plain Generic")
930 if (isinstance(base, GenericMeta) and
931 base.__origin__ is Generic):
932 if gvars is not None:
933 raise TypeError(
934 "Cannot inherit from Generic[...] multiple types.")
935 gvars = base.__parameters__
936 if gvars is None:
937 gvars = tvars
938 else:
939 tvarset = set(tvars)
940 gvarset = set(gvars)
941 if not tvarset <= gvarset:
942 raise TypeError(
943 "Some type variables (%s) "
944 "are not listed in Generic[%s]" %
945 (", ".join(str(t) for t in tvars if t not in gvarset),
946 ", ".join(str(g) for g in gvars)))
947 tvars = gvars
948
949 self.__parameters__ = tvars
950 self.__args__ = args
951 self.__origin__ = origin
Guido van Rossum1cea70f2016-05-18 08:35:00 -0700952 self.__extra__ = extra
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700953 # Speed hack (https://github.com/python/typing/issues/196).
954 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700955 return self
956
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700957 def _get_type_vars(self, tvars):
958 if self.__origin__ and self.__parameters__:
959 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700960
961 def __repr__(self):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700962 if self.__origin__ is not None:
963 r = repr(self.__origin__)
964 else:
965 r = super().__repr__()
966 if self.__args__:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700967 r += '[%s]' % (
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700968 ', '.join(_type_repr(p) for p in self.__args__))
969 if self.__parameters__:
970 r += '<%s>' % (
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700971 ', '.join(_type_repr(p) for p in self.__parameters__))
972 return r
973
974 def __eq__(self, other):
975 if not isinstance(other, GenericMeta):
976 return NotImplemented
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700977 if self.__origin__ is not None:
978 return (self.__origin__ is other.__origin__ and
979 self.__args__ == other.__args__ and
980 self.__parameters__ == other.__parameters__)
981 else:
982 return self is other
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700983
984 def __hash__(self):
985 return hash((self.__name__, self.__parameters__))
986
987 def __getitem__(self, params):
988 if not isinstance(params, tuple):
989 params = (params,)
990 if not params:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700991 raise TypeError(
992 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700993 msg = "Parameters to generic types must be types."
994 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700995 if self is Generic:
996 # Generic can only be subscripted with unique type variables.
997 if not all(isinstance(p, TypeVar) for p in params):
998 raise TypeError(
999 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +02001000 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -07001001 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001002 "Parameters to Generic[...] must all be unique")
1003 tvars = params
1004 args = None
1005 elif self is _Protocol:
1006 # _Protocol is internal, don't check anything.
1007 tvars = params
1008 args = None
1009 elif self.__origin__ in (Generic, _Protocol):
1010 # Can't subscript Generic[...] or _Protocol[...].
1011 raise TypeError("Cannot subscript already-subscripted %s" %
1012 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001013 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001014 # Subscripting a regular Generic subclass.
1015 if not self.__parameters__:
1016 raise TypeError("%s is not a generic class" % repr(self))
1017 alen = len(params)
1018 elen = len(self.__parameters__)
1019 if alen != elen:
1020 raise TypeError(
1021 "Too %s parameters for %s; actual %s, expected %s" %
1022 ("many" if alen > elen else "few", repr(self), alen, elen))
1023 tvars = _type_vars(params)
1024 args = params
1025 return self.__class__(self.__name__,
1026 (self,) + self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001027 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001028 tvars=tvars,
1029 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001030 origin=self,
1031 extra=self.__extra__)
1032
Guido van Rossum1b669102015-09-04 12:15:54 -07001033 def __instancecheck__(self, instance):
1034 # Since we extend ABC.__subclasscheck__ and
1035 # ABC.__instancecheck__ inlines the cache checking done by the
1036 # latter, we must extend __instancecheck__ too. For simplicity
1037 # we just skip the cache check -- instance checks for generic
1038 # classes are supposed to be rare anyways.
1039 return self.__subclasscheck__(instance.__class__)
1040
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001041 def __subclasscheck__(self, cls):
1042 if cls is Any:
1043 return True
1044 if isinstance(cls, GenericMeta):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001045 # For a covariant class C(Generic[T]),
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001046 # C[X] is a subclass of C[Y] iff X is a subclass of Y.
1047 origin = self.__origin__
1048 if origin is not None and origin is cls.__origin__:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001049 assert len(self.__args__) == len(origin.__parameters__)
1050 assert len(cls.__args__) == len(origin.__parameters__)
1051 for p_self, p_cls, p_origin in zip(self.__args__,
1052 cls.__args__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001053 origin.__parameters__):
1054 if isinstance(p_origin, TypeVar):
1055 if p_origin.__covariant__:
1056 # Covariant -- p_cls must be a subclass of p_self.
1057 if not issubclass(p_cls, p_self):
1058 break
1059 elif p_origin.__contravariant__:
1060 # Contravariant. I think it's the opposite. :-)
1061 if not issubclass(p_self, p_cls):
1062 break
1063 else:
1064 # Invariant -- p_cls and p_self must equal.
1065 if p_self != p_cls:
1066 break
1067 else:
1068 # If the origin's parameter is not a typevar,
1069 # insist on invariance.
1070 if p_self != p_cls:
1071 break
1072 else:
1073 return True
1074 # If we break out of the loop, the superclass gets a chance.
1075 if super().__subclasscheck__(cls):
1076 return True
1077 if self.__extra__ is None or isinstance(cls, GenericMeta):
1078 return False
1079 return issubclass(cls, self.__extra__)
1080
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001081
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001082# Prevent checks for Generic to crash when defining Generic.
1083Generic = None
1084
1085
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001086class Generic(metaclass=GenericMeta):
1087 """Abstract base class for generic types.
1088
1089 A generic type is typically declared by inheriting from an
1090 instantiation of this class with one or more type variables.
1091 For example, a generic mapping type might be defined as::
1092
1093 class Mapping(Generic[KT, VT]):
1094 def __getitem__(self, key: KT) -> VT:
1095 ...
1096 # Etc.
1097
1098 This class can then be used as follows::
1099
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001100 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001101 try:
1102 return mapping[key]
1103 except KeyError:
1104 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001105 """
1106
Guido van Rossumd70fe632015-08-05 12:11:06 +02001107 __slots__ = ()
1108
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001109 def __new__(cls, *args, **kwds):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001110 if cls.__origin__ is None:
1111 return cls.__next_in_mro__.__new__(cls)
1112 else:
1113 origin = _gorg(cls)
1114 obj = cls.__next_in_mro__.__new__(origin)
1115 obj.__init__(*args, **kwds)
1116 return obj
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001117
1118
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001119class _ClassVar(metaclass=TypingMeta, _root=True):
1120 """Special type construct to mark class variables.
1121
1122 An annotation wrapped in ClassVar indicates that a given
1123 attribute is intended to be used as a class variable and
1124 should not be set on instances of that class. Usage::
1125
1126 class Starship:
1127 stats: ClassVar[Dict[str, int]] = {} # class variable
1128 damage: int = 10 # instance variable
1129
1130 ClassVar accepts only types and cannot be further subscribed.
1131
1132 Note that ClassVar is not a class itself, and should not
1133 be used with isinstance() or issubclass().
1134 """
1135
1136 def __init__(self, tp=None, _root=False):
1137 cls = type(self)
1138 if _root:
1139 self.__type__ = tp
1140 else:
1141 raise TypeError('Cannot initialize {}'.format(cls.__name__[1:]))
1142
1143 def __getitem__(self, item):
1144 cls = type(self)
1145 if self.__type__ is None:
1146 return cls(_type_check(item,
1147 '{} accepts only types.'.format(cls.__name__[1:])),
1148 _root=True)
1149 raise TypeError('{} cannot be further subscripted'
1150 .format(cls.__name__[1:]))
1151
1152 def _eval_type(self, globalns, localns):
1153 return type(self)(_eval_type(self.__type__, globalns, localns),
1154 _root=True)
1155
1156 def _get_type_vars(self, tvars):
1157 if self.__type__:
1158 _get_type_vars(self.__type__, tvars)
1159
1160 def __repr__(self):
1161 cls = type(self)
1162 if not self.__type__:
1163 return '{}.{}'.format(cls.__module__, cls.__name__[1:])
1164 return '{}.{}[{}]'.format(cls.__module__, cls.__name__[1:],
1165 _type_repr(self.__type__))
1166
1167 def __hash__(self):
1168 return hash((type(self).__name__, self.__type__))
1169
1170 def __eq__(self, other):
1171 if not isinstance(other, _ClassVar):
1172 return NotImplemented
1173 if self.__type__ is not None:
1174 return self.__type__ == other.__type__
1175 return self is other
1176
1177ClassVar = _ClassVar(_root=True)
1178
1179
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001180def cast(typ, val):
1181 """Cast a value to a type.
1182
1183 This returns the value unchanged. To the type checker this
1184 signals that the return value has the designated type, but at
1185 runtime we intentionally don't check anything (we want this
1186 to be as fast as possible).
1187 """
1188 return val
1189
1190
1191def _get_defaults(func):
1192 """Internal helper to extract the default arguments, by name."""
1193 code = func.__code__
1194 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001195 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001196 arg_names = arg_names[:pos_count]
1197 defaults = func.__defaults__ or ()
1198 kwdefaults = func.__kwdefaults__
1199 res = dict(kwdefaults) if kwdefaults else {}
1200 pos_offset = pos_count - len(defaults)
1201 for name, value in zip(arg_names[pos_offset:], defaults):
1202 assert name not in res
1203 res[name] = value
1204 return res
1205
1206
1207def get_type_hints(obj, globalns=None, localns=None):
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001208 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001209
1210 This is often the same as obj.__annotations__, but it handles
1211 forward references encoded as string literals, and if necessary
1212 adds Optional[t] if a default value equal to None is set.
1213
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001214 The argument may be a module, class, method, or function. The annotations
1215 are returned as a dictionary, or in the case of a class, a ChainMap of
1216 dictionaries.
1217
1218 TypeError is raised if the argument is not of a type that can contain
1219 annotations, and an empty dictionary is returned if no annotations are
1220 present.
1221
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001222 BEWARE -- the behavior of globalns and localns is counterintuitive
1223 (unless you are familiar with how eval() and exec() work). The
1224 search order is locals first, then globals.
1225
1226 - If no dict arguments are passed, an attempt is made to use the
1227 globals from obj, and these are also used as the locals. If the
1228 object does not appear to have globals, an exception is raised.
1229
1230 - If one dict argument is passed, it is used for both globals and
1231 locals.
1232
1233 - If two dict arguments are passed, they specify globals and
1234 locals, respectively.
1235 """
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001236
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001237 if getattr(obj, '__no_type_check__', None):
1238 return {}
1239 if globalns is None:
1240 globalns = getattr(obj, '__globals__', {})
1241 if localns is None:
1242 localns = globalns
1243 elif localns is None:
1244 localns = globalns
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001245
1246 if (isinstance(obj, types.FunctionType) or
1247 isinstance(obj, types.BuiltinFunctionType) or
1248 isinstance(obj, types.MethodType)):
1249 defaults = _get_defaults(obj)
1250 hints = obj.__annotations__
1251 for name, value in hints.items():
1252 if value is None:
1253 value = type(None)
1254 if isinstance(value, str):
1255 value = _ForwardRef(value)
1256 value = _eval_type(value, globalns, localns)
1257 if name in defaults and defaults[name] is None:
1258 value = Optional[value]
1259 hints[name] = value
1260 return hints
1261
1262 if isinstance(obj, types.ModuleType):
1263 try:
1264 hints = obj.__annotations__
1265 except AttributeError:
1266 return {}
1267 # we keep only those annotations that can be accessed on module
1268 members = obj.__dict__
1269 hints = {name: value for name, value in hints.items()
1270 if name in members}
1271 for name, value in hints.items():
1272 if value is None:
1273 value = type(None)
1274 if isinstance(value, str):
1275 value = _ForwardRef(value)
1276 value = _eval_type(value, globalns, localns)
1277 hints[name] = value
1278 return hints
1279
1280 if isinstance(object, type):
1281 cmap = None
1282 for base in reversed(obj.__mro__):
1283 new_map = collections.ChainMap if cmap is None else cmap.new_child
1284 try:
1285 hints = base.__dict__['__annotations__']
1286 except KeyError:
1287 cmap = new_map()
1288 else:
1289 for name, value in hints.items():
1290 if value is None:
1291 value = type(None)
1292 if isinstance(value, str):
1293 value = _ForwardRef(value)
1294 value = _eval_type(value, globalns, localns)
1295 hints[name] = value
1296 cmap = new_map(hints)
1297 return cmap
1298
1299 raise TypeError('{!r} is not a module, class, method, '
1300 'or function.'.format(obj))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001301
1302
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001303def no_type_check(arg):
1304 """Decorator to indicate that annotations are not type hints.
1305
1306 The argument must be a class or function; if it is a class, it
1307 applies recursively to all methods defined in that class (but not
1308 to methods defined in its superclasses or subclasses).
1309
1310 This mutates the function(s) in place.
1311 """
1312 if isinstance(arg, type):
1313 for obj in arg.__dict__.values():
1314 if isinstance(obj, types.FunctionType):
1315 obj.__no_type_check__ = True
1316 else:
1317 arg.__no_type_check__ = True
1318 return arg
1319
1320
1321def no_type_check_decorator(decorator):
1322 """Decorator to give another decorator the @no_type_check effect.
1323
1324 This wraps the decorator with something that wraps the decorated
1325 function in @no_type_check.
1326 """
1327
1328 @functools.wraps(decorator)
1329 def wrapped_decorator(*args, **kwds):
1330 func = decorator(*args, **kwds)
1331 func = no_type_check(func)
1332 return func
1333
1334 return wrapped_decorator
1335
1336
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001337def _overload_dummy(*args, **kwds):
1338 """Helper for @overload to raise when called."""
1339 raise NotImplementedError(
1340 "You should not call an overloaded function. "
1341 "A series of @overload-decorated functions "
1342 "outside a stub module should always be followed "
1343 "by an implementation that is not @overload-ed.")
1344
1345
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001346def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001347 """Decorator for overloaded functions/methods.
1348
1349 In a stub file, place two or more stub definitions for the same
1350 function in a row, each decorated with @overload. For example:
1351
1352 @overload
1353 def utf8(value: None) -> None: ...
1354 @overload
1355 def utf8(value: bytes) -> bytes: ...
1356 @overload
1357 def utf8(value: str) -> bytes: ...
1358
1359 In a non-stub file (i.e. a regular .py file), do the same but
1360 follow it with an implementation. The implementation should *not*
1361 be decorated with @overload. For example:
1362
1363 @overload
1364 def utf8(value: None) -> None: ...
1365 @overload
1366 def utf8(value: bytes) -> bytes: ...
1367 @overload
1368 def utf8(value: str) -> bytes: ...
1369 def utf8(value):
1370 # implementation goes here
1371 """
1372 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001373
1374
1375class _ProtocolMeta(GenericMeta):
1376 """Internal metaclass for _Protocol.
1377
1378 This exists so _Protocol classes can be generic without deriving
1379 from Generic.
1380 """
1381
Guido van Rossumd70fe632015-08-05 12:11:06 +02001382 def __instancecheck__(self, obj):
1383 raise TypeError("Protocols cannot be used with isinstance().")
1384
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001385 def __subclasscheck__(self, cls):
1386 if not self._is_protocol:
1387 # No structural checks since this isn't a protocol.
1388 return NotImplemented
1389
1390 if self is _Protocol:
1391 # Every class is a subclass of the empty protocol.
1392 return True
1393
1394 # Find all attributes defined in the protocol.
1395 attrs = self._get_protocol_attrs()
1396
1397 for attr in attrs:
1398 if not any(attr in d.__dict__ for d in cls.__mro__):
1399 return False
1400 return True
1401
1402 def _get_protocol_attrs(self):
1403 # Get all Protocol base classes.
1404 protocol_bases = []
1405 for c in self.__mro__:
1406 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1407 protocol_bases.append(c)
1408
1409 # Get attributes included in protocol.
1410 attrs = set()
1411 for base in protocol_bases:
1412 for attr in base.__dict__.keys():
1413 # Include attributes not defined in any non-protocol bases.
1414 for c in self.__mro__:
1415 if (c is not base and attr in c.__dict__ and
1416 not getattr(c, '_is_protocol', False)):
1417 break
1418 else:
1419 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001420 attr != '__abstractmethods__' and
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001421 attr != '__annotations__' and
1422 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001423 attr != '_is_protocol' and
1424 attr != '__dict__' and
1425 attr != '__args__' and
1426 attr != '__slots__' and
1427 attr != '_get_protocol_attrs' and
1428 attr != '__next_in_mro__' and
1429 attr != '__parameters__' and
1430 attr != '__origin__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001431 attr != '__extra__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001432 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001433 attrs.add(attr)
1434
1435 return attrs
1436
1437
1438class _Protocol(metaclass=_ProtocolMeta):
1439 """Internal base class for protocol classes.
1440
1441 This implements a simple-minded structural isinstance check
1442 (similar but more general than the one-offs in collections.abc
1443 such as Hashable).
1444 """
1445
Guido van Rossumd70fe632015-08-05 12:11:06 +02001446 __slots__ = ()
1447
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001448 _is_protocol = True
1449
1450
1451# Various ABCs mimicking those in collections.abc.
1452# A few are simply re-exported for completeness.
1453
1454Hashable = collections_abc.Hashable # Not generic.
1455
1456
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001457if hasattr(collections_abc, 'Awaitable'):
1458 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1459 __slots__ = ()
1460else:
1461 Awaitable = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001462
1463
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001464if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001465
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001466 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1467 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001468
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001469 class AsyncIterator(AsyncIterable[T_co],
1470 extra=collections_abc.AsyncIterator):
1471 __slots__ = ()
1472
1473else:
1474 AsyncIterable = None
1475 AsyncIterator = None
Guido van Rossumf17c2002015-12-03 17:31:24 -08001476
1477
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001478class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001479 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001480
1481
1482class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001483 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001484
1485
1486class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001487 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001488
1489 @abstractmethod
1490 def __int__(self) -> int:
1491 pass
1492
1493
1494class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001495 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001496
1497 @abstractmethod
1498 def __float__(self) -> float:
1499 pass
1500
1501
1502class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001503 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001504
1505 @abstractmethod
1506 def __complex__(self) -> complex:
1507 pass
1508
1509
1510class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001511 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001512
1513 @abstractmethod
1514 def __bytes__(self) -> bytes:
1515 pass
1516
1517
Guido van Rossumd70fe632015-08-05 12:11:06 +02001518class SupportsAbs(_Protocol[T_co]):
1519 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001520
1521 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001522 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001523 pass
1524
1525
Guido van Rossumd70fe632015-08-05 12:11:06 +02001526class SupportsRound(_Protocol[T_co]):
1527 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001528
1529 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001530 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001531 pass
1532
1533
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001534if hasattr(collections_abc, 'Reversible'):
1535 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1536 __slots__ = ()
1537else:
1538 class Reversible(_Protocol[T_co]):
1539 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001540
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001541 @abstractmethod
1542 def __reversed__(self) -> 'Iterator[T_co]':
1543 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001544
1545
1546Sized = collections_abc.Sized # Not generic.
1547
1548
1549class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001550 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001551
1552
Guido van Rossumefa798d2016-08-23 11:01:50 -07001553if hasattr(collections_abc, 'Collection'):
1554 class Collection(Sized, Iterable[T_co], Container[T_co],
1555 extra=collections_abc.Collection):
1556 __slots__ = ()
1557
1558 __all__.append('Collection')
1559
1560
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001561# Callable was defined earlier.
1562
Guido van Rossumefa798d2016-08-23 11:01:50 -07001563if hasattr(collections_abc, 'Collection'):
1564 class AbstractSet(Collection[T_co],
1565 extra=collections_abc.Set):
1566 pass
1567else:
1568 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1569 extra=collections_abc.Set):
1570 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001571
1572
1573class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
1574 pass
1575
1576
Guido van Rossumefa798d2016-08-23 11:01:50 -07001577# NOTE: It is only covariant in the value type.
1578if hasattr(collections_abc, 'Collection'):
1579 class Mapping(Collection[KT], Generic[KT, VT_co],
1580 extra=collections_abc.Mapping):
1581 pass
1582else:
1583 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1584 extra=collections_abc.Mapping):
1585 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001586
1587
1588class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
1589 pass
1590
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001591if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001592 if hasattr(collections_abc, 'Collection'):
1593 class Sequence(Reversible[T_co], Collection[T_co],
1594 extra=collections_abc.Sequence):
1595 pass
1596 else:
1597 class Sequence(Sized, Reversible[T_co], Container[T_co],
1598 extra=collections_abc.Sequence):
1599 pass
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001600else:
1601 class Sequence(Sized, Iterable[T_co], Container[T_co],
1602 extra=collections_abc.Sequence):
1603 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001604
1605
1606class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
1607 pass
1608
1609
1610class ByteString(Sequence[int], extra=collections_abc.ByteString):
1611 pass
1612
1613
1614ByteString.register(type(memoryview(b'')))
1615
1616
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001617class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001618
1619 def __new__(cls, *args, **kwds):
1620 if _geqv(cls, List):
1621 raise TypeError("Type List cannot be instantiated; "
1622 "use list() instead")
1623 return list.__new__(cls, *args, **kwds)
1624
1625
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001626class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001627
1628 def __new__(cls, *args, **kwds):
1629 if _geqv(cls, Set):
1630 raise TypeError("Type Set cannot be instantiated; "
1631 "use set() instead")
1632 return set.__new__(cls, *args, **kwds)
1633
1634
Guido van Rossumd70fe632015-08-05 12:11:06 +02001635class _FrozenSetMeta(GenericMeta):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001636 """This metaclass ensures set is not a subclass of FrozenSet.
1637
1638 Without this metaclass, set would be considered a subclass of
1639 FrozenSet, because FrozenSet.__extra__ is collections.abc.Set, and
1640 set is a subclass of that.
1641 """
1642
1643 def __subclasscheck__(self, cls):
1644 if issubclass(cls, Set):
1645 return False
1646 return super().__subclasscheck__(cls)
1647
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001648
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001649class FrozenSet(frozenset, AbstractSet[T_co], metaclass=_FrozenSetMeta,
1650 extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001651 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001652
1653 def __new__(cls, *args, **kwds):
1654 if _geqv(cls, FrozenSet):
1655 raise TypeError("Type FrozenSet cannot be instantiated; "
1656 "use frozenset() instead")
1657 return frozenset.__new__(cls, *args, **kwds)
1658
1659
1660class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
1661 pass
1662
1663
Guido van Rossumd70fe632015-08-05 12:11:06 +02001664class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001665 extra=collections_abc.KeysView):
1666 pass
1667
1668
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001669class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001670 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001671 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001672 extra=collections_abc.ItemsView):
1673 pass
1674
1675
1676class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
1677 pass
1678
1679
Brett Cannonf3ad0422016-04-15 10:51:30 -07001680if hasattr(contextlib, 'AbstractContextManager'):
1681 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1682 __slots__ = ()
1683 __all__.append('ContextManager')
1684
1685
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001686class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001687
1688 def __new__(cls, *args, **kwds):
1689 if _geqv(cls, Dict):
1690 raise TypeError("Type Dict cannot be instantiated; "
1691 "use dict() instead")
1692 return dict.__new__(cls, *args, **kwds)
1693
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001694class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
1695 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001696
1697 def __new__(cls, *args, **kwds):
1698 if _geqv(cls, DefaultDict):
1699 raise TypeError("Type DefaultDict cannot be instantiated; "
1700 "use collections.defaultdict() instead")
1701 return collections.defaultdict.__new__(cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001702
1703# Determine what base class to use for Generator.
1704if hasattr(collections_abc, 'Generator'):
1705 # Sufficiently recent versions of 3.5 have a Generator ABC.
1706 _G_base = collections_abc.Generator
1707else:
1708 # Fall back on the exact type.
1709 _G_base = types.GeneratorType
1710
1711
1712class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1713 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001714 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001715
1716 def __new__(cls, *args, **kwds):
1717 if _geqv(cls, Generator):
1718 raise TypeError("Type Generator cannot be instantiated; "
1719 "create a subclass instead")
1720 return super().__new__(cls, *args, **kwds)
1721
1722
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001723# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07001724CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001725
1726
Guido van Rossumb22c7082016-05-26 09:56:19 -07001727# This is not a real generic class. Don't use outside annotations.
Guido van Rossumefa798d2016-08-23 11:01:50 -07001728class Type(type, Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07001729 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001730
1731 For example, suppose we have the following classes::
1732
1733 class User: ... # Abstract base for User classes
1734 class BasicUser(User): ...
1735 class ProUser(User): ...
1736 class TeamUser(User): ...
1737
1738 And a function that takes a class argument that's a subclass of
1739 User and returns an instance of the corresponding class::
1740
1741 U = TypeVar('U', bound=User)
1742 def new_user(user_class: Type[U]) -> U:
1743 user = user_class()
1744 # (Here we could write the user object to a database)
1745 return user
1746
1747 joe = new_user(BasicUser)
1748
1749 At this point the type checker knows that joe has type BasicUser.
1750 """
1751
1752
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001753def NamedTuple(typename, fields):
1754 """Typed version of namedtuple.
1755
1756 Usage::
1757
1758 Employee = typing.NamedTuple('Employee', [('name', str), 'id', int)])
1759
1760 This is equivalent to::
1761
1762 Employee = collections.namedtuple('Employee', ['name', 'id'])
1763
1764 The resulting class has one extra attribute: _field_types,
1765 giving a dict mapping field names to types. (The field names
1766 are in the _fields attribute, which is part of the namedtuple
1767 API.)
1768 """
1769 fields = [(n, t) for n, t in fields]
1770 cls = collections.namedtuple(typename, [n for n, t in fields])
1771 cls._field_types = dict(fields)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001772 # Set the module to the caller's module (otherwise it'd be 'typing').
1773 try:
1774 cls.__module__ = sys._getframe(1).f_globals.get('__name__', '__main__')
1775 except (AttributeError, ValueError):
1776 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001777 return cls
1778
1779
Guido van Rossum91185fe2016-06-08 11:19:11 -07001780def NewType(name, tp):
1781 """NewType creates simple unique types with almost zero
1782 runtime overhead. NewType(name, tp) is considered a subtype of tp
1783 by static type checkers. At runtime, NewType(name, tp) returns
1784 a dummy function that simply returns its argument. Usage::
1785
1786 UserId = NewType('UserId', int)
1787
1788 def name_by_id(user_id: UserId) -> str:
1789 ...
1790
1791 UserId('user') # Fails type check
1792
1793 name_by_id(42) # Fails type check
1794 name_by_id(UserId(42)) # OK
1795
1796 num = UserId(5) + 1 # type: int
1797 """
1798
1799 def new_type(x):
1800 return x
1801
1802 new_type.__name__ = name
1803 new_type.__supertype__ = tp
1804 return new_type
1805
1806
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001807# Python-version-specific alias (Python 2: unicode; Python 3: str)
1808Text = str
1809
1810
Guido van Rossum91185fe2016-06-08 11:19:11 -07001811# Constant that's True when type checking, but False here.
1812TYPE_CHECKING = False
1813
1814
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001815class IO(Generic[AnyStr]):
1816 """Generic base class for TextIO and BinaryIO.
1817
1818 This is an abstract, generic version of the return of open().
1819
1820 NOTE: This does not distinguish between the different possible
1821 classes (text vs. binary, read vs. write vs. read/write,
1822 append-only, unbuffered). The TextIO and BinaryIO subclasses
1823 below capture the distinctions between text vs. binary, which is
1824 pervasive in the interface; however we currently do not offer a
1825 way to track the other distinctions in the type system.
1826 """
1827
Guido van Rossumd70fe632015-08-05 12:11:06 +02001828 __slots__ = ()
1829
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001830 @abstractproperty
1831 def mode(self) -> str:
1832 pass
1833
1834 @abstractproperty
1835 def name(self) -> str:
1836 pass
1837
1838 @abstractmethod
1839 def close(self) -> None:
1840 pass
1841
1842 @abstractmethod
1843 def closed(self) -> bool:
1844 pass
1845
1846 @abstractmethod
1847 def fileno(self) -> int:
1848 pass
1849
1850 @abstractmethod
1851 def flush(self) -> None:
1852 pass
1853
1854 @abstractmethod
1855 def isatty(self) -> bool:
1856 pass
1857
1858 @abstractmethod
1859 def read(self, n: int = -1) -> AnyStr:
1860 pass
1861
1862 @abstractmethod
1863 def readable(self) -> bool:
1864 pass
1865
1866 @abstractmethod
1867 def readline(self, limit: int = -1) -> AnyStr:
1868 pass
1869
1870 @abstractmethod
1871 def readlines(self, hint: int = -1) -> List[AnyStr]:
1872 pass
1873
1874 @abstractmethod
1875 def seek(self, offset: int, whence: int = 0) -> int:
1876 pass
1877
1878 @abstractmethod
1879 def seekable(self) -> bool:
1880 pass
1881
1882 @abstractmethod
1883 def tell(self) -> int:
1884 pass
1885
1886 @abstractmethod
1887 def truncate(self, size: int = None) -> int:
1888 pass
1889
1890 @abstractmethod
1891 def writable(self) -> bool:
1892 pass
1893
1894 @abstractmethod
1895 def write(self, s: AnyStr) -> int:
1896 pass
1897
1898 @abstractmethod
1899 def writelines(self, lines: List[AnyStr]) -> None:
1900 pass
1901
1902 @abstractmethod
1903 def __enter__(self) -> 'IO[AnyStr]':
1904 pass
1905
1906 @abstractmethod
1907 def __exit__(self, type, value, traceback) -> None:
1908 pass
1909
1910
1911class BinaryIO(IO[bytes]):
1912 """Typed version of the return of open() in binary mode."""
1913
Guido van Rossumd70fe632015-08-05 12:11:06 +02001914 __slots__ = ()
1915
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001916 @abstractmethod
1917 def write(self, s: Union[bytes, bytearray]) -> int:
1918 pass
1919
1920 @abstractmethod
1921 def __enter__(self) -> 'BinaryIO':
1922 pass
1923
1924
1925class TextIO(IO[str]):
1926 """Typed version of the return of open() in text mode."""
1927
Guido van Rossumd70fe632015-08-05 12:11:06 +02001928 __slots__ = ()
1929
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001930 @abstractproperty
1931 def buffer(self) -> BinaryIO:
1932 pass
1933
1934 @abstractproperty
1935 def encoding(self) -> str:
1936 pass
1937
1938 @abstractproperty
1939 def errors(self) -> str:
1940 pass
1941
1942 @abstractproperty
1943 def line_buffering(self) -> bool:
1944 pass
1945
1946 @abstractproperty
1947 def newlines(self) -> Any:
1948 pass
1949
1950 @abstractmethod
1951 def __enter__(self) -> 'TextIO':
1952 pass
1953
1954
1955class io:
1956 """Wrapper namespace for IO generic classes."""
1957
1958 __all__ = ['IO', 'TextIO', 'BinaryIO']
1959 IO = IO
1960 TextIO = TextIO
1961 BinaryIO = BinaryIO
1962
1963io.__name__ = __name__ + '.io'
1964sys.modules[io.__name__] = io
1965
1966
1967Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
1968 lambda p: p.pattern)
1969Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
1970 lambda m: m.re.pattern)
1971
1972
1973class re:
1974 """Wrapper namespace for re type aliases."""
1975
1976 __all__ = ['Pattern', 'Match']
1977 Pattern = Pattern
1978 Match = Match
1979
1980re.__name__ = __name__ + '.re'
1981sys.modules[re.__name__] = re