blob: f94996daebd6ed8ce05c7ba32bae1a24a93fab6d [file] [log] [blame]
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001"""
2The typing module: Support for gradual typing as defined by PEP 484.
3
4At large scale, the structure of the module is following:
Tim McNamara5265b3a2018-09-01 20:56:58 +12005* Imports and exports, all public names should be explicitly added to __all__.
Ivan Levkivskyid911e402018-01-20 11:23:59 +00006* Internal helper functions: these should never be used in code outside this module.
7* _SpecialForm and its instances (special forms): Any, NoReturn, ClassVar, Union, Optional
8* Two classes whose instances can be type arguments in addition to types: ForwardRef and TypeVar
9* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
10 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
11 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010012* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000013* Public helper functions: get_type_hints, overload, cast, no_type_check,
14 no_type_check_decorator.
15* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053016* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000017* Wrapper submodules for re and io related types.
18"""
19
HongWeipeng6ce03ec2019-09-27 15:54:26 +080020from abc import abstractmethod, ABCMeta
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070021import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000022import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070023import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070024import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030025import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070026import re as stdlib_re # Avoid confusion with the re we export.
27import sys
28import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070029from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070030
31# Please keep __all__ alphabetized within each category.
32__all__ = [
33 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010034 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070035 'Any',
36 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070037 'ClassVar',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010038 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070039 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070040 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010041 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070042 'Optional',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010043 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070044 'Tuple',
45 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070046 'TypeVar',
47 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070048
49 # ABCs (from collections.abc).
50 'AbstractSet', # collections.abc.Set.
51 'ByteString',
52 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020053 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070054 'Hashable',
55 'ItemsView',
56 'Iterable',
57 'Iterator',
58 'KeysView',
59 'Mapping',
60 'MappingView',
61 'MutableMapping',
62 'MutableSequence',
63 'MutableSet',
64 'Sequence',
65 'Sized',
66 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000067 'Awaitable',
68 'AsyncIterator',
69 'AsyncIterable',
70 'Coroutine',
71 'Collection',
72 'AsyncGenerator',
73 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070074
75 # Structural checks, a.k.a. protocols.
76 'Reversible',
77 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020078 'SupportsBytes',
79 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070080 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070081 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070082 'SupportsInt',
83 'SupportsRound',
84
85 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070086 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010087 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080088 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070089 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070090 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070091 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070092 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070093 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070094 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070095 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +010096 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070097 'Generator',
98
99 # One-off things.
100 'AnyStr',
101 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100102 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100103 'get_args',
104 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700105 'get_type_hints',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700106 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700107 'no_type_check',
108 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400109 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700110 'overload',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100111 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700112 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700113 'TYPE_CHECKING',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700114]
115
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700116# The pseudo-submodules 're' and 'io' are part of the public
117# namespace, but excluded from __all__ because they might stomp on
118# legitimate imports of those modules.
119
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700120
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700121def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000122 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700123
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000124 As a special case, accept None and return type(None) instead. Also wrap strings
125 into ForwardRef instances. Consider several corner cases, for example plain
126 special forms like Union are not valid, while Union[int, str] is OK, etc.
127 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700128
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000129 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700130
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000131 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700132 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100133 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700134 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100135 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400136
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000137 if arg is None:
138 return type(None)
139 if isinstance(arg, str):
140 return ForwardRef(arg)
141 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400142 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000143 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300144 if arg in (Any, NoReturn):
145 return arg
146 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 raise TypeError(f"Plain {arg} is not valid as type argument")
148 if isinstance(arg, (type, TypeVar, ForwardRef)):
149 return arg
150 if not callable(arg):
151 raise TypeError(f"{msg} Got {arg!r:.100}.")
152 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700153
154
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000155def _type_repr(obj):
156 """Return the repr() of an object, special-casing types (internal helper).
157
158 If obj is a type, we return a shorter version than the default
159 type.__repr__, based on the module and qualified name, which is
160 typically enough to uniquely identify a type. For everything
161 else, we fall back on repr(obj).
162 """
163 if isinstance(obj, type):
164 if obj.__module__ == 'builtins':
165 return obj.__qualname__
166 return f'{obj.__module__}.{obj.__qualname__}'
167 if obj is ...:
168 return('...')
169 if isinstance(obj, types.FunctionType):
170 return obj.__name__
171 return repr(obj)
172
173
174def _collect_type_vars(types):
175 """Collect all type variable contained in types in order of
176 first appearance (lexicographic order). For example::
177
178 _collect_type_vars((T, List[S, T])) == (T, S)
179 """
180 tvars = []
181 for t in types:
182 if isinstance(t, TypeVar) and t not in tvars:
183 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300184 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000185 tvars.extend([t for t in t.__parameters__ if t not in tvars])
186 return tuple(tvars)
187
188
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300189def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000190 """Check correct count for parameters of a generic cls (internal helper).
191 This gives a nice error message in case of count mismatch.
192 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300193 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000194 raise TypeError(f"{cls} is not a generic class")
195 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000196 if alen != elen:
197 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
198 f" actual {alen}, expected {elen}")
199
200
201def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700202 """An internal helper for Union creation and substitution: flatten Unions
203 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000204 """
205 # Flatten out Union[Union[...], ...].
206 params = []
207 for p in parameters:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300208 if isinstance(p, _UnionGenericAlias):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000209 params.extend(p.__args__)
210 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
211 params.extend(p[1:])
212 else:
213 params.append(p)
214 # Weed out strict duplicates, preserving the first of each occurrence.
215 all_params = set(params)
216 if len(all_params) < len(params):
217 new_params = []
218 for t in params:
219 if t in all_params:
220 new_params.append(t)
221 all_params.remove(t)
222 params = new_params
223 assert not all_params, all_params
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700224 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000225
226
227_cleanups = []
228
229
230def _tp_cache(func):
231 """Internal wrapper caching __getitem__ of generic types with a fallback to
232 original function for non-hashable arguments.
233 """
234 cached = functools.lru_cache()(func)
235 _cleanups.append(cached.cache_clear)
236
237 @functools.wraps(func)
238 def inner(*args, **kwds):
239 try:
240 return cached(*args, **kwds)
241 except TypeError:
242 pass # All real errors (not unhashable args) are raised below.
243 return func(*args, **kwds)
244 return inner
245
246
247def _eval_type(t, globalns, localns):
248 """Evaluate all forward reverences in the given type t.
249 For use of globalns and localns see the docstring for get_type_hints().
250 """
251 if isinstance(t, ForwardRef):
252 return t._evaluate(globalns, localns)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300253 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000254 ev_args = tuple(_eval_type(a, globalns, localns) for a in t.__args__)
255 if ev_args == t.__args__:
256 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300257 if isinstance(t, GenericAlias):
258 return GenericAlias(t.__origin__, ev_args)
259 else:
260 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000261 return t
262
263
264class _Final:
265 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700266
Guido van Rossum83ec3022017-01-17 20:43:28 -0800267 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700268
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300269 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000270 if '_root' not in kwds:
271 raise TypeError("Cannot subclass special typing classes")
272
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100273class _Immutable:
274 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300275 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000276
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100277 def __copy__(self):
278 return self
279
280 def __deepcopy__(self, memo):
281 return self
282
283
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300284# Internal indicator of special typing constructs.
285# See __doc__ instance attribute for specific docs.
286class _SpecialForm(_Final, _root=True):
287 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000288
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300289 def __init__(self, getitem):
290 self._getitem = getitem
291 self._name = getitem.__name__
292 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000293
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300294 def __mro_entries__(self, bases):
295 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700296
297 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000298 return 'typing.' + self._name
299
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100300 def __reduce__(self):
301 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700302
303 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000304 raise TypeError(f"Cannot instantiate {self!r}")
305
306 def __instancecheck__(self, obj):
307 raise TypeError(f"{self} cannot be used with isinstance()")
308
309 def __subclasscheck__(self, cls):
310 raise TypeError(f"{self} cannot be used with issubclass()")
311
312 @_tp_cache
313 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300314 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700315
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300316@_SpecialForm
317def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000318 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700319
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000320 - Any is compatible with every type.
321 - Any assumed to have all methods.
322 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700323
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000324 Note that all the above statements are true from the point of view of
325 static type checkers. At runtime, Any should not be used with instance
326 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300327 """
328 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200329
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300330@_SpecialForm
331def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000332 """Special type indicating functions that never return.
333 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700334
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000335 from typing import NoReturn
336
337 def stop() -> NoReturn:
338 raise Exception('no way')
339
340 This type is invalid in other positions, e.g., ``List[NoReturn]``
341 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300342 """
343 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000344
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300345@_SpecialForm
346def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000347 """Special type construct to mark class variables.
348
349 An annotation wrapped in ClassVar indicates that a given
350 attribute is intended to be used as a class variable and
351 should not be set on instances of that class. Usage::
352
353 class Starship:
354 stats: ClassVar[Dict[str, int]] = {} # class variable
355 damage: int = 10 # instance variable
356
357 ClassVar accepts only types and cannot be further subscribed.
358
359 Note that ClassVar is not a class itself, and should not
360 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300361 """
362 item = _type_check(parameters, f'{self} accepts only single type.')
363 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000364
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300365@_SpecialForm
366def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100367 """Special typing construct to indicate final names to type checkers.
368
369 A final name cannot be re-assigned or overridden in a subclass.
370 For example:
371
372 MAX_SIZE: Final = 9000
373 MAX_SIZE += 1 # Error reported by type checker
374
375 class Connection:
376 TIMEOUT: Final[int] = 10
377
378 class FastConnector(Connection):
379 TIMEOUT = 1 # Error reported by type checker
380
381 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300382 """
383 item = _type_check(parameters, f'{self} accepts only single type.')
384 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100385
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300386@_SpecialForm
387def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000388 """Union type; Union[X, Y] means either X or Y.
389
390 To define a union, use e.g. Union[int, str]. Details:
391 - The arguments must be types and there must be at least one.
392 - None as an argument is a special case and is replaced by
393 type(None).
394 - Unions of unions are flattened, e.g.::
395
396 Union[Union[int, str], float] == Union[int, str, float]
397
398 - Unions of a single argument vanish, e.g.::
399
400 Union[int] == int # The constructor actually returns int
401
402 - Redundant arguments are skipped, e.g.::
403
404 Union[int, str, int] == Union[int, str]
405
406 - When comparing unions, the argument order is ignored, e.g.::
407
408 Union[int, str] == Union[str, int]
409
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000410 - You cannot subclass or instantiate a union.
411 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300412 """
413 if parameters == ():
414 raise TypeError("Cannot take a Union of no types.")
415 if not isinstance(parameters, tuple):
416 parameters = (parameters,)
417 msg = "Union[arg, ...]: each arg must be a type."
418 parameters = tuple(_type_check(p, msg) for p in parameters)
419 parameters = _remove_dups_flatten(parameters)
420 if len(parameters) == 1:
421 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300422 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000423
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300424@_SpecialForm
425def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000426 """Optional type.
427
428 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300429 """
430 arg = _type_check(parameters, f"{self} requires a single type.")
431 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700432
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300433@_SpecialForm
434def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100435 """Special typing form to define literal types (a.k.a. value types).
436
437 This form can be used to indicate to type checkers that the corresponding
438 variable or function parameter has a value equivalent to the provided
439 literal (or one of several literals):
440
441 def validate_simple(data: Any) -> Literal[True]: # always returns True
442 ...
443
444 MODE = Literal['r', 'rb', 'w', 'wb']
445 def open_helper(file: str, mode: MODE) -> str:
446 ...
447
448 open_helper('/some/path', 'r') # Passes type check
449 open_helper('/other/path', 'typo') # Error in type checker
450
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300451 Literal[...] cannot be subclassed. At runtime, an arbitrary value
452 is allowed as type argument to Literal[...], but type checkers may
453 impose restrictions.
454 """
455 # There is no '_type_check' call because arguments to Literal[...] are
456 # values, not types.
457 return _GenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100458
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700459
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000460class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800461 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700462
Guido van Rossum4cefe742016-09-27 15:20:12 -0700463 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400464 '__forward_evaluated__', '__forward_value__',
465 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700466
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700467 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700468 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000469 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700470 try:
471 code = compile(arg, '<string>', 'eval')
472 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000473 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700474 self.__forward_arg__ = arg
475 self.__forward_code__ = code
476 self.__forward_evaluated__ = False
477 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400478 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700479
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000480 def _evaluate(self, globalns, localns):
Guido van Rossumdad17902016-11-10 08:29:18 -0800481 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700482 if globalns is None and localns is None:
483 globalns = localns = {}
484 elif globalns is None:
485 globalns = localns
486 elif localns is None:
487 localns = globalns
488 self.__forward_value__ = _type_check(
489 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400490 "Forward references must evaluate to types.",
491 is_argument=self.__forward_is_argument__)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700492 self.__forward_evaluated__ = True
493 return self.__forward_value__
494
Guido van Rossum4cefe742016-09-27 15:20:12 -0700495 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000496 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700497 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100498 if self.__forward_evaluated__ and other.__forward_evaluated__:
499 return (self.__forward_arg__ == other.__forward_arg__ and
500 self.__forward_value__ == other.__forward_value__)
501 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700502
503 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100504 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700505
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700506 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000507 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700508
509
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100510class TypeVar(_Final, _Immutable, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700511 """Type variable.
512
513 Usage::
514
515 T = TypeVar('T') # Can be anything
516 A = TypeVar('A', str, bytes) # Must be str or bytes
517
518 Type variables exist primarily for the benefit of static type
519 checkers. They serve as the parameters for generic types as well
520 as for generic function definitions. See class Generic for more
521 information on generic types. Generic functions work as follows:
522
Guido van Rossumb24569a2016-11-20 18:01:29 -0800523 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700524 '''Return a list containing n references to x.'''
525 return [x]*n
526
527 def longest(x: A, y: A) -> A:
528 '''Return the longest of two strings.'''
529 return x if len(x) >= len(y) else y
530
531 The latter example's signature is essentially the overloading
532 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
533 that if the arguments are instances of some subclass of str,
534 the return type is still plain str.
535
Guido van Rossumb24569a2016-11-20 18:01:29 -0800536 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700537
Guido van Rossumefa798d2016-08-23 11:01:50 -0700538 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100539 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700540 See PEP 484 for more details. By default generic types are invariant
541 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700542
543 Type variables can be introspected. e.g.:
544
545 T.__name__ == 'T'
546 T.__constraints__ == ()
547 T.__covariant__ == False
548 T.__contravariant__ = False
549 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100550
551 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700552 """
553
Guido van Rossum4cefe742016-09-27 15:20:12 -0700554 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300555 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700556
557 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800558 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700559 self.__name__ = name
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700560 if covariant and contravariant:
Guido van Rossumefa798d2016-08-23 11:01:50 -0700561 raise ValueError("Bivariant types are not supported.")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700562 self.__covariant__ = bool(covariant)
563 self.__contravariant__ = bool(contravariant)
564 if constraints and bound is not None:
565 raise TypeError("Constraints cannot be combined with bound=...")
566 if constraints and len(constraints) == 1:
567 raise TypeError("A single constraint is not allowed")
568 msg = "TypeVar(name, constraint, ...): constraints must be types."
569 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
570 if bound:
571 self.__bound__ = _type_check(bound, "Bound must be a type.")
572 else:
573 self.__bound__ = None
HongWeipenga25a04f2020-04-21 04:01:53 +0800574 try:
575 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
576 except (AttributeError, ValueError):
577 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300578 if def_mod != 'typing':
579 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700580
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700581 def __repr__(self):
582 if self.__covariant__:
583 prefix = '+'
584 elif self.__contravariant__:
585 prefix = '-'
586 else:
587 prefix = '~'
588 return prefix + self.__name__
589
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100590 def __reduce__(self):
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300591 return self.__name__
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100592
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700593
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000594def _is_dunder(attr):
595 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800596
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300597class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000598 """The central part of internal API.
599
600 This represents a generic version of type 'origin' with type arguments 'params'.
601 There are two kind of these aliases: user defined and special. The special ones
602 are wrappers around builtin collections and ABCs in collections.abc. These must
603 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
604 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700605 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300606 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000607 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000608 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700609 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000610 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300611
612 def __call__(self, *args, **kwargs):
613 if not self._inst:
614 raise TypeError(f"Type {self._name} cannot be instantiated; "
615 f"use {self.__origin__.__name__}() instead")
616 result = self.__origin__(*args, **kwargs)
617 try:
618 result.__orig_class__ = self
619 except AttributeError:
620 pass
621 return result
622
623 def __mro_entries__(self, bases):
624 res = []
625 if self.__origin__ not in bases:
626 res.append(self.__origin__)
627 i = bases.index(self)
628 for b in bases[i+1:]:
629 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
630 break
631 else:
632 res.append(Generic)
633 return tuple(res)
634
635 def __getattr__(self, attr):
636 # We are careful for copy and pickle.
637 # Also for simplicity we just don't relay all dunder names
638 if '__origin__' in self.__dict__ and not _is_dunder(attr):
639 return getattr(self.__origin__, attr)
640 raise AttributeError(attr)
641
642 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300643 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300644 super().__setattr__(attr, val)
645 else:
646 setattr(self.__origin__, attr, val)
647
648 def __instancecheck__(self, obj):
649 return self.__subclasscheck__(type(obj))
650
651 def __subclasscheck__(self, cls):
652 raise TypeError("Subscripted generics cannot be used with"
653 " class and instance checks")
654
655
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300656# Special typing constructs Union, Optional, Generic, Callable and Tuple
657# use three special attributes for internal bookkeeping of generic types:
658# * __parameters__ is a tuple of unique free type parameters of a generic
659# type, for example, Dict[T, T].__parameters__ == (T,);
660# * __origin__ keeps a reference to a type that was subscripted,
661# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
662# the type.
663# * __args__ is a tuple of all arguments used in subscripting,
664# e.g., Dict[T, int].__args__ == (T, int).
665
666
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300667class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300668 def __init__(self, origin, params, *, inst=True, name=None):
669 super().__init__(origin, inst=inst, name=name)
670 if not isinstance(params, tuple):
671 params = (params,)
672 self.__args__ = tuple(... if a is _TypingEllipsis else
673 () if a is _TypingEmpty else
674 a for a in params)
675 self.__parameters__ = _collect_type_vars(params)
676 if not name:
677 self.__module__ = origin.__module__
678
679 def __eq__(self, other):
680 if not isinstance(other, _GenericAlias):
681 return NotImplemented
682 return (self.__origin__ == other.__origin__
683 and self.__args__ == other.__args__)
684
685 def __hash__(self):
686 return hash((self.__origin__, self.__args__))
687
Guido van Rossum4cefe742016-09-27 15:20:12 -0700688 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700689 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100690 if self.__origin__ in (Generic, Protocol):
691 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000692 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700693 if not isinstance(params, tuple):
694 params = (params,)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700695 msg = "Parameters to generic types must be types."
696 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300697 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300698
699 subst = dict(zip(self.__parameters__, params))
700 new_args = []
701 for arg in self.__args__:
702 if isinstance(arg, TypeVar):
703 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300704 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300705 subparams = arg.__parameters__
706 if subparams:
707 subargs = tuple(subst[x] for x in subparams)
708 arg = arg[subargs]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300709 new_args.append(arg)
710 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100711
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000712 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300713 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700714
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000715 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300716 if self._name:
717 name = 'typing.' + self._name
718 else:
719 name = _type_repr(self.__origin__)
720 args = ", ".join([_type_repr(a) for a in self.__args__])
721 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000722
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300723 def __reduce__(self):
724 if self._name:
725 origin = globals()[self._name]
726 else:
727 origin = self.__origin__
728 args = tuple(self.__args__)
729 if len(args) == 1 and not isinstance(args[0], tuple):
730 args, = args
731 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000732
733 def __mro_entries__(self, bases):
734 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300735 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000736 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100737 if Protocol in bases:
738 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000739 i = bases.index(self)
740 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300741 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000742 return ()
743 return (self.__origin__,)
744
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000745
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300746# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
747# 1 for List and 2 for Dict. It may be -1 if variable number of
748# parameters are accepted (needs custom __getitem__).
749
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300750class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300751 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300752 if name is None:
753 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300754 super().__init__(origin, inst=inst, name=name)
755 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300756 if origin.__module__ == 'builtins':
757 self.__doc__ = f'A generic version of {origin.__qualname__}.'
758 else:
759 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000760
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300761 @_tp_cache
762 def __getitem__(self, params):
763 if not isinstance(params, tuple):
764 params = (params,)
765 msg = "Parameters to generic types must be types."
766 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300767 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300768 return self.copy_with(params)
769
770 def copy_with(self, params):
771 return _GenericAlias(self.__origin__, params,
772 name=self._name, inst=self._inst)
773
774 def __repr__(self):
775 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000776
777 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300778 if isinstance(cls, _SpecialGenericAlias):
779 return issubclass(cls.__origin__, self.__origin__)
780 if not isinstance(cls, _GenericAlias):
781 return issubclass(cls, self.__origin__)
782 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700783
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100784 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300785 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100786
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700787
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300788class _CallableGenericAlias(_GenericAlias, _root=True):
789 def __repr__(self):
790 assert self._name == 'Callable'
791 if len(self.__args__) == 2 and self.__args__[0] is Ellipsis:
792 return super().__repr__()
793 return (f'typing.Callable'
794 f'[[{", ".join([_type_repr(a) for a in self.__args__[:-1]])}], '
795 f'{_type_repr(self.__args__[-1])}]')
796
797 def __reduce__(self):
798 args = self.__args__
799 if not (len(args) == 2 and args[0] is ...):
800 args = list(args[:-1]), args[-1]
801 return operator.getitem, (Callable, args)
802
803
804class _CallableType(_SpecialGenericAlias, _root=True):
805 def copy_with(self, params):
806 return _CallableGenericAlias(self.__origin__, params,
807 name=self._name, inst=self._inst)
808
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000809 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000810 if not isinstance(params, tuple) or len(params) != 2:
811 raise TypeError("Callable must be used as "
812 "Callable[[arg, ...], result].")
813 args, result = params
814 if args is Ellipsis:
815 params = (Ellipsis, result)
816 else:
817 if not isinstance(args, list):
818 raise TypeError(f"Callable[args, result]: args must be a list."
819 f" Got {args}")
820 params = (tuple(args), result)
821 return self.__getitem_inner__(params)
822
823 @_tp_cache
824 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300825 args, result = params
826 msg = "Callable[args, result]: result must be a type."
827 result = _type_check(result, msg)
828 if args is Ellipsis:
829 return self.copy_with((_TypingEllipsis, result))
830 msg = "Callable[[arg, ...], result]: each arg must be a type."
831 args = tuple(_type_check(arg, msg) for arg in args)
832 params = args + (result,)
833 return self.copy_with(params)
834
835
836class _TupleType(_SpecialGenericAlias, _root=True):
837 @_tp_cache
838 def __getitem__(self, params):
839 if params == ():
840 return self.copy_with((_TypingEmpty,))
841 if not isinstance(params, tuple):
842 params = (params,)
843 if len(params) == 2 and params[1] is ...:
844 msg = "Tuple[t, ...]: t must be a type."
845 p = _type_check(params[0], msg)
846 return self.copy_with((p, _TypingEllipsis))
847 msg = "Tuple[t0, t1, ...]: each t must be a type."
848 params = tuple(_type_check(p, msg) for p in params)
849 return self.copy_with(params)
850
851
852class _UnionGenericAlias(_GenericAlias, _root=True):
853 def copy_with(self, params):
854 return Union[params]
855
856 def __eq__(self, other):
857 if not isinstance(other, _UnionGenericAlias):
858 return NotImplemented
859 return set(self.__args__) == set(other.__args__)
860
861 def __hash__(self):
862 return hash(frozenset(self.__args__))
863
864 def __repr__(self):
865 args = self.__args__
866 if len(args) == 2:
867 if args[0] is type(None):
868 return f'typing.Optional[{_type_repr(args[1])}]'
869 elif args[1] is type(None):
870 return f'typing.Optional[{_type_repr(args[0])}]'
871 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000872
873
874class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700875 """Abstract base class for generic types.
876
Guido van Rossumb24569a2016-11-20 18:01:29 -0800877 A generic type is typically declared by inheriting from
878 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700879 For example, a generic mapping type might be defined as::
880
881 class Mapping(Generic[KT, VT]):
882 def __getitem__(self, key: KT) -> VT:
883 ...
884 # Etc.
885
886 This class can then be used as follows::
887
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700888 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700889 try:
890 return mapping[key]
891 except KeyError:
892 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700893 """
Guido van Rossumd70fe632015-08-05 12:11:06 +0200894 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100895 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +0200896
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700897 def __new__(cls, *args, **kwds):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100898 if cls in (Generic, Protocol):
899 raise TypeError(f"Type {cls.__name__} cannot be instantiated; "
Guido van Rossum62fe1bb2016-10-29 16:05:26 -0700900 "it can be used only as a base class")
Ivan Levkivskyib551e9f2018-05-10 23:10:10 -0400901 if super().__new__ is object.__new__ and cls.__init__ is not object.__init__:
Ivan Levkivskyi43d12a62018-05-09 02:23:46 +0100902 obj = super().__new__(cls)
903 else:
904 obj = super().__new__(cls, *args, **kwds)
905 return obj
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000906
907 @_tp_cache
908 def __class_getitem__(cls, params):
909 if not isinstance(params, tuple):
910 params = (params,)
911 if not params and cls is not Tuple:
912 raise TypeError(
913 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
914 msg = "Parameters to generic types must be types."
915 params = tuple(_type_check(p, msg) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100916 if cls in (Generic, Protocol):
917 # Generic and Protocol can only be subscripted with unique type variables.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000918 if not all(isinstance(p, TypeVar) for p in params):
919 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100920 f"Parameters to {cls.__name__}[...] must all be type variables")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000921 if len(set(params)) != len(params):
922 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100923 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000924 else:
925 # Subscripting a regular Generic subclass.
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300926 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000927 return _GenericAlias(cls, params)
928
929 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +0100930 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000931 tvars = []
932 if '__orig_bases__' in cls.__dict__:
933 error = Generic in cls.__orig_bases__
934 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100935 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000936 if error:
937 raise TypeError("Cannot inherit from plain Generic")
938 if '__orig_bases__' in cls.__dict__:
939 tvars = _collect_type_vars(cls.__orig_bases__)
940 # Look for Generic[T1, ..., Tn].
941 # If found, tvars must be a subset of it.
942 # If not found, tvars is it.
943 # Also check for and reject plain Generic,
944 # and reject multiple Generic[...].
945 gvars = None
946 for base in cls.__orig_bases__:
947 if (isinstance(base, _GenericAlias) and
948 base.__origin__ is Generic):
949 if gvars is not None:
950 raise TypeError(
951 "Cannot inherit from Generic[...] multiple types.")
952 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100953 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000954 tvarset = set(tvars)
955 gvarset = set(gvars)
956 if not tvarset <= gvarset:
957 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
958 s_args = ', '.join(str(g) for g in gvars)
959 raise TypeError(f"Some type variables ({s_vars}) are"
960 f" not listed in Generic[{s_args}]")
961 tvars = gvars
962 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700963
964
965class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800966 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
967 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700968 to sneak in where prohibited.
969 """
970
971
972class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -0800973 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700974
975
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100976_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
977 '_is_protocol', '_is_runtime_protocol']
978
979_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
980 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -0700981 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100982
983# These special attributes will be not collected as protocol members.
984EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
985
986
987def _get_protocol_attrs(cls):
988 """Collect protocol members from a protocol class objects.
989
990 This includes names actually defined in the class dictionary, as well
991 as names that appear in annotations. Special names (above) are skipped.
992 """
993 attrs = set()
994 for base in cls.__mro__[:-1]: # without object
995 if base.__name__ in ('Protocol', 'Generic'):
996 continue
997 annotations = getattr(base, '__annotations__', {})
998 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
999 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1000 attrs.add(attr)
1001 return attrs
1002
1003
1004def _is_callable_members_only(cls):
1005 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1006 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1007
1008
1009def _no_init(self, *args, **kwargs):
1010 if type(self)._is_protocol:
1011 raise TypeError('Protocols cannot be instantiated')
1012
1013
1014def _allow_reckless_class_cheks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001015 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001016
1017 The abc and functools modules indiscriminately call isinstance() and
1018 issubclass() on the whole MRO of a user class, which may contain protocols.
1019 """
1020 try:
1021 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1022 except (AttributeError, ValueError): # For platforms without _getframe().
1023 return True
1024
1025
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001026_PROTO_WHITELIST = {
1027 'collections.abc': [
1028 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1029 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1030 ],
1031 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1032}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001033
1034
1035class _ProtocolMeta(ABCMeta):
1036 # This metaclass is really unfortunate and exists only because of
1037 # the lack of __instancehook__.
1038 def __instancecheck__(cls, instance):
1039 # We need this method for situations where attributes are
1040 # assigned in __init__.
1041 if ((not getattr(cls, '_is_protocol', False) or
1042 _is_callable_members_only(cls)) and
1043 issubclass(instance.__class__, cls)):
1044 return True
1045 if cls._is_protocol:
1046 if all(hasattr(instance, attr) and
1047 # All *methods* can be blocked by setting them to None.
1048 (not callable(getattr(cls, attr, None)) or
1049 getattr(instance, attr) is not None)
1050 for attr in _get_protocol_attrs(cls)):
1051 return True
1052 return super().__instancecheck__(instance)
1053
1054
1055class Protocol(Generic, metaclass=_ProtocolMeta):
1056 """Base class for protocol classes.
1057
1058 Protocol classes are defined as::
1059
1060 class Proto(Protocol):
1061 def meth(self) -> int:
1062 ...
1063
1064 Such classes are primarily used with static type checkers that recognize
1065 structural subtyping (static duck-typing), for example::
1066
1067 class C:
1068 def meth(self) -> int:
1069 return 0
1070
1071 def func(x: Proto) -> int:
1072 return x.meth()
1073
1074 func(C()) # Passes static type check
1075
1076 See PEP 544 for details. Protocol classes decorated with
1077 @typing.runtime_checkable act as simple-minded runtime protocols that check
1078 only the presence of given attributes, ignoring their type signatures.
1079 Protocol classes can be generic, they are defined as::
1080
1081 class GenProto(Protocol[T]):
1082 def meth(self) -> T:
1083 ...
1084 """
1085 __slots__ = ()
1086 _is_protocol = True
1087 _is_runtime_protocol = False
1088
1089 def __init_subclass__(cls, *args, **kwargs):
1090 super().__init_subclass__(*args, **kwargs)
1091
1092 # Determine if this is a protocol or a concrete subclass.
1093 if not cls.__dict__.get('_is_protocol', False):
1094 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1095
1096 # Set (or override) the protocol subclass hook.
1097 def _proto_hook(other):
1098 if not cls.__dict__.get('_is_protocol', False):
1099 return NotImplemented
1100
1101 # First, perform various sanity checks.
1102 if not getattr(cls, '_is_runtime_protocol', False):
1103 if _allow_reckless_class_cheks():
1104 return NotImplemented
1105 raise TypeError("Instance and class checks can only be used with"
1106 " @runtime_checkable protocols")
1107 if not _is_callable_members_only(cls):
1108 if _allow_reckless_class_cheks():
1109 return NotImplemented
1110 raise TypeError("Protocols with non-method members"
1111 " don't support issubclass()")
1112 if not isinstance(other, type):
1113 # Same error message as for issubclass(1, int).
1114 raise TypeError('issubclass() arg 1 must be a class')
1115
1116 # Second, perform the actual structural compatibility check.
1117 for attr in _get_protocol_attrs(cls):
1118 for base in other.__mro__:
1119 # Check if the members appears in the class dictionary...
1120 if attr in base.__dict__:
1121 if base.__dict__[attr] is None:
1122 return NotImplemented
1123 break
1124
1125 # ...or in annotations, if it is a sub-protocol.
1126 annotations = getattr(base, '__annotations__', {})
1127 if (isinstance(annotations, collections.abc.Mapping) and
1128 attr in annotations and
1129 issubclass(other, Generic) and other._is_protocol):
1130 break
1131 else:
1132 return NotImplemented
1133 return True
1134
1135 if '__subclasshook__' not in cls.__dict__:
1136 cls.__subclasshook__ = _proto_hook
1137
1138 # We have nothing more to do for non-protocols...
1139 if not cls._is_protocol:
1140 return
1141
1142 # ... otherwise check consistency of bases, and prohibit instantiation.
1143 for base in cls.__bases__:
1144 if not (base in (object, Generic) or
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001145 base.__module__ in _PROTO_WHITELIST and
1146 base.__name__ in _PROTO_WHITELIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001147 issubclass(base, Generic) and base._is_protocol):
1148 raise TypeError('Protocols can only inherit from other'
1149 ' protocols, got %r' % base)
1150 cls.__init__ = _no_init
1151
1152
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001153class _AnnotatedAlias(_GenericAlias, _root=True):
1154 """Runtime representation of an annotated type.
1155
1156 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1157 with extra annotations. The alias behaves like a normal typing alias,
1158 instantiating is the same as instantiating the underlying type, binding
1159 it to types is also the same.
1160 """
1161 def __init__(self, origin, metadata):
1162 if isinstance(origin, _AnnotatedAlias):
1163 metadata = origin.__metadata__ + metadata
1164 origin = origin.__origin__
1165 super().__init__(origin, origin)
1166 self.__metadata__ = metadata
1167
1168 def copy_with(self, params):
1169 assert len(params) == 1
1170 new_type = params[0]
1171 return _AnnotatedAlias(new_type, self.__metadata__)
1172
1173 def __repr__(self):
1174 return "typing.Annotated[{}, {}]".format(
1175 _type_repr(self.__origin__),
1176 ", ".join(repr(a) for a in self.__metadata__)
1177 )
1178
1179 def __reduce__(self):
1180 return operator.getitem, (
1181 Annotated, (self.__origin__,) + self.__metadata__
1182 )
1183
1184 def __eq__(self, other):
1185 if not isinstance(other, _AnnotatedAlias):
1186 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001187 return (self.__origin__ == other.__origin__
1188 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001189
1190 def __hash__(self):
1191 return hash((self.__origin__, self.__metadata__))
1192
1193
1194class Annotated:
1195 """Add context specific metadata to a type.
1196
1197 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1198 hypothetical runtime_check module that this type is an unsigned int.
1199 Every other consumer of this type can ignore this metadata and treat
1200 this type as int.
1201
1202 The first argument to Annotated must be a valid type.
1203
1204 Details:
1205
1206 - It's an error to call `Annotated` with less than two arguments.
1207 - Nested Annotated are flattened::
1208
1209 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1210
1211 - Instantiating an annotated type is equivalent to instantiating the
1212 underlying type::
1213
1214 Annotated[C, Ann1](5) == C(5)
1215
1216 - Annotated can be used as a generic type alias::
1217
1218 Optimized = Annotated[T, runtime.Optimize()]
1219 Optimized[int] == Annotated[int, runtime.Optimize()]
1220
1221 OptimizedList = Annotated[List[T], runtime.Optimize()]
1222 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1223 """
1224
1225 __slots__ = ()
1226
1227 def __new__(cls, *args, **kwargs):
1228 raise TypeError("Type Annotated cannot be instantiated.")
1229
1230 @_tp_cache
1231 def __class_getitem__(cls, params):
1232 if not isinstance(params, tuple) or len(params) < 2:
1233 raise TypeError("Annotated[...] should be used "
1234 "with at least two arguments (a type and an "
1235 "annotation).")
1236 msg = "Annotated[t, ...]: t must be a type."
1237 origin = _type_check(params[0], msg)
1238 metadata = tuple(params[1:])
1239 return _AnnotatedAlias(origin, metadata)
1240
1241 def __init_subclass__(cls, *args, **kwargs):
1242 raise TypeError(
1243 "Cannot subclass {}.Annotated".format(cls.__module__)
1244 )
1245
1246
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001247def runtime_checkable(cls):
1248 """Mark a protocol class as a runtime protocol.
1249
1250 Such protocol can be used with isinstance() and issubclass().
1251 Raise TypeError if applied to a non-protocol class.
1252 This allows a simple-minded structural check very similar to
1253 one trick ponies in collections.abc such as Iterable.
1254 For example::
1255
1256 @runtime_checkable
1257 class Closable(Protocol):
1258 def close(self): ...
1259
1260 assert isinstance(open('/some/file'), Closable)
1261
1262 Warning: this will check only the presence of the required methods,
1263 not their type signatures!
1264 """
1265 if not issubclass(cls, Generic) or not cls._is_protocol:
1266 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1267 ' got %r' % cls)
1268 cls._is_runtime_protocol = True
1269 return cls
1270
1271
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001272def cast(typ, val):
1273 """Cast a value to a type.
1274
1275 This returns the value unchanged. To the type checker this
1276 signals that the return value has the designated type, but at
1277 runtime we intentionally don't check anything (we want this
1278 to be as fast as possible).
1279 """
1280 return val
1281
1282
1283def _get_defaults(func):
1284 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001285 try:
1286 code = func.__code__
1287 except AttributeError:
1288 # Some built-in functions don't have __code__, __defaults__, etc.
1289 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001290 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001291 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001292 arg_names = arg_names[:pos_count]
1293 defaults = func.__defaults__ or ()
1294 kwdefaults = func.__kwdefaults__
1295 res = dict(kwdefaults) if kwdefaults else {}
1296 pos_offset = pos_count - len(defaults)
1297 for name, value in zip(arg_names[pos_offset:], defaults):
1298 assert name not in res
1299 res[name] = value
1300 return res
1301
1302
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001303_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1304 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001305 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001306
1307
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001308def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001309 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001310
Guido van Rossum991d14f2016-11-09 13:12:51 -08001311 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001312 forward references encoded as string literals, adds Optional[t] if a
1313 default value equal to None is set and recursively replaces all
1314 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001315
Guido van Rossum991d14f2016-11-09 13:12:51 -08001316 The argument may be a module, class, method, or function. The annotations
1317 are returned as a dictionary. For classes, annotations include also
1318 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001319
Guido van Rossum991d14f2016-11-09 13:12:51 -08001320 TypeError is raised if the argument is not of a type that can contain
1321 annotations, and an empty dictionary is returned if no annotations are
1322 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001323
Guido van Rossum991d14f2016-11-09 13:12:51 -08001324 BEWARE -- the behavior of globalns and localns is counterintuitive
1325 (unless you are familiar with how eval() and exec() work). The
1326 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001327
Guido van Rossum991d14f2016-11-09 13:12:51 -08001328 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001329 globals from obj (or the respective module's globals for classes),
1330 and these are also used as the locals. If the object does not appear
1331 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001332
Guido van Rossum991d14f2016-11-09 13:12:51 -08001333 - If one dict argument is passed, it is used for both globals and
1334 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001335
Guido van Rossum991d14f2016-11-09 13:12:51 -08001336 - If two dict arguments are passed, they specify globals and
1337 locals, respectively.
1338 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001339
Guido van Rossum991d14f2016-11-09 13:12:51 -08001340 if getattr(obj, '__no_type_check__', None):
1341 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001342 # Classes require a special treatment.
1343 if isinstance(obj, type):
1344 hints = {}
1345 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001346 if globalns is None:
1347 base_globals = sys.modules[base.__module__].__dict__
1348 else:
1349 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001350 ann = base.__dict__.get('__annotations__', {})
1351 for name, value in ann.items():
1352 if value is None:
1353 value = type(None)
1354 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001355 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001356 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001357 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001358 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001359
1360 if globalns is None:
1361 if isinstance(obj, types.ModuleType):
1362 globalns = obj.__dict__
1363 else:
benedwards140aca3a32019-11-21 17:24:58 +00001364 nsobj = obj
1365 # Find globalns for the unwrapped object.
1366 while hasattr(nsobj, '__wrapped__'):
1367 nsobj = nsobj.__wrapped__
1368 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001369 if localns is None:
1370 localns = globalns
1371 elif localns is None:
1372 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001373 hints = getattr(obj, '__annotations__', None)
1374 if hints is None:
1375 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001376 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001377 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001378 else:
1379 raise TypeError('{!r} is not a module, class, method, '
1380 'or function.'.format(obj))
1381 defaults = _get_defaults(obj)
1382 hints = dict(hints)
1383 for name, value in hints.items():
1384 if value is None:
1385 value = type(None)
1386 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001387 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001388 value = _eval_type(value, globalns, localns)
1389 if name in defaults and defaults[name] is None:
1390 value = Optional[value]
1391 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001392 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1393
1394
1395def _strip_annotations(t):
1396 """Strips the annotations from a given type.
1397 """
1398 if isinstance(t, _AnnotatedAlias):
1399 return _strip_annotations(t.__origin__)
1400 if isinstance(t, _GenericAlias):
1401 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1402 if stripped_args == t.__args__:
1403 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001404 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001405 if isinstance(t, GenericAlias):
1406 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1407 if stripped_args == t.__args__:
1408 return t
1409 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001410 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001411
1412
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001413def get_origin(tp):
1414 """Get the unsubscripted version of a type.
1415
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001416 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1417 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001418
1419 get_origin(Literal[42]) is Literal
1420 get_origin(int) is None
1421 get_origin(ClassVar[int]) is ClassVar
1422 get_origin(Generic) is Generic
1423 get_origin(Generic[T]) is Generic
1424 get_origin(Union[T, int]) is Union
1425 get_origin(List[Tuple[T, T]][int]) == list
1426 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001427 if isinstance(tp, _AnnotatedAlias):
1428 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001429 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001430 return tp.__origin__
1431 if tp is Generic:
1432 return Generic
1433 return None
1434
1435
1436def get_args(tp):
1437 """Get type arguments with all substitutions performed.
1438
1439 For unions, basic simplifications used by Union constructor are performed.
1440 Examples::
1441 get_args(Dict[str, int]) == (str, int)
1442 get_args(int) == ()
1443 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1444 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1445 get_args(Callable[[], T][int]) == ([], int)
1446 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001447 if isinstance(tp, _AnnotatedAlias):
1448 return (tp.__origin__,) + tp.__metadata__
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001449 if isinstance(tp, _GenericAlias):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001450 res = tp.__args__
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001451 if tp.__origin__ is collections.abc.Callable and res[0] is not Ellipsis:
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001452 res = (list(res[:-1]), res[-1])
1453 return res
Serhiy Storchaka6292be72020-04-27 10:27:21 +03001454 if isinstance(tp, GenericAlias):
1455 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001456 return ()
1457
1458
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001459def no_type_check(arg):
1460 """Decorator to indicate that annotations are not type hints.
1461
1462 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001463 applies recursively to all methods and classes defined in that class
1464 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001465
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001466 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001467 """
1468 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001469 arg_attrs = arg.__dict__.copy()
1470 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001471 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001472 arg_attrs.pop(attr)
1473 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001474 if isinstance(obj, types.FunctionType):
1475 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001476 if isinstance(obj, type):
1477 no_type_check(obj)
1478 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001479 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001480 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001481 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001482 return arg
1483
1484
1485def no_type_check_decorator(decorator):
1486 """Decorator to give another decorator the @no_type_check effect.
1487
1488 This wraps the decorator with something that wraps the decorated
1489 function in @no_type_check.
1490 """
1491
1492 @functools.wraps(decorator)
1493 def wrapped_decorator(*args, **kwds):
1494 func = decorator(*args, **kwds)
1495 func = no_type_check(func)
1496 return func
1497
1498 return wrapped_decorator
1499
1500
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001501def _overload_dummy(*args, **kwds):
1502 """Helper for @overload to raise when called."""
1503 raise NotImplementedError(
1504 "You should not call an overloaded function. "
1505 "A series of @overload-decorated functions "
1506 "outside a stub module should always be followed "
1507 "by an implementation that is not @overload-ed.")
1508
1509
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001510def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001511 """Decorator for overloaded functions/methods.
1512
1513 In a stub file, place two or more stub definitions for the same
1514 function in a row, each decorated with @overload. For example:
1515
1516 @overload
1517 def utf8(value: None) -> None: ...
1518 @overload
1519 def utf8(value: bytes) -> bytes: ...
1520 @overload
1521 def utf8(value: str) -> bytes: ...
1522
1523 In a non-stub file (i.e. a regular .py file), do the same but
1524 follow it with an implementation. The implementation should *not*
1525 be decorated with @overload. For example:
1526
1527 @overload
1528 def utf8(value: None) -> None: ...
1529 @overload
1530 def utf8(value: bytes) -> bytes: ...
1531 @overload
1532 def utf8(value: str) -> bytes: ...
1533 def utf8(value):
1534 # implementation goes here
1535 """
1536 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001537
1538
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001539def final(f):
1540 """A decorator to indicate final methods and final classes.
1541
1542 Use this decorator to indicate to type checkers that the decorated
1543 method cannot be overridden, and decorated class cannot be subclassed.
1544 For example:
1545
1546 class Base:
1547 @final
1548 def done(self) -> None:
1549 ...
1550 class Sub(Base):
1551 def done(self) -> None: # Error reported by type checker
1552 ...
1553
1554 @final
1555 class Leaf:
1556 ...
1557 class Other(Leaf): # Error reported by type checker
1558 ...
1559
1560 There is no runtime checking of these properties.
1561 """
1562 return f
1563
1564
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001565# Some unconstrained type variables. These are used by the container types.
1566# (These are not for export.)
1567T = TypeVar('T') # Any type.
1568KT = TypeVar('KT') # Key type.
1569VT = TypeVar('VT') # Value type.
1570T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1571V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1572VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1573T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1574# Internal type variable used for Type[].
1575CT_co = TypeVar('CT_co', covariant=True, bound=type)
1576
1577# A useful type variable with constraints. This represents string types.
1578# (This one *is* for export!)
1579AnyStr = TypeVar('AnyStr', bytes, str)
1580
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001581
1582# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001583_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001584
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001585Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1586Awaitable = _alias(collections.abc.Awaitable, 1)
1587Coroutine = _alias(collections.abc.Coroutine, 3)
1588AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1589AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1590Iterable = _alias(collections.abc.Iterable, 1)
1591Iterator = _alias(collections.abc.Iterator, 1)
1592Reversible = _alias(collections.abc.Reversible, 1)
1593Sized = _alias(collections.abc.Sized, 0) # Not generic.
1594Container = _alias(collections.abc.Container, 1)
1595Collection = _alias(collections.abc.Collection, 1)
1596Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001597Callable.__doc__ = \
1598 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001600 The subscription syntax must always be used with exactly two
1601 values: the argument list and the return type. The argument list
1602 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001603
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001604 There is no syntax to indicate optional or keyword arguments,
1605 such function types are rarely used as callback types.
1606 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001607AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1608MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001609# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001610Mapping = _alias(collections.abc.Mapping, 2)
1611MutableMapping = _alias(collections.abc.MutableMapping, 2)
1612Sequence = _alias(collections.abc.Sequence, 1)
1613MutableSequence = _alias(collections.abc.MutableSequence, 1)
1614ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1615# Tuple accepts variable number of parameters.
1616Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001617Tuple.__doc__ = \
1618 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001619
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001620 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1621 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1622 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001623
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001624 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1625 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001626List = _alias(list, 1, inst=False, name='List')
1627Deque = _alias(collections.deque, 1, name='Deque')
1628Set = _alias(set, 1, inst=False, name='Set')
1629FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1630MappingView = _alias(collections.abc.MappingView, 1)
1631KeysView = _alias(collections.abc.KeysView, 1)
1632ItemsView = _alias(collections.abc.ItemsView, 2)
1633ValuesView = _alias(collections.abc.ValuesView, 1)
1634ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1635AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1636Dict = _alias(dict, 2, inst=False, name='Dict')
1637DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1638OrderedDict = _alias(collections.OrderedDict, 2)
1639Counter = _alias(collections.Counter, 1)
1640ChainMap = _alias(collections.ChainMap, 2)
1641Generator = _alias(collections.abc.Generator, 3)
1642AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1643Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001644Type.__doc__ = \
1645 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001646
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001647 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001648
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001649 class User: ... # Abstract base for User classes
1650 class BasicUser(User): ...
1651 class ProUser(User): ...
1652 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001653
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001654 And a function that takes a class argument that's a subclass of
1655 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001656
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001657 U = TypeVar('U', bound=User)
1658 def new_user(user_class: Type[U]) -> U:
1659 user = user_class()
1660 # (Here we could write the user object to a database)
1661 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001662
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001663 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001664
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001665 At this point the type checker knows that joe has type BasicUser.
1666 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001667
1668
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001669@runtime_checkable
1670class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001671 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001672 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001673
1674 @abstractmethod
1675 def __int__(self) -> int:
1676 pass
1677
1678
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001679@runtime_checkable
1680class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001681 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001682 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001683
1684 @abstractmethod
1685 def __float__(self) -> float:
1686 pass
1687
1688
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001689@runtime_checkable
1690class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001691 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001692 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001693
1694 @abstractmethod
1695 def __complex__(self) -> complex:
1696 pass
1697
1698
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001699@runtime_checkable
1700class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001701 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001702 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001703
1704 @abstractmethod
1705 def __bytes__(self) -> bytes:
1706 pass
1707
1708
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001709@runtime_checkable
1710class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001711 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001712 __slots__ = ()
1713
1714 @abstractmethod
1715 def __index__(self) -> int:
1716 pass
1717
1718
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001719@runtime_checkable
1720class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001721 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001722 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001723
1724 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001725 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001726 pass
1727
1728
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001729@runtime_checkable
1730class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001731 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001732 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001733
1734 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001735 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001736 pass
1737
1738
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001739def _make_nmtuple(name, types, module, defaults = ()):
1740 fields = [n for n, t in types]
1741 types = {n: _type_check(t, f"field {n} annotation must be a type")
1742 for n, t in types}
1743 nm_tpl = collections.namedtuple(name, fields,
1744 defaults=defaults, module=module)
1745 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001746 return nm_tpl
1747
1748
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001749# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001750_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
1751 '_fields', '_field_defaults',
1752 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001753
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001754_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001755
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001756
Guido van Rossum2f841442016-11-15 09:48:06 -08001757class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001758
Guido van Rossum2f841442016-11-15 09:48:06 -08001759 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001760 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08001761 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001762 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08001763 for field_name in types:
1764 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001765 default_names.append(field_name)
1766 elif default_names:
1767 raise TypeError(f"Non-default namedtuple field {field_name} "
1768 f"cannot follow default field"
1769 f"{'s' if len(default_names) > 1 else ''} "
1770 f"{', '.join(default_names)}")
1771 nm_tpl = _make_nmtuple(typename, types.items(),
1772 defaults=[ns[n] for n in default_names],
1773 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08001774 # update from user namespace without overriding special namedtuple attributes
1775 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001776 if key in _prohibited:
1777 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
1778 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08001779 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08001780 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001781
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001782
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001783def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08001784 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001785
Guido van Rossum2f841442016-11-15 09:48:06 -08001786 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001787
Guido van Rossum2f841442016-11-15 09:48:06 -08001788 class Employee(NamedTuple):
1789 name: str
1790 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001791
Guido van Rossum2f841442016-11-15 09:48:06 -08001792 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001793
Guido van Rossum2f841442016-11-15 09:48:06 -08001794 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001795
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07001796 The resulting class has an extra __annotations__ attribute, giving a
1797 dict that maps field names to types. (The field names are also in
1798 the _fields attribute, which is part of the namedtuple API.)
1799 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001800
Guido van Rossum2f841442016-11-15 09:48:06 -08001801 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001802
Guido van Rossum2f841442016-11-15 09:48:06 -08001803 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001804
Guido van Rossum2f841442016-11-15 09:48:06 -08001805 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
1806 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001807 if fields is None:
1808 fields = kwargs.items()
1809 elif kwargs:
1810 raise TypeError("Either list of fields or keywords"
1811 " can be provided to NamedTuple, not both")
1812 try:
1813 module = sys._getframe(1).f_globals.get('__name__', '__main__')
1814 except (AttributeError, ValueError):
1815 module = None
1816 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001817
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001818_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
1819
1820def _namedtuple_mro_entries(bases):
1821 if len(bases) > 1:
1822 raise TypeError("Multiple inheritance with NamedTuple is not supported")
1823 assert bases[0] is NamedTuple
1824 return (_NamedTuple,)
1825
1826NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001827
1828
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001829class _TypedDictMeta(type):
1830 def __new__(cls, name, bases, ns, total=True):
1831 """Create new typed dict class object.
1832
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001833 This method is called when TypedDict is subclassed,
1834 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001835 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001836 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001837 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001838 for base in bases:
1839 if type(base) is not _TypedDictMeta:
1840 raise TypeError('cannot inherit from both a TypedDict type '
1841 'and a non-TypedDict base class')
1842 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001843
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001844 annotations = {}
1845 own_annotations = ns.get('__annotations__', {})
1846 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001847 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001848 own_annotations = {
1849 n: _type_check(tp, msg) for n, tp in own_annotations.items()
1850 }
1851 required_keys = set()
1852 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001853
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001854 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001855 annotations.update(base.__dict__.get('__annotations__', {}))
1856 required_keys.update(base.__dict__.get('__required_keys__', ()))
1857 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001858
Vlad Emelianov10e87e52020-02-13 20:53:29 +01001859 annotations.update(own_annotations)
1860 if total:
1861 required_keys.update(own_annotation_keys)
1862 else:
1863 optional_keys.update(own_annotation_keys)
1864
1865 tp_dict.__annotations__ = annotations
1866 tp_dict.__required_keys__ = frozenset(required_keys)
1867 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001868 if not hasattr(tp_dict, '__total__'):
1869 tp_dict.__total__ = total
1870 return tp_dict
1871
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001872 __call__ = dict # static method
1873
1874 def __subclasscheck__(cls, other):
1875 # Typed dicts are only for static structural subtyping.
1876 raise TypeError('TypedDict does not support instance and class checks')
1877
1878 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001879
1880
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001881def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001882 """A simple typed namespace. At runtime it is equivalent to a plain dict.
1883
1884 TypedDict creates a dictionary type that expects all of its
1885 instances to have a certain set of keys, where each key is
1886 associated with a value of a consistent type. This expectation
1887 is not checked at runtime but is only enforced by type checkers.
1888 Usage::
1889
1890 class Point2D(TypedDict):
1891 x: int
1892 y: int
1893 label: str
1894
1895 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
1896 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
1897
1898 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
1899
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11001900 The type info can be accessed via the Point2D.__annotations__ dict, and
1901 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
1902 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001903
1904 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
1905 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
1906
ananthan-123ab6423f2020-02-19 10:03:05 +05301907 By default, all keys must be present in a TypedDict. It is possible
1908 to override this by specifying totality.
1909 Usage::
1910
1911 class point2D(TypedDict, total=False):
1912 x: int
1913 y: int
1914
1915 This means that a point2D TypedDict can have any of the keys omitted.A type
1916 checker is only expected to support a literal False or True as the value of
1917 the total argument. True is the default, and makes all items defined in the
1918 class body be required.
1919
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001920 The class syntax is only supported in Python 3.6+, while two other
1921 syntax forms work for Python 2.7 and 3.2+
1922 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03001923 if fields is None:
1924 fields = kwargs
1925 elif kwargs:
1926 raise TypeError("TypedDict takes either a dict or keyword arguments,"
1927 " but not both")
1928
1929 ns = {'__annotations__': dict(fields), '__total__': total}
1930 try:
1931 # Setting correct module is necessary to make typed dict classes pickleable.
1932 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
1933 except (AttributeError, ValueError):
1934 pass
1935
1936 return _TypedDictMeta(typename, (), ns)
1937
1938_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
1939TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01001940
1941
Guido van Rossum91185fe2016-06-08 11:19:11 -07001942def NewType(name, tp):
1943 """NewType creates simple unique types with almost zero
1944 runtime overhead. NewType(name, tp) is considered a subtype of tp
1945 by static type checkers. At runtime, NewType(name, tp) returns
1946 a dummy function that simply returns its argument. Usage::
1947
1948 UserId = NewType('UserId', int)
1949
1950 def name_by_id(user_id: UserId) -> str:
1951 ...
1952
1953 UserId('user') # Fails type check
1954
1955 name_by_id(42) # Fails type check
1956 name_by_id(UserId(42)) # OK
1957
1958 num = UserId(5) + 1 # type: int
1959 """
1960
1961 def new_type(x):
1962 return x
1963
1964 new_type.__name__ = name
1965 new_type.__supertype__ = tp
1966 return new_type
1967
1968
Guido van Rossum0e0563c2016-04-05 14:54:25 -07001969# Python-version-specific alias (Python 2: unicode; Python 3: str)
1970Text = str
1971
1972
Guido van Rossum91185fe2016-06-08 11:19:11 -07001973# Constant that's True when type checking, but False here.
1974TYPE_CHECKING = False
1975
1976
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001977class IO(Generic[AnyStr]):
1978 """Generic base class for TextIO and BinaryIO.
1979
1980 This is an abstract, generic version of the return of open().
1981
1982 NOTE: This does not distinguish between the different possible
1983 classes (text vs. binary, read vs. write vs. read/write,
1984 append-only, unbuffered). The TextIO and BinaryIO subclasses
1985 below capture the distinctions between text vs. binary, which is
1986 pervasive in the interface; however we currently do not offer a
1987 way to track the other distinctions in the type system.
1988 """
1989
Guido van Rossumd70fe632015-08-05 12:11:06 +02001990 __slots__ = ()
1991
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001992 @property
1993 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001994 def mode(self) -> str:
1995 pass
1996
HongWeipeng6ce03ec2019-09-27 15:54:26 +08001997 @property
1998 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001999 def name(self) -> str:
2000 pass
2001
2002 @abstractmethod
2003 def close(self) -> None:
2004 pass
2005
Shantanu2e6569b2020-01-29 18:52:36 -08002006 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002007 @abstractmethod
2008 def closed(self) -> bool:
2009 pass
2010
2011 @abstractmethod
2012 def fileno(self) -> int:
2013 pass
2014
2015 @abstractmethod
2016 def flush(self) -> None:
2017 pass
2018
2019 @abstractmethod
2020 def isatty(self) -> bool:
2021 pass
2022
2023 @abstractmethod
2024 def read(self, n: int = -1) -> AnyStr:
2025 pass
2026
2027 @abstractmethod
2028 def readable(self) -> bool:
2029 pass
2030
2031 @abstractmethod
2032 def readline(self, limit: int = -1) -> AnyStr:
2033 pass
2034
2035 @abstractmethod
2036 def readlines(self, hint: int = -1) -> List[AnyStr]:
2037 pass
2038
2039 @abstractmethod
2040 def seek(self, offset: int, whence: int = 0) -> int:
2041 pass
2042
2043 @abstractmethod
2044 def seekable(self) -> bool:
2045 pass
2046
2047 @abstractmethod
2048 def tell(self) -> int:
2049 pass
2050
2051 @abstractmethod
2052 def truncate(self, size: int = None) -> int:
2053 pass
2054
2055 @abstractmethod
2056 def writable(self) -> bool:
2057 pass
2058
2059 @abstractmethod
2060 def write(self, s: AnyStr) -> int:
2061 pass
2062
2063 @abstractmethod
2064 def writelines(self, lines: List[AnyStr]) -> None:
2065 pass
2066
2067 @abstractmethod
2068 def __enter__(self) -> 'IO[AnyStr]':
2069 pass
2070
2071 @abstractmethod
2072 def __exit__(self, type, value, traceback) -> None:
2073 pass
2074
2075
2076class BinaryIO(IO[bytes]):
2077 """Typed version of the return of open() in binary mode."""
2078
Guido van Rossumd70fe632015-08-05 12:11:06 +02002079 __slots__ = ()
2080
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002081 @abstractmethod
2082 def write(self, s: Union[bytes, bytearray]) -> int:
2083 pass
2084
2085 @abstractmethod
2086 def __enter__(self) -> 'BinaryIO':
2087 pass
2088
2089
2090class TextIO(IO[str]):
2091 """Typed version of the return of open() in text mode."""
2092
Guido van Rossumd70fe632015-08-05 12:11:06 +02002093 __slots__ = ()
2094
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002095 @property
2096 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002097 def buffer(self) -> BinaryIO:
2098 pass
2099
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002100 @property
2101 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002102 def encoding(self) -> str:
2103 pass
2104
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002105 @property
2106 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002107 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002108 pass
2109
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002110 @property
2111 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002112 def line_buffering(self) -> bool:
2113 pass
2114
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002115 @property
2116 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002117 def newlines(self) -> Any:
2118 pass
2119
2120 @abstractmethod
2121 def __enter__(self) -> 'TextIO':
2122 pass
2123
2124
2125class io:
2126 """Wrapper namespace for IO generic classes."""
2127
2128 __all__ = ['IO', 'TextIO', 'BinaryIO']
2129 IO = IO
2130 TextIO = TextIO
2131 BinaryIO = BinaryIO
2132
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002133
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002134io.__name__ = __name__ + '.io'
2135sys.modules[io.__name__] = io
2136
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002137Pattern = _alias(stdlib_re.Pattern, 1)
2138Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002139
2140class re:
2141 """Wrapper namespace for re type aliases."""
2142
2143 __all__ = ['Pattern', 'Match']
2144 Pattern = Pattern
2145 Match = Match
2146
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002147
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002148re.__name__ = __name__ + '.re'
2149sys.modules[re.__name__] = re