blob: fe22b2b5e8e875106c3b1f85d27ae01efd87c42e [file] [log] [blame]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001import abc
2from abc import abstractmethod, abstractproperty
3import collections
Brett Cannonf3ad0422016-04-15 10:51:30 -07004import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07005import functools
6import re as stdlib_re # Avoid confusion with the re we export.
7import sys
8import types
9try:
10 import collections.abc as collections_abc
11except ImportError:
12 import collections as collections_abc # Fallback for PY3.2.
13
14
15# Please keep __all__ alphabetized within each category.
16__all__ = [
17 # Super-special typing primitives.
18 'Any',
19 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070020 'ClassVar',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070021 'Generic',
22 'Optional',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070023 'Tuple',
24 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070025 'TypeVar',
26 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070027
28 # ABCs (from collections.abc).
29 'AbstractSet', # collections.abc.Set.
30 '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',
Guido van Rossum62fe1bb2016-10-29 16:05:26 -070045 # The following are added depending on presence
46 # of their non-generic counterparts in stdlib:
47 # Awaitable,
48 # AsyncIterator,
49 # AsyncIterable,
50 # Coroutine,
51 # Collection,
52 # ContextManager
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070053
54 # Structural checks, a.k.a. protocols.
55 'Reversible',
56 'SupportsAbs',
57 'SupportsFloat',
58 'SupportsInt',
59 'SupportsRound',
60
61 # Concrete collection types.
62 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070063 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070064 'List',
65 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070066 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070067 'NamedTuple', # Not really a type.
68 'Generator',
69
70 # One-off things.
71 'AnyStr',
72 'cast',
73 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -070074 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070075 'no_type_check',
76 'no_type_check_decorator',
77 'overload',
Guido van Rossum0e0563c2016-04-05 14:54:25 -070078 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -070079 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070080]
81
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070082# The pseudo-submodules 're' and 'io' are part of the public
83# namespace, but excluded from __all__ because they might stomp on
84# legitimate imports of those modules.
85
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070086
87def _qualname(x):
88 if sys.version_info[:2] >= (3, 3):
89 return x.__qualname__
90 else:
91 # Fall back to just name.
92 return x.__name__
93
94
Guido van Rossum4cefe742016-09-27 15:20:12 -070095def _trim_name(nm):
96 if nm.startswith('_') and nm not in ('_TypeAlias',
97 '_ForwardRef', '_TypingBase', '_FinalTypingBase'):
98 nm = nm[1:]
99 return nm
100
101
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700102class TypingMeta(type):
103 """Metaclass for every type defined below.
104
105 This overrides __new__() to require an extra keyword parameter
106 '_root', which serves as a guard against naive subclassing of the
107 typing classes. Any legitimate class defined using a metaclass
108 derived from TypingMeta (including internal subclasses created by
109 e.g. Union[X, Y]) must pass _root=True.
110
111 This also defines a dummy constructor (all the work is done in
112 __new__) and a nicer repr().
113 """
114
115 _is_protocol = False
116
117 def __new__(cls, name, bases, namespace, *, _root=False):
118 if not _root:
119 raise TypeError("Cannot subclass %s" %
120 (', '.join(map(_type_repr, bases)) or '()'))
121 return super().__new__(cls, name, bases, namespace)
122
123 def __init__(self, *args, **kwds):
124 pass
125
126 def _eval_type(self, globalns, localns):
127 """Override this in subclasses to interpret forward references.
128
129 For example, Union['C'] is internally stored as
130 Union[_ForwardRef('C')], which should evaluate to _Union[C],
131 where C is an object found in globalns or localns (searching
132 localns first, of course).
133 """
134 return self
135
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700136 def _get_type_vars(self, tvars):
137 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700138
139 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700140 qname = _trim_name(_qualname(self))
141 return '%s.%s' % (self.__module__, qname)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700142
143
Guido van Rossum4cefe742016-09-27 15:20:12 -0700144class _TypingBase(metaclass=TypingMeta, _root=True):
145 """Indicator of special typing constructs."""
146
147 __slots__ = ()
148
Guido van Rossum4cefe742016-09-27 15:20:12 -0700149 def __init__(self, *args, **kwds):
150 pass
151
152 def __new__(cls, *args, **kwds):
153 """Constructor.
154
155 This only exists to give a better error message in case
156 someone tries to subclass a special typing object (not a good idea).
157 """
158 if (len(args) == 3 and
159 isinstance(args[0], str) and
160 isinstance(args[1], tuple)):
161 # Close enough.
162 raise TypeError("Cannot subclass %r" % cls)
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700163 return super().__new__(cls)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700164
165 # Things that are not classes also need these.
166 def _eval_type(self, globalns, localns):
167 return self
168
169 def _get_type_vars(self, tvars):
170 pass
171
172 def __repr__(self):
173 cls = type(self)
174 qname = _trim_name(_qualname(cls))
175 return '%s.%s' % (cls.__module__, qname)
176
177 def __call__(self, *args, **kwds):
178 raise TypeError("Cannot instantiate %r" % type(self))
179
180
181class _FinalTypingBase(_TypingBase, _root=True):
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700182 """Mix-in class to prevent instantiation.
183
184 Prevents instantiation unless _root=True is given in class call.
185 It is used to create pseudo-singleton instances Any, Union, Tuple, etc.
186 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700187
Guido van Rossumd70fe632015-08-05 12:11:06 +0200188 __slots__ = ()
189
Guido van Rossum4cefe742016-09-27 15:20:12 -0700190 def __new__(cls, *args, _root=False, **kwds):
191 self = super().__new__(cls, *args, **kwds)
192 if _root is True:
193 return self
194 raise TypeError("Cannot instantiate %r" % cls)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700195
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700196 def __reduce__(self):
197 return _trim_name(type(self).__name__)
198
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700199
Guido van Rossum4cefe742016-09-27 15:20:12 -0700200class _ForwardRef(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700201 """Wrapper to hold a forward reference."""
202
Guido van Rossum4cefe742016-09-27 15:20:12 -0700203 __slots__ = ('__forward_arg__', '__forward_code__',
Guido van Rossumc7b92952016-11-10 08:24:06 -0800204 '__forward_evaluated__', '__forward_value__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700205
206 def __init__(self, arg):
207 super().__init__(arg)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700208 if not isinstance(arg, str):
209 raise TypeError('ForwardRef must be a string -- got %r' % (arg,))
210 try:
211 code = compile(arg, '<string>', 'eval')
212 except SyntaxError:
213 raise SyntaxError('ForwardRef must be an expression -- got %r' %
214 (arg,))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700215 self.__forward_arg__ = arg
216 self.__forward_code__ = code
217 self.__forward_evaluated__ = False
218 self.__forward_value__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700219
220 def _eval_type(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800221 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700222 if globalns is None and localns is None:
223 globalns = localns = {}
224 elif globalns is None:
225 globalns = localns
226 elif localns is None:
227 localns = globalns
228 self.__forward_value__ = _type_check(
229 eval(self.__forward_code__, globalns, localns),
230 "Forward references must evaluate to types.")
231 self.__forward_evaluated__ = True
232 return self.__forward_value__
233
Guido van Rossum4cefe742016-09-27 15:20:12 -0700234 def __eq__(self, other):
235 if not isinstance(other, _ForwardRef):
236 return NotImplemented
237 return (self.__forward_arg__ == other.__forward_arg__ and
Guido van Rossumc7b92952016-11-10 08:24:06 -0800238 self.__forward_value__ == other.__forward_value__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700239
240 def __hash__(self):
Guido van Rossumc7b92952016-11-10 08:24:06 -0800241 return hash((self.__forward_arg__, self.__forward_value__))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700242
Guido van Rossumd70fe632015-08-05 12:11:06 +0200243 def __instancecheck__(self, obj):
244 raise TypeError("Forward references cannot be used with isinstance().")
245
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700246 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700247 raise TypeError("Forward references cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700248
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700249 def __repr__(self):
250 return '_ForwardRef(%r)' % (self.__forward_arg__,)
251
252
Guido van Rossum4cefe742016-09-27 15:20:12 -0700253class _TypeAlias(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700254 """Internal helper class for defining generic variants of concrete types.
255
Guido van Rossum4cefe742016-09-27 15:20:12 -0700256 Note that this is not a type; let's call it a pseudo-type. It cannot
257 be used in instance and subclass checks in parameterized form, i.e.
258 ``isinstance(42, Match[str])`` raises ``TypeError`` instead of returning
259 ``False``.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700260 """
261
Guido van Rossumd70fe632015-08-05 12:11:06 +0200262 __slots__ = ('name', 'type_var', 'impl_type', 'type_checker')
263
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700264 def __init__(self, name, type_var, impl_type, type_checker):
265 """Initializer.
266
267 Args:
268 name: The name, e.g. 'Pattern'.
269 type_var: The type parameter, e.g. AnyStr, or the
270 specific type, e.g. str.
271 impl_type: The implementation type.
272 type_checker: Function that takes an impl_type instance.
273 and returns a value that should be a type_var instance.
274 """
275 assert isinstance(name, str), repr(name)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700276 assert isinstance(impl_type, type), repr(impl_type)
277 assert not isinstance(impl_type, TypingMeta), repr(impl_type)
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700278 assert isinstance(type_var, (type, _TypingBase)), repr(type_var)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700279 self.name = name
280 self.type_var = type_var
281 self.impl_type = impl_type
282 self.type_checker = type_checker
283
284 def __repr__(self):
285 return "%s[%s]" % (self.name, _type_repr(self.type_var))
286
287 def __getitem__(self, parameter):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700288 if not isinstance(self.type_var, TypeVar):
289 raise TypeError("%s cannot be further parameterized." % self)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700290 if self.type_var.__constraints__ and isinstance(parameter, type):
291 if not issubclass(parameter, self.type_var.__constraints__):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700292 raise TypeError("%s is not a valid substitution for %s." %
293 (parameter, self.type_var))
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700294 if isinstance(parameter, TypeVar) and parameter is not self.type_var:
295 raise TypeError("%s cannot be re-parameterized." % self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700296 return self.__class__(self.name, parameter,
297 self.impl_type, self.type_checker)
298
Guido van Rossum4cefe742016-09-27 15:20:12 -0700299 def __eq__(self, other):
300 if not isinstance(other, _TypeAlias):
301 return NotImplemented
302 return self.name == other.name and self.type_var == other.type_var
303
304 def __hash__(self):
305 return hash((self.name, self.type_var))
306
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700307 def __instancecheck__(self, obj):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700308 if not isinstance(self.type_var, TypeVar):
309 raise TypeError("Parameterized type aliases cannot be used "
310 "with isinstance().")
311 return isinstance(obj, self.impl_type)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700312
313 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700314 if not isinstance(self.type_var, TypeVar):
315 raise TypeError("Parameterized type aliases cannot be used "
316 "with issubclass().")
317 return issubclass(cls, self.impl_type)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700318
319
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700320def _get_type_vars(types, tvars):
321 for t in types:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700322 if isinstance(t, TypingMeta) or isinstance(t, _TypingBase):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700323 t._get_type_vars(tvars)
324
325
326def _type_vars(types):
327 tvars = []
328 _get_type_vars(types, tvars)
329 return tuple(tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700330
331
332def _eval_type(t, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700333 if isinstance(t, TypingMeta) or isinstance(t, _TypingBase):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700334 return t._eval_type(globalns, localns)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700335 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700336
337
338def _type_check(arg, msg):
339 """Check that the argument is a type, and return it.
340
341 As a special case, accept None and return type(None) instead.
342 Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable.
343
344 The msg argument is a human-readable error message, e.g.
345
346 "Union[arg, ...]: arg should be a type."
347
348 We append the repr() of the actual value (truncated to 100 chars).
349 """
350 if arg is None:
351 return type(None)
352 if isinstance(arg, str):
353 arg = _ForwardRef(arg)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700354 if (isinstance(arg, _TypingBase) and type(arg).__name__ == '_ClassVar' or
355 not isinstance(arg, (type, _TypingBase)) and not callable(arg)):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700356 raise TypeError(msg + " Got %.100r." % (arg,))
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700357 # Bare Union etc. are not valid as type arguments
358 if (type(arg).__name__ in ('_Union', '_Optional')
359 and not getattr(arg, '__origin__', None)
360 or isinstance(arg, TypingMeta) and _gorg(arg) in (Generic, _Protocol)):
361 raise TypeError("Plain %s is not valid as type argument" % arg)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700362 return arg
363
364
365def _type_repr(obj):
366 """Return the repr() of an object, special-casing types.
367
368 If obj is a type, we return a shorter version than the default
369 type.__repr__, based on the module and qualified name, which is
370 typically enough to uniquely identify a type. For everything
371 else, we fall back on repr(obj).
372 """
373 if isinstance(obj, type) and not isinstance(obj, TypingMeta):
374 if obj.__module__ == 'builtins':
375 return _qualname(obj)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700376 return '%s.%s' % (obj.__module__, _qualname(obj))
377 if obj is ...:
378 return('...')
379 if isinstance(obj, types.FunctionType):
380 return obj.__name__
381 return repr(obj)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700382
383
Guido van Rossum4cefe742016-09-27 15:20:12 -0700384class _Any(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700385 """Special type indicating an unconstrained type.
386
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700387 - Any is compatible with every type.
388 - Any assumed to have all methods.
389 - All values assumed to be instances of Any.
390
391 Note that all the above statements are true from the point of view of
392 static type checkers. At runtime, Any should not be used with instance
393 or class checks.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700394 """
395
Guido van Rossumd70fe632015-08-05 12:11:06 +0200396 __slots__ = ()
397
Guido van Rossum4cefe742016-09-27 15:20:12 -0700398 def __instancecheck__(self, obj):
399 raise TypeError("Any cannot be used with isinstance().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700400
Guido van Rossum4cefe742016-09-27 15:20:12 -0700401 def __subclasscheck__(self, cls):
402 raise TypeError("Any cannot be used with issubclass().")
403
404
405Any = _Any(_root=True)
406
407
408class TypeVar(_TypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700409 """Type variable.
410
411 Usage::
412
413 T = TypeVar('T') # Can be anything
414 A = TypeVar('A', str, bytes) # Must be str or bytes
415
416 Type variables exist primarily for the benefit of static type
417 checkers. They serve as the parameters for generic types as well
418 as for generic function definitions. See class Generic for more
419 information on generic types. Generic functions work as follows:
420
421 def repeat(x: T, n: int) -> Sequence[T]:
422 '''Return a list containing n references to x.'''
423 return [x]*n
424
425 def longest(x: A, y: A) -> A:
426 '''Return the longest of two strings.'''
427 return x if len(x) >= len(y) else y
428
429 The latter example's signature is essentially the overloading
430 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
431 that if the arguments are instances of some subclass of str,
432 the return type is still plain str.
433
434 At runtime, isinstance(x, T) will raise TypeError. However,
435 issubclass(C, T) is true for any class C, and issubclass(str, A)
436 and issubclass(bytes, A) are true, and issubclass(int, A) is
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700437 false. (TODO: Why is this needed? This may change. See #136.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700438
Guido van Rossumefa798d2016-08-23 11:01:50 -0700439 Type variables defined with covariant=True or contravariant=True
440 can be used do declare covariant or contravariant generic types.
441 See PEP 484 for more details. By default generic types are invariant
442 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700443
444 Type variables can be introspected. e.g.:
445
446 T.__name__ == 'T'
447 T.__constraints__ == ()
448 T.__covariant__ == False
449 T.__contravariant__ = False
450 A.__constraints__ == (str, bytes)
451 """
452
Guido van Rossum4cefe742016-09-27 15:20:12 -0700453 __slots__ = ('__name__', '__bound__', '__constraints__',
454 '__covariant__', '__contravariant__')
455
456 def __init__(self, name, *constraints, bound=None,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700457 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700458 super().__init__(name, *constraints, bound=bound,
459 covariant=covariant, contravariant=contravariant)
460 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700461 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700462 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700463 self.__covariant__ = bool(covariant)
464 self.__contravariant__ = bool(contravariant)
465 if constraints and bound is not None:
466 raise TypeError("Constraints cannot be combined with bound=...")
467 if constraints and len(constraints) == 1:
468 raise TypeError("A single constraint is not allowed")
469 msg = "TypeVar(name, constraint, ...): constraints must be types."
470 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
471 if bound:
472 self.__bound__ = _type_check(bound, "Bound must be a type.")
473 else:
474 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700475
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700476 def _get_type_vars(self, tvars):
477 if self not in tvars:
478 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700479
480 def __repr__(self):
481 if self.__covariant__:
482 prefix = '+'
483 elif self.__contravariant__:
484 prefix = '-'
485 else:
486 prefix = '~'
487 return prefix + self.__name__
488
489 def __instancecheck__(self, instance):
490 raise TypeError("Type variables cannot be used with isinstance().")
491
492 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700493 raise TypeError("Type variables cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700494
495
496# Some unconstrained type variables. These are used by the container types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700497# (These are not for export.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700498T = TypeVar('T') # Any type.
499KT = TypeVar('KT') # Key type.
500VT = TypeVar('VT') # Value type.
501T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
502V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700503VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
504T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
505
506# A useful type variable with constraints. This represents string types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700507# (This one *is* for export!)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700508AnyStr = TypeVar('AnyStr', bytes, str)
509
510
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700511def _replace_arg(arg, tvars, args):
512 """ A helper fuunction: replace arg if it is a type variable
513 found in tvars with corresponding substitution from args or
514 with corresponding substitution sub-tree if arg is a generic type.
515 """
516
517 if tvars is None:
518 tvars = []
519 if hasattr(arg, '_subs_tree'):
520 return arg._subs_tree(tvars, args)
521 if isinstance(arg, TypeVar):
522 for i, tvar in enumerate(tvars):
523 if arg == tvar:
524 return args[i]
525 return arg
526
527
528def _subs_tree(cls, tvars=None, args=None):
529 """ Calculate substitution tree for generic cls after
530 replacing its type parameters with substitutions in tvars -> args (if any).
531 Repeat the same cyclicaly following __origin__'s.
532 """
533
534 if cls.__origin__ is None:
535 return cls
536 # Make of chain of origins (i.e. cls -> cls.__origin__)
537 current = cls.__origin__
538 orig_chain = []
539 while current.__origin__ is not None:
540 orig_chain.append(current)
541 current = current.__origin__
542 # Replace type variables in __args__ if asked ...
543 tree_args = []
544 for arg in cls.__args__:
545 tree_args.append(_replace_arg(arg, tvars, args))
546 # ... then continue replacing down the origin chain.
547 for ocls in orig_chain:
548 new_tree_args = []
549 for i, arg in enumerate(ocls.__args__):
550 new_tree_args.append(_replace_arg(arg, ocls.__parameters__, tree_args))
551 tree_args = new_tree_args
552 return tree_args
553
554
555def _remove_dups_flatten(parameters):
556 """ A helper for Union creation and substitution: flatten Union's
557 among parameters, then remove duplicates and strict subclasses.
558 """
559
560 # Flatten out Union[Union[...], ...].
561 params = []
562 for p in parameters:
563 if isinstance(p, _Union) and p.__origin__ is Union:
564 params.extend(p.__args__)
565 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
566 params.extend(p[1:])
567 else:
568 params.append(p)
569 # Weed out strict duplicates, preserving the first of each occurrence.
570 all_params = set(params)
571 if len(all_params) < len(params):
572 new_params = []
573 for t in params:
574 if t in all_params:
575 new_params.append(t)
576 all_params.remove(t)
577 params = new_params
578 assert not all_params, all_params
579 # Weed out subclasses.
580 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
581 # If object is present it will be sole survivor among proper classes.
582 # Never discard type variables.
583 # (In particular, Union[str, AnyStr] != AnyStr.)
584 all_params = set(params)
585 for t1 in params:
586 if not isinstance(t1, type):
587 continue
588 if any(isinstance(t2, type) and issubclass(t1, t2)
589 for t2 in all_params - {t1}
590 if not (isinstance(t2, GenericMeta) and
591 t2.__origin__ is not None)):
592 all_params.remove(t1)
593 return tuple(t for t in params if t in all_params)
594
595
596def _check_generic(cls, parameters):
597 # Check correct count for parameters of a generic cls.
598 if not cls.__parameters__:
599 raise TypeError("%s is not a generic class" % repr(cls))
600 alen = len(parameters)
601 elen = len(cls.__parameters__)
602 if alen != elen:
603 raise TypeError("Too %s parameters for %s; actual %s, expected %s" %
604 ("many" if alen > elen else "few", repr(cls), alen, elen))
605
606
Guido van Rossum9b107562016-11-09 13:23:04 -0800607_cleanups = []
608
609
Guido van Rossum4cefe742016-09-27 15:20:12 -0700610def _tp_cache(func):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700611 """ Caching for __getitem__ of generic types with a fallback to
612 original function for non-hashable arguments.
613 """
614
Guido van Rossum4cefe742016-09-27 15:20:12 -0700615 cached = functools.lru_cache()(func)
Guido van Rossum9b107562016-11-09 13:23:04 -0800616 _cleanups.append(cached.cache_clear)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700617 @functools.wraps(func)
618 def inner(*args, **kwds):
619 try:
620 return cached(*args, **kwds)
621 except TypeError:
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700622 pass # All real errors (not unhashable args) are raised below.
Guido van Rossum4cefe742016-09-27 15:20:12 -0700623 return func(*args, **kwds)
624 return inner
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700625
626
Guido van Rossum4cefe742016-09-27 15:20:12 -0700627class _Union(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700628 """Union type; Union[X, Y] means either X or Y.
629
630 To define a union, use e.g. Union[int, str]. Details:
631
632 - The arguments must be types and there must be at least one.
633
634 - None as an argument is a special case and is replaced by
635 type(None).
636
637 - Unions of unions are flattened, e.g.::
638
639 Union[Union[int, str], float] == Union[int, str, float]
640
641 - Unions of a single argument vanish, e.g.::
642
643 Union[int] == int # The constructor actually returns int
644
645 - Redundant arguments are skipped, e.g.::
646
647 Union[int, str, int] == Union[int, str]
648
649 - When comparing unions, the argument order is ignored, e.g.::
650
651 Union[int, str] == Union[str, int]
652
653 - When two arguments have a subclass relationship, the least
654 derived argument is kept, e.g.::
655
656 class Employee: pass
657 class Manager(Employee): pass
658 Union[int, Employee, Manager] == Union[int, Employee]
659 Union[Manager, int, Employee] == Union[int, Employee]
660 Union[Employee, Manager] == Employee
661
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700662 - Similar for object::
663
664 Union[int, object] == object
665
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700666 - You cannot subclass or instantiate a union.
667
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700668 - You can use Optional[X] as a shorthand for Union[X, None].
669 """
670
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700671 __slots__ = ('__parameters__', '__args__', '__origin__', '__tree_hash__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700672
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700673 def __new__(cls, parameters=None, origin=None, *args, _root=False):
674 self = super().__new__(cls, parameters, origin, *args, _root=_root)
675 if origin is None:
676 self.__parameters__ = None
677 self.__args__ = None
678 self.__origin__ = None
679 self.__tree_hash__ = hash(frozenset(('Union',)))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700680 return self
681 if not isinstance(parameters, tuple):
682 raise TypeError("Expected parameters=<tuple>")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700683 if origin is Union:
684 parameters = _remove_dups_flatten(parameters)
685 # It's not a union if there's only one type left.
686 if len(parameters) == 1:
687 return parameters[0]
688 self.__parameters__ = _type_vars(parameters)
689 self.__args__ = parameters
690 self.__origin__ = origin
691 # Pre-calculate the __hash__ on instantiation.
692 # This improves speed for complex substitutions.
693 subs_tree = self._subs_tree()
694 if isinstance(subs_tree, tuple):
695 self.__tree_hash__ = hash(frozenset(subs_tree))
696 else:
697 self.__tree_hash__ = hash(subs_tree)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700698 return self
699
700 def _eval_type(self, globalns, localns):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700701 if self.__args__ is None:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700702 return self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700703 ev_args = tuple(_eval_type(t, globalns, localns) for t in self.__args__)
704 ev_origin = _eval_type(self.__origin__, globalns, localns)
705 if ev_args == self.__args__ and ev_origin == self.__origin__:
706 # Everything is already evaluated.
707 return self
708 return self.__class__(ev_args, ev_origin, _root=True)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700709
710 def _get_type_vars(self, tvars):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700711 if self.__origin__ and self.__parameters__:
712 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700713
714 def __repr__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700715 if self.__origin__ is None:
716 return super().__repr__()
717 tree = self._subs_tree()
718 if not isinstance(tree, tuple):
719 return repr(tree)
720 return tree[0]._tree_repr(tree)
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700721
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700722 def _tree_repr(self, tree):
723 arg_list = []
724 for arg in tree[1:]:
725 if not isinstance(arg, tuple):
726 arg_list.append(_type_repr(arg))
727 else:
728 arg_list.append(arg[0]._tree_repr(arg))
729 return super().__repr__() + '[%s]' % ', '.join(arg_list)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700730
731 @_tp_cache
732 def __getitem__(self, parameters):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700733 if parameters == ():
734 raise TypeError("Cannot take a Union of no types.")
735 if not isinstance(parameters, tuple):
736 parameters = (parameters,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700737 if self.__origin__ is None:
738 msg = "Union[arg, ...]: each arg must be a type."
739 else:
740 msg = "Parameters to generic types must be types."
741 parameters = tuple(_type_check(p, msg) for p in parameters)
742 if self is not Union:
743 _check_generic(self, parameters)
744 return self.__class__(parameters, origin=self, _root=True)
745
746 def _subs_tree(self, tvars=None, args=None):
747 if self is Union:
748 return Union # Nothing to substitute
749 tree_args = _subs_tree(self, tvars, args)
750 tree_args = _remove_dups_flatten(tree_args)
751 if len(tree_args) == 1:
752 return tree_args[0] # Union of a single type is that type
753 return (Union,) + tree_args
Guido van Rossum4cefe742016-09-27 15:20:12 -0700754
755 def __eq__(self, other):
756 if not isinstance(other, _Union):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700757 return self._subs_tree() == other
758 return self.__tree_hash__ == other.__tree_hash__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700759
760 def __hash__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700761 return self.__tree_hash__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700762
763 def __instancecheck__(self, obj):
764 raise TypeError("Unions cannot be used with isinstance().")
765
766 def __subclasscheck__(self, cls):
767 raise TypeError("Unions cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700768
769
Guido van Rossum4cefe742016-09-27 15:20:12 -0700770Union = _Union(_root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700771
772
Guido van Rossum4cefe742016-09-27 15:20:12 -0700773class _Optional(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700774 """Optional type.
775
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700776 Optional[X] is equivalent to Union[X, None].
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700777 """
778
Guido van Rossumd70fe632015-08-05 12:11:06 +0200779 __slots__ = ()
780
Guido van Rossum4cefe742016-09-27 15:20:12 -0700781 @_tp_cache
782 def __getitem__(self, arg):
783 arg = _type_check(arg, "Optional[t] requires a single type.")
784 return Union[arg, type(None)]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700785
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700786
Guido van Rossum4cefe742016-09-27 15:20:12 -0700787Optional = _Optional(_root=True)
788
789
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700790def _gorg(a):
791 """Return the farthest origin of a generic class."""
792 assert isinstance(a, GenericMeta)
793 while a.__origin__ is not None:
794 a = a.__origin__
795 return a
796
797
798def _geqv(a, b):
799 """Return whether two generic classes are equivalent.
800
801 The intention is to consider generic class X and any of its
802 parameterized forms (X[T], X[int], etc.) as equivalent.
803
804 However, X is not equivalent to a subclass of X.
805
806 The relation is reflexive, symmetric and transitive.
807 """
808 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
809 # Reduce each to its origin.
810 return _gorg(a) is _gorg(b)
811
812
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700813def _next_in_mro(cls):
814 """Helper for Generic.__new__.
815
816 Returns the class after the last occurrence of Generic or
817 Generic[...] in cls.__mro__.
818 """
819 next_in_mro = object
820 # Look for the last occurrence of Generic or Generic[...].
821 for i, c in enumerate(cls.__mro__[:-1]):
822 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
823 next_in_mro = cls.__mro__[i+1]
824 return next_in_mro
825
826
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700827def _valid_for_check(cls):
828 if cls is Generic:
829 raise TypeError("Class %r cannot be used with class "
830 "or instance checks" % cls)
831 if (cls.__origin__ is not None and
832 sys._getframe(3).f_globals['__name__'] not in ['abc', 'functools']):
833 raise TypeError("Parameterized generics cannot be used with class "
834 "or instance checks")
835
836
837def _make_subclasshook(cls):
838 """Construct a __subclasshook__ callable that incorporates
839 the associated __extra__ class in subclass checks performed
840 against cls.
841 """
842 if isinstance(cls.__extra__, abc.ABCMeta):
843 # The logic mirrors that of ABCMeta.__subclasscheck__.
844 # Registered classes need not be checked here because
845 # cls and its extra share the same _abc_registry.
846 def __extrahook__(subclass):
847 _valid_for_check(cls)
848 res = cls.__extra__.__subclasshook__(subclass)
849 if res is not NotImplemented:
850 return res
851 if cls.__extra__ in subclass.__mro__:
852 return True
853 for scls in cls.__extra__.__subclasses__():
854 if isinstance(scls, GenericMeta):
855 continue
856 if issubclass(subclass, scls):
857 return True
858 return NotImplemented
859 else:
860 # For non-ABC extras we'll just call issubclass().
861 def __extrahook__(subclass):
862 _valid_for_check(cls)
863 if cls.__extra__ and issubclass(subclass, cls.__extra__):
864 return True
865 return NotImplemented
866 return __extrahook__
867
868
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700869class GenericMeta(TypingMeta, abc.ABCMeta):
870 """Metaclass for generic types."""
871
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700872 def __new__(cls, name, bases, namespace,
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700873 tvars=None, args=None, origin=None, extra=None, orig_bases=None):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700874 if tvars is not None:
875 # Called from __getitem__() below.
876 assert origin is not None
877 assert all(isinstance(t, TypeVar) for t in tvars), tvars
878 else:
879 # Called from class statement.
880 assert tvars is None, tvars
881 assert args is None, args
882 assert origin is None, origin
883
884 # Get the full set of tvars from the bases.
885 tvars = _type_vars(bases)
886 # Look for Generic[T1, ..., Tn].
887 # If found, tvars must be a subset of it.
888 # If not found, tvars is it.
889 # Also check for and reject plain Generic,
890 # and reject multiple Generic[...].
891 gvars = None
892 for base in bases:
893 if base is Generic:
894 raise TypeError("Cannot inherit from plain Generic")
895 if (isinstance(base, GenericMeta) and
896 base.__origin__ is Generic):
897 if gvars is not None:
898 raise TypeError(
899 "Cannot inherit from Generic[...] multiple types.")
900 gvars = base.__parameters__
901 if gvars is None:
902 gvars = tvars
903 else:
904 tvarset = set(tvars)
905 gvarset = set(gvars)
906 if not tvarset <= gvarset:
907 raise TypeError(
908 "Some type variables (%s) "
909 "are not listed in Generic[%s]" %
910 (", ".join(str(t) for t in tvars if t not in gvarset),
911 ", ".join(str(g) for g in gvars)))
912 tvars = gvars
913
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700914 initial_bases = bases
915 if extra is not None and type(extra) is abc.ABCMeta and extra not in bases:
916 bases = (extra,) + bases
917 bases = tuple(_gorg(b) if isinstance(b, GenericMeta) else b for b in bases)
918
919 # remove bare Generic from bases if there are other generic bases
920 if any(isinstance(b, GenericMeta) and b is not Generic for b in bases):
921 bases = tuple(b for b in bases if b is not Generic)
922 self = super().__new__(cls, name, bases, namespace, _root=True)
923
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700924 self.__parameters__ = tvars
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700925 # Be prepared that GenericMeta will be subclassed by TupleMeta
926 # and CallableMeta, those two allow ..., (), or [] in __args___.
927 self.__args__ = tuple(... if a is _TypingEllipsis else
928 () if a is _TypingEmpty else
929 a for a in args) if args else None
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700930 self.__origin__ = origin
Guido van Rossum1cea70f2016-05-18 08:35:00 -0700931 self.__extra__ = extra
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700932 # Speed hack (https://github.com/python/typing/issues/196).
933 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700934 # Preserve base classes on subclassing (__bases__ are type erased now).
935 if orig_bases is None:
936 self.__orig_bases__ = initial_bases
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700937
938 # This allows unparameterized generic collections to be used
939 # with issubclass() and isinstance() in the same way as their
940 # collections.abc counterparts (e.g., isinstance([], Iterable)).
Guido van Rossume2592672016-10-08 20:27:22 -0700941 if ('__subclasshook__' not in namespace and extra # allow overriding
942 or hasattr(self.__subclasshook__, '__name__') and
943 self.__subclasshook__.__name__ == '__extrahook__'):
944 self.__subclasshook__ = _make_subclasshook(self)
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700945 if isinstance(extra, abc.ABCMeta):
946 self._abc_registry = extra._abc_registry
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700947
948 if origin and hasattr(origin, '__qualname__'): # Fix for Python 3.2.
949 self.__qualname__ = origin.__qualname__
950 self.__tree_hash__ = hash(self._subs_tree()) if origin else hash((self.__name__,))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700951 return self
952
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700953 def _get_type_vars(self, tvars):
954 if self.__origin__ and self.__parameters__:
955 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700956
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700957 def _eval_type(self, globalns, localns):
958 ev_origin = (self.__origin__._eval_type(globalns, localns)
959 if self.__origin__ else None)
960 ev_args = tuple(_eval_type(a, globalns, localns) for a
961 in self.__args__) if self.__args__ else None
962 if ev_origin == self.__origin__ and ev_args == self.__args__:
963 return self
964 return self.__class__(self.__name__,
965 self.__bases__,
966 dict(self.__dict__),
967 tvars=_type_vars(ev_args) if ev_args else None,
968 args=ev_args,
969 origin=ev_origin,
970 extra=self.__extra__,
971 orig_bases=self.__orig_bases__)
972
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700973 def __repr__(self):
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700974 if self.__origin__ is None:
975 return super().__repr__()
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700976 return self._tree_repr(self._subs_tree())
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700977
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700978 def _tree_repr(self, tree):
979 arg_list = []
980 for arg in tree[1:]:
981 if arg == ():
982 arg_list.append('()')
983 elif not isinstance(arg, tuple):
984 arg_list.append(_type_repr(arg))
985 else:
986 arg_list.append(arg[0]._tree_repr(arg))
987 return super().__repr__() + '[%s]' % ', '.join(arg_list)
988
989 def _subs_tree(self, tvars=None, args=None):
990 if self.__origin__ is None:
991 return self
992 tree_args = _subs_tree(self, tvars, args)
993 return (_gorg(self),) + tuple(tree_args)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700994
995 def __eq__(self, other):
996 if not isinstance(other, GenericMeta):
997 return NotImplemented
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700998 if self.__origin__ is None or other.__origin__ is None:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700999 return self is other
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001000 return self.__tree_hash__ == other.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001001
1002 def __hash__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001003 return self.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001004
Guido van Rossum4cefe742016-09-27 15:20:12 -07001005 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001006 def __getitem__(self, params):
1007 if not isinstance(params, tuple):
1008 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001009 if not params and not _gorg(self) is Tuple:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001010 raise TypeError(
1011 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001012 msg = "Parameters to generic types must be types."
1013 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001014 if self is Generic:
1015 # Generic can only be subscripted with unique type variables.
1016 if not all(isinstance(p, TypeVar) for p in params):
1017 raise TypeError(
1018 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +02001019 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -07001020 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001021 "Parameters to Generic[...] must all be unique")
1022 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001023 args = params
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001024 elif self in (Tuple, Callable):
1025 tvars = _type_vars(params)
1026 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001027 elif self is _Protocol:
1028 # _Protocol is internal, don't check anything.
1029 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001030 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001031 elif self.__origin__ in (Generic, _Protocol):
1032 # Can't subscript Generic[...] or _Protocol[...].
1033 raise TypeError("Cannot subscript already-subscripted %s" %
1034 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001035 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001036 # Subscripting a regular Generic subclass.
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001037 _check_generic(self, params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001038 tvars = _type_vars(params)
1039 args = params
1040 return self.__class__(self.__name__,
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001041 self.__bases__,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001042 dict(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001043 tvars=tvars,
1044 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001045 origin=self,
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001046 extra=self.__extra__,
1047 orig_bases=self.__orig_bases__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001048
Guido van Rossum1b669102015-09-04 12:15:54 -07001049 def __instancecheck__(self, instance):
1050 # Since we extend ABC.__subclasscheck__ and
1051 # ABC.__instancecheck__ inlines the cache checking done by the
1052 # latter, we must extend __instancecheck__ too. For simplicity
1053 # we just skip the cache check -- instance checks for generic
1054 # classes are supposed to be rare anyways.
Guido van Rossumb47c9d22016-10-03 08:40:50 -07001055 return issubclass(instance.__class__, self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001056
Guido van Rossumb7dedc82016-10-29 12:44:29 -07001057 def __copy__(self):
1058 return self.__class__(self.__name__, self.__bases__, dict(self.__dict__),
1059 self.__parameters__, self.__args__, self.__origin__,
1060 self.__extra__, self.__orig_bases__)
1061
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001062
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001063# Prevent checks for Generic to crash when defining Generic.
1064Generic = None
1065
1066
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001067def _generic_new(base_cls, cls, *args, **kwds):
1068 # Assure type is erased on instantiation,
1069 # but attempt to store it in __orig_class__
1070 if cls.__origin__ is None:
1071 return base_cls.__new__(cls)
1072 else:
1073 origin = _gorg(cls)
1074 obj = base_cls.__new__(origin)
1075 try:
1076 obj.__orig_class__ = cls
1077 except AttributeError:
1078 pass
1079 obj.__init__(*args, **kwds)
1080 return obj
1081
1082
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001083class Generic(metaclass=GenericMeta):
1084 """Abstract base class for generic types.
1085
1086 A generic type is typically declared by inheriting from an
1087 instantiation of this class with one or more type variables.
1088 For example, a generic mapping type might be defined as::
1089
1090 class Mapping(Generic[KT, VT]):
1091 def __getitem__(self, key: KT) -> VT:
1092 ...
1093 # Etc.
1094
1095 This class can then be used as follows::
1096
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001097 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001098 try:
1099 return mapping[key]
1100 except KeyError:
1101 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001102 """
1103
Guido van Rossumd70fe632015-08-05 12:11:06 +02001104 __slots__ = ()
1105
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001106 def __new__(cls, *args, **kwds):
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001107 if _geqv(cls, Generic):
1108 raise TypeError("Type Generic cannot be instantiated; "
1109 "it can be used only as a base class")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001110 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
1111
1112
1113class _TypingEmpty:
1114 """Placeholder for () or []. Used by TupleMeta and CallableMeta
1115 to allow empy list/tuple in specific places, without allowing them
1116 to sneak in where prohibited.
1117 """
1118
1119
1120class _TypingEllipsis:
1121 """Ditto for ..."""
1122
1123
1124class TupleMeta(GenericMeta):
1125 """Metaclass for Tuple"""
1126
1127 @_tp_cache
1128 def __getitem__(self, parameters):
1129 if self.__origin__ is not None or not _geqv(self, Tuple):
1130 # Normal generic rules apply if this is not the first subscription
1131 # or a subscription of a subclass.
1132 return super().__getitem__(parameters)
1133 if parameters == ():
1134 return super().__getitem__((_TypingEmpty,))
1135 if not isinstance(parameters, tuple):
1136 parameters = (parameters,)
1137 if len(parameters) == 2 and parameters[1] is ...:
1138 msg = "Tuple[t, ...]: t must be a type."
1139 p = _type_check(parameters[0], msg)
1140 return super().__getitem__((p, _TypingEllipsis))
1141 msg = "Tuple[t0, t1, ...]: each t must be a type."
1142 parameters = tuple(_type_check(p, msg) for p in parameters)
1143 return super().__getitem__(parameters)
1144
1145 def __instancecheck__(self, obj):
1146 if self.__args__ == None:
1147 return isinstance(obj, tuple)
1148 raise TypeError("Parameterized Tuple cannot be used "
1149 "with isinstance().")
1150
1151 def __subclasscheck__(self, cls):
1152 if self.__args__ == None:
1153 return issubclass(cls, tuple)
1154 raise TypeError("Parameterized Tuple cannot be used "
1155 "with issubclass().")
1156
1157
1158class Tuple(tuple, extra=tuple, metaclass=TupleMeta):
1159 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
1160
1161 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1162 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1163 of an int, a float and a string.
1164
1165 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1166 """
1167
1168 __slots__ = ()
1169
1170 def __new__(cls, *args, **kwds):
1171 if _geqv(cls, Tuple):
1172 raise TypeError("Type Tuple cannot be instantiated; "
1173 "use tuple() instead")
1174 return _generic_new(tuple, cls, *args, **kwds)
1175
1176
1177class CallableMeta(GenericMeta):
1178 """ Metaclass for Callable."""
1179
1180 def __repr__(self):
1181 if self.__origin__ is None:
1182 return super().__repr__()
1183 return self._tree_repr(self._subs_tree())
1184
1185 def _tree_repr(self, tree):
1186 if _gorg(self) is not Callable:
1187 return super()._tree_repr(tree)
1188 # For actual Callable (not its subclass) we override
1189 # super()._tree_repr() for nice formatting.
1190 arg_list = []
1191 for arg in tree[1:]:
Guido van Rossum991d14f2016-11-09 13:12:51 -08001192 if not isinstance(arg, tuple):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001193 arg_list.append(_type_repr(arg))
1194 else:
1195 arg_list.append(arg[0]._tree_repr(arg))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001196 if arg_list[0] == '...':
1197 return repr(tree[0]) + '[..., %s]' % arg_list[1]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001198 return (repr(tree[0]) +
1199 '[[%s], %s]' % (', '.join(arg_list[:-1]), arg_list[-1]))
1200
1201 def __getitem__(self, parameters):
1202 """ A thin wrapper around __getitem_inner__ to provide the latter
1203 with hashable arguments to improve speed.
1204 """
1205
1206 if self.__origin__ is not None or not _geqv(self, Callable):
1207 return super().__getitem__(parameters)
1208 if not isinstance(parameters, tuple) or len(parameters) != 2:
1209 raise TypeError("Callable must be used as "
1210 "Callable[[arg, ...], result].")
1211 args, result = parameters
Guido van Rossum991d14f2016-11-09 13:12:51 -08001212 if args is Ellipsis:
1213 parameters = (Ellipsis, result)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001214 else:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001215 if not isinstance(args, list):
1216 raise TypeError("Callable[args, result]: args must be a list."
1217 " Got %.100r." % (args,))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001218 parameters = (tuple(args), result)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001219 return self.__getitem_inner__(parameters)
1220
1221 @_tp_cache
1222 def __getitem_inner__(self, parameters):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001223 args, result = parameters
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001224 msg = "Callable[args, result]: result must be a type."
1225 result = _type_check(result, msg)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001226 if args is Ellipsis:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001227 return super().__getitem__((_TypingEllipsis, result))
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001228 msg = "Callable[[arg, ...], result]: each arg must be a type."
1229 args = tuple(_type_check(arg, msg) for arg in args)
1230 parameters = args + (result,)
1231 return super().__getitem__(parameters)
1232
1233
1234class Callable(extra=collections_abc.Callable, metaclass = CallableMeta):
1235 """Callable type; Callable[[int], str] is a function of (int) -> str.
1236
1237 The subscription syntax must always be used with exactly two
1238 values: the argument list and the return type. The argument list
1239 must be a list of types; the return type must be a single type.
1240
1241 There is no syntax to indicate optional or keyword arguments,
1242 such function types are rarely used as callback types.
1243 """
1244
1245 __slots__ = ()
1246
1247 def __new__(cls, *args, **kwds):
1248 if _geqv(cls, Callable):
1249 raise TypeError("Type Callable cannot be instantiated; "
1250 "use a non-abstract subclass instead")
1251 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001252
1253
Guido van Rossum4cefe742016-09-27 15:20:12 -07001254class _ClassVar(_FinalTypingBase, _root=True):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001255 """Special type construct to mark class variables.
1256
1257 An annotation wrapped in ClassVar indicates that a given
1258 attribute is intended to be used as a class variable and
1259 should not be set on instances of that class. Usage::
1260
1261 class Starship:
1262 stats: ClassVar[Dict[str, int]] = {} # class variable
1263 damage: int = 10 # instance variable
1264
1265 ClassVar accepts only types and cannot be further subscribed.
1266
1267 Note that ClassVar is not a class itself, and should not
1268 be used with isinstance() or issubclass().
1269 """
1270
Guido van Rossum4cefe742016-09-27 15:20:12 -07001271 __slots__ = ('__type__',)
1272
1273 def __init__(self, tp=None, **kwds):
1274 self.__type__ = tp
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001275
1276 def __getitem__(self, item):
1277 cls = type(self)
1278 if self.__type__ is None:
1279 return cls(_type_check(item,
Guido van Rossum4cefe742016-09-27 15:20:12 -07001280 '{} accepts only single type.'.format(cls.__name__[1:])),
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001281 _root=True)
1282 raise TypeError('{} cannot be further subscripted'
1283 .format(cls.__name__[1:]))
1284
1285 def _eval_type(self, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001286 new_tp = _eval_type(self.__type__, globalns, localns)
1287 if new_tp == self.__type__:
1288 return self
1289 return type(self)(new_tp, _root=True)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001290
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001291 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001292 r = super().__repr__()
1293 if self.__type__ is not None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001294 r += '[{}]'.format(_type_repr(self.__type__))
Guido van Rossum4cefe742016-09-27 15:20:12 -07001295 return r
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001296
1297 def __hash__(self):
1298 return hash((type(self).__name__, self.__type__))
1299
1300 def __eq__(self, other):
1301 if not isinstance(other, _ClassVar):
1302 return NotImplemented
1303 if self.__type__ is not None:
1304 return self.__type__ == other.__type__
1305 return self is other
1306
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001307
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001308ClassVar = _ClassVar(_root=True)
1309
1310
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001311def cast(typ, val):
1312 """Cast a value to a type.
1313
1314 This returns the value unchanged. To the type checker this
1315 signals that the return value has the designated type, but at
1316 runtime we intentionally don't check anything (we want this
1317 to be as fast as possible).
1318 """
1319 return val
1320
1321
1322def _get_defaults(func):
1323 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001324 try:
1325 code = func.__code__
1326 except AttributeError:
1327 # Some built-in functions don't have __code__, __defaults__, etc.
1328 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001329 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001330 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001331 arg_names = arg_names[:pos_count]
1332 defaults = func.__defaults__ or ()
1333 kwdefaults = func.__kwdefaults__
1334 res = dict(kwdefaults) if kwdefaults else {}
1335 pos_offset = pos_count - len(defaults)
1336 for name, value in zip(arg_names[pos_offset:], defaults):
1337 assert name not in res
1338 res[name] = value
1339 return res
1340
1341
Guido van Rossum991d14f2016-11-09 13:12:51 -08001342def get_type_hints(obj, globalns=None, localns=None):
1343 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001344
Guido van Rossum991d14f2016-11-09 13:12:51 -08001345 This is often the same as obj.__annotations__, but it handles
1346 forward references encoded as string literals, and if necessary
1347 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001348
Guido van Rossum991d14f2016-11-09 13:12:51 -08001349 The argument may be a module, class, method, or function. The annotations
1350 are returned as a dictionary. For classes, annotations include also
1351 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001352
Guido van Rossum991d14f2016-11-09 13:12:51 -08001353 TypeError is raised if the argument is not of a type that can contain
1354 annotations, and an empty dictionary is returned if no annotations are
1355 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001356
Guido van Rossum991d14f2016-11-09 13:12:51 -08001357 BEWARE -- the behavior of globalns and localns is counterintuitive
1358 (unless you are familiar with how eval() and exec() work). The
1359 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001360
Guido van Rossum991d14f2016-11-09 13:12:51 -08001361 - If no dict arguments are passed, an attempt is made to use the
1362 globals from obj, and these are also used as the locals. If the
1363 object does not appear to have globals, an exception is raised.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001364
Guido van Rossum991d14f2016-11-09 13:12:51 -08001365 - If one dict argument is passed, it is used for both globals and
1366 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001367
Guido van Rossum991d14f2016-11-09 13:12:51 -08001368 - If two dict arguments are passed, they specify globals and
1369 locals, respectively.
1370 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001371
Guido van Rossum991d14f2016-11-09 13:12:51 -08001372 if getattr(obj, '__no_type_check__', None):
1373 return {}
1374 if globalns is None:
1375 globalns = getattr(obj, '__globals__', {})
1376 if localns is None:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001377 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001378 elif localns is None:
1379 localns = globalns
1380 # Classes require a special treatment.
1381 if isinstance(obj, type):
1382 hints = {}
1383 for base in reversed(obj.__mro__):
1384 ann = base.__dict__.get('__annotations__', {})
1385 for name, value in ann.items():
1386 if value is None:
1387 value = type(None)
1388 if isinstance(value, str):
1389 value = _ForwardRef(value)
1390 value = _eval_type(value, globalns, localns)
1391 hints[name] = value
1392 return hints
1393 hints = getattr(obj, '__annotations__', None)
1394 if hints is None:
1395 # Return empty annotations for something that _could_ have them.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001396 if (isinstance(obj, types.FunctionType) or
1397 isinstance(obj, types.BuiltinFunctionType) or
Guido van Rossum991d14f2016-11-09 13:12:51 -08001398 isinstance(obj, types.MethodType) or
1399 isinstance(obj, types.ModuleType)):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001400 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001401 else:
1402 raise TypeError('{!r} is not a module, class, method, '
1403 'or function.'.format(obj))
1404 defaults = _get_defaults(obj)
1405 hints = dict(hints)
1406 for name, value in hints.items():
1407 if value is None:
1408 value = type(None)
1409 if isinstance(value, str):
1410 value = _ForwardRef(value)
1411 value = _eval_type(value, globalns, localns)
1412 if name in defaults and defaults[name] is None:
1413 value = Optional[value]
1414 hints[name] = value
1415 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001416
1417
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001418def no_type_check(arg):
1419 """Decorator to indicate that annotations are not type hints.
1420
1421 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001422 applies recursively to all methods and classes defined in that class
1423 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001424
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001425 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001426 """
1427 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001428 arg_attrs = arg.__dict__.copy()
1429 for attr, val in arg.__dict__.items():
1430 if val in arg.__bases__:
1431 arg_attrs.pop(attr)
1432 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001433 if isinstance(obj, types.FunctionType):
1434 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001435 if isinstance(obj, type):
1436 no_type_check(obj)
1437 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001438 arg.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001439 except TypeError: # built-in classes
1440 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001441 return arg
1442
1443
1444def no_type_check_decorator(decorator):
1445 """Decorator to give another decorator the @no_type_check effect.
1446
1447 This wraps the decorator with something that wraps the decorated
1448 function in @no_type_check.
1449 """
1450
1451 @functools.wraps(decorator)
1452 def wrapped_decorator(*args, **kwds):
1453 func = decorator(*args, **kwds)
1454 func = no_type_check(func)
1455 return func
1456
1457 return wrapped_decorator
1458
1459
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001460def _overload_dummy(*args, **kwds):
1461 """Helper for @overload to raise when called."""
1462 raise NotImplementedError(
1463 "You should not call an overloaded function. "
1464 "A series of @overload-decorated functions "
1465 "outside a stub module should always be followed "
1466 "by an implementation that is not @overload-ed.")
1467
1468
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001469def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001470 """Decorator for overloaded functions/methods.
1471
1472 In a stub file, place two or more stub definitions for the same
1473 function in a row, each decorated with @overload. For example:
1474
1475 @overload
1476 def utf8(value: None) -> None: ...
1477 @overload
1478 def utf8(value: bytes) -> bytes: ...
1479 @overload
1480 def utf8(value: str) -> bytes: ...
1481
1482 In a non-stub file (i.e. a regular .py file), do the same but
1483 follow it with an implementation. The implementation should *not*
1484 be decorated with @overload. For example:
1485
1486 @overload
1487 def utf8(value: None) -> None: ...
1488 @overload
1489 def utf8(value: bytes) -> bytes: ...
1490 @overload
1491 def utf8(value: str) -> bytes: ...
1492 def utf8(value):
1493 # implementation goes here
1494 """
1495 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001496
1497
1498class _ProtocolMeta(GenericMeta):
1499 """Internal metaclass for _Protocol.
1500
1501 This exists so _Protocol classes can be generic without deriving
1502 from Generic.
1503 """
1504
Guido van Rossumd70fe632015-08-05 12:11:06 +02001505 def __instancecheck__(self, obj):
1506 raise TypeError("Protocols cannot be used with isinstance().")
1507
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001508 def __subclasscheck__(self, cls):
1509 if not self._is_protocol:
1510 # No structural checks since this isn't a protocol.
1511 return NotImplemented
1512
1513 if self is _Protocol:
1514 # Every class is a subclass of the empty protocol.
1515 return True
1516
1517 # Find all attributes defined in the protocol.
1518 attrs = self._get_protocol_attrs()
1519
1520 for attr in attrs:
1521 if not any(attr in d.__dict__ for d in cls.__mro__):
1522 return False
1523 return True
1524
1525 def _get_protocol_attrs(self):
1526 # Get all Protocol base classes.
1527 protocol_bases = []
1528 for c in self.__mro__:
1529 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1530 protocol_bases.append(c)
1531
1532 # Get attributes included in protocol.
1533 attrs = set()
1534 for base in protocol_bases:
1535 for attr in base.__dict__.keys():
1536 # Include attributes not defined in any non-protocol bases.
1537 for c in self.__mro__:
1538 if (c is not base and attr in c.__dict__ and
1539 not getattr(c, '_is_protocol', False)):
1540 break
1541 else:
1542 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001543 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001544 attr != '__annotations__' and
1545 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001546 attr != '_is_protocol' and
1547 attr != '__dict__' and
1548 attr != '__args__' and
1549 attr != '__slots__' and
1550 attr != '_get_protocol_attrs' and
1551 attr != '__next_in_mro__' and
1552 attr != '__parameters__' and
1553 attr != '__origin__' and
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001554 attr != '__orig_bases__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001555 attr != '__extra__' and
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001556 attr != '__tree_hash__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001557 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001558 attrs.add(attr)
1559
1560 return attrs
1561
1562
1563class _Protocol(metaclass=_ProtocolMeta):
1564 """Internal base class for protocol classes.
1565
1566 This implements a simple-minded structural isinstance check
1567 (similar but more general than the one-offs in collections.abc
1568 such as Hashable).
1569 """
1570
Guido van Rossumd70fe632015-08-05 12:11:06 +02001571 __slots__ = ()
1572
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001573 _is_protocol = True
1574
1575
1576# Various ABCs mimicking those in collections.abc.
1577# A few are simply re-exported for completeness.
1578
1579Hashable = collections_abc.Hashable # Not generic.
1580
1581
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001582if hasattr(collections_abc, 'Awaitable'):
1583 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1584 __slots__ = ()
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001585
1586 __all__.append('Awaitable')
1587
1588
1589if hasattr(collections_abc, 'Coroutine'):
1590 class Coroutine(Awaitable[V_co], Generic[T_co, T_contra, V_co],
1591 extra=collections_abc.Coroutine):
1592 __slots__ = ()
1593
1594 __all__.append('Coroutine')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001595
1596
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001597if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001598
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001599 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1600 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001601
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001602 class AsyncIterator(AsyncIterable[T_co],
1603 extra=collections_abc.AsyncIterator):
1604 __slots__ = ()
1605
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001606 __all__.append('AsyncIterable')
1607 __all__.append('AsyncIterator')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001608
1609
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001610class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001611 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001612
1613
1614class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001615 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001616
1617
1618class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001619 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001620
1621 @abstractmethod
1622 def __int__(self) -> int:
1623 pass
1624
1625
1626class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001627 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001628
1629 @abstractmethod
1630 def __float__(self) -> float:
1631 pass
1632
1633
1634class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001635 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001636
1637 @abstractmethod
1638 def __complex__(self) -> complex:
1639 pass
1640
1641
1642class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001643 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001644
1645 @abstractmethod
1646 def __bytes__(self) -> bytes:
1647 pass
1648
1649
Guido van Rossumd70fe632015-08-05 12:11:06 +02001650class SupportsAbs(_Protocol[T_co]):
1651 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001652
1653 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001654 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001655 pass
1656
1657
Guido van Rossumd70fe632015-08-05 12:11:06 +02001658class SupportsRound(_Protocol[T_co]):
1659 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001660
1661 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001662 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001663 pass
1664
1665
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001666if hasattr(collections_abc, 'Reversible'):
1667 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1668 __slots__ = ()
1669else:
1670 class Reversible(_Protocol[T_co]):
1671 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001672
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001673 @abstractmethod
1674 def __reversed__(self) -> 'Iterator[T_co]':
1675 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001676
1677
1678Sized = collections_abc.Sized # Not generic.
1679
1680
1681class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001682 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001683
1684
Guido van Rossumefa798d2016-08-23 11:01:50 -07001685if hasattr(collections_abc, 'Collection'):
1686 class Collection(Sized, Iterable[T_co], Container[T_co],
1687 extra=collections_abc.Collection):
1688 __slots__ = ()
1689
1690 __all__.append('Collection')
1691
1692
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001693# Callable was defined earlier.
1694
Guido van Rossumefa798d2016-08-23 11:01:50 -07001695if hasattr(collections_abc, 'Collection'):
1696 class AbstractSet(Collection[T_co],
1697 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001698 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001699else:
1700 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1701 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001702 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001703
1704
1705class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001706 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001707
1708
Guido van Rossumefa798d2016-08-23 11:01:50 -07001709# NOTE: It is only covariant in the value type.
1710if hasattr(collections_abc, 'Collection'):
1711 class Mapping(Collection[KT], Generic[KT, VT_co],
1712 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001713 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001714else:
1715 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1716 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001717 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001718
1719
1720class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001721 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001722
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001723if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001724 if hasattr(collections_abc, 'Collection'):
1725 class Sequence(Reversible[T_co], Collection[T_co],
1726 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001727 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001728 else:
1729 class Sequence(Sized, Reversible[T_co], Container[T_co],
1730 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001731 __slots__ = ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001732else:
1733 class Sequence(Sized, Iterable[T_co], Container[T_co],
1734 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001735 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001736
1737
1738class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001739 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001740
1741
1742class ByteString(Sequence[int], extra=collections_abc.ByteString):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001743 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001744
1745
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001746class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001747
Guido van Rossum4cefe742016-09-27 15:20:12 -07001748 __slots__ = ()
1749
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001750 def __new__(cls, *args, **kwds):
1751 if _geqv(cls, List):
1752 raise TypeError("Type List cannot be instantiated; "
1753 "use list() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001754 return _generic_new(list, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001755
1756
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001757class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001758
Guido van Rossum4cefe742016-09-27 15:20:12 -07001759 __slots__ = ()
1760
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001761 def __new__(cls, *args, **kwds):
1762 if _geqv(cls, Set):
1763 raise TypeError("Type Set cannot be instantiated; "
1764 "use set() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001765 return _generic_new(set, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001766
1767
Guido van Rossum4cefe742016-09-27 15:20:12 -07001768class FrozenSet(frozenset, AbstractSet[T_co], extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001769 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001770
1771 def __new__(cls, *args, **kwds):
1772 if _geqv(cls, FrozenSet):
1773 raise TypeError("Type FrozenSet cannot be instantiated; "
1774 "use frozenset() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001775 return _generic_new(frozenset, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001776
1777
1778class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001779 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001780
1781
Guido van Rossumd70fe632015-08-05 12:11:06 +02001782class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001783 extra=collections_abc.KeysView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001784 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001785
1786
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001787class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001788 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001789 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001790 extra=collections_abc.ItemsView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001791 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001792
1793
1794class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001795 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001796
1797
Brett Cannonf3ad0422016-04-15 10:51:30 -07001798if hasattr(contextlib, 'AbstractContextManager'):
1799 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1800 __slots__ = ()
1801 __all__.append('ContextManager')
1802
1803
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001804class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001805
Guido van Rossum4cefe742016-09-27 15:20:12 -07001806 __slots__ = ()
1807
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001808 def __new__(cls, *args, **kwds):
1809 if _geqv(cls, Dict):
1810 raise TypeError("Type Dict cannot be instantiated; "
1811 "use dict() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001812 return _generic_new(dict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001813
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001814class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
1815 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001816
Guido van Rossum4cefe742016-09-27 15:20:12 -07001817 __slots__ = ()
1818
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001819 def __new__(cls, *args, **kwds):
1820 if _geqv(cls, DefaultDict):
1821 raise TypeError("Type DefaultDict cannot be instantiated; "
1822 "use collections.defaultdict() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001823 return _generic_new(collections.defaultdict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001824
1825# Determine what base class to use for Generator.
1826if hasattr(collections_abc, 'Generator'):
1827 # Sufficiently recent versions of 3.5 have a Generator ABC.
1828 _G_base = collections_abc.Generator
1829else:
1830 # Fall back on the exact type.
1831 _G_base = types.GeneratorType
1832
1833
1834class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1835 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001836 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001837
1838 def __new__(cls, *args, **kwds):
1839 if _geqv(cls, Generator):
1840 raise TypeError("Type Generator cannot be instantiated; "
1841 "create a subclass instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001842 return _generic_new(_G_base, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001843
1844
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001845# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07001846CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001847
1848
Guido van Rossumb22c7082016-05-26 09:56:19 -07001849# This is not a real generic class. Don't use outside annotations.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001850class Type(Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07001851 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001852
1853 For example, suppose we have the following classes::
1854
1855 class User: ... # Abstract base for User classes
1856 class BasicUser(User): ...
1857 class ProUser(User): ...
1858 class TeamUser(User): ...
1859
1860 And a function that takes a class argument that's a subclass of
1861 User and returns an instance of the corresponding class::
1862
1863 U = TypeVar('U', bound=User)
1864 def new_user(user_class: Type[U]) -> U:
1865 user = user_class()
1866 # (Here we could write the user object to a database)
1867 return user
1868
1869 joe = new_user(BasicUser)
1870
1871 At this point the type checker knows that joe has type BasicUser.
1872 """
1873
Guido van Rossum4cefe742016-09-27 15:20:12 -07001874 __slots__ = ()
1875
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001876
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001877def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08001878 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
1879 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001880 nm_tpl = collections.namedtuple(name, [n for n, t in types])
1881 nm_tpl._field_types = dict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001882 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001883 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001884 except (AttributeError, ValueError):
1885 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001886 return nm_tpl
1887
1888
Guido van Rossum2f841442016-11-15 09:48:06 -08001889_PY36 = sys.version_info[:2] >= (3, 6)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001890
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001891
Guido van Rossum2f841442016-11-15 09:48:06 -08001892class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001893
Guido van Rossum2f841442016-11-15 09:48:06 -08001894 def __new__(cls, typename, bases, ns):
1895 if ns.get('_root', False):
1896 return super().__new__(cls, typename, bases, ns)
1897 if not _PY36:
1898 raise TypeError("Class syntax for NamedTuple is only supported"
1899 " in Python 3.6+")
1900 types = ns.get('__annotations__', {})
1901 return _make_nmtuple(typename, types.items())
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001902
Guido van Rossum2f841442016-11-15 09:48:06 -08001903class NamedTuple(metaclass=NamedTupleMeta):
1904 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001905
Guido van Rossum2f841442016-11-15 09:48:06 -08001906 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001907
Guido van Rossum2f841442016-11-15 09:48:06 -08001908 class Employee(NamedTuple):
1909 name: str
1910 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001911
Guido van Rossum2f841442016-11-15 09:48:06 -08001912 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001913
Guido van Rossum2f841442016-11-15 09:48:06 -08001914 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001915
Guido van Rossum2f841442016-11-15 09:48:06 -08001916 The resulting class has one extra attribute: _field_types,
1917 giving a dict mapping field names to types. (The field names
1918 are in the _fields attribute, which is part of the namedtuple
1919 API.) Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001920
Guido van Rossum2f841442016-11-15 09:48:06 -08001921 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001922
Guido van Rossum2f841442016-11-15 09:48:06 -08001923 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001924
Guido van Rossum2f841442016-11-15 09:48:06 -08001925 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1926 """
1927 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001928
Guido van Rossum2f841442016-11-15 09:48:06 -08001929 def __new__(self, typename, fields=None, **kwargs):
1930 if kwargs and not _PY36:
1931 raise TypeError("Keyword syntax for NamedTuple is only supported"
1932 " in Python 3.6+")
1933 if fields is None:
1934 fields = kwargs.items()
1935 elif kwargs:
1936 raise TypeError("Either list of fields or keywords"
1937 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001938 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001939
1940
Guido van Rossum91185fe2016-06-08 11:19:11 -07001941def NewType(name, tp):
1942 """NewType creates simple unique types with almost zero
1943 runtime overhead. NewType(name, tp) is considered a subtype of tp
1944 by static type checkers. At runtime, NewType(name, tp) returns
1945 a dummy function that simply returns its argument. Usage::
1946
1947 UserId = NewType('UserId', int)
1948
1949 def name_by_id(user_id: UserId) -> str:
1950 ...
1951
1952 UserId('user') # Fails type check
1953
1954 name_by_id(42) # Fails type check
1955 name_by_id(UserId(42)) # OK
1956
1957 num = UserId(5) + 1 # type: int
1958 """
1959
1960 def new_type(x):
1961 return x
1962
1963 new_type.__name__ = name
1964 new_type.__supertype__ = tp
1965 return new_type
1966
1967
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001968# Python-version-specific alias (Python 2: unicode; Python 3: str)
1969Text = str
1970
1971
Guido van Rossum91185fe2016-06-08 11:19:11 -07001972# Constant that's True when type checking, but False here.
1973TYPE_CHECKING = False
1974
1975
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001976class IO(Generic[AnyStr]):
1977 """Generic base class for TextIO and BinaryIO.
1978
1979 This is an abstract, generic version of the return of open().
1980
1981 NOTE: This does not distinguish between the different possible
1982 classes (text vs. binary, read vs. write vs. read/write,
1983 append-only, unbuffered). The TextIO and BinaryIO subclasses
1984 below capture the distinctions between text vs. binary, which is
1985 pervasive in the interface; however we currently do not offer a
1986 way to track the other distinctions in the type system.
1987 """
1988
Guido van Rossumd70fe632015-08-05 12:11:06 +02001989 __slots__ = ()
1990
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001991 @abstractproperty
1992 def mode(self) -> str:
1993 pass
1994
1995 @abstractproperty
1996 def name(self) -> str:
1997 pass
1998
1999 @abstractmethod
2000 def close(self) -> None:
2001 pass
2002
2003 @abstractmethod
2004 def closed(self) -> bool:
2005 pass
2006
2007 @abstractmethod
2008 def fileno(self) -> int:
2009 pass
2010
2011 @abstractmethod
2012 def flush(self) -> None:
2013 pass
2014
2015 @abstractmethod
2016 def isatty(self) -> bool:
2017 pass
2018
2019 @abstractmethod
2020 def read(self, n: int = -1) -> AnyStr:
2021 pass
2022
2023 @abstractmethod
2024 def readable(self) -> bool:
2025 pass
2026
2027 @abstractmethod
2028 def readline(self, limit: int = -1) -> AnyStr:
2029 pass
2030
2031 @abstractmethod
2032 def readlines(self, hint: int = -1) -> List[AnyStr]:
2033 pass
2034
2035 @abstractmethod
2036 def seek(self, offset: int, whence: int = 0) -> int:
2037 pass
2038
2039 @abstractmethod
2040 def seekable(self) -> bool:
2041 pass
2042
2043 @abstractmethod
2044 def tell(self) -> int:
2045 pass
2046
2047 @abstractmethod
2048 def truncate(self, size: int = None) -> int:
2049 pass
2050
2051 @abstractmethod
2052 def writable(self) -> bool:
2053 pass
2054
2055 @abstractmethod
2056 def write(self, s: AnyStr) -> int:
2057 pass
2058
2059 @abstractmethod
2060 def writelines(self, lines: List[AnyStr]) -> None:
2061 pass
2062
2063 @abstractmethod
2064 def __enter__(self) -> 'IO[AnyStr]':
2065 pass
2066
2067 @abstractmethod
2068 def __exit__(self, type, value, traceback) -> None:
2069 pass
2070
2071
2072class BinaryIO(IO[bytes]):
2073 """Typed version of the return of open() in binary mode."""
2074
Guido van Rossumd70fe632015-08-05 12:11:06 +02002075 __slots__ = ()
2076
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002077 @abstractmethod
2078 def write(self, s: Union[bytes, bytearray]) -> int:
2079 pass
2080
2081 @abstractmethod
2082 def __enter__(self) -> 'BinaryIO':
2083 pass
2084
2085
2086class TextIO(IO[str]):
2087 """Typed version of the return of open() in text mode."""
2088
Guido van Rossumd70fe632015-08-05 12:11:06 +02002089 __slots__ = ()
2090
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002091 @abstractproperty
2092 def buffer(self) -> BinaryIO:
2093 pass
2094
2095 @abstractproperty
2096 def encoding(self) -> str:
2097 pass
2098
2099 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08002100 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002101 pass
2102
2103 @abstractproperty
2104 def line_buffering(self) -> bool:
2105 pass
2106
2107 @abstractproperty
2108 def newlines(self) -> Any:
2109 pass
2110
2111 @abstractmethod
2112 def __enter__(self) -> 'TextIO':
2113 pass
2114
2115
2116class io:
2117 """Wrapper namespace for IO generic classes."""
2118
2119 __all__ = ['IO', 'TextIO', 'BinaryIO']
2120 IO = IO
2121 TextIO = TextIO
2122 BinaryIO = BinaryIO
2123
2124io.__name__ = __name__ + '.io'
2125sys.modules[io.__name__] = io
2126
2127
2128Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
2129 lambda p: p.pattern)
2130Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
2131 lambda m: m.re.pattern)
2132
2133
2134class re:
2135 """Wrapper namespace for re type aliases."""
2136
2137 __all__ = ['Pattern', 'Match']
2138 Pattern = Pattern
2139 Match = Match
2140
2141re.__name__ = __name__ + '.re'
2142sys.modules[re.__name__] = re