blob: 6224930c3b0275fc5cfdc4e73fd32283f8a272b6 [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',
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100117 'runtime_checkable',
Guido van Rossum0e0563c2016-04-05 14:54:25 -0700118 'Text',
Guido van Rossum91185fe2016-06-08 11:19:11 -0700119 'TYPE_CHECKING',
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300120 'TypeAlias',
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700121]
122
Guido van Rossumbd5b9a02016-04-05 08:28:52 -0700123# The pseudo-submodules 're' and 'io' are part of the public
124# namespace, but excluded from __all__ because they might stomp on
125# legitimate imports of those modules.
126
kj463c7d32020-12-14 02:38:24 +0800127
128def _type_convert(arg):
129 """For converting None to type(None), and strings to ForwardRef."""
130 if arg is None:
131 return type(None)
132 if isinstance(arg, str):
133 return ForwardRef(arg)
134 return arg
135
136
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700137def _type_check(arg, msg, is_argument=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000138 """Check that the argument is a type, and return it (internal helper).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700139
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000140 As a special case, accept None and return type(None) instead. Also wrap strings
141 into ForwardRef instances. Consider several corner cases, for example plain
142 special forms like Union are not valid, while Union[int, str] is OK, etc.
143 The msg argument is a human-readable error message, e.g::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700144
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000145 "Union[arg, ...]: arg should be a type."
Guido van Rossum4cefe742016-09-27 15:20:12 -0700146
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000147 We append the repr() of the actual value (truncated to 100 chars).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700148 """
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100149 invalid_generic_forms = (Generic, Protocol)
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700150 if is_argument:
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100151 invalid_generic_forms = invalid_generic_forms + (ClassVar, Final)
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400152
kj463c7d32020-12-14 02:38:24 +0800153 arg = _type_convert(arg)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000154 if (isinstance(arg, _GenericAlias) and
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400155 arg.__origin__ in invalid_generic_forms):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000156 raise TypeError(f"{arg} is not valid as type argument")
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300157 if arg in (Any, NoReturn):
158 return arg
159 if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000160 raise TypeError(f"Plain {arg} is not valid as type argument")
kj73607be2020-12-24 12:33:48 +0800161 if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000162 return arg
163 if not callable(arg):
164 raise TypeError(f"{msg} Got {arg!r:.100}.")
165 return arg
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700166
167
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000168def _type_repr(obj):
169 """Return the repr() of an object, special-casing types (internal helper).
170
171 If obj is a type, we return a shorter version than the default
172 type.__repr__, based on the module and qualified name, which is
173 typically enough to uniquely identify a type. For everything
174 else, we fall back on repr(obj).
175 """
kj1f7dfb22020-11-02 02:13:38 +0800176 if isinstance(obj, types.GenericAlias):
177 return repr(obj)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000178 if isinstance(obj, type):
179 if obj.__module__ == 'builtins':
180 return obj.__qualname__
181 return f'{obj.__module__}.{obj.__qualname__}'
182 if obj is ...:
183 return('...')
184 if isinstance(obj, types.FunctionType):
185 return obj.__name__
186 return repr(obj)
187
188
189def _collect_type_vars(types):
kj73607be2020-12-24 12:33:48 +0800190 """Collect all type variable-like variables contained
191 in types in order of first appearance (lexicographic order). For example::
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000192
193 _collect_type_vars((T, List[S, T])) == (T, S)
194 """
195 tvars = []
196 for t in types:
kj73607be2020-12-24 12:33:48 +0800197 if isinstance(t, _TypeVarLike) and t not in tvars:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000198 tvars.append(t)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300199 if isinstance(t, (_GenericAlias, GenericAlias)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000200 tvars.extend([t for t in t.__parameters__ if t not in tvars])
201 return tuple(tvars)
202
203
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300204def _check_generic(cls, parameters, elen):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000205 """Check correct count for parameters of a generic cls (internal helper).
206 This gives a nice error message in case of count mismatch.
207 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300208 if not elen:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000209 raise TypeError(f"{cls} is not a generic class")
210 alen = len(parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000211 if alen != elen:
212 raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
213 f" actual {alen}, expected {elen}")
214
kj73607be2020-12-24 12:33:48 +0800215def _prepare_paramspec_params(cls, params):
216 """Prepares the parameters for a Generic containing ParamSpec
217 variables (internal helper).
218 """
219 # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
220 if len(cls.__parameters__) == 1 and len(params) > 1:
221 return (params,)
222 else:
223 _params = []
224 # Convert lists to tuples to help other libraries cache the results.
225 for p, tvar in zip(params, cls.__parameters__):
226 if isinstance(tvar, ParamSpec) and isinstance(p, list):
227 p = tuple(p)
228 _params.append(p)
229 return tuple(_params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000230
Yurii Karabasf03d3182020-11-17 04:23:19 +0200231def _deduplicate(params):
232 # Weed out strict duplicates, preserving the first of each occurrence.
233 all_params = set(params)
234 if len(all_params) < len(params):
235 new_params = []
236 for t in params:
237 if t in all_params:
238 new_params.append(t)
239 all_params.remove(t)
240 params = new_params
241 assert not all_params, all_params
242 return params
243
244
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000245def _remove_dups_flatten(parameters):
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700246 """An internal helper for Union creation and substitution: flatten Unions
247 among parameters, then remove duplicates.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000248 """
249 # Flatten out Union[Union[...], ...].
250 params = []
251 for p in parameters:
Maggie Moss1b4552c2020-09-09 13:23:24 -0700252 if isinstance(p, (_UnionGenericAlias, types.Union)):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000253 params.extend(p.__args__)
254 elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
255 params.extend(p[1:])
256 else:
257 params.append(p)
Yurii Karabasf03d3182020-11-17 04:23:19 +0200258
259 return tuple(_deduplicate(params))
260
261
262def _flatten_literal_params(parameters):
263 """An internal helper for Literal creation: flatten Literals among parameters"""
264 params = []
265 for p in parameters:
266 if isinstance(p, _LiteralGenericAlias):
267 params.extend(p.__args__)
268 else:
269 params.append(p)
Ivan Levkivskyif65e31f2018-05-18 16:00:38 -0700270 return tuple(params)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000271
272
273_cleanups = []
274
275
Yurii Karabasf03d3182020-11-17 04:23:19 +0200276def _tp_cache(func=None, /, *, typed=False):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000277 """Internal wrapper caching __getitem__ of generic types with a fallback to
278 original function for non-hashable arguments.
279 """
Yurii Karabasf03d3182020-11-17 04:23:19 +0200280 def decorator(func):
281 cached = functools.lru_cache(typed=typed)(func)
282 _cleanups.append(cached.cache_clear)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000283
Yurii Karabasf03d3182020-11-17 04:23:19 +0200284 @functools.wraps(func)
285 def inner(*args, **kwds):
286 try:
287 return cached(*args, **kwds)
288 except TypeError:
289 pass # All real errors (not unhashable args) are raised below.
290 return func(*args, **kwds)
291 return inner
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000292
Yurii Karabasf03d3182020-11-17 04:23:19 +0200293 if func is not None:
294 return decorator(func)
295
296 return decorator
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000297
wyfo653f4202020-07-22 21:47:28 +0200298def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
Graham Bleaney84ef33c2020-09-08 18:41:10 -0400299 """Evaluate all forward references in the given type t.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000300 For use of globalns and localns see the docstring for get_type_hints().
wyfo653f4202020-07-22 21:47:28 +0200301 recursive_guard is used to prevent prevent infinite recursion
302 with recursive ForwardRef.
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000303 """
304 if isinstance(t, ForwardRef):
wyfo653f4202020-07-22 21:47:28 +0200305 return t._evaluate(globalns, localns, recursive_guard)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300306 if isinstance(t, (_GenericAlias, GenericAlias)):
wyfo653f4202020-07-22 21:47:28 +0200307 ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000308 if ev_args == t.__args__:
309 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300310 if isinstance(t, GenericAlias):
311 return GenericAlias(t.__origin__, ev_args)
312 else:
313 return t.copy_with(ev_args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000314 return t
315
316
317class _Final:
318 """Mixin to prohibit subclassing"""
Guido van Rossum4cefe742016-09-27 15:20:12 -0700319
Guido van Rossum83ec3022017-01-17 20:43:28 -0800320 __slots__ = ('__weakref__',)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700321
Serhiy Storchaka2085bd02019-06-01 11:00:15 +0300322 def __init_subclass__(self, /, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000323 if '_root' not in kwds:
324 raise TypeError("Cannot subclass special typing classes")
325
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100326class _Immutable:
327 """Mixin to indicate that object should not be copied."""
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300328 __slots__ = ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000329
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100330 def __copy__(self):
331 return self
332
333 def __deepcopy__(self, memo):
334 return self
335
336
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300337# Internal indicator of special typing constructs.
338# See __doc__ instance attribute for specific docs.
339class _SpecialForm(_Final, _root=True):
340 __slots__ = ('_name', '__doc__', '_getitem')
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000341
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300342 def __init__(self, getitem):
343 self._getitem = getitem
344 self._name = getitem.__name__
345 self.__doc__ = getitem.__doc__
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000346
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300347 def __mro_entries__(self, bases):
348 raise TypeError(f"Cannot subclass {self!r}")
Guido van Rossum4cefe742016-09-27 15:20:12 -0700349
350 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000351 return 'typing.' + self._name
352
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100353 def __reduce__(self):
354 return self._name
Guido van Rossum4cefe742016-09-27 15:20:12 -0700355
356 def __call__(self, *args, **kwds):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000357 raise TypeError(f"Cannot instantiate {self!r}")
358
359 def __instancecheck__(self, obj):
360 raise TypeError(f"{self} cannot be used with isinstance()")
361
362 def __subclasscheck__(self, cls):
363 raise TypeError(f"{self} cannot be used with issubclass()")
364
365 @_tp_cache
366 def __getitem__(self, parameters):
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300367 return self._getitem(self, parameters)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700368
Yurii Karabasf03d3182020-11-17 04:23:19 +0200369
370class _LiteralSpecialForm(_SpecialForm, _root=True):
371 @_tp_cache(typed=True)
372 def __getitem__(self, parameters):
373 return self._getitem(self, parameters)
374
375
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300376@_SpecialForm
377def Any(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000378 """Special type indicating an unconstrained type.
Guido van Rossumb47c9d22016-10-03 08:40:50 -0700379
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000380 - Any is compatible with every type.
381 - Any assumed to have all methods.
382 - All values assumed to be instances of Any.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700383
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000384 Note that all the above statements are true from the point of view of
385 static type checkers. At runtime, Any should not be used with instance
386 or class checks.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300387 """
388 raise TypeError(f"{self} is not subscriptable")
Guido van Rossumd70fe632015-08-05 12:11:06 +0200389
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300390@_SpecialForm
391def NoReturn(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000392 """Special type indicating functions that never return.
393 Example::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700394
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000395 from typing import NoReturn
396
397 def stop() -> NoReturn:
398 raise Exception('no way')
399
400 This type is invalid in other positions, e.g., ``List[NoReturn]``
401 will fail in static type checkers.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300402 """
403 raise TypeError(f"{self} is not subscriptable")
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000404
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300405@_SpecialForm
406def ClassVar(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000407 """Special type construct to mark class variables.
408
409 An annotation wrapped in ClassVar indicates that a given
410 attribute is intended to be used as a class variable and
411 should not be set on instances of that class. Usage::
412
413 class Starship:
414 stats: ClassVar[Dict[str, int]] = {} # class variable
415 damage: int = 10 # instance variable
416
417 ClassVar accepts only types and cannot be further subscribed.
418
419 Note that ClassVar is not a class itself, and should not
420 be used with isinstance() or issubclass().
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300421 """
422 item = _type_check(parameters, f'{self} accepts only single type.')
423 return _GenericAlias(self, (item,))
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000424
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300425@_SpecialForm
426def Final(self, parameters):
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100427 """Special typing construct to indicate final names to type checkers.
428
429 A final name cannot be re-assigned or overridden in a subclass.
430 For example:
431
432 MAX_SIZE: Final = 9000
433 MAX_SIZE += 1 # Error reported by type checker
434
435 class Connection:
436 TIMEOUT: Final[int] = 10
437
438 class FastConnector(Connection):
439 TIMEOUT = 1 # Error reported by type checker
440
441 There is no runtime checking of these properties.
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300442 """
443 item = _type_check(parameters, f'{self} accepts only single type.')
444 return _GenericAlias(self, (item,))
Ivan Levkivskyif3672422019-05-26 09:37:07 +0100445
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300446@_SpecialForm
447def Union(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000448 """Union type; Union[X, Y] means either X or Y.
449
450 To define a union, use e.g. Union[int, str]. Details:
451 - The arguments must be types and there must be at least one.
452 - None as an argument is a special case and is replaced by
453 type(None).
454 - Unions of unions are flattened, e.g.::
455
456 Union[Union[int, str], float] == Union[int, str, float]
457
458 - Unions of a single argument vanish, e.g.::
459
460 Union[int] == int # The constructor actually returns int
461
462 - Redundant arguments are skipped, e.g.::
463
464 Union[int, str, int] == Union[int, str]
465
466 - When comparing unions, the argument order is ignored, e.g.::
467
468 Union[int, str] == Union[str, int]
469
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000470 - You cannot subclass or instantiate a union.
471 - You can use Optional[X] as a shorthand for Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300472 """
473 if parameters == ():
474 raise TypeError("Cannot take a Union of no types.")
475 if not isinstance(parameters, tuple):
476 parameters = (parameters,)
477 msg = "Union[arg, ...]: each arg must be a type."
478 parameters = tuple(_type_check(p, msg) for p in parameters)
479 parameters = _remove_dups_flatten(parameters)
480 if len(parameters) == 1:
481 return parameters[0]
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300482 return _UnionGenericAlias(self, parameters)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000483
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300484@_SpecialForm
485def Optional(self, parameters):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000486 """Optional type.
487
488 Optional[X] is equivalent to Union[X, None].
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300489 """
490 arg = _type_check(parameters, f"{self} requires a single type.")
491 return Union[arg, type(None)]
Guido van Rossumb7dedc82016-10-29 12:44:29 -0700492
Yurii Karabasf03d3182020-11-17 04:23:19 +0200493@_LiteralSpecialForm
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300494def Literal(self, parameters):
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100495 """Special typing form to define literal types (a.k.a. value types).
496
497 This form can be used to indicate to type checkers that the corresponding
498 variable or function parameter has a value equivalent to the provided
499 literal (or one of several literals):
500
501 def validate_simple(data: Any) -> Literal[True]: # always returns True
502 ...
503
504 MODE = Literal['r', 'rb', 'w', 'wb']
505 def open_helper(file: str, mode: MODE) -> str:
506 ...
507
508 open_helper('/some/path', 'r') # Passes type check
509 open_helper('/other/path', 'typo') # Error in type checker
510
Serhiy Storchaka40ded942020-04-23 21:26:48 +0300511 Literal[...] cannot be subclassed. At runtime, an arbitrary value
512 is allowed as type argument to Literal[...], but type checkers may
513 impose restrictions.
514 """
515 # There is no '_type_check' call because arguments to Literal[...] are
516 # values, not types.
Yurii Karabasf03d3182020-11-17 04:23:19 +0200517 if not isinstance(parameters, tuple):
518 parameters = (parameters,)
519
520 parameters = _flatten_literal_params(parameters)
521
522 try:
523 parameters = tuple(p for p, _ in _deduplicate(list(_value_and_type_iter(parameters))))
524 except TypeError: # unhashable parameters
525 pass
526
527 return _LiteralGenericAlias(self, parameters)
Ivan Levkivskyib891c462019-05-26 09:37:48 +0100528
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700529
Mikhail Golubev4f3c2502020-10-08 00:44:31 +0300530@_SpecialForm
531def TypeAlias(self, parameters):
532 """Special marker indicating that an assignment should
533 be recognized as a proper type alias definition by type
534 checkers.
535
536 For example::
537
538 Predicate: TypeAlias = Callable[..., bool]
539
540 It's invalid when used anywhere except as in the example above.
541 """
542 raise TypeError(f"{self} is not subscriptable")
543
544
kj73607be2020-12-24 12:33:48 +0800545@_SpecialForm
546def Concatenate(self, parameters):
Ken Jin11276cd2021-01-02 08:45:50 +0800547 """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
548 higher order function which adds, removes or transforms parameters of a
549 callable.
kj73607be2020-12-24 12:33:48 +0800550
551 For example::
552
553 Callable[Concatenate[int, P], int]
554
555 See PEP 612 for detailed information.
556 """
557 if parameters == ():
558 raise TypeError("Cannot take a Concatenate of no types.")
559 if not isinstance(parameters, tuple):
560 parameters = (parameters,)
561 if not isinstance(parameters[-1], ParamSpec):
562 raise TypeError("The last parameter to Concatenate should be a "
563 "ParamSpec variable.")
564 msg = "Concatenate[arg, ...]: each arg must be a type."
565 parameters = tuple(_type_check(p, msg) for p in parameters)
566 return _ConcatenateGenericAlias(self, parameters)
567
568
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000569class ForwardRef(_Final, _root=True):
Guido van Rossumb24569a2016-11-20 18:01:29 -0800570 """Internal wrapper to hold a forward reference."""
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700571
Guido van Rossum4cefe742016-09-27 15:20:12 -0700572 __slots__ = ('__forward_arg__', '__forward_code__',
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400573 '__forward_evaluated__', '__forward_value__',
574 '__forward_is_argument__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700575
Nina Zakharenko0e61dff2018-05-22 20:32:10 -0700576 def __init__(self, arg, is_argument=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700577 if not isinstance(arg, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000578 raise TypeError(f"Forward reference must be a string -- got {arg!r}")
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300579
580 # Double-stringified forward references is a result of activating
581 # the 'annotations' future by default. This way, we eliminate them in
582 # the runtime.
583 if arg.startswith(("'", '\"')) and arg.endswith(("'", '"')):
584 arg = arg[1:-1]
585
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700586 try:
587 code = compile(arg, '<string>', 'eval')
588 except SyntaxError:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000589 raise SyntaxError(f"Forward reference must be an expression -- got {arg!r}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700590 self.__forward_arg__ = arg
591 self.__forward_code__ = code
592 self.__forward_evaluated__ = False
593 self.__forward_value__ = None
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400594 self.__forward_is_argument__ = is_argument
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700595
wyfo653f4202020-07-22 21:47:28 +0200596 def _evaluate(self, globalns, localns, recursive_guard):
597 if self.__forward_arg__ in recursive_guard:
598 return self
Guido van Rossumdad17902016-11-10 08:29:18 -0800599 if not self.__forward_evaluated__ or localns is not globalns:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700600 if globalns is None and localns is None:
601 globalns = localns = {}
602 elif globalns is None:
603 globalns = localns
604 elif localns is None:
605 localns = globalns
wyfo653f4202020-07-22 21:47:28 +0200606 type_ =_type_check(
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700607 eval(self.__forward_code__, globalns, localns),
Nina Zakharenko2d2d3b12018-05-16 12:27:03 -0400608 "Forward references must evaluate to types.",
wyfo653f4202020-07-22 21:47:28 +0200609 is_argument=self.__forward_is_argument__,
610 )
611 self.__forward_value__ = _eval_type(
612 type_, globalns, localns, recursive_guard | {self.__forward_arg__}
613 )
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700614 self.__forward_evaluated__ = True
615 return self.__forward_value__
616
Guido van Rossum4cefe742016-09-27 15:20:12 -0700617 def __eq__(self, other):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000618 if not isinstance(other, ForwardRef):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700619 return NotImplemented
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100620 if self.__forward_evaluated__ and other.__forward_evaluated__:
621 return (self.__forward_arg__ == other.__forward_arg__ and
622 self.__forward_value__ == other.__forward_value__)
623 return self.__forward_arg__ == other.__forward_arg__
Guido van Rossum4cefe742016-09-27 15:20:12 -0700624
625 def __hash__(self):
plokmijnuhbye082e7c2019-09-13 20:40:54 +0100626 return hash(self.__forward_arg__)
Guido van Rossum4cefe742016-09-27 15:20:12 -0700627
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700628 def __repr__(self):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000629 return f'ForwardRef({self.__forward_arg__!r})'
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700630
kj73607be2020-12-24 12:33:48 +0800631class _TypeVarLike:
632 """Mixin for TypeVar-like types (TypeVar and ParamSpec)."""
633 def __init__(self, bound, covariant, contravariant):
634 """Used to setup TypeVars and ParamSpec's bound, covariant and
635 contravariant attributes.
636 """
637 if covariant and contravariant:
638 raise ValueError("Bivariant types are not supported.")
639 self.__covariant__ = bool(covariant)
640 self.__contravariant__ = bool(contravariant)
641 if bound:
642 self.__bound__ = _type_check(bound, "Bound must be a type.")
643 else:
644 self.__bound__ = None
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700645
kj73607be2020-12-24 12:33:48 +0800646 def __or__(self, right):
647 return Union[self, right]
648
649 def __ror__(self, right):
650 return Union[self, right]
651
652 def __repr__(self):
653 if self.__covariant__:
654 prefix = '+'
655 elif self.__contravariant__:
656 prefix = '-'
657 else:
658 prefix = '~'
659 return prefix + self.__name__
660
661 def __reduce__(self):
662 return self.__name__
663
664
665class TypeVar( _Final, _Immutable, _TypeVarLike, _root=True):
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700666 """Type variable.
667
668 Usage::
669
670 T = TypeVar('T') # Can be anything
671 A = TypeVar('A', str, bytes) # Must be str or bytes
672
673 Type variables exist primarily for the benefit of static type
674 checkers. They serve as the parameters for generic types as well
675 as for generic function definitions. See class Generic for more
676 information on generic types. Generic functions work as follows:
677
Guido van Rossumb24569a2016-11-20 18:01:29 -0800678 def repeat(x: T, n: int) -> List[T]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700679 '''Return a list containing n references to x.'''
680 return [x]*n
681
682 def longest(x: A, y: A) -> A:
683 '''Return the longest of two strings.'''
684 return x if len(x) >= len(y) else y
685
686 The latter example's signature is essentially the overloading
687 of (str, str) -> str and (bytes, bytes) -> bytes. Also note
688 that if the arguments are instances of some subclass of str,
689 the return type is still plain str.
690
Guido van Rossumb24569a2016-11-20 18:01:29 -0800691 At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700692
Guido van Rossumefa798d2016-08-23 11:01:50 -0700693 Type variables defined with covariant=True or contravariant=True
João D. Ferreira86bfed32018-07-07 16:41:20 +0100694 can be used to declare covariant or contravariant generic types.
Guido van Rossumefa798d2016-08-23 11:01:50 -0700695 See PEP 484 for more details. By default generic types are invariant
696 in all type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700697
698 Type variables can be introspected. e.g.:
699
700 T.__name__ == 'T'
701 T.__constraints__ == ()
702 T.__covariant__ == False
703 T.__contravariant__ = False
704 A.__constraints__ == (str, bytes)
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100705
706 Note that only type variables defined in global scope can be pickled.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700707 """
708
Guido van Rossum4cefe742016-09-27 15:20:12 -0700709 __slots__ = ('__name__', '__bound__', '__constraints__',
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300710 '__covariant__', '__contravariant__', '__dict__')
Guido van Rossum4cefe742016-09-27 15:20:12 -0700711
712 def __init__(self, name, *constraints, bound=None,
Guido van Rossumd7adfe12017-01-22 17:43:53 -0800713 covariant=False, contravariant=False):
Guido van Rossum4cefe742016-09-27 15:20:12 -0700714 self.__name__ = name
kj73607be2020-12-24 12:33:48 +0800715 super().__init__(bound, covariant, contravariant)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700716 if constraints and bound is not None:
717 raise TypeError("Constraints cannot be combined with bound=...")
718 if constraints and len(constraints) == 1:
719 raise TypeError("A single constraint is not allowed")
720 msg = "TypeVar(name, constraint, ...): constraints must be types."
721 self.__constraints__ = tuple(_type_check(t, msg) for t in constraints)
HongWeipenga25a04f2020-04-21 04:01:53 +0800722 try:
723 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__') # for pickling
724 except (AttributeError, ValueError):
725 def_mod = None
Serhiy Storchaka09f32212018-05-26 21:19:26 +0300726 if def_mod != 'typing':
727 self.__module__ = def_mod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700728
Maggie Moss1b4552c2020-09-09 13:23:24 -0700729
kj73607be2020-12-24 12:33:48 +0800730class ParamSpec(_Final, _Immutable, _TypeVarLike, _root=True):
731 """Parameter specification variable.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700732
kj73607be2020-12-24 12:33:48 +0800733 Usage::
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700734
kj73607be2020-12-24 12:33:48 +0800735 P = ParamSpec('P')
736
737 Parameter specification variables exist primarily for the benefit of static
738 type checkers. They are used to forward the parameter types of one
Ken Jin11276cd2021-01-02 08:45:50 +0800739 callable to another callable, a pattern commonly found in higher order
740 functions and decorators. They are only valid when used in ``Concatenate``,
741 or s the first argument to ``Callable``, or as parameters for user-defined
742 Generics. See class Generic for more information on generic types. An
743 example for annotating a decorator::
kj73607be2020-12-24 12:33:48 +0800744
745 T = TypeVar('T')
746 P = ParamSpec('P')
747
748 def add_logging(f: Callable[P, T]) -> Callable[P, T]:
749 '''A type-safe decorator to add logging to a function.'''
750 def inner(*args: P.args, **kwargs: P.kwargs) -> T:
751 logging.info(f'{f.__name__} was called')
752 return f(*args, **kwargs)
753 return inner
754
755 @add_logging
756 def add_two(x: float, y: float) -> float:
757 '''Add two numbers together.'''
758 return x + y
759
760 Parameter specification variables defined with covariant=True or
761 contravariant=True can be used to declare covariant or contravariant
762 generic types. These keyword arguments are valid, but their actual semantics
763 are yet to be decided. See PEP 612 for details.
764
765 Parameter specification variables can be introspected. e.g.:
766
767 P.__name__ == 'T'
768 P.__bound__ == None
769 P.__covariant__ == False
770 P.__contravariant__ == False
771
772 Note that only parameter specification variables defined in global scope can
773 be pickled.
774 """
775
776 __slots__ = ('__name__', '__bound__', '__covariant__', '__contravariant__',
777 '__dict__')
778
779 args = object()
780 kwargs = object()
781
Ken Jinace008c2021-01-11 08:11:41 +0800782 def __init__(self, name, *, bound=None, covariant=False, contravariant=False):
kj73607be2020-12-24 12:33:48 +0800783 self.__name__ = name
784 super().__init__(bound, covariant, contravariant)
785 try:
786 def_mod = sys._getframe(1).f_globals.get('__name__', '__main__')
787 except (AttributeError, ValueError):
788 def_mod = None
789 if def_mod != 'typing':
790 self.__module__ = def_mod
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100791
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700792
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000793def _is_dunder(attr):
794 return attr.startswith('__') and attr.endswith('__')
Guido van Rossum83ec3022017-01-17 20:43:28 -0800795
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300796class _BaseGenericAlias(_Final, _root=True):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000797 """The central part of internal API.
798
799 This represents a generic version of type 'origin' with type arguments 'params'.
800 There are two kind of these aliases: user defined and special. The special ones
801 are wrappers around builtin collections and ABCs in collections.abc. These must
802 have 'name' always set. If 'inst' is False, then the alias can't be instantiated,
803 this is used by e.g. typing.List and typing.Dict.
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700804 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300805 def __init__(self, origin, *, inst=True, name=None):
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000806 self._inst = inst
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000807 self._name = name
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700808 self.__origin__ = origin
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000809 self.__slots__ = None # This is not documented.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300810
811 def __call__(self, *args, **kwargs):
812 if not self._inst:
813 raise TypeError(f"Type {self._name} cannot be instantiated; "
814 f"use {self.__origin__.__name__}() instead")
815 result = self.__origin__(*args, **kwargs)
816 try:
817 result.__orig_class__ = self
818 except AttributeError:
819 pass
820 return result
821
822 def __mro_entries__(self, bases):
823 res = []
824 if self.__origin__ not in bases:
825 res.append(self.__origin__)
826 i = bases.index(self)
827 for b in bases[i+1:]:
828 if isinstance(b, _BaseGenericAlias) or issubclass(b, Generic):
829 break
830 else:
831 res.append(Generic)
832 return tuple(res)
833
834 def __getattr__(self, attr):
835 # We are careful for copy and pickle.
836 # Also for simplicity we just don't relay all dunder names
837 if '__origin__' in self.__dict__ and not _is_dunder(attr):
838 return getattr(self.__origin__, attr)
839 raise AttributeError(attr)
840
841 def __setattr__(self, attr, val):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300842 if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300843 super().__setattr__(attr, val)
844 else:
845 setattr(self.__origin__, attr, val)
846
847 def __instancecheck__(self, obj):
848 return self.__subclasscheck__(type(obj))
849
850 def __subclasscheck__(self, cls):
851 raise TypeError("Subscripted generics cannot be used with"
852 " class and instance checks")
853
854
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300855# Special typing constructs Union, Optional, Generic, Callable and Tuple
856# use three special attributes for internal bookkeeping of generic types:
857# * __parameters__ is a tuple of unique free type parameters of a generic
858# type, for example, Dict[T, T].__parameters__ == (T,);
859# * __origin__ keeps a reference to a type that was subscripted,
860# e.g., Union[T, int].__origin__ == Union, or the non-generic version of
861# the type.
862# * __args__ is a tuple of all arguments used in subscripting,
863# e.g., Dict[T, int].__args__ == (T, int).
864
865
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300866class _GenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300867 def __init__(self, origin, params, *, inst=True, name=None):
868 super().__init__(origin, inst=inst, name=name)
869 if not isinstance(params, tuple):
870 params = (params,)
871 self.__args__ = tuple(... if a is _TypingEllipsis else
872 () if a is _TypingEmpty else
873 a for a in params)
874 self.__parameters__ = _collect_type_vars(params)
875 if not name:
876 self.__module__ = origin.__module__
877
878 def __eq__(self, other):
879 if not isinstance(other, _GenericAlias):
880 return NotImplemented
881 return (self.__origin__ == other.__origin__
882 and self.__args__ == other.__args__)
883
884 def __hash__(self):
885 return hash((self.__origin__, self.__args__))
886
Maggie Moss1b4552c2020-09-09 13:23:24 -0700887 def __or__(self, right):
888 return Union[self, right]
889
890 def __ror__(self, right):
891 return Union[self, right]
892
Guido van Rossum4cefe742016-09-27 15:20:12 -0700893 @_tp_cache
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700894 def __getitem__(self, params):
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100895 if self.__origin__ in (Generic, Protocol):
896 # Can't subscript Generic[...] or Protocol[...].
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000897 raise TypeError(f"Cannot subscript already-subscripted {self}")
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700898 if not isinstance(params, tuple):
899 params = (params,)
kj73607be2020-12-24 12:33:48 +0800900 params = tuple(_type_convert(p) for p in params)
901 if any(isinstance(t, ParamSpec) for t in self.__parameters__):
902 params = _prepare_paramspec_params(self, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300903 _check_generic(self, params, len(self.__parameters__))
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300904
905 subst = dict(zip(self.__parameters__, params))
906 new_args = []
907 for arg in self.__args__:
kj73607be2020-12-24 12:33:48 +0800908 if isinstance(arg, _TypeVarLike):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300909 arg = subst[arg]
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300910 elif isinstance(arg, (_GenericAlias, GenericAlias)):
Serhiy Storchaka0122d482020-05-10 13:39:40 +0300911 subparams = arg.__parameters__
912 if subparams:
913 subargs = tuple(subst[x] for x in subparams)
914 arg = arg[subargs]
kj73607be2020-12-24 12:33:48 +0800915 # Required to flatten out the args for CallableGenericAlias
916 if self.__origin__ == collections.abc.Callable and isinstance(arg, tuple):
917 new_args.extend(arg)
918 else:
919 new_args.append(arg)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300920 return self.copy_with(tuple(new_args))
Ivan Levkivskyib692dc82017-02-13 22:50:14 +0100921
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000922 def copy_with(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300923 return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -0700924
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000925 def __repr__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300926 if self._name:
927 name = 'typing.' + self._name
928 else:
929 name = _type_repr(self.__origin__)
930 args = ", ".join([_type_repr(a) for a in self.__args__])
931 return f'{name}[{args}]'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000932
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300933 def __reduce__(self):
934 if self._name:
935 origin = globals()[self._name]
936 else:
937 origin = self.__origin__
938 args = tuple(self.__args__)
939 if len(args) == 1 and not isinstance(args[0], tuple):
940 args, = args
941 return operator.getitem, (origin, args)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000942
943 def __mro_entries__(self, bases):
944 if self._name: # generic version of an ABC or built-in class
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300945 return super().__mro_entries__(bases)
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000946 if self.__origin__ is Generic:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +0100947 if Protocol in bases:
948 return ()
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000949 i = bases.index(self)
950 for b in bases[i+1:]:
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300951 if isinstance(b, _BaseGenericAlias) and b is not self:
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000952 return ()
953 return (self.__origin__,)
954
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000955
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300956# _nparams is the number of accepted parameters, e.g. 0 for Hashable,
957# 1 for List and 2 for Dict. It may be -1 if variable number of
958# parameters are accepted (needs custom __getitem__).
959
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300960class _SpecialGenericAlias(_BaseGenericAlias, _root=True):
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300961 def __init__(self, origin, nparams, *, inst=True, name=None):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300962 if name is None:
963 name = origin.__name__
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300964 super().__init__(origin, inst=inst, name=name)
965 self._nparams = nparams
Serhiy Storchaka2fbc57a2020-05-10 15:14:27 +0300966 if origin.__module__ == 'builtins':
967 self.__doc__ = f'A generic version of {origin.__qualname__}.'
968 else:
969 self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}.'
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000970
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300971 @_tp_cache
972 def __getitem__(self, params):
973 if not isinstance(params, tuple):
974 params = (params,)
975 msg = "Parameters to generic types must be types."
976 params = tuple(_type_check(p, msg) for p in params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +0300977 _check_generic(self, params, self._nparams)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300978 return self.copy_with(params)
979
980 def copy_with(self, params):
981 return _GenericAlias(self.__origin__, params,
982 name=self._name, inst=self._inst)
983
984 def __repr__(self):
985 return 'typing.' + self._name
Ivan Levkivskyid911e402018-01-20 11:23:59 +0000986
987 def __subclasscheck__(self, cls):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300988 if isinstance(cls, _SpecialGenericAlias):
989 return issubclass(cls.__origin__, self.__origin__)
990 if not isinstance(cls, _GenericAlias):
991 return issubclass(cls, self.__origin__)
992 return super().__subclasscheck__(cls)
Guido van Rossum5fc25a82016-10-29 08:54:56 -0700993
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100994 def __reduce__(self):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +0300995 return self._name
Ivan Levkivskyi83494032018-03-26 23:01:12 +0100996
Maggie Moss1b4552c2020-09-09 13:23:24 -0700997 def __or__(self, right):
998 return Union[self, right]
999
1000 def __ror__(self, right):
1001 return Union[self, right]
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001002
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001003class _CallableGenericAlias(_GenericAlias, _root=True):
1004 def __repr__(self):
1005 assert self._name == 'Callable'
kj73607be2020-12-24 12:33:48 +08001006 args = self.__args__
1007 if len(args) == 2 and (args[0] is Ellipsis
1008 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001009 return super().__repr__()
1010 return (f'typing.Callable'
kj73607be2020-12-24 12:33:48 +08001011 f'[[{", ".join([_type_repr(a) for a in args[:-1]])}], '
1012 f'{_type_repr(args[-1])}]')
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001013
1014 def __reduce__(self):
1015 args = self.__args__
kj73607be2020-12-24 12:33:48 +08001016 if not (len(args) == 2 and (args[0] is Ellipsis
1017 or isinstance(args[0], (ParamSpec, _ConcatenateGenericAlias)))):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001018 args = list(args[:-1]), args[-1]
1019 return operator.getitem, (Callable, args)
1020
1021
1022class _CallableType(_SpecialGenericAlias, _root=True):
1023 def copy_with(self, params):
1024 return _CallableGenericAlias(self.__origin__, params,
1025 name=self._name, inst=self._inst)
1026
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001027 def __getitem__(self, params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001028 if not isinstance(params, tuple) or len(params) != 2:
1029 raise TypeError("Callable must be used as "
1030 "Callable[[arg, ...], result].")
1031 args, result = params
kj463c7d32020-12-14 02:38:24 +08001032 # This relaxes what args can be on purpose to allow things like
1033 # PEP 612 ParamSpec. Responsibility for whether a user is using
1034 # Callable[...] properly is deferred to static type checkers.
1035 if isinstance(args, list):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001036 params = (tuple(args), result)
kj463c7d32020-12-14 02:38:24 +08001037 else:
1038 params = (args, result)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001039 return self.__getitem_inner__(params)
1040
1041 @_tp_cache
1042 def __getitem_inner__(self, params):
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001043 args, result = params
1044 msg = "Callable[args, result]: result must be a type."
1045 result = _type_check(result, msg)
1046 if args is Ellipsis:
1047 return self.copy_with((_TypingEllipsis, result))
kj463c7d32020-12-14 02:38:24 +08001048 if not isinstance(args, tuple):
1049 args = (args,)
1050 args = tuple(_type_convert(arg) for arg in args)
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001051 params = args + (result,)
1052 return self.copy_with(params)
1053
1054
1055class _TupleType(_SpecialGenericAlias, _root=True):
1056 @_tp_cache
1057 def __getitem__(self, params):
1058 if params == ():
1059 return self.copy_with((_TypingEmpty,))
1060 if not isinstance(params, tuple):
1061 params = (params,)
1062 if len(params) == 2 and params[1] is ...:
1063 msg = "Tuple[t, ...]: t must be a type."
1064 p = _type_check(params[0], msg)
1065 return self.copy_with((p, _TypingEllipsis))
1066 msg = "Tuple[t0, t1, ...]: each t must be a type."
1067 params = tuple(_type_check(p, msg) for p in params)
1068 return self.copy_with(params)
1069
1070
1071class _UnionGenericAlias(_GenericAlias, _root=True):
1072 def copy_with(self, params):
1073 return Union[params]
1074
1075 def __eq__(self, other):
1076 if not isinstance(other, _UnionGenericAlias):
1077 return NotImplemented
1078 return set(self.__args__) == set(other.__args__)
1079
1080 def __hash__(self):
1081 return hash(frozenset(self.__args__))
1082
1083 def __repr__(self):
1084 args = self.__args__
1085 if len(args) == 2:
1086 if args[0] is type(None):
1087 return f'typing.Optional[{_type_repr(args[1])}]'
1088 elif args[1] is type(None):
1089 return f'typing.Optional[{_type_repr(args[0])}]'
1090 return super().__repr__()
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001091
Maggie Moss1b4552c2020-09-09 13:23:24 -07001092 def __instancecheck__(self, obj):
1093 return self.__subclasscheck__(type(obj))
1094
1095 def __subclasscheck__(self, cls):
1096 for arg in self.__args__:
1097 if issubclass(cls, arg):
1098 return True
1099
1100
Yurii Karabasf03d3182020-11-17 04:23:19 +02001101def _value_and_type_iter(parameters):
1102 return ((p, type(p)) for p in parameters)
1103
1104
1105class _LiteralGenericAlias(_GenericAlias, _root=True):
1106
1107 def __eq__(self, other):
1108 if not isinstance(other, _LiteralGenericAlias):
1109 return NotImplemented
1110
1111 return set(_value_and_type_iter(self.__args__)) == set(_value_and_type_iter(other.__args__))
1112
1113 def __hash__(self):
Yurii Karabas1b540772020-11-19 18:17:38 +02001114 return hash(frozenset(_value_and_type_iter(self.__args__)))
Yurii Karabasf03d3182020-11-17 04:23:19 +02001115
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001116
kj73607be2020-12-24 12:33:48 +08001117class _ConcatenateGenericAlias(_GenericAlias, _root=True):
1118 pass
1119
1120
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001121class Generic:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001122 """Abstract base class for generic types.
1123
Guido van Rossumb24569a2016-11-20 18:01:29 -08001124 A generic type is typically declared by inheriting from
1125 this class parameterized with one or more type variables.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001126 For example, a generic mapping type might be defined as::
1127
1128 class Mapping(Generic[KT, VT]):
1129 def __getitem__(self, key: KT) -> VT:
1130 ...
1131 # Etc.
1132
1133 This class can then be used as follows::
1134
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001135 def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001136 try:
1137 return mapping[key]
1138 except KeyError:
1139 return default
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001140 """
Guido van Rossumd70fe632015-08-05 12:11:06 +02001141 __slots__ = ()
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001142 _is_protocol = False
Guido van Rossumd70fe632015-08-05 12:11:06 +02001143
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001144 @_tp_cache
1145 def __class_getitem__(cls, params):
1146 if not isinstance(params, tuple):
1147 params = (params,)
1148 if not params and cls is not Tuple:
1149 raise TypeError(
1150 f"Parameter list to {cls.__qualname__}[...] cannot be empty")
kj73607be2020-12-24 12:33:48 +08001151 params = tuple(_type_convert(p) for p in params)
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001152 if cls in (Generic, Protocol):
1153 # Generic and Protocol can only be subscripted with unique type variables.
kj73607be2020-12-24 12:33:48 +08001154 if not all(isinstance(p, _TypeVarLike) for p in params):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001155 raise TypeError(
kj73607be2020-12-24 12:33:48 +08001156 f"Parameters to {cls.__name__}[...] must all be type variables "
1157 f"or parameter specification variables.")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001158 if len(set(params)) != len(params):
1159 raise TypeError(
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001160 f"Parameters to {cls.__name__}[...] must all be unique")
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001161 else:
1162 # Subscripting a regular Generic subclass.
kj73607be2020-12-24 12:33:48 +08001163 if any(isinstance(t, ParamSpec) for t in cls.__parameters__):
1164 params = _prepare_paramspec_params(cls, params)
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001165 _check_generic(cls, params, len(cls.__parameters__))
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001166 return _GenericAlias(cls, params)
1167
1168 def __init_subclass__(cls, *args, **kwargs):
Ivan Levkivskyiee566fe2018-04-04 17:00:15 +01001169 super().__init_subclass__(*args, **kwargs)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001170 tvars = []
1171 if '__orig_bases__' in cls.__dict__:
1172 error = Generic in cls.__orig_bases__
1173 else:
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001174 error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001175 if error:
1176 raise TypeError("Cannot inherit from plain Generic")
1177 if '__orig_bases__' in cls.__dict__:
1178 tvars = _collect_type_vars(cls.__orig_bases__)
1179 # Look for Generic[T1, ..., Tn].
1180 # If found, tvars must be a subset of it.
1181 # If not found, tvars is it.
1182 # Also check for and reject plain Generic,
1183 # and reject multiple Generic[...].
1184 gvars = None
1185 for base in cls.__orig_bases__:
1186 if (isinstance(base, _GenericAlias) and
1187 base.__origin__ is Generic):
1188 if gvars is not None:
1189 raise TypeError(
1190 "Cannot inherit from Generic[...] multiple types.")
1191 gvars = base.__parameters__
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001192 if gvars is not None:
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001193 tvarset = set(tvars)
1194 gvarset = set(gvars)
1195 if not tvarset <= gvarset:
1196 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1197 s_args = ', '.join(str(g) for g in gvars)
1198 raise TypeError(f"Some type variables ({s_vars}) are"
1199 f" not listed in Generic[{s_args}]")
1200 tvars = gvars
1201 cls.__parameters__ = tuple(tvars)
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001202
1203
1204class _TypingEmpty:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001205 """Internal placeholder for () or []. Used by TupleMeta and CallableMeta
1206 to allow empty list/tuple in specific places, without allowing them
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001207 to sneak in where prohibited.
1208 """
1209
1210
1211class _TypingEllipsis:
Guido van Rossumb24569a2016-11-20 18:01:29 -08001212 """Internal placeholder for ... (ellipsis)."""
Guido van Rossum5fc25a82016-10-29 08:54:56 -07001213
1214
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001215_TYPING_INTERNALS = ['__parameters__', '__orig_bases__', '__orig_class__',
1216 '_is_protocol', '_is_runtime_protocol']
1217
1218_SPECIAL_NAMES = ['__abstractmethods__', '__annotations__', '__dict__', '__doc__',
1219 '__init__', '__module__', '__new__', '__slots__',
Guido van Rossum48b069a2020-04-07 09:50:06 -07001220 '__subclasshook__', '__weakref__', '__class_getitem__']
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001221
1222# These special attributes will be not collected as protocol members.
1223EXCLUDED_ATTRIBUTES = _TYPING_INTERNALS + _SPECIAL_NAMES + ['_MutableMapping__marker']
1224
1225
1226def _get_protocol_attrs(cls):
1227 """Collect protocol members from a protocol class objects.
1228
1229 This includes names actually defined in the class dictionary, as well
1230 as names that appear in annotations. Special names (above) are skipped.
1231 """
1232 attrs = set()
1233 for base in cls.__mro__[:-1]: # without object
1234 if base.__name__ in ('Protocol', 'Generic'):
1235 continue
1236 annotations = getattr(base, '__annotations__', {})
1237 for attr in list(base.__dict__.keys()) + list(annotations.keys()):
1238 if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
1239 attrs.add(attr)
1240 return attrs
1241
1242
1243def _is_callable_members_only(cls):
1244 # PEP 544 prohibits using issubclass() with protocols that have non-method members.
1245 return all(callable(getattr(cls, attr, None)) for attr in _get_protocol_attrs(cls))
1246
1247
1248def _no_init(self, *args, **kwargs):
1249 if type(self)._is_protocol:
1250 raise TypeError('Protocols cannot be instantiated')
1251
1252
Rossc1af1282020-12-29 11:55:28 +00001253def _allow_reckless_class_checks():
Nickolena Fishercfaf4c02020-04-26 12:49:11 -05001254 """Allow instance and class checks for special stdlib modules.
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001255
1256 The abc and functools modules indiscriminately call isinstance() and
1257 issubclass() on the whole MRO of a user class, which may contain protocols.
1258 """
1259 try:
1260 return sys._getframe(3).f_globals['__name__'] in ['abc', 'functools']
1261 except (AttributeError, ValueError): # For platforms without _getframe().
1262 return True
1263
1264
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001265_PROTO_ALLOWLIST = {
Divij Rajkumar692a0dc2019-09-12 11:13:51 +01001266 'collections.abc': [
1267 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
1268 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
1269 ],
1270 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
1271}
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001272
1273
1274class _ProtocolMeta(ABCMeta):
1275 # This metaclass is really unfortunate and exists only because of
1276 # the lack of __instancehook__.
1277 def __instancecheck__(cls, instance):
1278 # We need this method for situations where attributes are
1279 # assigned in __init__.
1280 if ((not getattr(cls, '_is_protocol', False) or
1281 _is_callable_members_only(cls)) and
1282 issubclass(instance.__class__, cls)):
1283 return True
1284 if cls._is_protocol:
1285 if all(hasattr(instance, attr) and
1286 # All *methods* can be blocked by setting them to None.
1287 (not callable(getattr(cls, attr, None)) or
1288 getattr(instance, attr) is not None)
1289 for attr in _get_protocol_attrs(cls)):
1290 return True
1291 return super().__instancecheck__(instance)
1292
1293
1294class Protocol(Generic, metaclass=_ProtocolMeta):
1295 """Base class for protocol classes.
1296
1297 Protocol classes are defined as::
1298
1299 class Proto(Protocol):
1300 def meth(self) -> int:
1301 ...
1302
1303 Such classes are primarily used with static type checkers that recognize
1304 structural subtyping (static duck-typing), for example::
1305
1306 class C:
1307 def meth(self) -> int:
1308 return 0
1309
1310 def func(x: Proto) -> int:
1311 return x.meth()
1312
1313 func(C()) # Passes static type check
1314
1315 See PEP 544 for details. Protocol classes decorated with
1316 @typing.runtime_checkable act as simple-minded runtime protocols that check
1317 only the presence of given attributes, ignoring their type signatures.
1318 Protocol classes can be generic, they are defined as::
1319
1320 class GenProto(Protocol[T]):
1321 def meth(self) -> T:
1322 ...
1323 """
1324 __slots__ = ()
1325 _is_protocol = True
1326 _is_runtime_protocol = False
1327
1328 def __init_subclass__(cls, *args, **kwargs):
1329 super().__init_subclass__(*args, **kwargs)
1330
1331 # Determine if this is a protocol or a concrete subclass.
1332 if not cls.__dict__.get('_is_protocol', False):
1333 cls._is_protocol = any(b is Protocol for b in cls.__bases__)
1334
1335 # Set (or override) the protocol subclass hook.
1336 def _proto_hook(other):
1337 if not cls.__dict__.get('_is_protocol', False):
1338 return NotImplemented
1339
1340 # First, perform various sanity checks.
1341 if not getattr(cls, '_is_runtime_protocol', False):
Rossc1af1282020-12-29 11:55:28 +00001342 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001343 return NotImplemented
1344 raise TypeError("Instance and class checks can only be used with"
1345 " @runtime_checkable protocols")
1346 if not _is_callable_members_only(cls):
Rossc1af1282020-12-29 11:55:28 +00001347 if _allow_reckless_class_checks():
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001348 return NotImplemented
1349 raise TypeError("Protocols with non-method members"
1350 " don't support issubclass()")
1351 if not isinstance(other, type):
1352 # Same error message as for issubclass(1, int).
1353 raise TypeError('issubclass() arg 1 must be a class')
1354
1355 # Second, perform the actual structural compatibility check.
1356 for attr in _get_protocol_attrs(cls):
1357 for base in other.__mro__:
1358 # Check if the members appears in the class dictionary...
1359 if attr in base.__dict__:
1360 if base.__dict__[attr] is None:
1361 return NotImplemented
1362 break
1363
1364 # ...or in annotations, if it is a sub-protocol.
1365 annotations = getattr(base, '__annotations__', {})
1366 if (isinstance(annotations, collections.abc.Mapping) and
1367 attr in annotations and
1368 issubclass(other, Generic) and other._is_protocol):
1369 break
1370 else:
1371 return NotImplemented
1372 return True
1373
1374 if '__subclasshook__' not in cls.__dict__:
1375 cls.__subclasshook__ = _proto_hook
1376
1377 # We have nothing more to do for non-protocols...
1378 if not cls._is_protocol:
1379 return
1380
1381 # ... otherwise check consistency of bases, and prohibit instantiation.
1382 for base in cls.__bases__:
1383 if not (base in (object, Generic) or
Victor Stinner0e95bbf2020-08-12 10:53:12 +02001384 base.__module__ in _PROTO_ALLOWLIST and
1385 base.__name__ in _PROTO_ALLOWLIST[base.__module__] or
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001386 issubclass(base, Generic) and base._is_protocol):
1387 raise TypeError('Protocols can only inherit from other'
1388 ' protocols, got %r' % base)
1389 cls.__init__ = _no_init
1390
1391
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001392class _AnnotatedAlias(_GenericAlias, _root=True):
1393 """Runtime representation of an annotated type.
1394
1395 At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
1396 with extra annotations. The alias behaves like a normal typing alias,
1397 instantiating is the same as instantiating the underlying type, binding
1398 it to types is also the same.
1399 """
1400 def __init__(self, origin, metadata):
1401 if isinstance(origin, _AnnotatedAlias):
1402 metadata = origin.__metadata__ + metadata
1403 origin = origin.__origin__
1404 super().__init__(origin, origin)
1405 self.__metadata__ = metadata
1406
1407 def copy_with(self, params):
1408 assert len(params) == 1
1409 new_type = params[0]
1410 return _AnnotatedAlias(new_type, self.__metadata__)
1411
1412 def __repr__(self):
1413 return "typing.Annotated[{}, {}]".format(
1414 _type_repr(self.__origin__),
1415 ", ".join(repr(a) for a in self.__metadata__)
1416 )
1417
1418 def __reduce__(self):
1419 return operator.getitem, (
1420 Annotated, (self.__origin__,) + self.__metadata__
1421 )
1422
1423 def __eq__(self, other):
1424 if not isinstance(other, _AnnotatedAlias):
1425 return NotImplemented
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001426 return (self.__origin__ == other.__origin__
1427 and self.__metadata__ == other.__metadata__)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001428
1429 def __hash__(self):
1430 return hash((self.__origin__, self.__metadata__))
1431
1432
1433class Annotated:
1434 """Add context specific metadata to a type.
1435
1436 Example: Annotated[int, runtime_check.Unsigned] indicates to the
1437 hypothetical runtime_check module that this type is an unsigned int.
1438 Every other consumer of this type can ignore this metadata and treat
1439 this type as int.
1440
1441 The first argument to Annotated must be a valid type.
1442
1443 Details:
1444
1445 - It's an error to call `Annotated` with less than two arguments.
1446 - Nested Annotated are flattened::
1447
1448 Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
1449
1450 - Instantiating an annotated type is equivalent to instantiating the
1451 underlying type::
1452
1453 Annotated[C, Ann1](5) == C(5)
1454
1455 - Annotated can be used as a generic type alias::
1456
1457 Optimized = Annotated[T, runtime.Optimize()]
1458 Optimized[int] == Annotated[int, runtime.Optimize()]
1459
1460 OptimizedList = Annotated[List[T], runtime.Optimize()]
1461 OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
1462 """
1463
1464 __slots__ = ()
1465
1466 def __new__(cls, *args, **kwargs):
1467 raise TypeError("Type Annotated cannot be instantiated.")
1468
1469 @_tp_cache
1470 def __class_getitem__(cls, params):
1471 if not isinstance(params, tuple) or len(params) < 2:
1472 raise TypeError("Annotated[...] should be used "
1473 "with at least two arguments (a type and an "
1474 "annotation).")
1475 msg = "Annotated[t, ...]: t must be a type."
1476 origin = _type_check(params[0], msg)
1477 metadata = tuple(params[1:])
1478 return _AnnotatedAlias(origin, metadata)
1479
1480 def __init_subclass__(cls, *args, **kwargs):
1481 raise TypeError(
1482 "Cannot subclass {}.Annotated".format(cls.__module__)
1483 )
1484
1485
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001486def runtime_checkable(cls):
1487 """Mark a protocol class as a runtime protocol.
1488
1489 Such protocol can be used with isinstance() and issubclass().
1490 Raise TypeError if applied to a non-protocol class.
1491 This allows a simple-minded structural check very similar to
1492 one trick ponies in collections.abc such as Iterable.
1493 For example::
1494
1495 @runtime_checkable
1496 class Closable(Protocol):
1497 def close(self): ...
1498
1499 assert isinstance(open('/some/file'), Closable)
1500
1501 Warning: this will check only the presence of the required methods,
1502 not their type signatures!
1503 """
1504 if not issubclass(cls, Generic) or not cls._is_protocol:
1505 raise TypeError('@runtime_checkable can be only applied to protocol classes,'
1506 ' got %r' % cls)
1507 cls._is_runtime_protocol = True
1508 return cls
1509
1510
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001511def cast(typ, val):
1512 """Cast a value to a type.
1513
1514 This returns the value unchanged. To the type checker this
1515 signals that the return value has the designated type, but at
1516 runtime we intentionally don't check anything (we want this
1517 to be as fast as possible).
1518 """
1519 return val
1520
1521
1522def _get_defaults(func):
1523 """Internal helper to extract the default arguments, by name."""
Guido van Rossum991d14f2016-11-09 13:12:51 -08001524 try:
1525 code = func.__code__
1526 except AttributeError:
1527 # Some built-in functions don't have __code__, __defaults__, etc.
1528 return {}
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001529 pos_count = code.co_argcount
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001530 arg_names = code.co_varnames
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001531 arg_names = arg_names[:pos_count]
1532 defaults = func.__defaults__ or ()
1533 kwdefaults = func.__kwdefaults__
1534 res = dict(kwdefaults) if kwdefaults else {}
1535 pos_offset = pos_count - len(defaults)
1536 for name, value in zip(arg_names[pos_offset:], defaults):
1537 assert name not in res
1538 res[name] = value
1539 return res
1540
1541
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001542_allowed_types = (types.FunctionType, types.BuiltinFunctionType,
1543 types.MethodType, types.ModuleType,
Ivan Levkivskyif06e0212017-05-02 19:14:07 +02001544 WrapperDescriptorType, MethodWrapperType, MethodDescriptorType)
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001545
1546
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001547def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
Guido van Rossum991d14f2016-11-09 13:12:51 -08001548 """Return type hints for an object.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001549
Guido van Rossum991d14f2016-11-09 13:12:51 -08001550 This is often the same as obj.__annotations__, but it handles
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001551 forward references encoded as string literals, adds Optional[t] if a
1552 default value equal to None is set and recursively replaces all
1553 'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001554
Guido van Rossum991d14f2016-11-09 13:12:51 -08001555 The argument may be a module, class, method, or function. The annotations
1556 are returned as a dictionary. For classes, annotations include also
1557 inherited members.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001558
Guido van Rossum991d14f2016-11-09 13:12:51 -08001559 TypeError is raised if the argument is not of a type that can contain
1560 annotations, and an empty dictionary is returned if no annotations are
1561 present.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001562
Guido van Rossum991d14f2016-11-09 13:12:51 -08001563 BEWARE -- the behavior of globalns and localns is counterintuitive
1564 (unless you are familiar with how eval() and exec() work). The
1565 search order is locals first, then globals.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001566
Guido van Rossum991d14f2016-11-09 13:12:51 -08001567 - If no dict arguments are passed, an attempt is made to use the
Łukasz Langaf350a262017-09-14 14:33:00 -04001568 globals from obj (or the respective module's globals for classes),
1569 and these are also used as the locals. If the object does not appear
1570 to have globals, an empty dictionary is used.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001571
Guido van Rossum991d14f2016-11-09 13:12:51 -08001572 - If one dict argument is passed, it is used for both globals and
1573 locals.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001574
Guido van Rossum991d14f2016-11-09 13:12:51 -08001575 - If two dict arguments are passed, they specify globals and
1576 locals, respectively.
1577 """
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001578
Guido van Rossum991d14f2016-11-09 13:12:51 -08001579 if getattr(obj, '__no_type_check__', None):
1580 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001581 # Classes require a special treatment.
1582 if isinstance(obj, type):
1583 hints = {}
1584 for base in reversed(obj.__mro__):
Łukasz Langaf350a262017-09-14 14:33:00 -04001585 if globalns is None:
1586 base_globals = sys.modules[base.__module__].__dict__
1587 else:
1588 base_globals = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001589 ann = base.__dict__.get('__annotations__', {})
1590 for name, value in ann.items():
1591 if value is None:
1592 value = type(None)
1593 if isinstance(value, str):
Nina Zakharenko0e61dff2018-05-22 20:32:10 -07001594 value = ForwardRef(value, is_argument=False)
Łukasz Langaf350a262017-09-14 14:33:00 -04001595 value = _eval_type(value, base_globals, localns)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001596 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001597 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
Łukasz Langaf350a262017-09-14 14:33:00 -04001598
1599 if globalns is None:
1600 if isinstance(obj, types.ModuleType):
1601 globalns = obj.__dict__
1602 else:
benedwards140aca3a32019-11-21 17:24:58 +00001603 nsobj = obj
1604 # Find globalns for the unwrapped object.
1605 while hasattr(nsobj, '__wrapped__'):
1606 nsobj = nsobj.__wrapped__
1607 globalns = getattr(nsobj, '__globals__', {})
Łukasz Langaf350a262017-09-14 14:33:00 -04001608 if localns is None:
1609 localns = globalns
1610 elif localns is None:
1611 localns = globalns
Guido van Rossum991d14f2016-11-09 13:12:51 -08001612 hints = getattr(obj, '__annotations__', None)
1613 if hints is None:
1614 # Return empty annotations for something that _could_ have them.
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01001615 if isinstance(obj, _allowed_types):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001616 return {}
Guido van Rossum991d14f2016-11-09 13:12:51 -08001617 else:
1618 raise TypeError('{!r} is not a module, class, method, '
1619 'or function.'.format(obj))
1620 defaults = _get_defaults(obj)
1621 hints = dict(hints)
1622 for name, value in hints.items():
1623 if value is None:
1624 value = type(None)
1625 if isinstance(value, str):
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001626 value = ForwardRef(value)
Guido van Rossum991d14f2016-11-09 13:12:51 -08001627 value = _eval_type(value, globalns, localns)
1628 if name in defaults and defaults[name] is None:
1629 value = Optional[value]
1630 hints[name] = value
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001631 return hints if include_extras else {k: _strip_annotations(t) for k, t in hints.items()}
1632
1633
1634def _strip_annotations(t):
1635 """Strips the annotations from a given type.
1636 """
1637 if isinstance(t, _AnnotatedAlias):
1638 return _strip_annotations(t.__origin__)
1639 if isinstance(t, _GenericAlias):
1640 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1641 if stripped_args == t.__args__:
1642 return t
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001643 return t.copy_with(stripped_args)
Serhiy Storchaka68b352a2020-04-26 21:21:08 +03001644 if isinstance(t, GenericAlias):
1645 stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
1646 if stripped_args == t.__args__:
1647 return t
1648 return GenericAlias(t.__origin__, stripped_args)
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001649 return t
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001650
1651
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001652def get_origin(tp):
1653 """Get the unsubscripted version of a type.
1654
Jakub Stasiak38aaaaa2020-02-07 02:15:12 +01001655 This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
1656 and Annotated. Return None for unsupported types. Examples::
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001657
1658 get_origin(Literal[42]) is Literal
1659 get_origin(int) is None
1660 get_origin(ClassVar[int]) is ClassVar
1661 get_origin(Generic) is Generic
1662 get_origin(Generic[T]) is Generic
1663 get_origin(Union[T, int]) is Union
1664 get_origin(List[Tuple[T, T]][int]) == list
1665 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001666 if isinstance(tp, _AnnotatedAlias):
1667 return Annotated
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001668 if isinstance(tp, (_BaseGenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001669 return tp.__origin__
1670 if tp is Generic:
1671 return Generic
Ken Jinefb1f092020-12-29 10:26:19 +08001672 if isinstance(tp, types.Union):
1673 return types.Union
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001674 return None
1675
1676
1677def get_args(tp):
1678 """Get type arguments with all substitutions performed.
1679
1680 For unions, basic simplifications used by Union constructor are performed.
1681 Examples::
1682 get_args(Dict[str, int]) == (str, int)
1683 get_args(int) == ()
1684 get_args(Union[int, Union[T, int], str][int]) == (int, str)
1685 get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
1686 get_args(Callable[[], T][int]) == ([], int)
1687 """
Jakub Stasiakcf5b1092020-02-05 02:10:19 +01001688 if isinstance(tp, _AnnotatedAlias):
1689 return (tp.__origin__,) + tp.__metadata__
Ken Jin4140f102020-12-29 04:06:19 +08001690 if isinstance(tp, (_GenericAlias, GenericAlias)):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001691 res = tp.__args__
Ken Jinefb1f092020-12-29 10:26:19 +08001692 if (tp.__origin__ is collections.abc.Callable
1693 and not (res[0] is Ellipsis
1694 or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001695 res = (list(res[:-1]), res[-1])
1696 return res
Ken Jinefb1f092020-12-29 10:26:19 +08001697 if isinstance(tp, types.Union):
1698 return tp.__args__
Ivan Levkivskyi4c23aff2019-05-31 00:10:07 +01001699 return ()
1700
1701
Patrick Reader0705ec82020-09-16 05:58:32 +01001702def is_typeddict(tp):
1703 """Check if an annotation is a TypedDict class
1704
1705 For example::
1706 class Film(TypedDict):
1707 title: str
1708 year: int
1709
1710 is_typeddict(Film) # => True
1711 is_typeddict(Union[list, str]) # => False
1712 """
1713 return isinstance(tp, _TypedDictMeta)
1714
1715
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001716def no_type_check(arg):
1717 """Decorator to indicate that annotations are not type hints.
1718
1719 The argument must be a class or function; if it is a class, it
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001720 applies recursively to all methods and classes defined in that class
1721 (but not to methods defined in its superclasses or subclasses).
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001722
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001723 This mutates the function(s) or class(es) in place.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001724 """
1725 if isinstance(arg, type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001726 arg_attrs = arg.__dict__.copy()
1727 for attr, val in arg.__dict__.items():
Ivan Levkivskyi65bc6202017-09-14 01:25:15 +02001728 if val in arg.__bases__ + (arg,):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001729 arg_attrs.pop(attr)
1730 for obj in arg_attrs.values():
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001731 if isinstance(obj, types.FunctionType):
1732 obj.__no_type_check__ = True
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001733 if isinstance(obj, type):
1734 no_type_check(obj)
1735 try:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001736 arg.__no_type_check__ = True
Guido van Rossumd7adfe12017-01-22 17:43:53 -08001737 except TypeError: # built-in classes
Guido van Rossum0a6976d2016-09-11 15:34:56 -07001738 pass
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001739 return arg
1740
1741
1742def no_type_check_decorator(decorator):
1743 """Decorator to give another decorator the @no_type_check effect.
1744
1745 This wraps the decorator with something that wraps the decorated
1746 function in @no_type_check.
1747 """
1748
1749 @functools.wraps(decorator)
1750 def wrapped_decorator(*args, **kwds):
1751 func = decorator(*args, **kwds)
1752 func = no_type_check(func)
1753 return func
1754
1755 return wrapped_decorator
1756
1757
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001758def _overload_dummy(*args, **kwds):
1759 """Helper for @overload to raise when called."""
1760 raise NotImplementedError(
1761 "You should not call an overloaded function. "
1762 "A series of @overload-decorated functions "
1763 "outside a stub module should always be followed "
1764 "by an implementation that is not @overload-ed.")
1765
1766
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001767def overload(func):
Guido van Rossumbd5b9a02016-04-05 08:28:52 -07001768 """Decorator for overloaded functions/methods.
1769
1770 In a stub file, place two or more stub definitions for the same
1771 function in a row, each decorated with @overload. For example:
1772
1773 @overload
1774 def utf8(value: None) -> None: ...
1775 @overload
1776 def utf8(value: bytes) -> bytes: ...
1777 @overload
1778 def utf8(value: str) -> bytes: ...
1779
1780 In a non-stub file (i.e. a regular .py file), do the same but
1781 follow it with an implementation. The implementation should *not*
1782 be decorated with @overload. For example:
1783
1784 @overload
1785 def utf8(value: None) -> None: ...
1786 @overload
1787 def utf8(value: bytes) -> bytes: ...
1788 @overload
1789 def utf8(value: str) -> bytes: ...
1790 def utf8(value):
1791 # implementation goes here
1792 """
1793 return _overload_dummy
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001794
1795
Ivan Levkivskyif3672422019-05-26 09:37:07 +01001796def final(f):
1797 """A decorator to indicate final methods and final classes.
1798
1799 Use this decorator to indicate to type checkers that the decorated
1800 method cannot be overridden, and decorated class cannot be subclassed.
1801 For example:
1802
1803 class Base:
1804 @final
1805 def done(self) -> None:
1806 ...
1807 class Sub(Base):
1808 def done(self) -> None: # Error reported by type checker
1809 ...
1810
1811 @final
1812 class Leaf:
1813 ...
1814 class Other(Leaf): # Error reported by type checker
1815 ...
1816
1817 There is no runtime checking of these properties.
1818 """
1819 return f
1820
1821
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001822# Some unconstrained type variables. These are used by the container types.
1823# (These are not for export.)
1824T = TypeVar('T') # Any type.
1825KT = TypeVar('KT') # Key type.
1826VT = TypeVar('VT') # Value type.
1827T_co = TypeVar('T_co', covariant=True) # Any type covariant containers.
1828V_co = TypeVar('V_co', covariant=True) # Any type covariant containers.
1829VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers.
1830T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant.
1831# Internal type variable used for Type[].
1832CT_co = TypeVar('CT_co', covariant=True, bound=type)
1833
1834# A useful type variable with constraints. This represents string types.
1835# (This one *is* for export!)
1836AnyStr = TypeVar('AnyStr', bytes, str)
1837
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001838
1839# Various ABCs mimicking those in collections.abc.
Serhiy Storchakac1c7d8e2020-05-07 04:09:33 +03001840_alias = _SpecialGenericAlias
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001841
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001842Hashable = _alias(collections.abc.Hashable, 0) # Not generic.
1843Awaitable = _alias(collections.abc.Awaitable, 1)
1844Coroutine = _alias(collections.abc.Coroutine, 3)
1845AsyncIterable = _alias(collections.abc.AsyncIterable, 1)
1846AsyncIterator = _alias(collections.abc.AsyncIterator, 1)
1847Iterable = _alias(collections.abc.Iterable, 1)
1848Iterator = _alias(collections.abc.Iterator, 1)
1849Reversible = _alias(collections.abc.Reversible, 1)
1850Sized = _alias(collections.abc.Sized, 0) # Not generic.
1851Container = _alias(collections.abc.Container, 1)
1852Collection = _alias(collections.abc.Collection, 1)
1853Callable = _CallableType(collections.abc.Callable, 2)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001854Callable.__doc__ = \
1855 """Callable type; Callable[[int], str] is a function of (int) -> str.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001856
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001857 The subscription syntax must always be used with exactly two
1858 values: the argument list and the return type. The argument list
1859 must be a list of types or ellipsis; the return type must be a single type.
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001860
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001861 There is no syntax to indicate optional or keyword arguments,
1862 such function types are rarely used as callback types.
1863 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001864AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet')
1865MutableSet = _alias(collections.abc.MutableSet, 1)
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001866# NOTE: Mapping is only covariant in the value type.
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001867Mapping = _alias(collections.abc.Mapping, 2)
1868MutableMapping = _alias(collections.abc.MutableMapping, 2)
1869Sequence = _alias(collections.abc.Sequence, 1)
1870MutableSequence = _alias(collections.abc.MutableSequence, 1)
1871ByteString = _alias(collections.abc.ByteString, 0) # Not generic
1872# Tuple accepts variable number of parameters.
1873Tuple = _TupleType(tuple, -1, inst=False, name='Tuple')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001874Tuple.__doc__ = \
1875 """Tuple type; Tuple[X, Y] is the cross-product type of X and Y.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001876
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001877 Example: Tuple[T1, T2] is a tuple of two elements corresponding
1878 to type variables T1 and T2. Tuple[int, float, str] is a tuple
1879 of an int, a float and a string.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001880
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001881 To specify a variable-length tuple of homogeneous type, use Tuple[T, ...].
1882 """
Serhiy Storchakafcb28562020-05-10 11:53:16 +03001883List = _alias(list, 1, inst=False, name='List')
1884Deque = _alias(collections.deque, 1, name='Deque')
1885Set = _alias(set, 1, inst=False, name='Set')
1886FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet')
1887MappingView = _alias(collections.abc.MappingView, 1)
1888KeysView = _alias(collections.abc.KeysView, 1)
1889ItemsView = _alias(collections.abc.ItemsView, 2)
1890ValuesView = _alias(collections.abc.ValuesView, 1)
1891ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
1892AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
1893Dict = _alias(dict, 2, inst=False, name='Dict')
1894DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
1895OrderedDict = _alias(collections.OrderedDict, 2)
1896Counter = _alias(collections.Counter, 1)
1897ChainMap = _alias(collections.ChainMap, 2)
1898Generator = _alias(collections.abc.Generator, 3)
1899AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
1900Type = _alias(type, 1, inst=False, name='Type')
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001901Type.__doc__ = \
1902 """A special construct usable to annotate class objects.
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001903
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001904 For example, suppose we have the following classes::
Guido van Rossum62fe1bb2016-10-29 16:05:26 -07001905
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001906 class User: ... # Abstract base for User classes
1907 class BasicUser(User): ...
1908 class ProUser(User): ...
1909 class TeamUser(User): ...
Guido van Rossumf17c2002015-12-03 17:31:24 -08001910
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001911 And a function that takes a class argument that's a subclass of
1912 User and returns an instance of the corresponding class::
Guido van Rossumf17c2002015-12-03 17:31:24 -08001913
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001914 U = TypeVar('U', bound=User)
1915 def new_user(user_class: Type[U]) -> U:
1916 user = user_class()
1917 # (Here we could write the user object to a database)
1918 return user
Guido van Rossumf17c2002015-12-03 17:31:24 -08001919
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001920 joe = new_user(BasicUser)
Guido van Rossumf17c2002015-12-03 17:31:24 -08001921
Ivan Levkivskyid911e402018-01-20 11:23:59 +00001922 At this point the type checker knows that joe has type BasicUser.
1923 """
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001924
1925
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001926@runtime_checkable
1927class SupportsInt(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001928 """An ABC with one abstract method __int__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001929 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001930
1931 @abstractmethod
1932 def __int__(self) -> int:
1933 pass
1934
1935
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001936@runtime_checkable
1937class SupportsFloat(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001938 """An ABC with one abstract method __float__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001939 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001940
1941 @abstractmethod
1942 def __float__(self) -> float:
1943 pass
1944
1945
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001946@runtime_checkable
1947class SupportsComplex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001948 """An ABC with one abstract method __complex__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001949 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001950
1951 @abstractmethod
1952 def __complex__(self) -> complex:
1953 pass
1954
1955
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001956@runtime_checkable
1957class SupportsBytes(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001958 """An ABC with one abstract method __bytes__."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001959 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001960
1961 @abstractmethod
1962 def __bytes__(self) -> bytes:
1963 pass
1964
1965
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001966@runtime_checkable
1967class SupportsIndex(Protocol):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001968 """An ABC with one abstract method __index__."""
Paul Dagnelie4c7a46e2019-05-22 07:23:01 -07001969 __slots__ = ()
1970
1971 @abstractmethod
1972 def __index__(self) -> int:
1973 pass
1974
1975
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001976@runtime_checkable
1977class SupportsAbs(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001978 """An ABC with one abstract method __abs__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001979 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001980
1981 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001982 def __abs__(self) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001983 pass
1984
1985
Ivan Levkivskyi74d7f762019-05-28 08:40:15 +01001986@runtime_checkable
1987class SupportsRound(Protocol[T_co]):
Serhiy Storchaka8252c522019-10-08 16:30:17 +03001988 """An ABC with one abstract method __round__ that is covariant in its return type."""
Guido van Rossumd70fe632015-08-05 12:11:06 +02001989 __slots__ = ()
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001990
1991 @abstractmethod
Guido van Rossumd70fe632015-08-05 12:11:06 +02001992 def __round__(self, ndigits: int = 0) -> T_co:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07001993 pass
1994
1995
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03001996def _make_nmtuple(name, types, module, defaults = ()):
1997 fields = [n for n, t in types]
1998 types = {n: _type_check(t, f"field {n} annotation must be a type")
1999 for n, t in types}
2000 nm_tpl = collections.namedtuple(name, fields,
2001 defaults=defaults, module=module)
2002 nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = types
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002003 return nm_tpl
2004
2005
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002006# attributes prohibited to set in NamedTuple class syntax
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002007_prohibited = frozenset({'__new__', '__init__', '__slots__', '__getnewargs__',
2008 '_fields', '_field_defaults',
2009 '_make', '_replace', '_asdict', '_source'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002010
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002011_special = frozenset({'__module__', '__name__', '__annotations__'})
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002012
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002013
Guido van Rossum2f841442016-11-15 09:48:06 -08002014class NamedTupleMeta(type):
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002015
Guido van Rossum2f841442016-11-15 09:48:06 -08002016 def __new__(cls, typename, bases, ns):
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002017 assert bases[0] is _NamedTuple
Guido van Rossum2f841442016-11-15 09:48:06 -08002018 types = ns.get('__annotations__', {})
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002019 default_names = []
Guido van Rossum3c268be2017-01-18 08:03:50 -08002020 for field_name in types:
2021 if field_name in ns:
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002022 default_names.append(field_name)
2023 elif default_names:
2024 raise TypeError(f"Non-default namedtuple field {field_name} "
2025 f"cannot follow default field"
2026 f"{'s' if len(default_names) > 1 else ''} "
2027 f"{', '.join(default_names)}")
2028 nm_tpl = _make_nmtuple(typename, types.items(),
2029 defaults=[ns[n] for n in default_names],
2030 module=ns['__module__'])
Guido van Rossum95919c02017-01-22 17:47:20 -08002031 # update from user namespace without overriding special namedtuple attributes
2032 for key in ns:
Ivan Levkivskyib692dc82017-02-13 22:50:14 +01002033 if key in _prohibited:
2034 raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
2035 elif key not in _special and key not in nm_tpl._fields:
Guido van Rossum95919c02017-01-22 17:47:20 -08002036 setattr(nm_tpl, key, ns[key])
Guido van Rossum3c268be2017-01-18 08:03:50 -08002037 return nm_tpl
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002038
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002039
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002040def NamedTuple(typename, fields=None, /, **kwargs):
Guido van Rossum2f841442016-11-15 09:48:06 -08002041 """Typed version of namedtuple.
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002042
Guido van Rossum2f841442016-11-15 09:48:06 -08002043 Usage in Python versions >= 3.6::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002044
Guido van Rossum2f841442016-11-15 09:48:06 -08002045 class Employee(NamedTuple):
2046 name: str
2047 id: int
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002048
Guido van Rossum2f841442016-11-15 09:48:06 -08002049 This is equivalent to::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002050
Guido van Rossum2f841442016-11-15 09:48:06 -08002051 Employee = collections.namedtuple('Employee', ['name', 'id'])
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002052
Raymond Hettingerf7b57df2019-03-18 09:53:56 -07002053 The resulting class has an extra __annotations__ attribute, giving a
2054 dict that maps field names to types. (The field names are also in
2055 the _fields attribute, which is part of the namedtuple API.)
2056 Alternative equivalent keyword syntax is also accepted::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002057
Guido van Rossum2f841442016-11-15 09:48:06 -08002058 Employee = NamedTuple('Employee', name=str, id=int)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002059
Guido van Rossum2f841442016-11-15 09:48:06 -08002060 In Python versions <= 3.5 use::
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002061
Guido van Rossum2f841442016-11-15 09:48:06 -08002062 Employee = NamedTuple('Employee', [('name', str), ('id', int)])
2063 """
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002064 if fields is None:
2065 fields = kwargs.items()
2066 elif kwargs:
2067 raise TypeError("Either list of fields or keywords"
2068 " can be provided to NamedTuple, not both")
2069 try:
2070 module = sys._getframe(1).f_globals.get('__name__', '__main__')
2071 except (AttributeError, ValueError):
2072 module = None
2073 return _make_nmtuple(typename, fields, module=module)
Guido van Rossum0a6976d2016-09-11 15:34:56 -07002074
Serhiy Storchakaa2ec0692020-04-08 10:59:04 +03002075_NamedTuple = type.__new__(NamedTupleMeta, 'NamedTuple', (), {})
2076
2077def _namedtuple_mro_entries(bases):
2078 if len(bases) > 1:
2079 raise TypeError("Multiple inheritance with NamedTuple is not supported")
2080 assert bases[0] is NamedTuple
2081 return (_NamedTuple,)
2082
2083NamedTuple.__mro_entries__ = _namedtuple_mro_entries
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002084
2085
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002086class _TypedDictMeta(type):
2087 def __new__(cls, name, bases, ns, total=True):
2088 """Create new typed dict class object.
2089
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002090 This method is called when TypedDict is subclassed,
2091 or when TypedDict is instantiated. This way
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002092 TypedDict supports all three syntax forms described in its docstring.
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002093 Subclasses and instances of TypedDict return actual dictionaries.
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002094 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002095 for base in bases:
2096 if type(base) is not _TypedDictMeta:
2097 raise TypeError('cannot inherit from both a TypedDict type '
2098 'and a non-TypedDict base class')
2099 tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002100
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002101 annotations = {}
2102 own_annotations = ns.get('__annotations__', {})
2103 own_annotation_keys = set(own_annotations.keys())
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002104 msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002105 own_annotations = {
2106 n: _type_check(tp, msg) for n, tp in own_annotations.items()
2107 }
2108 required_keys = set()
2109 optional_keys = set()
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002110
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002111 for base in bases:
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002112 annotations.update(base.__dict__.get('__annotations__', {}))
2113 required_keys.update(base.__dict__.get('__required_keys__', ()))
2114 optional_keys.update(base.__dict__.get('__optional_keys__', ()))
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002115
Vlad Emelianov10e87e52020-02-13 20:53:29 +01002116 annotations.update(own_annotations)
2117 if total:
2118 required_keys.update(own_annotation_keys)
2119 else:
2120 optional_keys.update(own_annotation_keys)
2121
2122 tp_dict.__annotations__ = annotations
2123 tp_dict.__required_keys__ = frozenset(required_keys)
2124 tp_dict.__optional_keys__ = frozenset(optional_keys)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002125 if not hasattr(tp_dict, '__total__'):
2126 tp_dict.__total__ = total
2127 return tp_dict
2128
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002129 __call__ = dict # static method
2130
2131 def __subclasscheck__(cls, other):
2132 # Typed dicts are only for static structural subtyping.
2133 raise TypeError('TypedDict does not support instance and class checks')
2134
2135 __instancecheck__ = __subclasscheck__
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002136
2137
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002138def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002139 """A simple typed namespace. At runtime it is equivalent to a plain dict.
2140
2141 TypedDict creates a dictionary type that expects all of its
2142 instances to have a certain set of keys, where each key is
2143 associated with a value of a consistent type. This expectation
2144 is not checked at runtime but is only enforced by type checkers.
2145 Usage::
2146
2147 class Point2D(TypedDict):
2148 x: int
2149 y: int
2150 label: str
2151
2152 a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
2153 b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
2154
2155 assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
2156
Zac Hatfield-Dodds665ad3d2019-11-24 21:48:48 +11002157 The type info can be accessed via the Point2D.__annotations__ dict, and
2158 the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
2159 TypedDict supports two additional equivalent forms::
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002160
2161 Point2D = TypedDict('Point2D', x=int, y=int, label=str)
2162 Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
2163
ananthan-123ab6423f2020-02-19 10:03:05 +05302164 By default, all keys must be present in a TypedDict. It is possible
2165 to override this by specifying totality.
2166 Usage::
2167
2168 class point2D(TypedDict, total=False):
2169 x: int
2170 y: int
2171
2172 This means that a point2D TypedDict can have any of the keys omitted.A type
2173 checker is only expected to support a literal False or True as the value of
2174 the total argument. True is the default, and makes all items defined in the
2175 class body be required.
2176
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002177 The class syntax is only supported in Python 3.6+, while two other
2178 syntax forms work for Python 2.7 and 3.2+
2179 """
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002180 if fields is None:
2181 fields = kwargs
2182 elif kwargs:
2183 raise TypeError("TypedDict takes either a dict or keyword arguments,"
2184 " but not both")
2185
Alex Grönholm67b769f2020-12-10 23:49:05 +02002186 ns = {'__annotations__': dict(fields)}
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002187 try:
2188 # Setting correct module is necessary to make typed dict classes pickleable.
2189 ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
2190 except (AttributeError, ValueError):
2191 pass
2192
Alex Grönholm67b769f2020-12-10 23:49:05 +02002193 return _TypedDictMeta(typename, (), ns, total=total)
Serhiy Storchakaf228bf22020-04-08 11:03:27 +03002194
2195_TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
2196TypedDict.__mro_entries__ = lambda bases: (_TypedDict,)
Ivan Levkivskyi135c6a52019-05-26 09:39:24 +01002197
2198
Guido van Rossum91185fe2016-06-08 11:19:11 -07002199def NewType(name, tp):
2200 """NewType creates simple unique types with almost zero
2201 runtime overhead. NewType(name, tp) is considered a subtype of tp
2202 by static type checkers. At runtime, NewType(name, tp) returns
2203 a dummy function that simply returns its argument. Usage::
2204
2205 UserId = NewType('UserId', int)
2206
2207 def name_by_id(user_id: UserId) -> str:
2208 ...
2209
2210 UserId('user') # Fails type check
2211
2212 name_by_id(42) # Fails type check
2213 name_by_id(UserId(42)) # OK
2214
2215 num = UserId(5) + 1 # type: int
2216 """
2217
2218 def new_type(x):
2219 return x
2220
2221 new_type.__name__ = name
2222 new_type.__supertype__ = tp
2223 return new_type
2224
2225
Guido van Rossum0e0563c2016-04-05 14:54:25 -07002226# Python-version-specific alias (Python 2: unicode; Python 3: str)
2227Text = str
2228
2229
Guido van Rossum91185fe2016-06-08 11:19:11 -07002230# Constant that's True when type checking, but False here.
2231TYPE_CHECKING = False
2232
2233
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002234class IO(Generic[AnyStr]):
2235 """Generic base class for TextIO and BinaryIO.
2236
2237 This is an abstract, generic version of the return of open().
2238
2239 NOTE: This does not distinguish between the different possible
2240 classes (text vs. binary, read vs. write vs. read/write,
2241 append-only, unbuffered). The TextIO and BinaryIO subclasses
2242 below capture the distinctions between text vs. binary, which is
2243 pervasive in the interface; however we currently do not offer a
2244 way to track the other distinctions in the type system.
2245 """
2246
Guido van Rossumd70fe632015-08-05 12:11:06 +02002247 __slots__ = ()
2248
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002249 @property
2250 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002251 def mode(self) -> str:
2252 pass
2253
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002254 @property
2255 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002256 def name(self) -> str:
2257 pass
2258
2259 @abstractmethod
2260 def close(self) -> None:
2261 pass
2262
Shantanu2e6569b2020-01-29 18:52:36 -08002263 @property
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002264 @abstractmethod
2265 def closed(self) -> bool:
2266 pass
2267
2268 @abstractmethod
2269 def fileno(self) -> int:
2270 pass
2271
2272 @abstractmethod
2273 def flush(self) -> None:
2274 pass
2275
2276 @abstractmethod
2277 def isatty(self) -> bool:
2278 pass
2279
2280 @abstractmethod
2281 def read(self, n: int = -1) -> AnyStr:
2282 pass
2283
2284 @abstractmethod
2285 def readable(self) -> bool:
2286 pass
2287
2288 @abstractmethod
2289 def readline(self, limit: int = -1) -> AnyStr:
2290 pass
2291
2292 @abstractmethod
2293 def readlines(self, hint: int = -1) -> List[AnyStr]:
2294 pass
2295
2296 @abstractmethod
2297 def seek(self, offset: int, whence: int = 0) -> int:
2298 pass
2299
2300 @abstractmethod
2301 def seekable(self) -> bool:
2302 pass
2303
2304 @abstractmethod
2305 def tell(self) -> int:
2306 pass
2307
2308 @abstractmethod
2309 def truncate(self, size: int = None) -> int:
2310 pass
2311
2312 @abstractmethod
2313 def writable(self) -> bool:
2314 pass
2315
2316 @abstractmethod
2317 def write(self, s: AnyStr) -> int:
2318 pass
2319
2320 @abstractmethod
2321 def writelines(self, lines: List[AnyStr]) -> None:
2322 pass
2323
2324 @abstractmethod
2325 def __enter__(self) -> 'IO[AnyStr]':
2326 pass
2327
2328 @abstractmethod
2329 def __exit__(self, type, value, traceback) -> None:
2330 pass
2331
2332
2333class BinaryIO(IO[bytes]):
2334 """Typed version of the return of open() in binary mode."""
2335
Guido van Rossumd70fe632015-08-05 12:11:06 +02002336 __slots__ = ()
2337
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002338 @abstractmethod
2339 def write(self, s: Union[bytes, bytearray]) -> int:
2340 pass
2341
2342 @abstractmethod
2343 def __enter__(self) -> 'BinaryIO':
2344 pass
2345
2346
2347class TextIO(IO[str]):
2348 """Typed version of the return of open() in text mode."""
2349
Guido van Rossumd70fe632015-08-05 12:11:06 +02002350 __slots__ = ()
2351
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002352 @property
2353 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002354 def buffer(self) -> BinaryIO:
2355 pass
2356
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002357 @property
2358 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002359 def encoding(self) -> str:
2360 pass
2361
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002362 @property
2363 @abstractmethod
Guido van Rossum991d14f2016-11-09 13:12:51 -08002364 def errors(self) -> Optional[str]:
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002365 pass
2366
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002367 @property
2368 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002369 def line_buffering(self) -> bool:
2370 pass
2371
HongWeipeng6ce03ec2019-09-27 15:54:26 +08002372 @property
2373 @abstractmethod
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002374 def newlines(self) -> Any:
2375 pass
2376
2377 @abstractmethod
2378 def __enter__(self) -> 'TextIO':
2379 pass
2380
2381
2382class io:
2383 """Wrapper namespace for IO generic classes."""
2384
2385 __all__ = ['IO', 'TextIO', 'BinaryIO']
2386 IO = IO
2387 TextIO = TextIO
2388 BinaryIO = BinaryIO
2389
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002390
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002391io.__name__ = __name__ + '.io'
2392sys.modules[io.__name__] = io
2393
Serhiy Storchakafcb28562020-05-10 11:53:16 +03002394Pattern = _alias(stdlib_re.Pattern, 1)
2395Match = _alias(stdlib_re.Match, 1)
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002396
2397class re:
2398 """Wrapper namespace for re type aliases."""
2399
2400 __all__ = ['Pattern', 'Match']
2401 Pattern = Pattern
2402 Match = Match
2403
Guido van Rossumd7adfe12017-01-22 17:43:53 -08002404
Guido van Rossum46dbb7d2015-05-22 10:14:11 -07002405re.__name__ = __name__ + '.re'
2406sys.modules[re.__name__] = re