blob: dc2a7a478835dbca51f1b6fe7006c057892ee484 [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.
kj73607be2020-12-24 12:33:48 +08007* _SpecialForm and its instances (special forms):
8 Any, NoReturn, ClassVar, Union, Optional, Concatenate
9* Classes whose instances can be type arguments in addition to types:
10 ForwardRef, TypeVar and ParamSpec
Ivan Levkivskyid911e402018-01-20 11:23:59 +000011* The core of internal generics API: _GenericAlias and _VariadicGenericAlias, the latter is
12 currently only used by Tuple and Callable. All subscripted types like X[int], Union[int, str],
13 etc., are instances of either of these classes.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010014* The public counterpart of the generics API consists of two classes: Generic and Protocol.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000015* Public helper functions: get_type_hints, overload, cast, no_type_check,
16 no_type_check_decorator.
17* Generic aliases for collections.abc ABCs and few additional protocols.
ananthan-123ab6423f2020-02-19 10:03:05 +053018* Special types: NewType, NamedTuple, TypedDict.
Ivan Levkivskyid911e402018-01-20 11:23:59 +000019* Wrapper submodules for re and io related types.
20"""
21
HongWeipeng6ce03ec2019-09-27 15:54:26 +080022from abc import abstractmethod, ABCMeta
Batuhan Taskaya044a1042020-10-06 23:03:02 +030023import ast
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070024import collections
Ivan Levkivskyid911e402018-01-20 11:23:59 +000025import collections.abc
Brett Cannonf3ad0422016-04-15 10:51:30 -070026import contextlib
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070027import functools
Serhiy Storchaka09f32212018-05-26 21:19:26 +030028import operator
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070029import re as stdlib_re # Avoid confusion with the re we export.
30import sys
31import types
Guido van Rossum48b069a2020-04-07 09:50:06 -070032from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070033
34# Please keep __all__ alphabetized within each category.
35__all__ = [
36 # Super-special typing primitives.
Jakub Stasiakcf5b1092020-02-05 02:10:19 +010037 'Annotated',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070038 'Any',
39 'Callable',
Guido van Rossum0a6976d2016-09-11 15:34:56 -070040 'ClassVar',
kj73607be2020-12-24 12:33:48 +080041 'Concatenate',
Ivan Levkivskyif3672422019-05-26 09:37:07 +010042 'Final',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070043 'ForwardRef',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070044 'Generic',
Ivan Levkivskyib891c462019-05-26 09:37:48 +010045 'Literal',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070046 'Optional',
kj73607be2020-12-24 12:33:48 +080047 'ParamSpec',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +010048 'Protocol',
Guido van Rossumeb9aca32016-05-24 16:38:22 -070049 'Tuple',
50 'Type',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070051 'TypeVar',
52 'Union',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070053
54 # ABCs (from collections.abc).
55 'AbstractSet', # collections.abc.Set.
56 'ByteString',
57 'Container',
Ivan Levkivskyi29fda8d2017-06-10 21:57:56 +020058 'ContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070059 'Hashable',
60 'ItemsView',
61 'Iterable',
62 'Iterator',
63 'KeysView',
64 'Mapping',
65 'MappingView',
66 'MutableMapping',
67 'MutableSequence',
68 'MutableSet',
69 'Sequence',
70 'Sized',
71 'ValuesView',
Ivan Levkivskyid911e402018-01-20 11:23:59 +000072 'Awaitable',
73 'AsyncIterator',
74 'AsyncIterable',
75 'Coroutine',
76 'Collection',
77 'AsyncGenerator',
78 'AsyncContextManager',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070079
80 # Structural checks, a.k.a. protocols.
81 'Reversible',
82 'SupportsAbs',
Ivan Levkivskyif06e0212017-05-02 19:14:07 +020083 'SupportsBytes',
84 'SupportsComplex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070085 'SupportsFloat',
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -070086 'SupportsIndex',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070087 'SupportsInt',
88 'SupportsRound',
89
90 # Concrete collection types.
Anthony Sottiled30da5d2019-05-29 11:19:38 -070091 'ChainMap',
Ivan Levkivskyib692dc82017-02-13 22:50:14 +010092 'Counter',
Raymond Hettinger80490522017-01-16 22:42:37 -080093 'Deque',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070094 'Dict',
Guido van Rossumbd5b9a02016-04-05 08:28:52 -070095 'DefaultDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070096 'List',
Anthony Sottiled30da5d2019-05-29 11:19:38 -070097 'OrderedDict',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -070098 'Set',
Guido van Rossumefa798d2016-08-23 11:01:50 -070099 'FrozenSet',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700100 'NamedTuple', # Not really a type.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +0100101 'TypedDict', # Not really a type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700102 'Generator',
103
104 # One-off things.
105 'AnyStr',
106 'cast',
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100107 'final',
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +0100108 'get_args',
109 'get_origin',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700110 'get_type_hints',
Patrick Reader0705ec82020-09-16 05:58:32 +0100111 'is_typeddict',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700112 'NewType',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700113 'no_type_check',
114 'no_type_check_decorator',
aetracht45738202018-03-19 14:41:32 -0400115 'NoReturn',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700116 'overload',
Jelle Zijlstra52243362021-04-10 19:57:05 -0700117 'ParamSpecArgs',
118 'ParamSpecKwargs',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100119 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700120 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700121 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300122 'TypeAlias',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700123]
124
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700125# The pseudo-submodules 're' and 'io' are part of the public
126# namespace, but excluded from __all__ because they might stomp on
127# legitimate imports of those modules.
128
kj463c7d32020-12-14 02:38:24 +0800129
130def _type_convert(arg):
131 """For converting None to type(None), and strings to ForwardRef."""
132 if arg is None:
133 return type(None)
134 if isinstance(arg, str):
135 return ForwardRef(arg)
136 return arg
137
138
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700139def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000140 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700141
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000142 As a special case, accept None and return type(None) instead. Also wrap strings
143 into ForwardRef instances. Consider several corner cases, for example plain
144 special forms like Union are not valid, while Union[int, str] is OK, etc.
145 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700146
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700148
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000149 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700150 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100151 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700152 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100153 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400154
kj463c7d32020-12-14 02:38:24 +0800155 arg = _type_convert(arg)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400157 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000158 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300159 if arg in (Any, NoReturn):
160 return arg
161 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000162 raise TypeError(f"Plain {arg} is not valid as type argument")
kj73607be2020-12-24 12:33:48 +0800163 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000164 return arg
165 if not callable(arg):
166 raise TypeError(f"{msg} Got {arg!r:.100}.")
167 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700168
169
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000170def _type_repr(obj):
171 """Return the repr() of an object, special-casing types (internal helper).
172
173 If obj is a type, we return a shorter version than the default
174 type.__repr__, based on the module and qualified name, which is
175 typically enough to uniquely identify a type. For everything
176 else, we fall back on repr(obj).
177 """
kj1f7dfb22020-11-02 02:13:38 +0800178 if isinstance(obj, types.GenericAlias):
179 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000180 if isinstance(obj, type):
181 if obj.__module__ == 'builtins':
182 return obj.__qualname__
183 return f'{obj.__module__}.{obj.__qualname__}'
184 if obj is ...:
185 return('...')
186 if isinstance(obj, types.FunctionType):
187 return obj.__name__
188 return repr(obj)
189
190
191def _collect_type_vars(types):
kj73607be2020-12-24 12:33:48 +0800192 """Collect all type variable-like variables contained
193 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000194
195 _collect_type_vars((T, List[S, T])) == (T, S)
196 """
197 tvars = []
198 for t in types:
kj73607be2020-12-24 12:33:48 +0800199 if isinstance(t, _TypeVarLike) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000200 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300201 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000202 tvars.extend([t for t in t.__parameters__ if t not in tvars])
203 return tuple(tvars)
204
205
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300206def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000207 """Check correct count for parameters of a generic cls (internal helper).
208 This gives a nice error message in case of count mismatch.
209 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300210 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 raise TypeError(f"{cls} is not a generic class")
212 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000213 if alen != elen:
214 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
215 f" actual {alen}, expected {elen}")
216
kj73607be2020-12-24 12:33:48 +0800217def _prepare_paramspec_params(cls, params):
218 """Prepares the parameters for a Generic containing ParamSpec
219 variables (internal helper).
220 """
221 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
222 if len(cls.__parameters__) == 1 and len(params) > 1:
223 return (params,)
224 else:
225 _params = []
226 # Convert lists to tuples to help other libraries cache the results.
227 for p, tvar in zip(params, cls.__parameters__):
228 if isinstance(tvar, ParamSpec) and isinstance(p, list):
229 p = tuple(p)
230 _params.append(p)
231 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000232
Yurii Karabasf03d3182020-11-17 04:23:19 +0200233def _deduplicate(params):
234 # Weed out strict duplicates, preserving the first of each occurrence.
235 all_params = set(params)
236 if len(all_params) < len(params):
237 new_params = []
238 for t in params:
239 if t in all_params:
240 new_params.append(t)
241 all_params.remove(t)
242 params = new_params
243 assert not all_params, all_params
244 return params
245
246
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000247def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700248 """An internal helper for Union creation and substitution: flatten Unions
249 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000250 """
251 # Flatten out Union[Union[...], ...].
252 params = []
253 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700254 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000255 params.extend(p.__args__)
256 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
257 params.extend(p[1:])
258 else:
259 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200260
261 return tuple(_deduplicate(params))
262
263
264def _flatten_literal_params(parameters):
265 """An internal helper for Literal creation: flatten Literals among parameters"""
266 params = []
267 for p in parameters:
268 if isinstance(p, _LiteralGenericAlias):
269 params.extend(p.__args__)
270 else:
271 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700272 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000273
274
275_cleanups = []
276
277
Yurii Karabasf03d3182020-11-17 04:23:19 +0200278def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000279 """Internal wrapper caching __getitem__ of generic types with a fallback to
280 original function for non-hashable arguments.
281 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200282 def decorator(func):
283 cached = functools.lru_cache(typed=typed)(func)
284 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000285
Yurii Karabasf03d3182020-11-17 04:23:19 +0200286 @functools.wraps(func)
287 def inner(*args, **kwds):
288 try:
289 return cached(*args, **kwds)
290 except TypeError:
291 pass # All real errors (not unhashable args) are raised below.
292 return func(*args, **kwds)
293 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000294
Yurii Karabasf03d3182020-11-17 04:23:19 +0200295 if func is not None:
296 return decorator(func)
297
298 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000299
wyfo653f4202020-07-22 21:47:28 +0200300def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400301 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000302 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200303 recursive_guard is used to prevent prevent infinite recursion
304 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000305 """
306 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200307 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300308 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200309 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000310 if ev_args == t.__args__:
311 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300312 if isinstance(t, GenericAlias):
313 return GenericAlias(t.__origin__, ev_args)
314 else:
315 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000316 return t
317
318
319class _Final:
320 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700321
Guido van Rossum83ec3022017-01-17 20:43:28 -0800322 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700323
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300324 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000325 if '_root' not in kwds:
326 raise TypeError("Cannot subclass special typing classes")
327
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100328class _Immutable:
329 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300330 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000331
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100332 def __copy__(self):
333 return self
334
335 def __deepcopy__(self, memo):
336 return self
337
338
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300339# Internal indicator of special typing constructs.
340# See __doc__ instance attribute for specific docs.
341class _SpecialForm(_Final, _root=True):
342 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000343
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300344 def __init__(self, getitem):
345 self._getitem = getitem
346 self._name = getitem.__name__
347 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000348
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300349 def __mro_entries__(self, bases):
350 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700351
352 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000353 return 'typing.' + self._name
354
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100355 def __reduce__(self):
356 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700357
358 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000359 raise TypeError(f"Cannot instantiate {self!r}")
360
361 def __instancecheck__(self, obj):
362 raise TypeError(f"{self} cannot be used with isinstance()")
363
364 def __subclasscheck__(self, cls):
365 raise TypeError(f"{self} cannot be used with issubclass()")
366
367 @_tp_cache
368 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300369 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700370
Yurii Karabasf03d3182020-11-17 04:23:19 +0200371
372class _LiteralSpecialForm(_SpecialForm, _root=True):
373 @_tp_cache(typed=True)
374 def __getitem__(self, parameters):
375 return self._getitem(self, parameters)
376
377
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300378@_SpecialForm
379def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000380 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700381
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000382 - Any is compatible with every type.
383 - Any assumed to have all methods.
384 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700385
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000386 Note that all the above statements are true from the point of view of
387 static type checkers. At runtime, Any should not be used with instance
388 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300389 """
390 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200391
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300392@_SpecialForm
393def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000394 """Special type indicating functions that never return.
395 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700396
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000397 from typing import NoReturn
398
399 def stop() -> NoReturn:
400 raise Exception('no way')
401
402 This type is invalid in other positions, e.g., ``List[NoReturn]``
403 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300404 """
405 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000406
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300407@_SpecialForm
408def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000409 """Special type construct to mark class variables.
410
411 An annotation wrapped in ClassVar indicates that a given
412 attribute is intended to be used as a class variable and
413 should not be set on instances of that class. Usage::
414
415 class Starship:
416 stats: ClassVar[Dict[str, int]] = {} # class variable
417 damage: int = 10 # instance variable
418
419 ClassVar accepts only types and cannot be further subscribed.
420
421 Note that ClassVar is not a class itself, and should not
422 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300423 """
424 item = _type_check(parameters, f'{self} accepts only single type.')
425 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000426
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300427@_SpecialForm
428def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100429 """Special typing construct to indicate final names to type checkers.
430
431 A final name cannot be re-assigned or overridden in a subclass.
432 For example:
433
434 MAX_SIZE: Final = 9000
435 MAX_SIZE += 1 # Error reported by type checker
436
437 class Connection:
438 TIMEOUT: Final[int] = 10
439
440 class FastConnector(Connection):
441 TIMEOUT = 1 # Error reported by type checker
442
443 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300444 """
445 item = _type_check(parameters, f'{self} accepts only single type.')
446 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100447
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300448@_SpecialForm
449def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000450 """Union type; Union[X, Y] means either X or Y.
451
452 To define a union, use e.g. Union[int, str]. Details:
453 - The arguments must be types and there must be at least one.
454 - None as an argument is a special case and is replaced by
455 type(None).
456 - Unions of unions are flattened, e.g.::
457
458 Union[Union[int, str], float] == Union[int, str, float]
459
460 - Unions of a single argument vanish, e.g.::
461
462 Union[int] == int # The constructor actually returns int
463
464 - Redundant arguments are skipped, e.g.::
465
466 Union[int, str, int] == Union[int, str]
467
468 - When comparing unions, the argument order is ignored, e.g.::
469
470 Union[int, str] == Union[str, int]
471
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000472 - You cannot subclass or instantiate a union.
473 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300474 """
475 if parameters == ():
476 raise TypeError("Cannot take a Union of no types.")
477 if not isinstance(parameters, tuple):
478 parameters = (parameters,)
479 msg = "Union[arg, ...]: each arg must be a type."
480 parameters = tuple(_type_check(p, msg) for p in parameters)
481 parameters = _remove_dups_flatten(parameters)
482 if len(parameters) == 1:
483 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300484 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000485
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300486@_SpecialForm
487def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000488 """Optional type.
489
490 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300491 """
492 arg = _type_check(parameters, f"{self} requires a single type.")
493 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700494
Yurii Karabasf03d3182020-11-17 04:23:19 +0200495@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300496def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100497 """Special typing form to define literal types (a.k.a. value types).
498
499 This form can be used to indicate to type checkers that the corresponding
500 variable or function parameter has a value equivalent to the provided
501 literal (or one of several literals):
502
503 def validate_simple(data: Any) -> Literal[True]: # always returns True
504 ...
505
506 MODE = Literal['r', 'rb', 'w', 'wb']
507 def open_helper(file: str, mode: MODE) -> str:
508 ...
509
510 open_helper('/some/path', 'r') # Passes type check
511 open_helper('/other/path', 'typo') # Error in type checker
512
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300513 Literal[...] cannot be subclassed. At runtime, an arbitrary value
514 is allowed as type argument to Literal[...], but type checkers may
515 impose restrictions.
516 """
517 # There is no '_type_check' call because arguments to Literal[...] are
518 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200519 if not isinstance(parameters, tuple):
520 parameters = (parameters,)
521
522 parameters = _flatten_literal_params(parameters)
523
524 try:
525 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
526 except TypeError: # unhashable parameters
527 pass
528
529 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100530
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700531
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300532@_SpecialForm
533def TypeAlias(self, parameters):
534 """Special marker indicating that an assignment should
535 be recognized as a proper type alias definition by type
536 checkers.
537
538 For example::
539
540 Predicate: TypeAlias = Callable[..., bool]
541
542 It's invalid when used anywhere except as in the example above.
543 """
544 raise TypeError(f"{self} is not subscriptable")
545
546
kj73607be2020-12-24 12:33:48 +0800547@_SpecialForm
548def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800549 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
550 higher order function which adds, removes or transforms parameters of a
551 callable.
kj73607be2020-12-24 12:33:48 +0800552
553 For example::
554
555 Callable[Concatenate[int, P], int]
556
557 See PEP 612 for detailed information.
558 """
559 if parameters == ():
560 raise TypeError("Cannot take a Concatenate of no types.")
561 if not isinstance(parameters, tuple):
562 parameters = (parameters,)
563 if not isinstance(parameters[-1], ParamSpec):
564 raise TypeError("The last parameter to Concatenate should be a "
565 "ParamSpec variable.")
566 msg = "Concatenate[arg, ...]: each arg must be a type."
567 parameters = tuple(_type_check(p, msg) for p in parameters)
568 return _ConcatenateGenericAlias(self, parameters)
569
570
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000571class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800572 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700573
Guido van Rossum4cefe742016-09-27 15:20:12 -0700574 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400575 '__forward_evaluated__', '__forward_value__',
576 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700577
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700578 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700579 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000580 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300581
582 # Double-stringified forward references is a result of activating
583 # the 'annotations' future by default. This way, we eliminate them in
584 # the runtime.
585 if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
586 arg = arg[1:-1]
587
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700588 try:
589 code = compile(arg, '<string>', 'eval')
590 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000591 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700592 self.__forward_arg__ = arg
593 self.__forward_code__ = code
594 self.__forward_evaluated__ = False
595 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400596 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700597
wyfo653f4202020-07-22 21:47:28 +0200598 def _evaluate(self, globalns, localns, recursive_guard):
599 if self.__forward_arg__ in recursive_guard:
600 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800601 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700602 if globalns is None and localns is None:
603 globalns = localns = {}
604 elif globalns is None:
605 globalns = localns
606 elif localns is None:
607 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200608 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700609 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400610 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200611 is_argument=self.__forward_is_argument__,
612 )
613 self.__forward_value__ = _eval_type(
614 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
615 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700616 self.__forward_evaluated__ = True
617 return self.__forward_value__
618
Guido van Rossum4cefe742016-09-27 15:20:12 -0700619 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000620 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700621 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100622 if self.__forward_evaluated__ and other.__forward_evaluated__:
623 return (self.__forward_arg__ == other.__forward_arg__ and
624 self.__forward_value__ == other.__forward_value__)
625 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700626
627 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100628 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700629
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700630 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000631 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700632
kj73607be2020-12-24 12:33:48 +0800633class _TypeVarLike:
634 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
635 def __init__(self, bound, covariant, contravariant):
636 """Used to setup TypeVars and ParamSpec's bound, covariant and
637 contravariant attributes.
638 """
639 if covariant and contravariant:
640 raise ValueError("Bivariant types are not supported.")
641 self.__covariant__ = bool(covariant)
642 self.__contravariant__ = bool(contravariant)
643 if bound:
644 self.__bound__ = _type_check(bound, "Bound must be a type.")
645 else:
646 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700647
kj73607be2020-12-24 12:33:48 +0800648 def __or__(self, right):
649 return Union[self, right]
650
Jelle Zijlstra90459192021-04-10 20:00:05 -0700651 def __ror__(self, left):
652 return Union[left, self]
kj73607be2020-12-24 12:33:48 +0800653
654 def __repr__(self):
655 if self.__covariant__:
656 prefix = '+'
657 elif self.__contravariant__:
658 prefix = '-'
659 else:
660 prefix = '~'
661 return prefix + self.__name__
662
663 def __reduce__(self):
664 return self.__name__
665
666
667class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700668 """Type variable.
669
670 Usage::
671
672 T = TypeVar('T') # Can be anything
673 A = TypeVar('A', str, bytes) # Must be str or bytes
674
675 Type variables exist primarily for the benefit of static type
676 checkers. They serve as the parameters for generic types as well
677 as for generic function definitions. See class Generic for more
678 information on generic types. Generic functions work as follows:
679
Guido van Rossumb24569a2016-11-20 18:01:29 -0800680 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700681 '''Return a list containing n references to x.'''
682 return [x]*n
683
684 def longest(x: A, y: A) -> A:
685 '''Return the longest of two strings.'''
686 return x if len(x) >= len(y) else y
687
688 The latter example's signature is essentially the overloading
689 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
690 that if the arguments are instances of some subclass of str,
691 the return type is still plain str.
692
Guido van Rossumb24569a2016-11-20 18:01:29 -0800693 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700694
Guido van Rossumefa798d2016-08-23 11:01:50 -0700695 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100696 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700697 See PEP 484 for more details. By default generic types are invariant
698 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700699
700 Type variables can be introspected. e.g.:
701
702 T.__name__ == 'T'
703 T.__constraints__ == ()
704 T.__covariant__ == False
705 T.__contravariant__ = False
706 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100707
708 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700709 """
710
Guido van Rossum4cefe742016-09-27 15:20:12 -0700711 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300712 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700713
714 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800715 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700716 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800717 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700718 if constraints and bound is not None:
719 raise TypeError("Constraints cannot be combined with bound=...")
720 if constraints and len(constraints) == 1:
721 raise TypeError("A single constraint is not allowed")
722 msg = "TypeVar(name, constraint, ...): constraints must be types."
723 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800724 try:
725 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
726 except (AttributeError, ValueError):
727 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300728 if def_mod != 'typing':
729 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700730
Maggie Moss1b4552c2020-09-09 13:23:24 -0700731
Jelle Zijlstra52243362021-04-10 19:57:05 -0700732class ParamSpecArgs(_Final, _Immutable, _root=True):
733 """The args for a ParamSpec object.
734
735 Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
736
737 ParamSpecArgs objects have a reference back to their ParamSpec:
738
739 P.args.__origin__ is P
740
741 This type is meant for runtime introspection and has no special meaning to
742 static type checkers.
743 """
744 def __init__(self, origin):
745 self.__origin__ = origin
746
747 def __repr__(self):
748 return f"{self.__origin__.__name__}.args"
749
750
751class ParamSpecKwargs(_Final, _Immutable, _root=True):
752 """The kwargs for a ParamSpec object.
753
754 Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
755
756 ParamSpecKwargs objects have a reference back to their ParamSpec:
757
758 P.kwargs.__origin__ is P
759
760 This type is meant for runtime introspection and has no special meaning to
761 static type checkers.
762 """
763 def __init__(self, origin):
764 self.__origin__ = origin
765
766 def __repr__(self):
767 return f"{self.__origin__.__name__}.kwargs"
768
769
kj73607be2020-12-24 12:33:48 +0800770class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
771 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700772
kj73607be2020-12-24 12:33:48 +0800773 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700774
kj73607be2020-12-24 12:33:48 +0800775 P = ParamSpec('P')
776
777 Parameter specification variables exist primarily for the benefit of static
778 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800779 callable to another callable, a pattern commonly found in higher order
780 functions and decorators. They are only valid when used in ``Concatenate``,
781 or s the first argument to ``Callable``, or as parameters for user-defined
782 Generics. See class Generic for more information on generic types. An
783 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800784
785 T = TypeVar('T')
786 P = ParamSpec('P')
787
788 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
789 '''A type-safe decorator to add logging to a function.'''
790 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
791 logging.info(f'{f.__name__} was called')
792 return f(*args, **kwargs)
793 return inner
794
795 @add_logging
796 def add_two(x: float, y: float) -> float:
797 '''Add two numbers together.'''
798 return x + y
799
800 Parameter specification variables defined with covariant=True or
801 contravariant=True can be used to declare covariant or contravariant
802 generic types. These keyword arguments are valid, but their actual semantics
803 are yet to be decided. See PEP 612 for details.
804
805 Parameter specification variables can be introspected. e.g.:
806
807 P.__name__ == 'T'
808 P.__bound__ == None
809 P.__covariant__ == False
810 P.__contravariant__ == False
811
812 Note that only parameter specification variables defined in global scope can
813 be pickled.
814 """
815
816 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
817 '__dict__')
818
Jelle Zijlstra52243362021-04-10 19:57:05 -0700819 @property
820 def args(self):
821 return ParamSpecArgs(self)
822
823 @property
824 def kwargs(self):
825 return ParamSpecKwargs(self)
kj73607be2020-12-24 12:33:48 +0800826
Ken Jinace008c2021-01-11 08:11:41 +0800827 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800828 self.__name__ = name
829 super().__init__(bound, covariant, contravariant)
830 try:
831 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
832 except (AttributeError, ValueError):
833 def_mod = None
834 if def_mod != 'typing':
835 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100836
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700837
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000838def _is_dunder(attr):
839 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800840
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300841class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000842 """The central part of internal API.
843
844 This represents a generic version of type 'origin' with type arguments 'params'.
845 There are two kind of these aliases: user defined and special. The special ones
846 are wrappers around builtin collections and ABCs in collections.abc. These must
847 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
848 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700849 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300850 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000851 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000852 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700853 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000854 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300855
856 def __call__(self, *args, **kwargs):
857 if not self._inst:
858 raise TypeError(f"Type {self._name} cannot be instantiated; "
859 f"use {self.__origin__.__name__}() instead")
860 result = self.__origin__(*args, **kwargs)
861 try:
862 result.__orig_class__ = self
863 except AttributeError:
864 pass
865 return result
866
867 def __mro_entries__(self, bases):
868 res = []
869 if self.__origin__ not in bases:
870 res.append(self.__origin__)
871 i = bases.index(self)
872 for b in bases[i+1:]:
873 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
874 break
875 else:
876 res.append(Generic)
877 return tuple(res)
878
879 def __getattr__(self, attr):
880 # We are careful for copy and pickle.
881 # Also for simplicity we just don't relay all dunder names
882 if '__origin__' in self.__dict__ and not _is_dunder(attr):
883 return getattr(self.__origin__, attr)
884 raise AttributeError(attr)
885
886 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300887 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300888 super().__setattr__(attr, val)
889 else:
890 setattr(self.__origin__, attr, val)
891
892 def __instancecheck__(self, obj):
893 return self.__subclasscheck__(type(obj))
894
895 def __subclasscheck__(self, cls):
896 raise TypeError("Subscripted generics cannot be used with"
897 " class and instance checks")
898
899
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300900# Special typing constructs Union, Optional, Generic, Callable and Tuple
901# use three special attributes for internal bookkeeping of generic types:
902# * __parameters__ is a tuple of unique free type parameters of a generic
903# type, for example, Dict[T, T].__parameters__ == (T,);
904# * __origin__ keeps a reference to a type that was subscripted,
905# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
906# the type.
907# * __args__ is a tuple of all arguments used in subscripting,
908# e.g., Dict[T, int].__args__ == (T, int).
909
910
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300911class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300912 def __init__(self, origin, params, *, inst=True, name=None):
913 super().__init__(origin, inst=inst, name=name)
914 if not isinstance(params, tuple):
915 params = (params,)
916 self.__args__ = tuple(... if a is _TypingEllipsis else
917 () if a is _TypingEmpty else
918 a for a in params)
919 self.__parameters__ = _collect_type_vars(params)
920 if not name:
921 self.__module__ = origin.__module__
922
923 def __eq__(self, other):
924 if not isinstance(other, _GenericAlias):
925 return NotImplemented
926 return (self.__origin__ == other.__origin__
927 and self.__args__ == other.__args__)
928
929 def __hash__(self):
930 return hash((self.__origin__, self.__args__))
931
Maggie Moss1b4552c2020-09-09 13:23:24 -0700932 def __or__(self, right):
933 return Union[self, right]
934
935 def __ror__(self, right):
936 return Union[self, right]
937
Guido van Rossum4cefe742016-09-27 15:20:12 -0700938 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700939 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100940 if self.__origin__ in (Generic, Protocol):
941 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000942 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700943 if not isinstance(params, tuple):
944 params = (params,)
kj73607be2020-12-24 12:33:48 +0800945 params = tuple(_type_convert(p) for p in params)
946 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
947 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300948 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300949
950 subst = dict(zip(self.__parameters__, params))
951 new_args = []
952 for arg in self.__args__:
kj73607be2020-12-24 12:33:48 +0800953 if isinstance(arg, _TypeVarLike):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300954 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300955 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300956 subparams = arg.__parameters__
957 if subparams:
958 subargs = tuple(subst[x] for x in subparams)
959 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +0800960 # Required to flatten out the args for CallableGenericAlias
961 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
962 new_args.extend(arg)
963 else:
964 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300965 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100966
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000967 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300968 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700969
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000970 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300971 if self._name:
972 name = 'typing.' + self._name
973 else:
974 name = _type_repr(self.__origin__)
975 args = ", ".join([_type_repr(a) for a in self.__args__])
976 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000977
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300978 def __reduce__(self):
979 if self._name:
980 origin = globals()[self._name]
981 else:
982 origin = self.__origin__
983 args = tuple(self.__args__)
984 if len(args) == 1 and not isinstance(args[0], tuple):
985 args, = args
986 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000987
988 def __mro_entries__(self, bases):
989 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300990 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000991 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100992 if Protocol in bases:
993 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000994 i = bases.index(self)
995 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300996 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000997 return ()
998 return (self.__origin__,)
999
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001000
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001001# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
1002# 1 for List and 2 for Dict. It may be -1 if variable number of
1003# parameters are accepted (needs custom __getitem__).
1004
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001005class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001006 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001007 if name is None:
1008 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001009 super().__init__(origin, inst=inst, name=name)
1010 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +03001011 if origin.__module__ == 'builtins':
1012 self.__doc__ = f'A generic version of {origin.__qualname__}.'
1013 else:
1014 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001015
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001016 @_tp_cache
1017 def __getitem__(self, params):
1018 if not isinstance(params, tuple):
1019 params = (params,)
1020 msg = "Parameters to generic types must be types."
1021 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001022 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001023 return self.copy_with(params)
1024
1025 def copy_with(self, params):
1026 return _GenericAlias(self.__origin__, params,
1027 name=self._name, inst=self._inst)
1028
1029 def __repr__(self):
1030 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001031
1032 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001033 if isinstance(cls, _SpecialGenericAlias):
1034 return issubclass(cls.__origin__, self.__origin__)
1035 if not isinstance(cls, _GenericAlias):
1036 return issubclass(cls, self.__origin__)
1037 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001038
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001039 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001040 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +01001041
Maggie Moss1b4552c2020-09-09 13:23:24 -07001042 def __or__(self, right):
1043 return Union[self, right]
1044
1045 def __ror__(self, right):
1046 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001047
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001048class _CallableGenericAlias(_GenericAlias, _root=True):
1049 def __repr__(self):
1050 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001051 args = self.__args__
1052 if len(args) == 2 and (args[0] is Ellipsis
1053 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001054 return super().__repr__()
1055 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001056 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1057 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001058
1059 def __reduce__(self):
1060 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001061 if not (len(args) == 2 and (args[0] is Ellipsis
1062 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001063 args = list(args[:-1]), args[-1]
1064 return operator.getitem, (Callable, args)
1065
1066
1067class _CallableType(_SpecialGenericAlias, _root=True):
1068 def copy_with(self, params):
1069 return _CallableGenericAlias(self.__origin__, params,
1070 name=self._name, inst=self._inst)
1071
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001072 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001073 if not isinstance(params, tuple) or len(params) != 2:
1074 raise TypeError("Callable must be used as "
1075 "Callable[[arg, ...], result].")
1076 args, result = params
kj463c7d32020-12-14 02:38:24 +08001077 # This relaxes what args can be on purpose to allow things like
1078 # PEP 612 ParamSpec. Responsibility for whether a user is using
1079 # Callable[...] properly is deferred to static type checkers.
1080 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001081 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001082 else:
1083 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001084 return self.__getitem_inner__(params)
1085
1086 @_tp_cache
1087 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001088 args, result = params
1089 msg = "Callable[args, result]: result must be a type."
1090 result = _type_check(result, msg)
1091 if args is Ellipsis:
1092 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001093 if not isinstance(args, tuple):
1094 args = (args,)
1095 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001096 params = args + (result,)
1097 return self.copy_with(params)
1098
1099
1100class _TupleType(_SpecialGenericAlias, _root=True):
1101 @_tp_cache
1102 def __getitem__(self, params):
1103 if params == ():
1104 return self.copy_with((_TypingEmpty,))
1105 if not isinstance(params, tuple):
1106 params = (params,)
1107 if len(params) == 2 and params[1] is ...:
1108 msg = "Tuple[t, ...]: t must be a type."
1109 p = _type_check(params[0], msg)
1110 return self.copy_with((p, _TypingEllipsis))
1111 msg = "Tuple[t0, t1, ...]: each t must be a type."
1112 params = tuple(_type_check(p, msg) for p in params)
1113 return self.copy_with(params)
1114
1115
1116class _UnionGenericAlias(_GenericAlias, _root=True):
1117 def copy_with(self, params):
1118 return Union[params]
1119
1120 def __eq__(self, other):
1121 if not isinstance(other, _UnionGenericAlias):
1122 return NotImplemented
1123 return set(self.__args__) == set(other.__args__)
1124
1125 def __hash__(self):
1126 return hash(frozenset(self.__args__))
1127
1128 def __repr__(self):
1129 args = self.__args__
1130 if len(args) == 2:
1131 if args[0] is type(None):
1132 return f'typing.Optional[{_type_repr(args[1])}]'
1133 elif args[1] is type(None):
1134 return f'typing.Optional[{_type_repr(args[0])}]'
1135 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001136
Maggie Moss1b4552c2020-09-09 13:23:24 -07001137 def __instancecheck__(self, obj):
1138 return self.__subclasscheck__(type(obj))
1139
1140 def __subclasscheck__(self, cls):
1141 for arg in self.__args__:
1142 if issubclass(cls, arg):
1143 return True
1144
1145
Yurii Karabasf03d3182020-11-17 04:23:19 +02001146def _value_and_type_iter(parameters):
1147 return ((p, type(p)) for p in parameters)
1148
1149
1150class _LiteralGenericAlias(_GenericAlias, _root=True):
1151
1152 def __eq__(self, other):
1153 if not isinstance(other, _LiteralGenericAlias):
1154 return NotImplemented
1155
1156 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1157
1158 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001159 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001160
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001161
kj73607be2020-12-24 12:33:48 +08001162class _ConcatenateGenericAlias(_GenericAlias, _root=True):
1163 pass
1164
1165
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001166class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001167 """Abstract base class for generic types.
1168
Guido van Rossumb24569a2016-11-20 18:01:29 -08001169 A generic type is typically declared by inheriting from
1170 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001171 For example, a generic mapping type might be defined as::
1172
1173 class Mapping(Generic[KT, VT]):
1174 def __getitem__(self, key: KT) -> VT:
1175 ...
1176 # Etc.
1177
1178 This class can then be used as follows::
1179
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001180 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001181 try:
1182 return mapping[key]
1183 except KeyError:
1184 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001185 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001186 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001187 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001188
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001189 @_tp_cache
1190 def __class_getitem__(cls, params):
1191 if not isinstance(params, tuple):
1192 params = (params,)
1193 if not params and cls is not Tuple:
1194 raise TypeError(
1195 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001196 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001197 if cls in (Generic, Protocol):
1198 # Generic and Protocol can only be subscripted with unique type variables.
kj73607be2020-12-24 12:33:48 +08001199 if not all(isinstance(p, _TypeVarLike) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001200 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001201 f"Parameters to {cls.__name__}[...] must all be type variables "
1202 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001203 if len(set(params)) != len(params):
1204 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001205 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001206 else:
1207 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001208 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1209 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001210 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001211 return _GenericAlias(cls, params)
1212
1213 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001214 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001215 tvars = []
1216 if '__orig_bases__' in cls.__dict__:
1217 error = Generic in cls.__orig_bases__
1218 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001219 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001220 if error:
1221 raise TypeError("Cannot inherit from plain Generic")
1222 if '__orig_bases__' in cls.__dict__:
1223 tvars = _collect_type_vars(cls.__orig_bases__)
1224 # Look for Generic[T1, ..., Tn].
1225 # If found, tvars must be a subset of it.
1226 # If not found, tvars is it.
1227 # Also check for and reject plain Generic,
1228 # and reject multiple Generic[...].
1229 gvars = None
1230 for base in cls.__orig_bases__:
1231 if (isinstance(base, _GenericAlias) and
1232 base.__origin__ is Generic):
1233 if gvars is not None:
1234 raise TypeError(
1235 "Cannot inherit from Generic[...] multiple types.")
1236 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001237 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001238 tvarset = set(tvars)
1239 gvarset = set(gvars)
1240 if not tvarset <= gvarset:
1241 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1242 s_args = ', '.join(str(g) for g in gvars)
1243 raise TypeError(f"Some type variables ({s_vars}) are"
1244 f" not listed in Generic[{s_args}]")
1245 tvars = gvars
1246 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001247
1248
1249class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001250 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1251 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001252 to sneak in where prohibited.
1253 """
1254
1255
1256class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001257 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001258
1259
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001260_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1261 '_is_protocol', '_is_runtime_protocol']
1262
1263_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1264 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001265 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001266
1267# These special attributes will be not collected as protocol members.
1268EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1269
1270
1271def _get_protocol_attrs(cls):
1272 """Collect protocol members from a protocol class objects.
1273
1274 This includes names actually defined in the class dictionary, as well
1275 as names that appear in annotations. Special names (above) are skipped.
1276 """
1277 attrs = set()
1278 for base in cls.__mro__[:-1]: # without object
1279 if base.__name__ in ('Protocol', 'Generic'):
1280 continue
1281 annotations = getattr(base, '__annotations__', {})
1282 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1283 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1284 attrs.add(attr)
1285 return attrs
1286
1287
1288def _is_callable_members_only(cls):
1289 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1290 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1291
1292
1293def _no_init(self, *args, **kwargs):
1294 if type(self)._is_protocol:
1295 raise TypeError('Protocols cannot be instantiated')
1296
1297
Rossc1af1282020-12-29 11:55:28 +00001298def _allow_reckless_class_checks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001299 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001300
1301 The abc and functools modules indiscriminately call isinstance() and
1302 issubclass() on the whole MRO of a user class, which may contain protocols.
1303 """
1304 try:
1305 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1306 except (AttributeError, ValueError): # For platforms without _getframe().
1307 return True
1308
1309
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001310_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001311 'collections.abc': [
1312 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1313 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1314 ],
1315 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1316}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001317
1318
1319class _ProtocolMeta(ABCMeta):
1320 # This metaclass is really unfortunate and exists only because of
1321 # the lack of __instancehook__.
1322 def __instancecheck__(cls, instance):
1323 # We need this method for situations where attributes are
1324 # assigned in __init__.
1325 if ((not getattr(cls, '_is_protocol', False) or
1326 _is_callable_members_only(cls)) and
1327 issubclass(instance.__class__, cls)):
1328 return True
1329 if cls._is_protocol:
1330 if all(hasattr(instance, attr) and
1331 # All *methods* can be blocked by setting them to None.
1332 (not callable(getattr(cls, attr, None)) or
1333 getattr(instance, attr) is not None)
1334 for attr in _get_protocol_attrs(cls)):
1335 return True
1336 return super().__instancecheck__(instance)
1337
1338
1339class Protocol(Generic, metaclass=_ProtocolMeta):
1340 """Base class for protocol classes.
1341
1342 Protocol classes are defined as::
1343
1344 class Proto(Protocol):
1345 def meth(self) -> int:
1346 ...
1347
1348 Such classes are primarily used with static type checkers that recognize
1349 structural subtyping (static duck-typing), for example::
1350
1351 class C:
1352 def meth(self) -> int:
1353 return 0
1354
1355 def func(x: Proto) -> int:
1356 return x.meth()
1357
1358 func(C()) # Passes static type check
1359
1360 See PEP 544 for details. Protocol classes decorated with
1361 @typing.runtime_checkable act as simple-minded runtime protocols that check
1362 only the presence of given attributes, ignoring their type signatures.
1363 Protocol classes can be generic, they are defined as::
1364
1365 class GenProto(Protocol[T]):
1366 def meth(self) -> T:
1367 ...
1368 """
1369 __slots__ = ()
1370 _is_protocol = True
1371 _is_runtime_protocol = False
1372
1373 def __init_subclass__(cls, *args, **kwargs):
1374 super().__init_subclass__(*args, **kwargs)
1375
1376 # Determine if this is a protocol or a concrete subclass.
1377 if not cls.__dict__.get('_is_protocol', False):
1378 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1379
1380 # Set (or override) the protocol subclass hook.
1381 def _proto_hook(other):
1382 if not cls.__dict__.get('_is_protocol', False):
1383 return NotImplemented
1384
1385 # First, perform various sanity checks.
1386 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001387 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001388 return NotImplemented
1389 raise TypeError("Instance and class checks can only be used with"
1390 " @runtime_checkable protocols")
1391 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001392 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001393 return NotImplemented
1394 raise TypeError("Protocols with non-method members"
1395 " don't support issubclass()")
1396 if not isinstance(other, type):
1397 # Same error message as for issubclass(1, int).
1398 raise TypeError('issubclass() arg 1 must be a class')
1399
1400 # Second, perform the actual structural compatibility check.
1401 for attr in _get_protocol_attrs(cls):
1402 for base in other.__mro__:
1403 # Check if the members appears in the class dictionary...
1404 if attr in base.__dict__:
1405 if base.__dict__[attr] is None:
1406 return NotImplemented
1407 break
1408
1409 # ...or in annotations, if it is a sub-protocol.
1410 annotations = getattr(base, '__annotations__', {})
1411 if (isinstance(annotations, collections.abc.Mapping) and
1412 attr in annotations and
1413 issubclass(other, Generic) and other._is_protocol):
1414 break
1415 else:
1416 return NotImplemented
1417 return True
1418
1419 if '__subclasshook__' not in cls.__dict__:
1420 cls.__subclasshook__ = _proto_hook
1421
1422 # We have nothing more to do for non-protocols...
1423 if not cls._is_protocol:
1424 return
1425
1426 # ... otherwise check consistency of bases, and prohibit instantiation.
1427 for base in cls.__bases__:
1428 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001429 base.__module__ in _PROTO_ALLOWLIST and
1430 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001431 issubclass(base, Generic) and base._is_protocol):
1432 raise TypeError('Protocols can only inherit from other'
1433 ' protocols, got %r' % base)
1434 cls.__init__ = _no_init
1435
1436
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001437class _AnnotatedAlias(_GenericAlias, _root=True):
1438 """Runtime representation of an annotated type.
1439
1440 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1441 with extra annotations. The alias behaves like a normal typing alias,
1442 instantiating is the same as instantiating the underlying type, binding
1443 it to types is also the same.
1444 """
1445 def __init__(self, origin, metadata):
1446 if isinstance(origin, _AnnotatedAlias):
1447 metadata = origin.__metadata__ + metadata
1448 origin = origin.__origin__
1449 super().__init__(origin, origin)
1450 self.__metadata__ = metadata
1451
1452 def copy_with(self, params):
1453 assert len(params) == 1
1454 new_type = params[0]
1455 return _AnnotatedAlias(new_type, self.__metadata__)
1456
1457 def __repr__(self):
1458 return "typing.Annotated[{}, {}]".format(
1459 _type_repr(self.__origin__),
1460 ", ".join(repr(a) for a in self.__metadata__)
1461 )
1462
1463 def __reduce__(self):
1464 return operator.getitem, (
1465 Annotated, (self.__origin__,) + self.__metadata__
1466 )
1467
1468 def __eq__(self, other):
1469 if not isinstance(other, _AnnotatedAlias):
1470 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001471 return (self.__origin__ == other.__origin__
1472 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001473
1474 def __hash__(self):
1475 return hash((self.__origin__, self.__metadata__))
1476
1477
1478class Annotated:
1479 """Add context specific metadata to a type.
1480
1481 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1482 hypothetical runtime_check module that this type is an unsigned int.
1483 Every other consumer of this type can ignore this metadata and treat
1484 this type as int.
1485
1486 The first argument to Annotated must be a valid type.
1487
1488 Details:
1489
1490 - It's an error to call `Annotated` with less than two arguments.
1491 - Nested Annotated are flattened::
1492
1493 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1494
1495 - Instantiating an annotated type is equivalent to instantiating the
1496 underlying type::
1497
1498 Annotated[C, Ann1](5) == C(5)
1499
1500 - Annotated can be used as a generic type alias::
1501
1502 Optimized = Annotated[T, runtime.Optimize()]
1503 Optimized[int] == Annotated[int, runtime.Optimize()]
1504
1505 OptimizedList = Annotated[List[T], runtime.Optimize()]
1506 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1507 """
1508
1509 __slots__ = ()
1510
1511 def __new__(cls, *args, **kwargs):
1512 raise TypeError("Type Annotated cannot be instantiated.")
1513
1514 @_tp_cache
1515 def __class_getitem__(cls, params):
1516 if not isinstance(params, tuple) or len(params) < 2:
1517 raise TypeError("Annotated[...] should be used "
1518 "with at least two arguments (a type and an "
1519 "annotation).")
1520 msg = "Annotated[t, ...]: t must be a type."
1521 origin = _type_check(params[0], msg)
1522 metadata = tuple(params[1:])
1523 return _AnnotatedAlias(origin, metadata)
1524
1525 def __init_subclass__(cls, *args, **kwargs):
1526 raise TypeError(
1527 "Cannot subclass {}.Annotated".format(cls.__module__)
1528 )
1529
1530
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001531def runtime_checkable(cls):
1532 """Mark a protocol class as a runtime protocol.
1533
1534 Such protocol can be used with isinstance() and issubclass().
1535 Raise TypeError if applied to a non-protocol class.
1536 This allows a simple-minded structural check very similar to
1537 one trick ponies in collections.abc such as Iterable.
1538 For example::
1539
1540 @runtime_checkable
1541 class Closable(Protocol):
1542 def close(self): ...
1543
1544 assert isinstance(open('/some/file'), Closable)
1545
1546 Warning: this will check only the presence of the required methods,
1547 not their type signatures!
1548 """
1549 if not issubclass(cls, Generic) or not cls._is_protocol:
1550 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1551 ' got %r' % cls)
1552 cls._is_runtime_protocol = True
1553 return cls
1554
1555
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001556def cast(typ, val):
1557 """Cast a value to a type.
1558
1559 This returns the value unchanged. To the type checker this
1560 signals that the return value has the designated type, but at
1561 runtime we intentionally don't check anything (we want this
1562 to be as fast as possible).
1563 """
1564 return val
1565
1566
1567def _get_defaults(func):
1568 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001569 try:
1570 code = func.__code__
1571 except AttributeError:
1572 # Some built-in functions don't have __code__, __defaults__, etc.
1573 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001574 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001575 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001576 arg_names = arg_names[:pos_count]
1577 defaults = func.__defaults__ or ()
1578 kwdefaults = func.__kwdefaults__
1579 res = dict(kwdefaults) if kwdefaults else {}
1580 pos_offset = pos_count - len(defaults)
1581 for name, value in zip(arg_names[pos_offset:], defaults):
1582 assert name not in res
1583 res[name] = value
1584 return res
1585
1586
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001587_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1588 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001589 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001590
1591
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001592def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001593 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001594
Guido van Rossum991d14f2016-11-09 13:12:51 -08001595 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001596 forward references encoded as string literals, adds Optional[t] if a
1597 default value equal to None is set and recursively replaces all
1598 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001599
Guido van Rossum991d14f2016-11-09 13:12:51 -08001600 The argument may be a module, class, method, or function. The annotations
1601 are returned as a dictionary. For classes, annotations include also
1602 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001603
Guido van Rossum991d14f2016-11-09 13:12:51 -08001604 TypeError is raised if the argument is not of a type that can contain
1605 annotations, and an empty dictionary is returned if no annotations are
1606 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001607
Guido van Rossum991d14f2016-11-09 13:12:51 -08001608 BEWARE -- the behavior of globalns and localns is counterintuitive
1609 (unless you are familiar with how eval() and exec() work). The
1610 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001611
Guido van Rossum991d14f2016-11-09 13:12:51 -08001612 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001613 globals from obj (or the respective module's globals for classes),
1614 and these are also used as the locals. If the object does not appear
1615 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001616
Guido van Rossum991d14f2016-11-09 13:12:51 -08001617 - If one dict argument is passed, it is used for both globals and
1618 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001619
Guido van Rossum991d14f2016-11-09 13:12:51 -08001620 - If two dict arguments are passed, they specify globals and
1621 locals, respectively.
1622 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001623
Guido van Rossum991d14f2016-11-09 13:12:51 -08001624 if getattr(obj, '__no_type_check__', None):
1625 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001626 # Classes require a special treatment.
1627 if isinstance(obj, type):
1628 hints = {}
1629 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001630 if globalns is None:
Karthikeyan Singaravelana9cf69d2021-04-12 23:47:25 +05301631 try:
1632 base_globals = sys.modules[base.__module__].__dict__
1633 except KeyError:
1634 continue
Łukasz Langaf350a262017-09-14 14:33:00 -04001635 else:
1636 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001637 ann = base.__dict__.get('__annotations__', {})
Ken Jin852150d2021-04-13 01:23:12 +08001638 base_locals = dict(vars(base)) if localns is None else localns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001639 for name, value in ann.items():
1640 if value is None:
1641 value = type(None)
1642 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001643 value = ForwardRef(value, is_argument=False)
Ken Jin852150d2021-04-13 01:23:12 +08001644 value = _eval_type(value, base_globals, base_locals)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001645 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001646 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001647
1648 if globalns is None:
1649 if isinstance(obj, types.ModuleType):
1650 globalns = obj.__dict__
1651 else:
benedwards140aca3a32019-11-21 17:24:58 +00001652 nsobj = obj
1653 # Find globalns for the unwrapped object.
1654 while hasattr(nsobj, '__wrapped__'):
1655 nsobj = nsobj.__wrapped__
1656 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001657 if localns is None:
1658 localns = globalns
1659 elif localns is None:
1660 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001661 hints = getattr(obj, '__annotations__', None)
1662 if hints is None:
1663 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001664 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001665 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001666 else:
1667 raise TypeError('{!r} is not a module, class, method, '
1668 'or function.'.format(obj))
1669 defaults = _get_defaults(obj)
1670 hints = dict(hints)
1671 for name, value in hints.items():
1672 if value is None:
1673 value = type(None)
1674 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001675 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001676 value = _eval_type(value, globalns, localns)
1677 if name in defaults and defaults[name] is None:
1678 value = Optional[value]
1679 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001680 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1681
1682
1683def _strip_annotations(t):
1684 """Strips the annotations from a given type.
1685 """
1686 if isinstance(t, _AnnotatedAlias):
1687 return _strip_annotations(t.__origin__)
1688 if isinstance(t, _GenericAlias):
1689 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1690 if stripped_args == t.__args__:
1691 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001692 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001693 if isinstance(t, GenericAlias):
1694 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1695 if stripped_args == t.__args__:
1696 return t
1697 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001698 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001699
1700
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001701def get_origin(tp):
1702 """Get the unsubscripted version of a type.
1703
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001704 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1705 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001706
1707 get_origin(Literal[42]) is Literal
1708 get_origin(int) is None
1709 get_origin(ClassVar[int]) is ClassVar
1710 get_origin(Generic) is Generic
1711 get_origin(Generic[T]) is Generic
1712 get_origin(Union[T, int]) is Union
1713 get_origin(List[Tuple[T, T]][int]) == list
Jelle Zijlstra52243362021-04-10 19:57:05 -07001714 get_origin(P.args) is P
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001715 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001716 if isinstance(tp, _AnnotatedAlias):
1717 return Annotated
Jelle Zijlstra52243362021-04-10 19:57:05 -07001718 if isinstance(tp, (_BaseGenericAlias, GenericAlias,
1719 ParamSpecArgs, ParamSpecKwargs)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001720 return tp.__origin__
1721 if tp is Generic:
1722 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001723 if isinstance(tp, types.Union):
1724 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001725 return None
1726
1727
1728def get_args(tp):
1729 """Get type arguments with all substitutions performed.
1730
1731 For unions, basic simplifications used by Union constructor are performed.
1732 Examples::
1733 get_args(Dict[str, int]) == (str, int)
1734 get_args(int) == ()
1735 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1736 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1737 get_args(Callable[[], T][int]) == ([], int)
1738 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001739 if isinstance(tp, _AnnotatedAlias):
1740 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001741 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001742 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001743 if (tp.__origin__ is collections.abc.Callable
1744 and not (res[0] is Ellipsis
1745 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001746 res = (list(res[:-1]), res[-1])
1747 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001748 if isinstance(tp, types.Union):
1749 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001750 return ()
1751
1752
Patrick Reader0705ec82020-09-16 05:58:32 +01001753def is_typeddict(tp):
1754 """Check if an annotation is a TypedDict class
1755
1756 For example::
1757 class Film(TypedDict):
1758 title: str
1759 year: int
1760
1761 is_typeddict(Film) # => True
1762 is_typeddict(Union[list, str]) # => False
1763 """
1764 return isinstance(tp, _TypedDictMeta)
1765
1766
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001767def no_type_check(arg):
1768 """Decorator to indicate that annotations are not type hints.
1769
1770 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001771 applies recursively to all methods and classes defined in that class
1772 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001773
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001774 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001775 """
1776 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001777 arg_attrs = arg.__dict__.copy()
1778 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001779 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001780 arg_attrs.pop(attr)
1781 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001782 if isinstance(obj, types.FunctionType):
1783 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001784 if isinstance(obj, type):
1785 no_type_check(obj)
1786 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001787 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001788 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001789 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001790 return arg
1791
1792
1793def no_type_check_decorator(decorator):
1794 """Decorator to give another decorator the @no_type_check effect.
1795
1796 This wraps the decorator with something that wraps the decorated
1797 function in @no_type_check.
1798 """
1799
1800 @functools.wraps(decorator)
1801 def wrapped_decorator(*args, **kwds):
1802 func = decorator(*args, **kwds)
1803 func = no_type_check(func)
1804 return func
1805
1806 return wrapped_decorator
1807
1808
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001809def _overload_dummy(*args, **kwds):
1810 """Helper for @overload to raise when called."""
1811 raise NotImplementedError(
1812 "You should not call an overloaded function. "
1813 "A series of @overload-decorated functions "
1814 "outside a stub module should always be followed "
1815 "by an implementation that is not @overload-ed.")
1816
1817
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001818def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001819 """Decorator for overloaded functions/methods.
1820
1821 In a stub file, place two or more stub definitions for the same
1822 function in a row, each decorated with @overload. For example:
1823
1824 @overload
1825 def utf8(value: None) -> None: ...
1826 @overload
1827 def utf8(value: bytes) -> bytes: ...
1828 @overload
1829 def utf8(value: str) -> bytes: ...
1830
1831 In a non-stub file (i.e. a regular .py file), do the same but
1832 follow it with an implementation. The implementation should *not*
1833 be decorated with @overload. For example:
1834
1835 @overload
1836 def utf8(value: None) -> None: ...
1837 @overload
1838 def utf8(value: bytes) -> bytes: ...
1839 @overload
1840 def utf8(value: str) -> bytes: ...
1841 def utf8(value):
1842 # implementation goes here
1843 """
1844 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001845
1846
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001847def final(f):
1848 """A decorator to indicate final methods and final classes.
1849
1850 Use this decorator to indicate to type checkers that the decorated
1851 method cannot be overridden, and decorated class cannot be subclassed.
1852 For example:
1853
1854 class Base:
1855 @final
1856 def done(self) -> None:
1857 ...
1858 class Sub(Base):
1859 def done(self) -> None: # Error reported by type checker
1860 ...
1861
1862 @final
1863 class Leaf:
1864 ...
1865 class Other(Leaf): # Error reported by type checker
1866 ...
1867
1868 There is no runtime checking of these properties.
1869 """
1870 return f
1871
1872
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001873# Some unconstrained type variables. These are used by the container types.
1874# (These are not for export.)
1875T = TypeVar('T') # Any type.
1876KT = TypeVar('KT') # Key type.
1877VT = TypeVar('VT') # Value type.
1878T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1879V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1880VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1881T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1882# Internal type variable used for Type[].
1883CT_co = TypeVar('CT_co', covariant=True, bound=type)
1884
1885# A useful type variable with constraints. This represents string types.
1886# (This one *is* for export!)
1887AnyStr = TypeVar('AnyStr', bytes, str)
1888
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001889
1890# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001891_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001892
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001893Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1894Awaitable = _alias(collections.abc.Awaitable, 1)
1895Coroutine = _alias(collections.abc.Coroutine, 3)
1896AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1897AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1898Iterable = _alias(collections.abc.Iterable, 1)
1899Iterator = _alias(collections.abc.Iterator, 1)
1900Reversible = _alias(collections.abc.Reversible, 1)
1901Sized = _alias(collections.abc.Sized, 0) # Not generic.
1902Container = _alias(collections.abc.Container, 1)
1903Collection = _alias(collections.abc.Collection, 1)
1904Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001905Callable.__doc__ = \
1906 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001907
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001908 The subscription syntax must always be used with exactly two
1909 values: the argument list and the return type. The argument list
1910 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001911
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001912 There is no syntax to indicate optional or keyword arguments,
1913 such function types are rarely used as callback types.
1914 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001915AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1916MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001917# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001918Mapping = _alias(collections.abc.Mapping, 2)
1919MutableMapping = _alias(collections.abc.MutableMapping, 2)
1920Sequence = _alias(collections.abc.Sequence, 1)
1921MutableSequence = _alias(collections.abc.MutableSequence, 1)
1922ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1923# Tuple accepts variable number of parameters.
1924Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001925Tuple.__doc__ = \
1926 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001927
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001928 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1929 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1930 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001931
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001932 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1933 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001934List = _alias(list, 1, inst=False, name='List')
1935Deque = _alias(collections.deque, 1, name='Deque')
1936Set = _alias(set, 1, inst=False, name='Set')
1937FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1938MappingView = _alias(collections.abc.MappingView, 1)
1939KeysView = _alias(collections.abc.KeysView, 1)
1940ItemsView = _alias(collections.abc.ItemsView, 2)
1941ValuesView = _alias(collections.abc.ValuesView, 1)
1942ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1943AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1944Dict = _alias(dict, 2, inst=False, name='Dict')
1945DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1946OrderedDict = _alias(collections.OrderedDict, 2)
1947Counter = _alias(collections.Counter, 1)
1948ChainMap = _alias(collections.ChainMap, 2)
1949Generator = _alias(collections.abc.Generator, 3)
1950AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1951Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001952Type.__doc__ = \
1953 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001954
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001955 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001956
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001957 class User: ... # Abstract base for User classes
1958 class BasicUser(User): ...
1959 class ProUser(User): ...
1960 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001961
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001962 And a function that takes a class argument that's a subclass of
1963 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001964
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001965 U = TypeVar('U', bound=User)
1966 def new_user(user_class: Type[U]) -> U:
1967 user = user_class()
1968 # (Here we could write the user object to a database)
1969 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001970
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001971 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001972
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001973 At this point the type checker knows that joe has type BasicUser.
1974 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001975
1976
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001977@runtime_checkable
1978class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001979 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001980 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001981
1982 @abstractmethod
1983 def __int__(self) -> int:
1984 pass
1985
1986
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001987@runtime_checkable
1988class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001989 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001990 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001991
1992 @abstractmethod
1993 def __float__(self) -> float:
1994 pass
1995
1996
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001997@runtime_checkable
1998class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001999 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002000 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002001
2002 @abstractmethod
2003 def __complex__(self) -> complex:
2004 pass
2005
2006
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002007@runtime_checkable
2008class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002009 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002010 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002011
2012 @abstractmethod
2013 def __bytes__(self) -> bytes:
2014 pass
2015
2016
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002017@runtime_checkable
2018class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002019 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07002020 __slots__ = ()
2021
2022 @abstractmethod
2023 def __index__(self) -> int:
2024 pass
2025
2026
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002027@runtime_checkable
2028class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002029 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002030 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002031
2032 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002033 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002034 pass
2035
2036
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01002037@runtime_checkable
2038class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03002039 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02002040 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002041
2042 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02002043 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002044 pass
2045
2046
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002047def _make_nmtuple(name, types, module, defaults = ()):
2048 fields = [n for n, t in types]
2049 types = {n: _type_check(t, f"field {n} annotation must be a type")
2050 for n, t in types}
2051 nm_tpl = collections.namedtuple(name, fields,
2052 defaults=defaults, module=module)
2053 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002054 return nm_tpl
2055
2056
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002057# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002058_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2059 '_fields', '_field_defaults',
2060 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002061
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002062_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002063
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002064
Guido van Rossum2f841442016-11-15 09:48:06 -08002065class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002066
Guido van Rossum2f841442016-11-15 09:48:06 -08002067 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002068 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002069 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002070 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002071 for field_name in types:
2072 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002073 default_names.append(field_name)
2074 elif default_names:
2075 raise TypeError(f"Non-default namedtuple field {field_name} "
2076 f"cannot follow default field"
2077 f"{'s' if len(default_names) > 1 else ''} "
2078 f"{', '.join(default_names)}")
2079 nm_tpl = _make_nmtuple(typename, types.items(),
2080 defaults=[ns[n] for n in default_names],
2081 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002082 # update from user namespace without overriding special namedtuple attributes
2083 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002084 if key in _prohibited:
2085 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2086 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002087 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002088 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002089
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002090
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002091def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002092 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002093
Guido van Rossum2f841442016-11-15 09:48:06 -08002094 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002095
Guido van Rossum2f841442016-11-15 09:48:06 -08002096 class Employee(NamedTuple):
2097 name: str
2098 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002099
Guido van Rossum2f841442016-11-15 09:48:06 -08002100 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002101
Guido van Rossum2f841442016-11-15 09:48:06 -08002102 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002103
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002104 The resulting class has an extra __annotations__ attribute, giving a
2105 dict that maps field names to types. (The field names are also in
2106 the _fields attribute, which is part of the namedtuple API.)
2107 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002108
Guido van Rossum2f841442016-11-15 09:48:06 -08002109 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002110
Guido van Rossum2f841442016-11-15 09:48:06 -08002111 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002112
Guido van Rossum2f841442016-11-15 09:48:06 -08002113 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2114 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002115 if fields is None:
2116 fields = kwargs.items()
2117 elif kwargs:
2118 raise TypeError("Either list of fields or keywords"
2119 " can be provided to NamedTuple, not both")
2120 try:
2121 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2122 except (AttributeError, ValueError):
2123 module = None
2124 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002125
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002126_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2127
2128def _namedtuple_mro_entries(bases):
2129 if len(bases) > 1:
2130 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2131 assert bases[0] is NamedTuple
2132 return (_NamedTuple,)
2133
2134NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002135
2136
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002137class _TypedDictMeta(type):
2138 def __new__(cls, name, bases, ns, total=True):
2139 """Create new typed dict class object.
2140
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002141 This method is called when TypedDict is subclassed,
2142 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002143 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002144 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002145 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002146 for base in bases:
2147 if type(base) is not _TypedDictMeta:
2148 raise TypeError('cannot inherit from both a TypedDict type '
2149 'and a non-TypedDict base class')
2150 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002151
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002152 annotations = {}
2153 own_annotations = ns.get('__annotations__', {})
2154 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002155 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002156 own_annotations = {
2157 n: _type_check(tp, msg) for n, tp in own_annotations.items()
2158 }
2159 required_keys = set()
2160 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002161
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002162 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002163 annotations.update(base.__dict__.get('__annotations__', {}))
2164 required_keys.update(base.__dict__.get('__required_keys__', ()))
2165 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002166
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002167 annotations.update(own_annotations)
2168 if total:
2169 required_keys.update(own_annotation_keys)
2170 else:
2171 optional_keys.update(own_annotation_keys)
2172
2173 tp_dict.__annotations__ = annotations
2174 tp_dict.__required_keys__ = frozenset(required_keys)
2175 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002176 if not hasattr(tp_dict, '__total__'):
2177 tp_dict.__total__ = total
2178 return tp_dict
2179
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002180 __call__ = dict # static method
2181
2182 def __subclasscheck__(cls, other):
2183 # Typed dicts are only for static structural subtyping.
2184 raise TypeError('TypedDict does not support instance and class checks')
2185
2186 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002187
2188
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002189def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002190 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2191
2192 TypedDict creates a dictionary type that expects all of its
2193 instances to have a certain set of keys, where each key is
2194 associated with a value of a consistent type. This expectation
2195 is not checked at runtime but is only enforced by type checkers.
2196 Usage::
2197
2198 class Point2D(TypedDict):
2199 x: int
2200 y: int
2201 label: str
2202
2203 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2204 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2205
2206 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2207
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002208 The type info can be accessed via the Point2D.__annotations__ dict, and
2209 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2210 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002211
2212 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2213 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2214
ananthan-123ab6423f2020-02-19 10:03:05 +05302215 By default, all keys must be present in a TypedDict. It is possible
2216 to override this by specifying totality.
2217 Usage::
2218
2219 class point2D(TypedDict, total=False):
2220 x: int
2221 y: int
2222
2223 This means that a point2D TypedDict can have any of the keys omitted.A type
2224 checker is only expected to support a literal False or True as the value of
2225 the total argument. True is the default, and makes all items defined in the
2226 class body be required.
2227
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002228 The class syntax is only supported in Python 3.6+, while two other
2229 syntax forms work for Python 2.7 and 3.2+
2230 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002231 if fields is None:
2232 fields = kwargs
2233 elif kwargs:
2234 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2235 " but not both")
2236
Alex Grönholm67b769f2020-12-10 23:49:05 +02002237 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002238 try:
2239 # Setting correct module is necessary to make typed dict classes pickleable.
2240 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2241 except (AttributeError, ValueError):
2242 pass
2243
Alex Grönholm67b769f2020-12-10 23:49:05 +02002244 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002245
2246_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2247TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002248
2249
Guido van Rossum91185fe2016-06-08 11:19:11 -07002250def NewType(name, tp):
2251 """NewType creates simple unique types with almost zero
2252 runtime overhead. NewType(name, tp) is considered a subtype of tp
2253 by static type checkers. At runtime, NewType(name, tp) returns
2254 a dummy function that simply returns its argument. Usage::
2255
2256 UserId = NewType('UserId', int)
2257
2258 def name_by_id(user_id: UserId) -> str:
2259 ...
2260
2261 UserId('user') # Fails type check
2262
2263 name_by_id(42) # Fails type check
2264 name_by_id(UserId(42)) # OK
2265
2266 num = UserId(5) + 1 # type: int
2267 """
2268
2269 def new_type(x):
2270 return x
2271
2272 new_type.__name__ = name
2273 new_type.__supertype__ = tp
2274 return new_type
2275
2276
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002277# Python-version-specific alias (Python 2: unicode; Python 3: str)
2278Text = str
2279
2280
Guido van Rossum91185fe2016-06-08 11:19:11 -07002281# Constant that's True when type checking, but False here.
2282TYPE_CHECKING = False
2283
2284
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002285class IO(Generic[AnyStr]):
2286 """Generic base class for TextIO and BinaryIO.
2287
2288 This is an abstract, generic version of the return of open().
2289
2290 NOTE: This does not distinguish between the different possible
2291 classes (text vs. binary, read vs. write vs. read/write,
2292 append-only, unbuffered). The TextIO and BinaryIO subclasses
2293 below capture the distinctions between text vs. binary, which is
2294 pervasive in the interface; however we currently do not offer a
2295 way to track the other distinctions in the type system.
2296 """
2297
Guido van Rossumd70fe632015-08-05 12:11:06 +02002298 __slots__ = ()
2299
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002300 @property
2301 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002302 def mode(self) -> str:
2303 pass
2304
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002305 @property
2306 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002307 def name(self) -> str:
2308 pass
2309
2310 @abstractmethod
2311 def close(self) -> None:
2312 pass
2313
Shantanu2e6569b2020-01-29 18:52:36 -08002314 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002315 @abstractmethod
2316 def closed(self) -> bool:
2317 pass
2318
2319 @abstractmethod
2320 def fileno(self) -> int:
2321 pass
2322
2323 @abstractmethod
2324 def flush(self) -> None:
2325 pass
2326
2327 @abstractmethod
2328 def isatty(self) -> bool:
2329 pass
2330
2331 @abstractmethod
2332 def read(self, n: int = -1) -> AnyStr:
2333 pass
2334
2335 @abstractmethod
2336 def readable(self) -> bool:
2337 pass
2338
2339 @abstractmethod
2340 def readline(self, limit: int = -1) -> AnyStr:
2341 pass
2342
2343 @abstractmethod
2344 def readlines(self, hint: int = -1) -> List[AnyStr]:
2345 pass
2346
2347 @abstractmethod
2348 def seek(self, offset: int, whence: int = 0) -> int:
2349 pass
2350
2351 @abstractmethod
2352 def seekable(self) -> bool:
2353 pass
2354
2355 @abstractmethod
2356 def tell(self) -> int:
2357 pass
2358
2359 @abstractmethod
2360 def truncate(self, size: int = None) -> int:
2361 pass
2362
2363 @abstractmethod
2364 def writable(self) -> bool:
2365 pass
2366
2367 @abstractmethod
2368 def write(self, s: AnyStr) -> int:
2369 pass
2370
2371 @abstractmethod
2372 def writelines(self, lines: List[AnyStr]) -> None:
2373 pass
2374
2375 @abstractmethod
2376 def __enter__(self) -> 'IO[AnyStr]':
2377 pass
2378
2379 @abstractmethod
2380 def __exit__(self, type, value, traceback) -> None:
2381 pass
2382
2383
2384class BinaryIO(IO[bytes]):
2385 """Typed version of the return of open() in binary mode."""
2386
Guido van Rossumd70fe632015-08-05 12:11:06 +02002387 __slots__ = ()
2388
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002389 @abstractmethod
2390 def write(self, s: Union[bytes, bytearray]) -> int:
2391 pass
2392
2393 @abstractmethod
2394 def __enter__(self) -> 'BinaryIO':
2395 pass
2396
2397
2398class TextIO(IO[str]):
2399 """Typed version of the return of open() in text mode."""
2400
Guido van Rossumd70fe632015-08-05 12:11:06 +02002401 __slots__ = ()
2402
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002403 @property
2404 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002405 def buffer(self) -> BinaryIO:
2406 pass
2407
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002408 @property
2409 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002410 def encoding(self) -> str:
2411 pass
2412
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002413 @property
2414 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002415 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002416 pass
2417
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002418 @property
2419 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002420 def line_buffering(self) -> bool:
2421 pass
2422
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002423 @property
2424 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002425 def newlines(self) -> Any:
2426 pass
2427
2428 @abstractmethod
2429 def __enter__(self) -> 'TextIO':
2430 pass
2431
2432
2433class io:
2434 """Wrapper namespace for IO generic classes."""
2435
2436 __all__ = ['IO', 'TextIO', 'BinaryIO']
2437 IO = IO
2438 TextIO = TextIO
2439 BinaryIO = BinaryIO
2440
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002441
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002442io.__name__ = __name__ + '.io'
2443sys.modules[io.__name__] = io
2444
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002445Pattern = _alias(stdlib_re.Pattern, 1)
2446Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002447
2448class re:
2449 """Wrapper namespace for re type aliases."""
2450
2451 __all__ = ['Pattern', 'Match']
2452 Pattern = Pattern
2453 Match = Match
2454
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002455
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002456re.__name__ = __name__ + '.re'
2457sys.modules[re.__name__] = re