blob: 34845b747a297b60bc8aeb96b270e92f6929fbe6 [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):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800103 """Metaclass for most types defined in typing module
104 (not a part of public API).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700105
106 This overrides __new__() to require an extra keyword parameter
107 '_root', which serves as a guard against naive subclassing of the
108 typing classes. Any legitimate class defined using a metaclass
Guido van Rossumb24569a2016-11-20 18:01:29 -0800109 derived from TypingMeta must pass _root=True.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700110
Guido van Rossumb24569a2016-11-20 18:01:29 -0800111 This also defines a dummy constructor (all the work for most typing
112 constructs is done in __new__) and a nicer repr().
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700113 """
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
Guido van Rossumb24569a2016-11-20 18:01:29 -0800129 For example, List['C'] is internally stored as
130 List[_ForwardRef('C')], which should evaluate to List[C],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700131 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):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800145 """Internal indicator of special typing constructs."""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700146
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 Rossumb24569a2016-11-20 18:01:29 -0800182 """Internal mix-in class to prevent instantiation.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700183
184 Prevents instantiation unless _root=True is given in class call.
Guido van Rossumb24569a2016-11-20 18:01:29 -0800185 It is used to create pseudo-singleton instances Any, Union, Optional, etc.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700186 """
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 Rossumb24569a2016-11-20 18:01:29 -0800201 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700202
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):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800209 raise TypeError('Forward reference must be a string -- got %r' % (arg,))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700210 try:
211 code = compile(arg, '<string>', 'eval')
212 except SyntaxError:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800213 raise SyntaxError('Forward reference must be an expression -- got %r' %
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700214 (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):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800339 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700340
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):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800366 """Return the repr() of an object, special-casing types (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700367
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
Guido van Rossumb24569a2016-11-20 18:01:29 -0800421 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700422 '''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
Guido van Rossumb24569a2016-11-20 18:01:29 -0800434 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700435
Guido van Rossumefa798d2016-08-23 11:01:50 -0700436 Type variables defined with covariant=True or contravariant=True
437 can be used do declare covariant or contravariant generic types.
438 See PEP 484 for more details. By default generic types are invariant
439 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700440
441 Type variables can be introspected. e.g.:
442
443 T.__name__ == 'T'
444 T.__constraints__ == ()
445 T.__covariant__ == False
446 T.__contravariant__ = False
447 A.__constraints__ == (str, bytes)
448 """
449
Guido van Rossum4cefe742016-09-27 15:20:12 -0700450 __slots__ = ('__name__', '__bound__', '__constraints__',
451 '__covariant__', '__contravariant__')
452
453 def __init__(self, name, *constraints, bound=None,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700454 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700455 super().__init__(name, *constraints, bound=bound,
456 covariant=covariant, contravariant=contravariant)
457 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700458 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700459 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700460 self.__covariant__ = bool(covariant)
461 self.__contravariant__ = bool(contravariant)
462 if constraints and bound is not None:
463 raise TypeError("Constraints cannot be combined with bound=...")
464 if constraints and len(constraints) == 1:
465 raise TypeError("A single constraint is not allowed")
466 msg = "TypeVar(name, constraint, ...): constraints must be types."
467 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
468 if bound:
469 self.__bound__ = _type_check(bound, "Bound must be a type.")
470 else:
471 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700472
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700473 def _get_type_vars(self, tvars):
474 if self not in tvars:
475 tvars.append(self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700476
477 def __repr__(self):
478 if self.__covariant__:
479 prefix = '+'
480 elif self.__contravariant__:
481 prefix = '-'
482 else:
483 prefix = '~'
484 return prefix + self.__name__
485
486 def __instancecheck__(self, instance):
487 raise TypeError("Type variables cannot be used with isinstance().")
488
489 def __subclasscheck__(self, cls):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700490 raise TypeError("Type variables cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700491
492
493# Some unconstrained type variables. These are used by the container types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700494# (These are not for export.)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700495T = TypeVar('T') # Any type.
496KT = TypeVar('KT') # Key type.
497VT = TypeVar('VT') # Value type.
498T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
499V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700500VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
501T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
502
503# A useful type variable with constraints. This represents string types.
Guido van Rossumeb9aca32016-05-24 16:38:22 -0700504# (This one *is* for export!)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700505AnyStr = TypeVar('AnyStr', bytes, str)
506
507
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700508def _replace_arg(arg, tvars, args):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800509 """An internal helper function: replace arg if it is a type variable
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700510 found in tvars with corresponding substitution from args or
511 with corresponding substitution sub-tree if arg is a generic type.
512 """
513
514 if tvars is None:
515 tvars = []
516 if hasattr(arg, '_subs_tree'):
517 return arg._subs_tree(tvars, args)
518 if isinstance(arg, TypeVar):
519 for i, tvar in enumerate(tvars):
520 if arg == tvar:
521 return args[i]
522 return arg
523
524
525def _subs_tree(cls, tvars=None, args=None):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800526 """An internal helper function: calculate substitution tree
527 for generic cls after replacing its type parameters with
528 substitutions in tvars -> args (if any).
529 Repeat the same following __origin__'s.
530
531 Return a list of arguments with all possible substitutions
532 performed. Arguments that are generic classes themselves are represented
533 as tuples (so that no new classes are created by this function).
534 For example: _subs_tree(List[Tuple[int, T]][str]) == [(Tuple, int, str)]
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700535 """
536
537 if cls.__origin__ is None:
538 return cls
539 # Make of chain of origins (i.e. cls -> cls.__origin__)
540 current = cls.__origin__
541 orig_chain = []
542 while current.__origin__ is not None:
543 orig_chain.append(current)
544 current = current.__origin__
545 # Replace type variables in __args__ if asked ...
546 tree_args = []
547 for arg in cls.__args__:
548 tree_args.append(_replace_arg(arg, tvars, args))
549 # ... then continue replacing down the origin chain.
550 for ocls in orig_chain:
551 new_tree_args = []
552 for i, arg in enumerate(ocls.__args__):
553 new_tree_args.append(_replace_arg(arg, ocls.__parameters__, tree_args))
554 tree_args = new_tree_args
555 return tree_args
556
557
558def _remove_dups_flatten(parameters):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800559 """An internal helper for Union creation and substitution: flatten Union's
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700560 among parameters, then remove duplicates and strict subclasses.
561 """
562
563 # Flatten out Union[Union[...], ...].
564 params = []
565 for p in parameters:
566 if isinstance(p, _Union) and p.__origin__ is Union:
567 params.extend(p.__args__)
568 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
569 params.extend(p[1:])
570 else:
571 params.append(p)
572 # Weed out strict duplicates, preserving the first of each occurrence.
573 all_params = set(params)
574 if len(all_params) < len(params):
575 new_params = []
576 for t in params:
577 if t in all_params:
578 new_params.append(t)
579 all_params.remove(t)
580 params = new_params
581 assert not all_params, all_params
582 # Weed out subclasses.
583 # E.g. Union[int, Employee, Manager] == Union[int, Employee].
584 # If object is present it will be sole survivor among proper classes.
585 # Never discard type variables.
586 # (In particular, Union[str, AnyStr] != AnyStr.)
587 all_params = set(params)
588 for t1 in params:
589 if not isinstance(t1, type):
590 continue
591 if any(isinstance(t2, type) and issubclass(t1, t2)
592 for t2 in all_params - {t1}
593 if not (isinstance(t2, GenericMeta) and
594 t2.__origin__ is not None)):
595 all_params.remove(t1)
596 return tuple(t for t in params if t in all_params)
597
598
599def _check_generic(cls, parameters):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800600 # Check correct count for parameters of a generic cls (internal helper).
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700601 if not cls.__parameters__:
602 raise TypeError("%s is not a generic class" % repr(cls))
603 alen = len(parameters)
604 elen = len(cls.__parameters__)
605 if alen != elen:
606 raise TypeError("Too %s parameters for %s; actual %s, expected %s" %
607 ("many" if alen > elen else "few", repr(cls), alen, elen))
608
609
Guido van Rossum9b107562016-11-09 13:23:04 -0800610_cleanups = []
611
612
Guido van Rossum4cefe742016-09-27 15:20:12 -0700613def _tp_cache(func):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800614 """Internal wrapper caching __getitem__ of generic types with a fallback to
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700615 original function for non-hashable arguments.
616 """
617
Guido van Rossum4cefe742016-09-27 15:20:12 -0700618 cached = functools.lru_cache()(func)
Guido van Rossum9b107562016-11-09 13:23:04 -0800619 _cleanups.append(cached.cache_clear)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700620 @functools.wraps(func)
621 def inner(*args, **kwds):
622 try:
623 return cached(*args, **kwds)
624 except TypeError:
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700625 pass # All real errors (not unhashable args) are raised below.
Guido van Rossum4cefe742016-09-27 15:20:12 -0700626 return func(*args, **kwds)
627 return inner
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700628
629
Guido van Rossum4cefe742016-09-27 15:20:12 -0700630class _Union(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700631 """Union type; Union[X, Y] means either X or Y.
632
633 To define a union, use e.g. Union[int, str]. Details:
634
635 - The arguments must be types and there must be at least one.
636
637 - None as an argument is a special case and is replaced by
638 type(None).
639
640 - Unions of unions are flattened, e.g.::
641
642 Union[Union[int, str], float] == Union[int, str, float]
643
644 - Unions of a single argument vanish, e.g.::
645
646 Union[int] == int # The constructor actually returns int
647
648 - Redundant arguments are skipped, e.g.::
649
650 Union[int, str, int] == Union[int, str]
651
652 - When comparing unions, the argument order is ignored, e.g.::
653
654 Union[int, str] == Union[str, int]
655
656 - When two arguments have a subclass relationship, the least
657 derived argument is kept, e.g.::
658
659 class Employee: pass
660 class Manager(Employee): pass
661 Union[int, Employee, Manager] == Union[int, Employee]
662 Union[Manager, int, Employee] == Union[int, Employee]
663 Union[Employee, Manager] == Employee
664
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700665 - Similar for object::
666
667 Union[int, object] == object
668
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700669 - You cannot subclass or instantiate a union.
670
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700671 - You can use Optional[X] as a shorthand for Union[X, None].
672 """
673
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700674 __slots__ = ('__parameters__', '__args__', '__origin__', '__tree_hash__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700675
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700676 def __new__(cls, parameters=None, origin=None, *args, _root=False):
677 self = super().__new__(cls, parameters, origin, *args, _root=_root)
678 if origin is None:
679 self.__parameters__ = None
680 self.__args__ = None
681 self.__origin__ = None
682 self.__tree_hash__ = hash(frozenset(('Union',)))
Guido van Rossum4cefe742016-09-27 15:20:12 -0700683 return self
684 if not isinstance(parameters, tuple):
685 raise TypeError("Expected parameters=<tuple>")
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700686 if origin is Union:
687 parameters = _remove_dups_flatten(parameters)
688 # It's not a union if there's only one type left.
689 if len(parameters) == 1:
690 return parameters[0]
691 self.__parameters__ = _type_vars(parameters)
692 self.__args__ = parameters
693 self.__origin__ = origin
694 # Pre-calculate the __hash__ on instantiation.
695 # This improves speed for complex substitutions.
696 subs_tree = self._subs_tree()
697 if isinstance(subs_tree, tuple):
698 self.__tree_hash__ = hash(frozenset(subs_tree))
699 else:
700 self.__tree_hash__ = hash(subs_tree)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700701 return self
702
703 def _eval_type(self, globalns, localns):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700704 if self.__args__ is None:
Guido van Rossum4cefe742016-09-27 15:20:12 -0700705 return self
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700706 ev_args = tuple(_eval_type(t, globalns, localns) for t in self.__args__)
707 ev_origin = _eval_type(self.__origin__, globalns, localns)
708 if ev_args == self.__args__ and ev_origin == self.__origin__:
709 # Everything is already evaluated.
710 return self
711 return self.__class__(ev_args, ev_origin, _root=True)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700712
713 def _get_type_vars(self, tvars):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700714 if self.__origin__ and self.__parameters__:
715 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700716
717 def __repr__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700718 if self.__origin__ is None:
719 return super().__repr__()
720 tree = self._subs_tree()
721 if not isinstance(tree, tuple):
722 return repr(tree)
723 return tree[0]._tree_repr(tree)
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700724
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700725 def _tree_repr(self, tree):
726 arg_list = []
727 for arg in tree[1:]:
728 if not isinstance(arg, tuple):
729 arg_list.append(_type_repr(arg))
730 else:
731 arg_list.append(arg[0]._tree_repr(arg))
732 return super().__repr__() + '[%s]' % ', '.join(arg_list)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700733
734 @_tp_cache
735 def __getitem__(self, parameters):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700736 if parameters == ():
737 raise TypeError("Cannot take a Union of no types.")
738 if not isinstance(parameters, tuple):
739 parameters = (parameters,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700740 if self.__origin__ is None:
741 msg = "Union[arg, ...]: each arg must be a type."
742 else:
743 msg = "Parameters to generic types must be types."
744 parameters = tuple(_type_check(p, msg) for p in parameters)
745 if self is not Union:
746 _check_generic(self, parameters)
747 return self.__class__(parameters, origin=self, _root=True)
748
749 def _subs_tree(self, tvars=None, args=None):
750 if self is Union:
751 return Union # Nothing to substitute
752 tree_args = _subs_tree(self, tvars, args)
753 tree_args = _remove_dups_flatten(tree_args)
754 if len(tree_args) == 1:
755 return tree_args[0] # Union of a single type is that type
756 return (Union,) + tree_args
Guido van Rossum4cefe742016-09-27 15:20:12 -0700757
758 def __eq__(self, other):
759 if not isinstance(other, _Union):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700760 return self._subs_tree() == other
761 return self.__tree_hash__ == other.__tree_hash__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700762
763 def __hash__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700764 return self.__tree_hash__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700765
766 def __instancecheck__(self, obj):
767 raise TypeError("Unions cannot be used with isinstance().")
768
769 def __subclasscheck__(self, cls):
770 raise TypeError("Unions cannot be used with issubclass().")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700771
772
Guido van Rossum4cefe742016-09-27 15:20:12 -0700773Union = _Union(_root=True)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700774
775
Guido van Rossum4cefe742016-09-27 15:20:12 -0700776class _Optional(_FinalTypingBase, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700777 """Optional type.
778
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700779 Optional[X] is equivalent to Union[X, None].
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700780 """
781
Guido van Rossumd70fe632015-08-05 12:11:06 +0200782 __slots__ = ()
783
Guido van Rossum4cefe742016-09-27 15:20:12 -0700784 @_tp_cache
785 def __getitem__(self, arg):
786 arg = _type_check(arg, "Optional[t] requires a single type.")
787 return Union[arg, type(None)]
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700788
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700789
Guido van Rossum4cefe742016-09-27 15:20:12 -0700790Optional = _Optional(_root=True)
791
792
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700793def _gorg(a):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800794 """Return the farthest origin of a generic class (internal helper)."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700795 assert isinstance(a, GenericMeta)
796 while a.__origin__ is not None:
797 a = a.__origin__
798 return a
799
800
801def _geqv(a, b):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800802 """Return whether two generic classes are equivalent (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700803
804 The intention is to consider generic class X and any of its
Guido van Rossumb24569a2016-11-20 18:01:29 -0800805 parameterized forms (X[T], X[int], etc.) as equivalent.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700806
807 However, X is not equivalent to a subclass of X.
808
809 The relation is reflexive, symmetric and transitive.
810 """
811 assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta)
812 # Reduce each to its origin.
813 return _gorg(a) is _gorg(b)
814
815
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700816def _next_in_mro(cls):
817 """Helper for Generic.__new__.
818
819 Returns the class after the last occurrence of Generic or
820 Generic[...] in cls.__mro__.
821 """
822 next_in_mro = object
823 # Look for the last occurrence of Generic or Generic[...].
824 for i, c in enumerate(cls.__mro__[:-1]):
825 if isinstance(c, GenericMeta) and _gorg(c) is Generic:
826 next_in_mro = cls.__mro__[i+1]
827 return next_in_mro
828
829
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700830def _valid_for_check(cls):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800831 """An internal helper to prohibit isinstance([1], List[str]) etc."""
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700832 if cls is Generic:
833 raise TypeError("Class %r cannot be used with class "
834 "or instance checks" % cls)
835 if (cls.__origin__ is not None and
836 sys._getframe(3).f_globals['__name__'] not in ['abc', 'functools']):
837 raise TypeError("Parameterized generics cannot be used with class "
838 "or instance checks")
839
840
841def _make_subclasshook(cls):
842 """Construct a __subclasshook__ callable that incorporates
843 the associated __extra__ class in subclass checks performed
844 against cls.
845 """
846 if isinstance(cls.__extra__, abc.ABCMeta):
847 # The logic mirrors that of ABCMeta.__subclasscheck__.
848 # Registered classes need not be checked here because
849 # cls and its extra share the same _abc_registry.
850 def __extrahook__(subclass):
851 _valid_for_check(cls)
852 res = cls.__extra__.__subclasshook__(subclass)
853 if res is not NotImplemented:
854 return res
855 if cls.__extra__ in subclass.__mro__:
856 return True
857 for scls in cls.__extra__.__subclasses__():
858 if isinstance(scls, GenericMeta):
859 continue
860 if issubclass(subclass, scls):
861 return True
862 return NotImplemented
863 else:
864 # For non-ABC extras we'll just call issubclass().
865 def __extrahook__(subclass):
866 _valid_for_check(cls)
867 if cls.__extra__ and issubclass(subclass, cls.__extra__):
868 return True
869 return NotImplemented
870 return __extrahook__
871
872
Guido van Rossum61f0a022016-11-29 09:46:21 -0800873def _no_slots_copy(dct):
874 """Internal helper: copy class __dict__ and clean slots class variables.
875 (They will be re-created if necessary by normal class machinery.)
876 """
877 dict_copy = dict(dct)
878 if '__slots__' in dict_copy:
879 for slot in dict_copy['__slots__']:
880 dict_copy.pop(slot, None)
881 return dict_copy
882
883
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700884class GenericMeta(TypingMeta, abc.ABCMeta):
885 """Metaclass for generic types."""
886
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700887 def __new__(cls, name, bases, namespace,
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700888 tvars=None, args=None, origin=None, extra=None, orig_bases=None):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700889 if tvars is not None:
890 # Called from __getitem__() below.
891 assert origin is not None
892 assert all(isinstance(t, TypeVar) for t in tvars), tvars
893 else:
894 # Called from class statement.
895 assert tvars is None, tvars
896 assert args is None, args
897 assert origin is None, origin
898
899 # Get the full set of tvars from the bases.
900 tvars = _type_vars(bases)
901 # Look for Generic[T1, ..., Tn].
902 # If found, tvars must be a subset of it.
903 # If not found, tvars is it.
904 # Also check for and reject plain Generic,
905 # and reject multiple Generic[...].
906 gvars = None
907 for base in bases:
908 if base is Generic:
909 raise TypeError("Cannot inherit from plain Generic")
910 if (isinstance(base, GenericMeta) and
911 base.__origin__ is Generic):
912 if gvars is not None:
913 raise TypeError(
914 "Cannot inherit from Generic[...] multiple types.")
915 gvars = base.__parameters__
916 if gvars is None:
917 gvars = tvars
918 else:
919 tvarset = set(tvars)
920 gvarset = set(gvars)
921 if not tvarset <= gvarset:
922 raise TypeError(
923 "Some type variables (%s) "
924 "are not listed in Generic[%s]" %
925 (", ".join(str(t) for t in tvars if t not in gvarset),
926 ", ".join(str(g) for g in gvars)))
927 tvars = gvars
928
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700929 initial_bases = bases
930 if extra is not None and type(extra) is abc.ABCMeta and extra not in bases:
931 bases = (extra,) + bases
932 bases = tuple(_gorg(b) if isinstance(b, GenericMeta) else b for b in bases)
933
934 # remove bare Generic from bases if there are other generic bases
935 if any(isinstance(b, GenericMeta) and b is not Generic for b in bases):
936 bases = tuple(b for b in bases if b is not Generic)
937 self = super().__new__(cls, name, bases, namespace, _root=True)
938
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700939 self.__parameters__ = tvars
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700940 # Be prepared that GenericMeta will be subclassed by TupleMeta
941 # and CallableMeta, those two allow ..., (), or [] in __args___.
942 self.__args__ = tuple(... if a is _TypingEllipsis else
943 () if a is _TypingEmpty else
944 a for a in args) if args else None
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700945 self.__origin__ = origin
Guido van Rossum1cea70f2016-05-18 08:35:00 -0700946 self.__extra__ = extra
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700947 # Speed hack (https://github.com/python/typing/issues/196).
948 self.__next_in_mro__ = _next_in_mro(self)
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700949 # Preserve base classes on subclassing (__bases__ are type erased now).
950 if orig_bases is None:
951 self.__orig_bases__ = initial_bases
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700952
953 # This allows unparameterized generic collections to be used
954 # with issubclass() and isinstance() in the same way as their
955 # collections.abc counterparts (e.g., isinstance([], Iterable)).
Guido van Rossume2592672016-10-08 20:27:22 -0700956 if ('__subclasshook__' not in namespace and extra # allow overriding
957 or hasattr(self.__subclasshook__, '__name__') and
958 self.__subclasshook__.__name__ == '__extrahook__'):
959 self.__subclasshook__ = _make_subclasshook(self)
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700960 if isinstance(extra, abc.ABCMeta):
961 self._abc_registry = extra._abc_registry
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700962
963 if origin and hasattr(origin, '__qualname__'): # Fix for Python 3.2.
964 self.__qualname__ = origin.__qualname__
965 self.__tree_hash__ = hash(self._subs_tree()) if origin else hash((self.__name__,))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700966 return self
967
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700968 def _get_type_vars(self, tvars):
969 if self.__origin__ and self.__parameters__:
970 _get_type_vars(self.__parameters__, tvars)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700971
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700972 def _eval_type(self, globalns, localns):
973 ev_origin = (self.__origin__._eval_type(globalns, localns)
974 if self.__origin__ else None)
975 ev_args = tuple(_eval_type(a, globalns, localns) for a
976 in self.__args__) if self.__args__ else None
977 if ev_origin == self.__origin__ and ev_args == self.__args__:
978 return self
979 return self.__class__(self.__name__,
980 self.__bases__,
Guido van Rossum61f0a022016-11-29 09:46:21 -0800981 _no_slots_copy(self.__dict__),
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700982 tvars=_type_vars(ev_args) if ev_args else None,
983 args=ev_args,
984 origin=ev_origin,
985 extra=self.__extra__,
986 orig_bases=self.__orig_bases__)
987
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700988 def __repr__(self):
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700989 if self.__origin__ is None:
990 return super().__repr__()
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700991 return self._tree_repr(self._subs_tree())
Guido van Rossum7ef22d62016-10-21 14:27:58 -0700992
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700993 def _tree_repr(self, tree):
994 arg_list = []
995 for arg in tree[1:]:
996 if arg == ():
997 arg_list.append('()')
998 elif not isinstance(arg, tuple):
999 arg_list.append(_type_repr(arg))
1000 else:
1001 arg_list.append(arg[0]._tree_repr(arg))
1002 return super().__repr__() + '[%s]' % ', '.join(arg_list)
1003
1004 def _subs_tree(self, tvars=None, args=None):
1005 if self.__origin__ is None:
1006 return self
1007 tree_args = _subs_tree(self, tvars, args)
1008 return (_gorg(self),) + tuple(tree_args)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001009
1010 def __eq__(self, other):
1011 if not isinstance(other, GenericMeta):
1012 return NotImplemented
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001013 if self.__origin__ is None or other.__origin__ is None:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001014 return self is other
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001015 return self.__tree_hash__ == other.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001016
1017 def __hash__(self):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001018 return self.__tree_hash__
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001019
Guido van Rossum4cefe742016-09-27 15:20:12 -07001020 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001021 def __getitem__(self, params):
1022 if not isinstance(params, tuple):
1023 params = (params,)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001024 if not params and not _gorg(self) is Tuple:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001025 raise TypeError(
1026 "Parameter list to %s[...] cannot be empty" % _qualname(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001027 msg = "Parameters to generic types must be types."
1028 params = tuple(_type_check(p, msg) for p in params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001029 if self is Generic:
1030 # Generic can only be subscripted with unique type variables.
1031 if not all(isinstance(p, TypeVar) for p in params):
1032 raise TypeError(
1033 "Parameters to Generic[...] must all be type variables")
Guido van Rossumd70fe632015-08-05 12:11:06 +02001034 if len(set(params)) != len(params):
Guido van Rossum1b669102015-09-04 12:15:54 -07001035 raise TypeError(
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001036 "Parameters to Generic[...] must all be unique")
1037 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001038 args = params
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001039 elif self in (Tuple, Callable):
1040 tvars = _type_vars(params)
1041 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001042 elif self is _Protocol:
1043 # _Protocol is internal, don't check anything.
1044 tvars = params
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001045 args = params
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001046 elif self.__origin__ in (Generic, _Protocol):
1047 # Can't subscript Generic[...] or _Protocol[...].
1048 raise TypeError("Cannot subscript already-subscripted %s" %
1049 repr(self))
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001050 else:
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001051 # Subscripting a regular Generic subclass.
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001052 _check_generic(self, params)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001053 tvars = _type_vars(params)
1054 args = params
1055 return self.__class__(self.__name__,
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001056 self.__bases__,
Guido van Rossum61f0a022016-11-29 09:46:21 -08001057 _no_slots_copy(self.__dict__),
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001058 tvars=tvars,
1059 args=args,
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001060 origin=self,
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001061 extra=self.__extra__,
1062 orig_bases=self.__orig_bases__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001063
Guido van Rossum1b669102015-09-04 12:15:54 -07001064 def __instancecheck__(self, instance):
1065 # Since we extend ABC.__subclasscheck__ and
1066 # ABC.__instancecheck__ inlines the cache checking done by the
1067 # latter, we must extend __instancecheck__ too. For simplicity
1068 # we just skip the cache check -- instance checks for generic
1069 # classes are supposed to be rare anyways.
Guido van Rossumb47c9d22016-10-03 08:40:50 -07001070 return issubclass(instance.__class__, self)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001071
Guido van Rossumb7dedc82016-10-29 12:44:29 -07001072 def __copy__(self):
Guido van Rossum61f0a022016-11-29 09:46:21 -08001073 return self.__class__(self.__name__, self.__bases__,
1074 _no_slots_copy(self.__dict__),
Guido van Rossumb7dedc82016-10-29 12:44:29 -07001075 self.__parameters__, self.__args__, self.__origin__,
1076 self.__extra__, self.__orig_bases__)
1077
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001078
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001079# Prevent checks for Generic to crash when defining Generic.
1080Generic = None
1081
1082
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001083def _generic_new(base_cls, cls, *args, **kwds):
1084 # Assure type is erased on instantiation,
1085 # but attempt to store it in __orig_class__
1086 if cls.__origin__ is None:
1087 return base_cls.__new__(cls)
1088 else:
1089 origin = _gorg(cls)
1090 obj = base_cls.__new__(origin)
1091 try:
1092 obj.__orig_class__ = cls
1093 except AttributeError:
1094 pass
1095 obj.__init__(*args, **kwds)
1096 return obj
1097
1098
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001099class Generic(metaclass=GenericMeta):
1100 """Abstract base class for generic types.
1101
Guido van Rossumb24569a2016-11-20 18:01:29 -08001102 A generic type is typically declared by inheriting from
1103 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001104 For example, a generic mapping type might be defined as::
1105
1106 class Mapping(Generic[KT, VT]):
1107 def __getitem__(self, key: KT) -> VT:
1108 ...
1109 # Etc.
1110
1111 This class can then be used as follows::
1112
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001113 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001114 try:
1115 return mapping[key]
1116 except KeyError:
1117 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001118 """
1119
Guido van Rossumd70fe632015-08-05 12:11:06 +02001120 __slots__ = ()
1121
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001122 def __new__(cls, *args, **kwds):
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001123 if _geqv(cls, Generic):
1124 raise TypeError("Type Generic cannot be instantiated; "
1125 "it can be used only as a base class")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001126 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
1127
1128
1129class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001130 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1131 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001132 to sneak in where prohibited.
1133 """
1134
1135
1136class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001137 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001138
1139
1140class TupleMeta(GenericMeta):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001141 """Metaclass for Tuple (internal)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001142
1143 @_tp_cache
1144 def __getitem__(self, parameters):
1145 if self.__origin__ is not None or not _geqv(self, Tuple):
1146 # Normal generic rules apply if this is not the first subscription
1147 # or a subscription of a subclass.
1148 return super().__getitem__(parameters)
1149 if parameters == ():
1150 return super().__getitem__((_TypingEmpty,))
1151 if not isinstance(parameters, tuple):
1152 parameters = (parameters,)
1153 if len(parameters) == 2 and parameters[1] is ...:
1154 msg = "Tuple[t, ...]: t must be a type."
1155 p = _type_check(parameters[0], msg)
1156 return super().__getitem__((p, _TypingEllipsis))
1157 msg = "Tuple[t0, t1, ...]: each t must be a type."
1158 parameters = tuple(_type_check(p, msg) for p in parameters)
1159 return super().__getitem__(parameters)
1160
1161 def __instancecheck__(self, obj):
1162 if self.__args__ == None:
1163 return isinstance(obj, tuple)
1164 raise TypeError("Parameterized Tuple cannot be used "
1165 "with isinstance().")
1166
1167 def __subclasscheck__(self, cls):
1168 if self.__args__ == None:
1169 return issubclass(cls, tuple)
1170 raise TypeError("Parameterized Tuple cannot be used "
1171 "with issubclass().")
1172
1173
1174class Tuple(tuple, extra=tuple, metaclass=TupleMeta):
1175 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
1176
1177 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1178 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1179 of an int, a float and a string.
1180
1181 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1182 """
1183
1184 __slots__ = ()
1185
1186 def __new__(cls, *args, **kwds):
1187 if _geqv(cls, Tuple):
1188 raise TypeError("Type Tuple cannot be instantiated; "
1189 "use tuple() instead")
1190 return _generic_new(tuple, cls, *args, **kwds)
1191
1192
1193class CallableMeta(GenericMeta):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001194 """Metaclass for Callable (internal)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001195
1196 def __repr__(self):
1197 if self.__origin__ is None:
1198 return super().__repr__()
1199 return self._tree_repr(self._subs_tree())
1200
1201 def _tree_repr(self, tree):
1202 if _gorg(self) is not Callable:
1203 return super()._tree_repr(tree)
1204 # For actual Callable (not its subclass) we override
1205 # super()._tree_repr() for nice formatting.
1206 arg_list = []
1207 for arg in tree[1:]:
Guido van Rossum991d14f2016-11-09 13:12:51 -08001208 if not isinstance(arg, tuple):
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001209 arg_list.append(_type_repr(arg))
1210 else:
1211 arg_list.append(arg[0]._tree_repr(arg))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001212 if arg_list[0] == '...':
1213 return repr(tree[0]) + '[..., %s]' % arg_list[1]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001214 return (repr(tree[0]) +
1215 '[[%s], %s]' % (', '.join(arg_list[:-1]), arg_list[-1]))
1216
1217 def __getitem__(self, parameters):
Guido van Rossumb24569a2016-11-20 18:01:29 -08001218 """A thin wrapper around __getitem_inner__ to provide the latter
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001219 with hashable arguments to improve speed.
1220 """
1221
1222 if self.__origin__ is not None or not _geqv(self, Callable):
1223 return super().__getitem__(parameters)
1224 if not isinstance(parameters, tuple) or len(parameters) != 2:
1225 raise TypeError("Callable must be used as "
1226 "Callable[[arg, ...], result].")
1227 args, result = parameters
Guido van Rossum991d14f2016-11-09 13:12:51 -08001228 if args is Ellipsis:
1229 parameters = (Ellipsis, result)
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001230 else:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001231 if not isinstance(args, list):
1232 raise TypeError("Callable[args, result]: args must be a list."
1233 " Got %.100r." % (args,))
Guido van Rossum991d14f2016-11-09 13:12:51 -08001234 parameters = (tuple(args), result)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001235 return self.__getitem_inner__(parameters)
1236
1237 @_tp_cache
1238 def __getitem_inner__(self, parameters):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001239 args, result = parameters
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001240 msg = "Callable[args, result]: result must be a type."
1241 result = _type_check(result, msg)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001242 if args is Ellipsis:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001243 return super().__getitem__((_TypingEllipsis, result))
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001244 msg = "Callable[[arg, ...], result]: each arg must be a type."
1245 args = tuple(_type_check(arg, msg) for arg in args)
1246 parameters = args + (result,)
1247 return super().__getitem__(parameters)
1248
1249
1250class Callable(extra=collections_abc.Callable, metaclass = CallableMeta):
1251 """Callable type; Callable[[int], str] is a function of (int) -> str.
1252
1253 The subscription syntax must always be used with exactly two
1254 values: the argument list and the return type. The argument list
Guido van Rossumb24569a2016-11-20 18:01:29 -08001255 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001256
1257 There is no syntax to indicate optional or keyword arguments,
1258 such function types are rarely used as callback types.
1259 """
1260
1261 __slots__ = ()
1262
1263 def __new__(cls, *args, **kwds):
1264 if _geqv(cls, Callable):
1265 raise TypeError("Type Callable cannot be instantiated; "
1266 "use a non-abstract subclass instead")
1267 return _generic_new(cls.__next_in_mro__, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001268
1269
Guido van Rossum4cefe742016-09-27 15:20:12 -07001270class _ClassVar(_FinalTypingBase, _root=True):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001271 """Special type construct to mark class variables.
1272
1273 An annotation wrapped in ClassVar indicates that a given
1274 attribute is intended to be used as a class variable and
1275 should not be set on instances of that class. Usage::
1276
1277 class Starship:
1278 stats: ClassVar[Dict[str, int]] = {} # class variable
1279 damage: int = 10 # instance variable
1280
1281 ClassVar accepts only types and cannot be further subscribed.
1282
1283 Note that ClassVar is not a class itself, and should not
1284 be used with isinstance() or issubclass().
1285 """
1286
Guido van Rossum4cefe742016-09-27 15:20:12 -07001287 __slots__ = ('__type__',)
1288
1289 def __init__(self, tp=None, **kwds):
1290 self.__type__ = tp
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001291
1292 def __getitem__(self, item):
1293 cls = type(self)
1294 if self.__type__ is None:
1295 return cls(_type_check(item,
Guido van Rossum4cefe742016-09-27 15:20:12 -07001296 '{} accepts only single type.'.format(cls.__name__[1:])),
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001297 _root=True)
1298 raise TypeError('{} cannot be further subscripted'
1299 .format(cls.__name__[1:]))
1300
1301 def _eval_type(self, globalns, localns):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001302 new_tp = _eval_type(self.__type__, globalns, localns)
1303 if new_tp == self.__type__:
1304 return self
1305 return type(self)(new_tp, _root=True)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001306
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001307 def __repr__(self):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001308 r = super().__repr__()
1309 if self.__type__ is not None:
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001310 r += '[{}]'.format(_type_repr(self.__type__))
Guido van Rossum4cefe742016-09-27 15:20:12 -07001311 return r
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001312
1313 def __hash__(self):
1314 return hash((type(self).__name__, self.__type__))
1315
1316 def __eq__(self, other):
1317 if not isinstance(other, _ClassVar):
1318 return NotImplemented
1319 if self.__type__ is not None:
1320 return self.__type__ == other.__type__
1321 return self is other
1322
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001323
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001324ClassVar = _ClassVar(_root=True)
1325
1326
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001327def cast(typ, val):
1328 """Cast a value to a type.
1329
1330 This returns the value unchanged. To the type checker this
1331 signals that the return value has the designated type, but at
1332 runtime we intentionally don't check anything (we want this
1333 to be as fast as possible).
1334 """
1335 return val
1336
1337
1338def _get_defaults(func):
1339 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001340 try:
1341 code = func.__code__
1342 except AttributeError:
1343 # Some built-in functions don't have __code__, __defaults__, etc.
1344 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001345 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001346 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001347 arg_names = arg_names[:pos_count]
1348 defaults = func.__defaults__ or ()
1349 kwdefaults = func.__kwdefaults__
1350 res = dict(kwdefaults) if kwdefaults else {}
1351 pos_offset = pos_count - len(defaults)
1352 for name, value in zip(arg_names[pos_offset:], defaults):
1353 assert name not in res
1354 res[name] = value
1355 return res
1356
1357
Guido van Rossum991d14f2016-11-09 13:12:51 -08001358def get_type_hints(obj, globalns=None, localns=None):
1359 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001360
Guido van Rossum991d14f2016-11-09 13:12:51 -08001361 This is often the same as obj.__annotations__, but it handles
1362 forward references encoded as string literals, and if necessary
1363 adds Optional[t] if a default value equal to None is set.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001364
Guido van Rossum991d14f2016-11-09 13:12:51 -08001365 The argument may be a module, class, method, or function. The annotations
1366 are returned as a dictionary. For classes, annotations include also
1367 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001368
Guido van Rossum991d14f2016-11-09 13:12:51 -08001369 TypeError is raised if the argument is not of a type that can contain
1370 annotations, and an empty dictionary is returned if no annotations are
1371 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001372
Guido van Rossum991d14f2016-11-09 13:12:51 -08001373 BEWARE -- the behavior of globalns and localns is counterintuitive
1374 (unless you are familiar with how eval() and exec() work). The
1375 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001376
Guido van Rossum991d14f2016-11-09 13:12:51 -08001377 - If no dict arguments are passed, an attempt is made to use the
1378 globals from obj, and these are also used as the locals. If the
1379 object does not appear to have globals, an exception is raised.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001380
Guido van Rossum991d14f2016-11-09 13:12:51 -08001381 - If one dict argument is passed, it is used for both globals and
1382 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001383
Guido van Rossum991d14f2016-11-09 13:12:51 -08001384 - If two dict arguments are passed, they specify globals and
1385 locals, respectively.
1386 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001387
Guido van Rossum991d14f2016-11-09 13:12:51 -08001388 if getattr(obj, '__no_type_check__', None):
1389 return {}
1390 if globalns is None:
1391 globalns = getattr(obj, '__globals__', {})
1392 if localns is None:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001393 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001394 elif localns is None:
1395 localns = globalns
1396 # Classes require a special treatment.
1397 if isinstance(obj, type):
1398 hints = {}
1399 for base in reversed(obj.__mro__):
1400 ann = base.__dict__.get('__annotations__', {})
1401 for name, value in ann.items():
1402 if value is None:
1403 value = type(None)
1404 if isinstance(value, str):
1405 value = _ForwardRef(value)
1406 value = _eval_type(value, globalns, localns)
1407 hints[name] = value
1408 return hints
1409 hints = getattr(obj, '__annotations__', None)
1410 if hints is None:
1411 # Return empty annotations for something that _could_ have them.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001412 if (isinstance(obj, types.FunctionType) or
1413 isinstance(obj, types.BuiltinFunctionType) or
Guido van Rossum991d14f2016-11-09 13:12:51 -08001414 isinstance(obj, types.MethodType) or
1415 isinstance(obj, types.ModuleType)):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001416 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001417 else:
1418 raise TypeError('{!r} is not a module, class, method, '
1419 'or function.'.format(obj))
1420 defaults = _get_defaults(obj)
1421 hints = dict(hints)
1422 for name, value in hints.items():
1423 if value is None:
1424 value = type(None)
1425 if isinstance(value, str):
1426 value = _ForwardRef(value)
1427 value = _eval_type(value, globalns, localns)
1428 if name in defaults and defaults[name] is None:
1429 value = Optional[value]
1430 hints[name] = value
1431 return hints
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001432
1433
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001434def no_type_check(arg):
1435 """Decorator to indicate that annotations are not type hints.
1436
1437 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001438 applies recursively to all methods and classes defined in that class
1439 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001440
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001441 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001442 """
1443 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001444 arg_attrs = arg.__dict__.copy()
1445 for attr, val in arg.__dict__.items():
1446 if val in arg.__bases__:
1447 arg_attrs.pop(attr)
1448 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001449 if isinstance(obj, types.FunctionType):
1450 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001451 if isinstance(obj, type):
1452 no_type_check(obj)
1453 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001454 arg.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001455 except TypeError: # built-in classes
1456 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001457 return arg
1458
1459
1460def no_type_check_decorator(decorator):
1461 """Decorator to give another decorator the @no_type_check effect.
1462
1463 This wraps the decorator with something that wraps the decorated
1464 function in @no_type_check.
1465 """
1466
1467 @functools.wraps(decorator)
1468 def wrapped_decorator(*args, **kwds):
1469 func = decorator(*args, **kwds)
1470 func = no_type_check(func)
1471 return func
1472
1473 return wrapped_decorator
1474
1475
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001476def _overload_dummy(*args, **kwds):
1477 """Helper for @overload to raise when called."""
1478 raise NotImplementedError(
1479 "You should not call an overloaded function. "
1480 "A series of @overload-decorated functions "
1481 "outside a stub module should always be followed "
1482 "by an implementation that is not @overload-ed.")
1483
1484
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001485def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001486 """Decorator for overloaded functions/methods.
1487
1488 In a stub file, place two or more stub definitions for the same
1489 function in a row, each decorated with @overload. For example:
1490
1491 @overload
1492 def utf8(value: None) -> None: ...
1493 @overload
1494 def utf8(value: bytes) -> bytes: ...
1495 @overload
1496 def utf8(value: str) -> bytes: ...
1497
1498 In a non-stub file (i.e. a regular .py file), do the same but
1499 follow it with an implementation. The implementation should *not*
1500 be decorated with @overload. For example:
1501
1502 @overload
1503 def utf8(value: None) -> None: ...
1504 @overload
1505 def utf8(value: bytes) -> bytes: ...
1506 @overload
1507 def utf8(value: str) -> bytes: ...
1508 def utf8(value):
1509 # implementation goes here
1510 """
1511 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001512
1513
1514class _ProtocolMeta(GenericMeta):
1515 """Internal metaclass for _Protocol.
1516
1517 This exists so _Protocol classes can be generic without deriving
1518 from Generic.
1519 """
1520
Guido van Rossumd70fe632015-08-05 12:11:06 +02001521 def __instancecheck__(self, obj):
Guido van Rossumca4b2522016-11-19 10:32:41 -08001522 if _Protocol not in self.__bases__:
1523 return super().__instancecheck__(obj)
Guido van Rossumd70fe632015-08-05 12:11:06 +02001524 raise TypeError("Protocols cannot be used with isinstance().")
1525
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001526 def __subclasscheck__(self, cls):
1527 if not self._is_protocol:
1528 # No structural checks since this isn't a protocol.
1529 return NotImplemented
1530
1531 if self is _Protocol:
1532 # Every class is a subclass of the empty protocol.
1533 return True
1534
1535 # Find all attributes defined in the protocol.
1536 attrs = self._get_protocol_attrs()
1537
1538 for attr in attrs:
1539 if not any(attr in d.__dict__ for d in cls.__mro__):
1540 return False
1541 return True
1542
1543 def _get_protocol_attrs(self):
1544 # Get all Protocol base classes.
1545 protocol_bases = []
1546 for c in self.__mro__:
1547 if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol':
1548 protocol_bases.append(c)
1549
1550 # Get attributes included in protocol.
1551 attrs = set()
1552 for base in protocol_bases:
1553 for attr in base.__dict__.keys():
1554 # Include attributes not defined in any non-protocol bases.
1555 for c in self.__mro__:
1556 if (c is not base and attr in c.__dict__ and
1557 not getattr(c, '_is_protocol', False)):
1558 break
1559 else:
1560 if (not attr.startswith('_abc_') and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001561 attr != '__abstractmethods__' and
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001562 attr != '__annotations__' and
1563 attr != '__weakref__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001564 attr != '_is_protocol' and
1565 attr != '__dict__' and
1566 attr != '__args__' and
1567 attr != '__slots__' and
1568 attr != '_get_protocol_attrs' and
1569 attr != '__next_in_mro__' and
1570 attr != '__parameters__' and
1571 attr != '__origin__' and
Guido van Rossum7ef22d62016-10-21 14:27:58 -07001572 attr != '__orig_bases__' and
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001573 attr != '__extra__' and
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001574 attr != '__tree_hash__' and
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001575 attr != '__module__'):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001576 attrs.add(attr)
1577
1578 return attrs
1579
1580
1581class _Protocol(metaclass=_ProtocolMeta):
1582 """Internal base class for protocol classes.
1583
Guido van Rossumb24569a2016-11-20 18:01:29 -08001584 This implements a simple-minded structural issubclass check
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001585 (similar but more general than the one-offs in collections.abc
1586 such as Hashable).
1587 """
1588
Guido van Rossumd70fe632015-08-05 12:11:06 +02001589 __slots__ = ()
1590
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001591 _is_protocol = True
1592
1593
1594# Various ABCs mimicking those in collections.abc.
1595# A few are simply re-exported for completeness.
1596
1597Hashable = collections_abc.Hashable # Not generic.
1598
1599
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001600if hasattr(collections_abc, 'Awaitable'):
1601 class Awaitable(Generic[T_co], extra=collections_abc.Awaitable):
1602 __slots__ = ()
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001603
1604 __all__.append('Awaitable')
1605
1606
1607if hasattr(collections_abc, 'Coroutine'):
1608 class Coroutine(Awaitable[V_co], Generic[T_co, T_contra, V_co],
1609 extra=collections_abc.Coroutine):
1610 __slots__ = ()
1611
1612 __all__.append('Coroutine')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001613
1614
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001615if hasattr(collections_abc, 'AsyncIterable'):
Guido van Rossumf17c2002015-12-03 17:31:24 -08001616
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001617 class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable):
1618 __slots__ = ()
Guido van Rossumf17c2002015-12-03 17:31:24 -08001619
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001620 class AsyncIterator(AsyncIterable[T_co],
1621 extra=collections_abc.AsyncIterator):
1622 __slots__ = ()
1623
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001624 __all__.append('AsyncIterable')
1625 __all__.append('AsyncIterator')
Guido van Rossumf17c2002015-12-03 17:31:24 -08001626
1627
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001628class Iterable(Generic[T_co], extra=collections_abc.Iterable):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001629 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001630
1631
1632class Iterator(Iterable[T_co], extra=collections_abc.Iterator):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001633 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001634
1635
1636class SupportsInt(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001637 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001638
1639 @abstractmethod
1640 def __int__(self) -> int:
1641 pass
1642
1643
1644class SupportsFloat(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001645 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001646
1647 @abstractmethod
1648 def __float__(self) -> float:
1649 pass
1650
1651
1652class SupportsComplex(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001653 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001654
1655 @abstractmethod
1656 def __complex__(self) -> complex:
1657 pass
1658
1659
1660class SupportsBytes(_Protocol):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001661 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001662
1663 @abstractmethod
1664 def __bytes__(self) -> bytes:
1665 pass
1666
1667
Guido van Rossumd70fe632015-08-05 12:11:06 +02001668class SupportsAbs(_Protocol[T_co]):
1669 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001670
1671 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001672 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001673 pass
1674
1675
Guido van Rossumd70fe632015-08-05 12:11:06 +02001676class SupportsRound(_Protocol[T_co]):
1677 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001678
1679 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001680 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001681 pass
1682
1683
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001684if hasattr(collections_abc, 'Reversible'):
1685 class Reversible(Iterable[T_co], extra=collections_abc.Reversible):
1686 __slots__ = ()
1687else:
1688 class Reversible(_Protocol[T_co]):
1689 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001690
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001691 @abstractmethod
1692 def __reversed__(self) -> 'Iterator[T_co]':
1693 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001694
1695
1696Sized = collections_abc.Sized # Not generic.
1697
1698
1699class Container(Generic[T_co], extra=collections_abc.Container):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001700 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001701
1702
Guido van Rossumefa798d2016-08-23 11:01:50 -07001703if hasattr(collections_abc, 'Collection'):
1704 class Collection(Sized, Iterable[T_co], Container[T_co],
1705 extra=collections_abc.Collection):
1706 __slots__ = ()
1707
1708 __all__.append('Collection')
1709
1710
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001711# Callable was defined earlier.
1712
Guido van Rossumefa798d2016-08-23 11:01:50 -07001713if hasattr(collections_abc, 'Collection'):
1714 class AbstractSet(Collection[T_co],
1715 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001716 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001717else:
1718 class AbstractSet(Sized, Iterable[T_co], Container[T_co],
1719 extra=collections_abc.Set):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001720 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001721
1722
1723class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001724 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001725
1726
Guido van Rossumefa798d2016-08-23 11:01:50 -07001727# NOTE: It is only covariant in the value type.
1728if hasattr(collections_abc, 'Collection'):
1729 class Mapping(Collection[KT], Generic[KT, VT_co],
1730 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001731 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001732else:
1733 class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co],
1734 extra=collections_abc.Mapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001735 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001736
1737
1738class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001739 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001740
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001741if hasattr(collections_abc, 'Reversible'):
Guido van Rossumefa798d2016-08-23 11:01:50 -07001742 if hasattr(collections_abc, 'Collection'):
1743 class Sequence(Reversible[T_co], Collection[T_co],
1744 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001745 __slots__ = ()
Guido van Rossumefa798d2016-08-23 11:01:50 -07001746 else:
1747 class Sequence(Sized, Reversible[T_co], Container[T_co],
1748 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001749 __slots__ = ()
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001750else:
1751 class Sequence(Sized, Iterable[T_co], Container[T_co],
1752 extra=collections_abc.Sequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001753 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001754
1755
1756class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001757 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001758
1759
1760class ByteString(Sequence[int], extra=collections_abc.ByteString):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001761 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001762
1763
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001764class List(list, MutableSequence[T], extra=list):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001765
Guido van Rossum4cefe742016-09-27 15:20:12 -07001766 __slots__ = ()
1767
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001768 def __new__(cls, *args, **kwds):
1769 if _geqv(cls, List):
1770 raise TypeError("Type List cannot be instantiated; "
1771 "use list() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001772 return _generic_new(list, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001773
1774
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001775class Set(set, MutableSet[T], extra=set):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001776
Guido van Rossum4cefe742016-09-27 15:20:12 -07001777 __slots__ = ()
1778
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001779 def __new__(cls, *args, **kwds):
1780 if _geqv(cls, Set):
1781 raise TypeError("Type Set cannot be instantiated; "
1782 "use set() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001783 return _generic_new(set, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001784
1785
Guido van Rossum4cefe742016-09-27 15:20:12 -07001786class FrozenSet(frozenset, AbstractSet[T_co], extra=frozenset):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001787 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001788
1789 def __new__(cls, *args, **kwds):
1790 if _geqv(cls, FrozenSet):
1791 raise TypeError("Type FrozenSet cannot be instantiated; "
1792 "use frozenset() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001793 return _generic_new(frozenset, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001794
1795
1796class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001797 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001798
1799
Guido van Rossumd70fe632015-08-05 12:11:06 +02001800class KeysView(MappingView[KT], AbstractSet[KT],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001801 extra=collections_abc.KeysView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001802 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001803
1804
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001805class ItemsView(MappingView[Tuple[KT, VT_co]],
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001806 AbstractSet[Tuple[KT, VT_co]],
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001807 Generic[KT, VT_co],
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001808 extra=collections_abc.ItemsView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001809 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001810
1811
1812class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView):
Guido van Rossum4cefe742016-09-27 15:20:12 -07001813 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001814
1815
Brett Cannonf3ad0422016-04-15 10:51:30 -07001816if hasattr(contextlib, 'AbstractContextManager'):
1817 class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager):
1818 __slots__ = ()
1819 __all__.append('ContextManager')
1820
1821
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001822class Dict(dict, MutableMapping[KT, VT], extra=dict):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001823
Guido van Rossum4cefe742016-09-27 15:20:12 -07001824 __slots__ = ()
1825
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001826 def __new__(cls, *args, **kwds):
1827 if _geqv(cls, Dict):
1828 raise TypeError("Type Dict cannot be instantiated; "
1829 "use dict() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001830 return _generic_new(dict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001831
Guido van Rossum1cea70f2016-05-18 08:35:00 -07001832class DefaultDict(collections.defaultdict, MutableMapping[KT, VT],
1833 extra=collections.defaultdict):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001834
Guido van Rossum4cefe742016-09-27 15:20:12 -07001835 __slots__ = ()
1836
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001837 def __new__(cls, *args, **kwds):
1838 if _geqv(cls, DefaultDict):
1839 raise TypeError("Type DefaultDict cannot be instantiated; "
1840 "use collections.defaultdict() instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001841 return _generic_new(collections.defaultdict, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001842
1843# Determine what base class to use for Generator.
1844if hasattr(collections_abc, 'Generator'):
1845 # Sufficiently recent versions of 3.5 have a Generator ABC.
1846 _G_base = collections_abc.Generator
1847else:
1848 # Fall back on the exact type.
1849 _G_base = types.GeneratorType
1850
1851
1852class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co],
1853 extra=_G_base):
Guido van Rossumd70fe632015-08-05 12:11:06 +02001854 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001855
1856 def __new__(cls, *args, **kwds):
1857 if _geqv(cls, Generator):
1858 raise TypeError("Type Generator cannot be instantiated; "
1859 "create a subclass instead")
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001860 return _generic_new(_G_base, cls, *args, **kwds)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001861
1862
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001863# Internal type variable used for Type[].
Guido van Rossumefa798d2016-08-23 11:01:50 -07001864CT_co = TypeVar('CT_co', covariant=True, bound=type)
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001865
1866
Guido van Rossumb22c7082016-05-26 09:56:19 -07001867# This is not a real generic class. Don't use outside annotations.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001868class Type(Generic[CT_co], extra=type):
Guido van Rossumb22c7082016-05-26 09:56:19 -07001869 """A special construct usable to annotate class objects.
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001870
1871 For example, suppose we have the following classes::
1872
1873 class User: ... # Abstract base for User classes
1874 class BasicUser(User): ...
1875 class ProUser(User): ...
1876 class TeamUser(User): ...
1877
1878 And a function that takes a class argument that's a subclass of
1879 User and returns an instance of the corresponding class::
1880
1881 U = TypeVar('U', bound=User)
1882 def new_user(user_class: Type[U]) -> U:
1883 user = user_class()
1884 # (Here we could write the user object to a database)
1885 return user
1886
1887 joe = new_user(BasicUser)
1888
1889 At this point the type checker knows that joe has type BasicUser.
1890 """
1891
Guido van Rossum4cefe742016-09-27 15:20:12 -07001892 __slots__ = ()
1893
Guido van Rossumeb9aca32016-05-24 16:38:22 -07001894
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001895def _make_nmtuple(name, types):
Guido van Rossum2f841442016-11-15 09:48:06 -08001896 msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
1897 types = [(n, _type_check(t, msg)) for n, t in types]
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001898 nm_tpl = collections.namedtuple(name, [n for n, t in types])
1899 nm_tpl._field_types = dict(types)
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001900 try:
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001901 nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
Guido van Rossum557d1eb2015-11-19 08:16:31 -08001902 except (AttributeError, ValueError):
1903 pass
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001904 return nm_tpl
1905
1906
Guido van Rossum2f841442016-11-15 09:48:06 -08001907_PY36 = sys.version_info[:2] >= (3, 6)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001908
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001909
Guido van Rossum2f841442016-11-15 09:48:06 -08001910class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001911
Guido van Rossum2f841442016-11-15 09:48:06 -08001912 def __new__(cls, typename, bases, ns):
1913 if ns.get('_root', False):
1914 return super().__new__(cls, typename, bases, ns)
1915 if not _PY36:
1916 raise TypeError("Class syntax for NamedTuple is only supported"
1917 " in Python 3.6+")
1918 types = ns.get('__annotations__', {})
1919 return _make_nmtuple(typename, types.items())
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001920
Guido van Rossum2f841442016-11-15 09:48:06 -08001921class NamedTuple(metaclass=NamedTupleMeta):
1922 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001923
Guido van Rossum2f841442016-11-15 09:48:06 -08001924 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001925
Guido van Rossum2f841442016-11-15 09:48:06 -08001926 class Employee(NamedTuple):
1927 name: str
1928 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001929
Guido van Rossum2f841442016-11-15 09:48:06 -08001930 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001931
Guido van Rossum2f841442016-11-15 09:48:06 -08001932 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001933
Guido van Rossum2f841442016-11-15 09:48:06 -08001934 The resulting class has one extra attribute: _field_types,
1935 giving a dict mapping field names to types. (The field names
1936 are in the _fields attribute, which is part of the namedtuple
1937 API.) Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001938
Guido van Rossum2f841442016-11-15 09:48:06 -08001939 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001940
Guido van Rossum2f841442016-11-15 09:48:06 -08001941 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001942
Guido van Rossum2f841442016-11-15 09:48:06 -08001943 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1944 """
1945 _root = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001946
Guido van Rossum2f841442016-11-15 09:48:06 -08001947 def __new__(self, typename, fields=None, **kwargs):
1948 if kwargs and not _PY36:
1949 raise TypeError("Keyword syntax for NamedTuple is only supported"
1950 " in Python 3.6+")
1951 if fields is None:
1952 fields = kwargs.items()
1953 elif kwargs:
1954 raise TypeError("Either list of fields or keywords"
1955 " can be provided to NamedTuple, not both")
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001956 return _make_nmtuple(typename, fields)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001957
1958
Guido van Rossum91185fe2016-06-08 11:19:11 -07001959def NewType(name, tp):
1960 """NewType creates simple unique types with almost zero
1961 runtime overhead. NewType(name, tp) is considered a subtype of tp
1962 by static type checkers. At runtime, NewType(name, tp) returns
1963 a dummy function that simply returns its argument. Usage::
1964
1965 UserId = NewType('UserId', int)
1966
1967 def name_by_id(user_id: UserId) -> str:
1968 ...
1969
1970 UserId('user') # Fails type check
1971
1972 name_by_id(42) # Fails type check
1973 name_by_id(UserId(42)) # OK
1974
1975 num = UserId(5) + 1 # type: int
1976 """
1977
1978 def new_type(x):
1979 return x
1980
1981 new_type.__name__ = name
1982 new_type.__supertype__ = tp
1983 return new_type
1984
1985
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001986# Python-version-specific alias (Python 2: unicode; Python 3: str)
1987Text = str
1988
1989
Guido van Rossum91185fe2016-06-08 11:19:11 -07001990# Constant that's True when type checking, but False here.
1991TYPE_CHECKING = False
1992
1993
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001994class IO(Generic[AnyStr]):
1995 """Generic base class for TextIO and BinaryIO.
1996
1997 This is an abstract, generic version of the return of open().
1998
1999 NOTE: This does not distinguish between the different possible
2000 classes (text vs. binary, read vs. write vs. read/write,
2001 append-only, unbuffered). The TextIO and BinaryIO subclasses
2002 below capture the distinctions between text vs. binary, which is
2003 pervasive in the interface; however we currently do not offer a
2004 way to track the other distinctions in the type system.
2005 """
2006
Guido van Rossumd70fe632015-08-05 12:11:06 +02002007 __slots__ = ()
2008
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002009 @abstractproperty
2010 def mode(self) -> str:
2011 pass
2012
2013 @abstractproperty
2014 def name(self) -> str:
2015 pass
2016
2017 @abstractmethod
2018 def close(self) -> None:
2019 pass
2020
2021 @abstractmethod
2022 def closed(self) -> bool:
2023 pass
2024
2025 @abstractmethod
2026 def fileno(self) -> int:
2027 pass
2028
2029 @abstractmethod
2030 def flush(self) -> None:
2031 pass
2032
2033 @abstractmethod
2034 def isatty(self) -> bool:
2035 pass
2036
2037 @abstractmethod
2038 def read(self, n: int = -1) -> AnyStr:
2039 pass
2040
2041 @abstractmethod
2042 def readable(self) -> bool:
2043 pass
2044
2045 @abstractmethod
2046 def readline(self, limit: int = -1) -> AnyStr:
2047 pass
2048
2049 @abstractmethod
2050 def readlines(self, hint: int = -1) -> List[AnyStr]:
2051 pass
2052
2053 @abstractmethod
2054 def seek(self, offset: int, whence: int = 0) -> int:
2055 pass
2056
2057 @abstractmethod
2058 def seekable(self) -> bool:
2059 pass
2060
2061 @abstractmethod
2062 def tell(self) -> int:
2063 pass
2064
2065 @abstractmethod
2066 def truncate(self, size: int = None) -> int:
2067 pass
2068
2069 @abstractmethod
2070 def writable(self) -> bool:
2071 pass
2072
2073 @abstractmethod
2074 def write(self, s: AnyStr) -> int:
2075 pass
2076
2077 @abstractmethod
2078 def writelines(self, lines: List[AnyStr]) -> None:
2079 pass
2080
2081 @abstractmethod
2082 def __enter__(self) -> 'IO[AnyStr]':
2083 pass
2084
2085 @abstractmethod
2086 def __exit__(self, type, value, traceback) -> None:
2087 pass
2088
2089
2090class BinaryIO(IO[bytes]):
2091 """Typed version of the return of open() in binary mode."""
2092
Guido van Rossumd70fe632015-08-05 12:11:06 +02002093 __slots__ = ()
2094
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002095 @abstractmethod
2096 def write(self, s: Union[bytes, bytearray]) -> int:
2097 pass
2098
2099 @abstractmethod
2100 def __enter__(self) -> 'BinaryIO':
2101 pass
2102
2103
2104class TextIO(IO[str]):
2105 """Typed version of the return of open() in text mode."""
2106
Guido van Rossumd70fe632015-08-05 12:11:06 +02002107 __slots__ = ()
2108
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002109 @abstractproperty
2110 def buffer(self) -> BinaryIO:
2111 pass
2112
2113 @abstractproperty
2114 def encoding(self) -> str:
2115 pass
2116
2117 @abstractproperty
Guido van Rossum991d14f2016-11-09 13:12:51 -08002118 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002119 pass
2120
2121 @abstractproperty
2122 def line_buffering(self) -> bool:
2123 pass
2124
2125 @abstractproperty
2126 def newlines(self) -> Any:
2127 pass
2128
2129 @abstractmethod
2130 def __enter__(self) -> 'TextIO':
2131 pass
2132
2133
2134class io:
2135 """Wrapper namespace for IO generic classes."""
2136
2137 __all__ = ['IO', 'TextIO', 'BinaryIO']
2138 IO = IO
2139 TextIO = TextIO
2140 BinaryIO = BinaryIO
2141
2142io.__name__ = __name__ + '.io'
2143sys.modules[io.__name__] = io
2144
2145
2146Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')),
2147 lambda p: p.pattern)
2148Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')),
2149 lambda m: m.re.pattern)
2150
2151
2152class re:
2153 """Wrapper namespace for re type aliases."""
2154
2155 __all__ = ['Pattern', 'Match']
2156 Pattern = Pattern
2157 Match = Match
2158
2159re.__name__ = __name__ + '.re'
2160sys.modules[re.__name__] = re