Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1 | import abc |
| 2 | from abc import abstractmethod, abstractproperty |
| 3 | import collections |
Brett Cannon | f3ad042 | 2016-04-15 10:51:30 -0700 | [diff] [blame] | 4 | import contextlib |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 5 | import functools |
| 6 | import re as stdlib_re # Avoid confusion with the re we export. |
| 7 | import sys |
| 8 | import types |
| 9 | try: |
| 10 | import collections.abc as collections_abc |
| 11 | except ImportError: |
| 12 | import collections as collections_abc # Fallback for PY3.2. |
Ivan Levkivskyi | 29fda8d | 2017-06-10 21:57:56 +0200 | [diff] [blame^] | 13 | if sys.version_info[:2] >= (3, 6): |
| 14 | import _collections_abc # Needed for private function _check_methods # noqa |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 15 | try: |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 16 | from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 17 | except ImportError: |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 18 | WrapperDescriptorType = type(object.__init__) |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 19 | MethodWrapperType = type(object().__str__) |
| 20 | MethodDescriptorType = type(str.join) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | # Please keep __all__ alphabetized within each category. |
| 24 | __all__ = [ |
| 25 | # Super-special typing primitives. |
| 26 | 'Any', |
| 27 | 'Callable', |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 28 | 'ClassVar', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 29 | 'Generic', |
| 30 | 'Optional', |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 31 | 'Tuple', |
| 32 | 'Type', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 33 | 'TypeVar', |
| 34 | 'Union', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 35 | |
| 36 | # ABCs (from collections.abc). |
| 37 | 'AbstractSet', # collections.abc.Set. |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 38 | 'GenericMeta', # subclass of abc.ABCMeta and a metaclass |
| 39 | # for 'Generic' and ABCs below. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 40 | 'ByteString', |
| 41 | 'Container', |
Ivan Levkivskyi | 29fda8d | 2017-06-10 21:57:56 +0200 | [diff] [blame^] | 42 | 'ContextManager', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 43 | 'Hashable', |
| 44 | 'ItemsView', |
| 45 | 'Iterable', |
| 46 | 'Iterator', |
| 47 | 'KeysView', |
| 48 | 'Mapping', |
| 49 | 'MappingView', |
| 50 | 'MutableMapping', |
| 51 | 'MutableSequence', |
| 52 | 'MutableSet', |
| 53 | 'Sequence', |
| 54 | 'Sized', |
| 55 | 'ValuesView', |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 56 | # The following are added depending on presence |
| 57 | # of their non-generic counterparts in stdlib: |
| 58 | # Awaitable, |
| 59 | # AsyncIterator, |
| 60 | # AsyncIterable, |
| 61 | # Coroutine, |
| 62 | # Collection, |
Guido van Rossum | e9ed560 | 2017-01-18 13:10:31 -0800 | [diff] [blame] | 63 | # AsyncGenerator, |
Ivan Levkivskyi | 29fda8d | 2017-06-10 21:57:56 +0200 | [diff] [blame^] | 64 | # AsyncContextManager |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 65 | |
| 66 | # Structural checks, a.k.a. protocols. |
| 67 | 'Reversible', |
| 68 | 'SupportsAbs', |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 69 | 'SupportsBytes', |
| 70 | 'SupportsComplex', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 71 | 'SupportsFloat', |
| 72 | 'SupportsInt', |
| 73 | 'SupportsRound', |
| 74 | |
| 75 | # Concrete collection types. |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 76 | 'Counter', |
Raymond Hettinger | 8049052 | 2017-01-16 22:42:37 -0800 | [diff] [blame] | 77 | 'Deque', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 78 | 'Dict', |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 79 | 'DefaultDict', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 80 | 'List', |
| 81 | 'Set', |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 82 | 'FrozenSet', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 83 | 'NamedTuple', # Not really a type. |
| 84 | 'Generator', |
| 85 | |
| 86 | # One-off things. |
| 87 | 'AnyStr', |
| 88 | 'cast', |
| 89 | 'get_type_hints', |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 90 | 'NewType', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 91 | 'no_type_check', |
| 92 | 'no_type_check_decorator', |
| 93 | 'overload', |
Guido van Rossum | 0e0563c | 2016-04-05 14:54:25 -0700 | [diff] [blame] | 94 | 'Text', |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 95 | 'TYPE_CHECKING', |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 96 | ] |
| 97 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 98 | # The pseudo-submodules 're' and 'io' are part of the public |
| 99 | # namespace, but excluded from __all__ because they might stomp on |
| 100 | # legitimate imports of those modules. |
| 101 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 102 | |
| 103 | def _qualname(x): |
| 104 | if sys.version_info[:2] >= (3, 3): |
| 105 | return x.__qualname__ |
| 106 | else: |
| 107 | # Fall back to just name. |
| 108 | return x.__name__ |
| 109 | |
| 110 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 111 | def _trim_name(nm): |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 112 | whitelist = ('_TypeAlias', '_ForwardRef', '_TypingBase', '_FinalTypingBase') |
| 113 | if nm.startswith('_') and nm not in whitelist: |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 114 | nm = nm[1:] |
| 115 | return nm |
| 116 | |
| 117 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 118 | class TypingMeta(type): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 119 | """Metaclass for most types defined in typing module |
| 120 | (not a part of public API). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 121 | |
| 122 | This overrides __new__() to require an extra keyword parameter |
| 123 | '_root', which serves as a guard against naive subclassing of the |
| 124 | typing classes. Any legitimate class defined using a metaclass |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 125 | derived from TypingMeta must pass _root=True. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 126 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 127 | This also defines a dummy constructor (all the work for most typing |
| 128 | constructs is done in __new__) and a nicer repr(). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 129 | """ |
| 130 | |
| 131 | _is_protocol = False |
| 132 | |
| 133 | def __new__(cls, name, bases, namespace, *, _root=False): |
| 134 | if not _root: |
| 135 | raise TypeError("Cannot subclass %s" % |
| 136 | (', '.join(map(_type_repr, bases)) or '()')) |
| 137 | return super().__new__(cls, name, bases, namespace) |
| 138 | |
| 139 | def __init__(self, *args, **kwds): |
| 140 | pass |
| 141 | |
| 142 | def _eval_type(self, globalns, localns): |
| 143 | """Override this in subclasses to interpret forward references. |
| 144 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 145 | For example, List['C'] is internally stored as |
| 146 | List[_ForwardRef('C')], which should evaluate to List[C], |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 147 | where C is an object found in globalns or localns (searching |
| 148 | localns first, of course). |
| 149 | """ |
| 150 | return self |
| 151 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 152 | def _get_type_vars(self, tvars): |
| 153 | pass |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 154 | |
| 155 | def __repr__(self): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 156 | qname = _trim_name(_qualname(self)) |
| 157 | return '%s.%s' % (self.__module__, qname) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 158 | |
| 159 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 160 | class _TypingBase(metaclass=TypingMeta, _root=True): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 161 | """Internal indicator of special typing constructs.""" |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 162 | |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 163 | __slots__ = ('__weakref__',) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 164 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 165 | def __init__(self, *args, **kwds): |
| 166 | pass |
| 167 | |
| 168 | def __new__(cls, *args, **kwds): |
| 169 | """Constructor. |
| 170 | |
| 171 | This only exists to give a better error message in case |
| 172 | someone tries to subclass a special typing object (not a good idea). |
| 173 | """ |
| 174 | if (len(args) == 3 and |
| 175 | isinstance(args[0], str) and |
| 176 | isinstance(args[1], tuple)): |
| 177 | # Close enough. |
| 178 | raise TypeError("Cannot subclass %r" % cls) |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 179 | return super().__new__(cls) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 180 | |
| 181 | # Things that are not classes also need these. |
| 182 | def _eval_type(self, globalns, localns): |
| 183 | return self |
| 184 | |
| 185 | def _get_type_vars(self, tvars): |
| 186 | pass |
| 187 | |
| 188 | def __repr__(self): |
| 189 | cls = type(self) |
| 190 | qname = _trim_name(_qualname(cls)) |
| 191 | return '%s.%s' % (cls.__module__, qname) |
| 192 | |
| 193 | def __call__(self, *args, **kwds): |
| 194 | raise TypeError("Cannot instantiate %r" % type(self)) |
| 195 | |
| 196 | |
| 197 | class _FinalTypingBase(_TypingBase, _root=True): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 198 | """Internal mix-in class to prevent instantiation. |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 199 | |
| 200 | Prevents instantiation unless _root=True is given in class call. |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 201 | It is used to create pseudo-singleton instances Any, Union, Optional, etc. |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 202 | """ |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 203 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 204 | __slots__ = () |
| 205 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 206 | def __new__(cls, *args, _root=False, **kwds): |
| 207 | self = super().__new__(cls, *args, **kwds) |
| 208 | if _root is True: |
| 209 | return self |
| 210 | raise TypeError("Cannot instantiate %r" % cls) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 211 | |
Guido van Rossum | b7dedc8 | 2016-10-29 12:44:29 -0700 | [diff] [blame] | 212 | def __reduce__(self): |
| 213 | return _trim_name(type(self).__name__) |
| 214 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 215 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 216 | class _ForwardRef(_TypingBase, _root=True): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 217 | """Internal wrapper to hold a forward reference.""" |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 218 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 219 | __slots__ = ('__forward_arg__', '__forward_code__', |
Guido van Rossum | c7b9295 | 2016-11-10 08:24:06 -0800 | [diff] [blame] | 220 | '__forward_evaluated__', '__forward_value__') |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 221 | |
| 222 | def __init__(self, arg): |
| 223 | super().__init__(arg) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 224 | if not isinstance(arg, str): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 225 | raise TypeError('Forward reference must be a string -- got %r' % (arg,)) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 226 | try: |
| 227 | code = compile(arg, '<string>', 'eval') |
| 228 | except SyntaxError: |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 229 | raise SyntaxError('Forward reference must be an expression -- got %r' % |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 230 | (arg,)) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 231 | self.__forward_arg__ = arg |
| 232 | self.__forward_code__ = code |
| 233 | self.__forward_evaluated__ = False |
| 234 | self.__forward_value__ = None |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 235 | |
| 236 | def _eval_type(self, globalns, localns): |
Guido van Rossum | dad1790 | 2016-11-10 08:29:18 -0800 | [diff] [blame] | 237 | if not self.__forward_evaluated__ or localns is not globalns: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 238 | if globalns is None and localns is None: |
| 239 | globalns = localns = {} |
| 240 | elif globalns is None: |
| 241 | globalns = localns |
| 242 | elif localns is None: |
| 243 | localns = globalns |
| 244 | self.__forward_value__ = _type_check( |
| 245 | eval(self.__forward_code__, globalns, localns), |
| 246 | "Forward references must evaluate to types.") |
| 247 | self.__forward_evaluated__ = True |
| 248 | return self.__forward_value__ |
| 249 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 250 | def __eq__(self, other): |
| 251 | if not isinstance(other, _ForwardRef): |
| 252 | return NotImplemented |
| 253 | return (self.__forward_arg__ == other.__forward_arg__ and |
Guido van Rossum | c7b9295 | 2016-11-10 08:24:06 -0800 | [diff] [blame] | 254 | self.__forward_value__ == other.__forward_value__) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 255 | |
| 256 | def __hash__(self): |
Guido van Rossum | c7b9295 | 2016-11-10 08:24:06 -0800 | [diff] [blame] | 257 | return hash((self.__forward_arg__, self.__forward_value__)) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 258 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 259 | def __instancecheck__(self, obj): |
| 260 | raise TypeError("Forward references cannot be used with isinstance().") |
| 261 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 262 | def __subclasscheck__(self, cls): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 263 | raise TypeError("Forward references cannot be used with issubclass().") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 264 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 265 | def __repr__(self): |
| 266 | return '_ForwardRef(%r)' % (self.__forward_arg__,) |
| 267 | |
| 268 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 269 | class _TypeAlias(_TypingBase, _root=True): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 270 | """Internal helper class for defining generic variants of concrete types. |
| 271 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 272 | Note that this is not a type; let's call it a pseudo-type. It cannot |
| 273 | be used in instance and subclass checks in parameterized form, i.e. |
| 274 | ``isinstance(42, Match[str])`` raises ``TypeError`` instead of returning |
| 275 | ``False``. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 276 | """ |
| 277 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 278 | __slots__ = ('name', 'type_var', 'impl_type', 'type_checker') |
| 279 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 280 | def __init__(self, name, type_var, impl_type, type_checker): |
| 281 | """Initializer. |
| 282 | |
| 283 | Args: |
| 284 | name: The name, e.g. 'Pattern'. |
| 285 | type_var: The type parameter, e.g. AnyStr, or the |
| 286 | specific type, e.g. str. |
| 287 | impl_type: The implementation type. |
| 288 | type_checker: Function that takes an impl_type instance. |
| 289 | and returns a value that should be a type_var instance. |
| 290 | """ |
| 291 | assert isinstance(name, str), repr(name) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 292 | assert isinstance(impl_type, type), repr(impl_type) |
| 293 | assert not isinstance(impl_type, TypingMeta), repr(impl_type) |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 294 | assert isinstance(type_var, (type, _TypingBase)), repr(type_var) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 295 | self.name = name |
| 296 | self.type_var = type_var |
| 297 | self.impl_type = impl_type |
| 298 | self.type_checker = type_checker |
| 299 | |
| 300 | def __repr__(self): |
| 301 | return "%s[%s]" % (self.name, _type_repr(self.type_var)) |
| 302 | |
| 303 | def __getitem__(self, parameter): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 304 | if not isinstance(self.type_var, TypeVar): |
| 305 | raise TypeError("%s cannot be further parameterized." % self) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 306 | if self.type_var.__constraints__ and isinstance(parameter, type): |
| 307 | if not issubclass(parameter, self.type_var.__constraints__): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 308 | raise TypeError("%s is not a valid substitution for %s." % |
| 309 | (parameter, self.type_var)) |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 310 | if isinstance(parameter, TypeVar) and parameter is not self.type_var: |
| 311 | raise TypeError("%s cannot be re-parameterized." % self) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 312 | return self.__class__(self.name, parameter, |
| 313 | self.impl_type, self.type_checker) |
| 314 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 315 | def __eq__(self, other): |
| 316 | if not isinstance(other, _TypeAlias): |
| 317 | return NotImplemented |
| 318 | return self.name == other.name and self.type_var == other.type_var |
| 319 | |
| 320 | def __hash__(self): |
| 321 | return hash((self.name, self.type_var)) |
| 322 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 323 | def __instancecheck__(self, obj): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 324 | if not isinstance(self.type_var, TypeVar): |
| 325 | raise TypeError("Parameterized type aliases cannot be used " |
| 326 | "with isinstance().") |
| 327 | return isinstance(obj, self.impl_type) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 328 | |
| 329 | def __subclasscheck__(self, cls): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 330 | if not isinstance(self.type_var, TypeVar): |
| 331 | raise TypeError("Parameterized type aliases cannot be used " |
| 332 | "with issubclass().") |
| 333 | return issubclass(cls, self.impl_type) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 334 | |
| 335 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 336 | def _get_type_vars(types, tvars): |
| 337 | for t in types: |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 338 | if isinstance(t, TypingMeta) or isinstance(t, _TypingBase): |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 339 | t._get_type_vars(tvars) |
| 340 | |
| 341 | |
| 342 | def _type_vars(types): |
| 343 | tvars = [] |
| 344 | _get_type_vars(types, tvars) |
| 345 | return tuple(tvars) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 346 | |
| 347 | |
| 348 | def _eval_type(t, globalns, localns): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 349 | if isinstance(t, TypingMeta) or isinstance(t, _TypingBase): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 350 | return t._eval_type(globalns, localns) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 351 | return t |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 352 | |
| 353 | |
| 354 | def _type_check(arg, msg): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 355 | """Check that the argument is a type, and return it (internal helper). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 356 | |
| 357 | As a special case, accept None and return type(None) instead. |
| 358 | Also, _TypeAlias instances (e.g. Match, Pattern) are acceptable. |
| 359 | |
| 360 | The msg argument is a human-readable error message, e.g. |
| 361 | |
| 362 | "Union[arg, ...]: arg should be a type." |
| 363 | |
| 364 | We append the repr() of the actual value (truncated to 100 chars). |
| 365 | """ |
| 366 | if arg is None: |
| 367 | return type(None) |
| 368 | if isinstance(arg, str): |
| 369 | arg = _ForwardRef(arg) |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 370 | if ( |
| 371 | isinstance(arg, _TypingBase) and type(arg).__name__ == '_ClassVar' or |
| 372 | not isinstance(arg, (type, _TypingBase)) and not callable(arg) |
| 373 | ): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 374 | raise TypeError(msg + " Got %.100r." % (arg,)) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 375 | # Bare Union etc. are not valid as type arguments |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 376 | if ( |
| 377 | type(arg).__name__ in ('_Union', '_Optional') and |
| 378 | not getattr(arg, '__origin__', None) or |
| 379 | isinstance(arg, TypingMeta) and _gorg(arg) in (Generic, _Protocol) |
| 380 | ): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 381 | raise TypeError("Plain %s is not valid as type argument" % arg) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 382 | return arg |
| 383 | |
| 384 | |
| 385 | def _type_repr(obj): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 386 | """Return the repr() of an object, special-casing types (internal helper). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 387 | |
| 388 | If obj is a type, we return a shorter version than the default |
| 389 | type.__repr__, based on the module and qualified name, which is |
| 390 | typically enough to uniquely identify a type. For everything |
| 391 | else, we fall back on repr(obj). |
| 392 | """ |
| 393 | if isinstance(obj, type) and not isinstance(obj, TypingMeta): |
| 394 | if obj.__module__ == 'builtins': |
| 395 | return _qualname(obj) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 396 | return '%s.%s' % (obj.__module__, _qualname(obj)) |
| 397 | if obj is ...: |
| 398 | return('...') |
| 399 | if isinstance(obj, types.FunctionType): |
| 400 | return obj.__name__ |
| 401 | return repr(obj) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 402 | |
| 403 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 404 | class _Any(_FinalTypingBase, _root=True): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 405 | """Special type indicating an unconstrained type. |
| 406 | |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 407 | - Any is compatible with every type. |
| 408 | - Any assumed to have all methods. |
| 409 | - All values assumed to be instances of Any. |
| 410 | |
| 411 | Note that all the above statements are true from the point of view of |
| 412 | static type checkers. At runtime, Any should not be used with instance |
| 413 | or class checks. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 414 | """ |
| 415 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 416 | __slots__ = () |
| 417 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 418 | def __instancecheck__(self, obj): |
| 419 | raise TypeError("Any cannot be used with isinstance().") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 420 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 421 | def __subclasscheck__(self, cls): |
| 422 | raise TypeError("Any cannot be used with issubclass().") |
| 423 | |
| 424 | |
| 425 | Any = _Any(_root=True) |
| 426 | |
| 427 | |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 428 | class _NoReturn(_FinalTypingBase, _root=True): |
| 429 | """Special type indicating functions that never return. |
| 430 | Example:: |
| 431 | |
| 432 | from typing import NoReturn |
| 433 | |
| 434 | def stop() -> NoReturn: |
| 435 | raise Exception('no way') |
| 436 | |
| 437 | This type is invalid in other positions, e.g., ``List[NoReturn]`` |
| 438 | will fail in static type checkers. |
| 439 | """ |
| 440 | |
| 441 | __slots__ = () |
| 442 | |
| 443 | def __instancecheck__(self, obj): |
| 444 | raise TypeError("NoReturn cannot be used with isinstance().") |
| 445 | |
| 446 | def __subclasscheck__(self, cls): |
| 447 | raise TypeError("NoReturn cannot be used with issubclass().") |
| 448 | |
| 449 | |
| 450 | NoReturn = _NoReturn(_root=True) |
| 451 | |
| 452 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 453 | class TypeVar(_TypingBase, _root=True): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 454 | """Type variable. |
| 455 | |
| 456 | Usage:: |
| 457 | |
| 458 | T = TypeVar('T') # Can be anything |
| 459 | A = TypeVar('A', str, bytes) # Must be str or bytes |
| 460 | |
| 461 | Type variables exist primarily for the benefit of static type |
| 462 | checkers. They serve as the parameters for generic types as well |
| 463 | as for generic function definitions. See class Generic for more |
| 464 | information on generic types. Generic functions work as follows: |
| 465 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 466 | def repeat(x: T, n: int) -> List[T]: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 467 | '''Return a list containing n references to x.''' |
| 468 | return [x]*n |
| 469 | |
| 470 | def longest(x: A, y: A) -> A: |
| 471 | '''Return the longest of two strings.''' |
| 472 | return x if len(x) >= len(y) else y |
| 473 | |
| 474 | The latter example's signature is essentially the overloading |
| 475 | of (str, str) -> str and (bytes, bytes) -> bytes. Also note |
| 476 | that if the arguments are instances of some subclass of str, |
| 477 | the return type is still plain str. |
| 478 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 479 | At runtime, isinstance(x, T) and issubclass(C, T) will raise TypeError. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 480 | |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 481 | Type variables defined with covariant=True or contravariant=True |
| 482 | can be used do declare covariant or contravariant generic types. |
| 483 | See PEP 484 for more details. By default generic types are invariant |
| 484 | in all type variables. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 485 | |
| 486 | Type variables can be introspected. e.g.: |
| 487 | |
| 488 | T.__name__ == 'T' |
| 489 | T.__constraints__ == () |
| 490 | T.__covariant__ == False |
| 491 | T.__contravariant__ = False |
| 492 | A.__constraints__ == (str, bytes) |
| 493 | """ |
| 494 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 495 | __slots__ = ('__name__', '__bound__', '__constraints__', |
| 496 | '__covariant__', '__contravariant__') |
| 497 | |
| 498 | def __init__(self, name, *constraints, bound=None, |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 499 | covariant=False, contravariant=False): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 500 | super().__init__(name, *constraints, bound=bound, |
| 501 | covariant=covariant, contravariant=contravariant) |
| 502 | self.__name__ = name |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 503 | if covariant and contravariant: |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 504 | raise ValueError("Bivariant types are not supported.") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 505 | self.__covariant__ = bool(covariant) |
| 506 | self.__contravariant__ = bool(contravariant) |
| 507 | if constraints and bound is not None: |
| 508 | raise TypeError("Constraints cannot be combined with bound=...") |
| 509 | if constraints and len(constraints) == 1: |
| 510 | raise TypeError("A single constraint is not allowed") |
| 511 | msg = "TypeVar(name, constraint, ...): constraints must be types." |
| 512 | self.__constraints__ = tuple(_type_check(t, msg) for t in constraints) |
| 513 | if bound: |
| 514 | self.__bound__ = _type_check(bound, "Bound must be a type.") |
| 515 | else: |
| 516 | self.__bound__ = None |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 517 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 518 | def _get_type_vars(self, tvars): |
| 519 | if self not in tvars: |
| 520 | tvars.append(self) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 521 | |
| 522 | def __repr__(self): |
| 523 | if self.__covariant__: |
| 524 | prefix = '+' |
| 525 | elif self.__contravariant__: |
| 526 | prefix = '-' |
| 527 | else: |
| 528 | prefix = '~' |
| 529 | return prefix + self.__name__ |
| 530 | |
| 531 | def __instancecheck__(self, instance): |
| 532 | raise TypeError("Type variables cannot be used with isinstance().") |
| 533 | |
| 534 | def __subclasscheck__(self, cls): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 535 | raise TypeError("Type variables cannot be used with issubclass().") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 536 | |
| 537 | |
| 538 | # Some unconstrained type variables. These are used by the container types. |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 539 | # (These are not for export.) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 540 | T = TypeVar('T') # Any type. |
| 541 | KT = TypeVar('KT') # Key type. |
| 542 | VT = TypeVar('VT') # Value type. |
| 543 | T_co = TypeVar('T_co', covariant=True) # Any type covariant containers. |
| 544 | V_co = TypeVar('V_co', covariant=True) # Any type covariant containers. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 545 | VT_co = TypeVar('VT_co', covariant=True) # Value type covariant containers. |
| 546 | T_contra = TypeVar('T_contra', contravariant=True) # Ditto contravariant. |
| 547 | |
| 548 | # A useful type variable with constraints. This represents string types. |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 549 | # (This one *is* for export!) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 550 | AnyStr = TypeVar('AnyStr', bytes, str) |
| 551 | |
| 552 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 553 | def _replace_arg(arg, tvars, args): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 554 | """An internal helper function: replace arg if it is a type variable |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 555 | found in tvars with corresponding substitution from args or |
| 556 | with corresponding substitution sub-tree if arg is a generic type. |
| 557 | """ |
| 558 | |
| 559 | if tvars is None: |
| 560 | tvars = [] |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 561 | if hasattr(arg, '_subs_tree') and isinstance(arg, (GenericMeta, _TypingBase)): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 562 | return arg._subs_tree(tvars, args) |
| 563 | if isinstance(arg, TypeVar): |
| 564 | for i, tvar in enumerate(tvars): |
| 565 | if arg == tvar: |
| 566 | return args[i] |
| 567 | return arg |
| 568 | |
| 569 | |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 570 | # Special typing constructs Union, Optional, Generic, Callable and Tuple |
| 571 | # use three special attributes for internal bookkeeping of generic types: |
| 572 | # * __parameters__ is a tuple of unique free type parameters of a generic |
| 573 | # type, for example, Dict[T, T].__parameters__ == (T,); |
| 574 | # * __origin__ keeps a reference to a type that was subscripted, |
| 575 | # e.g., Union[T, int].__origin__ == Union; |
| 576 | # * __args__ is a tuple of all arguments used in subscripting, |
| 577 | # e.g., Dict[T, int].__args__ == (T, int). |
| 578 | |
| 579 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 580 | def _subs_tree(cls, tvars=None, args=None): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 581 | """An internal helper function: calculate substitution tree |
| 582 | for generic cls after replacing its type parameters with |
| 583 | substitutions in tvars -> args (if any). |
| 584 | Repeat the same following __origin__'s. |
| 585 | |
| 586 | Return a list of arguments with all possible substitutions |
| 587 | performed. Arguments that are generic classes themselves are represented |
| 588 | as tuples (so that no new classes are created by this function). |
| 589 | For example: _subs_tree(List[Tuple[int, T]][str]) == [(Tuple, int, str)] |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 590 | """ |
| 591 | |
| 592 | if cls.__origin__ is None: |
| 593 | return cls |
| 594 | # Make of chain of origins (i.e. cls -> cls.__origin__) |
| 595 | current = cls.__origin__ |
| 596 | orig_chain = [] |
| 597 | while current.__origin__ is not None: |
| 598 | orig_chain.append(current) |
| 599 | current = current.__origin__ |
| 600 | # Replace type variables in __args__ if asked ... |
| 601 | tree_args = [] |
| 602 | for arg in cls.__args__: |
| 603 | tree_args.append(_replace_arg(arg, tvars, args)) |
| 604 | # ... then continue replacing down the origin chain. |
| 605 | for ocls in orig_chain: |
| 606 | new_tree_args = [] |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 607 | for arg in ocls.__args__: |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 608 | new_tree_args.append(_replace_arg(arg, ocls.__parameters__, tree_args)) |
| 609 | tree_args = new_tree_args |
| 610 | return tree_args |
| 611 | |
| 612 | |
| 613 | def _remove_dups_flatten(parameters): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 614 | """An internal helper for Union creation and substitution: flatten Union's |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 615 | among parameters, then remove duplicates and strict subclasses. |
| 616 | """ |
| 617 | |
| 618 | # Flatten out Union[Union[...], ...]. |
| 619 | params = [] |
| 620 | for p in parameters: |
| 621 | if isinstance(p, _Union) and p.__origin__ is Union: |
| 622 | params.extend(p.__args__) |
| 623 | elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union: |
| 624 | params.extend(p[1:]) |
| 625 | else: |
| 626 | params.append(p) |
| 627 | # Weed out strict duplicates, preserving the first of each occurrence. |
| 628 | all_params = set(params) |
| 629 | if len(all_params) < len(params): |
| 630 | new_params = [] |
| 631 | for t in params: |
| 632 | if t in all_params: |
| 633 | new_params.append(t) |
| 634 | all_params.remove(t) |
| 635 | params = new_params |
| 636 | assert not all_params, all_params |
| 637 | # Weed out subclasses. |
| 638 | # E.g. Union[int, Employee, Manager] == Union[int, Employee]. |
| 639 | # If object is present it will be sole survivor among proper classes. |
| 640 | # Never discard type variables. |
| 641 | # (In particular, Union[str, AnyStr] != AnyStr.) |
| 642 | all_params = set(params) |
| 643 | for t1 in params: |
| 644 | if not isinstance(t1, type): |
| 645 | continue |
| 646 | if any(isinstance(t2, type) and issubclass(t1, t2) |
| 647 | for t2 in all_params - {t1} |
| 648 | if not (isinstance(t2, GenericMeta) and |
| 649 | t2.__origin__ is not None)): |
| 650 | all_params.remove(t1) |
| 651 | return tuple(t for t in params if t in all_params) |
| 652 | |
| 653 | |
| 654 | def _check_generic(cls, parameters): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 655 | # Check correct count for parameters of a generic cls (internal helper). |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 656 | if not cls.__parameters__: |
| 657 | raise TypeError("%s is not a generic class" % repr(cls)) |
| 658 | alen = len(parameters) |
| 659 | elen = len(cls.__parameters__) |
| 660 | if alen != elen: |
| 661 | raise TypeError("Too %s parameters for %s; actual %s, expected %s" % |
| 662 | ("many" if alen > elen else "few", repr(cls), alen, elen)) |
| 663 | |
| 664 | |
Guido van Rossum | 9b10756 | 2016-11-09 13:23:04 -0800 | [diff] [blame] | 665 | _cleanups = [] |
| 666 | |
| 667 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 668 | def _tp_cache(func): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 669 | """Internal wrapper caching __getitem__ of generic types with a fallback to |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 670 | original function for non-hashable arguments. |
| 671 | """ |
| 672 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 673 | cached = functools.lru_cache()(func) |
Guido van Rossum | 9b10756 | 2016-11-09 13:23:04 -0800 | [diff] [blame] | 674 | _cleanups.append(cached.cache_clear) |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 675 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 676 | @functools.wraps(func) |
| 677 | def inner(*args, **kwds): |
| 678 | try: |
| 679 | return cached(*args, **kwds) |
| 680 | except TypeError: |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 681 | pass # All real errors (not unhashable args) are raised below. |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 682 | return func(*args, **kwds) |
| 683 | return inner |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 684 | |
| 685 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 686 | class _Union(_FinalTypingBase, _root=True): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 687 | """Union type; Union[X, Y] means either X or Y. |
| 688 | |
| 689 | To define a union, use e.g. Union[int, str]. Details: |
| 690 | |
| 691 | - The arguments must be types and there must be at least one. |
| 692 | |
| 693 | - None as an argument is a special case and is replaced by |
| 694 | type(None). |
| 695 | |
| 696 | - Unions of unions are flattened, e.g.:: |
| 697 | |
| 698 | Union[Union[int, str], float] == Union[int, str, float] |
| 699 | |
| 700 | - Unions of a single argument vanish, e.g.:: |
| 701 | |
| 702 | Union[int] == int # The constructor actually returns int |
| 703 | |
| 704 | - Redundant arguments are skipped, e.g.:: |
| 705 | |
| 706 | Union[int, str, int] == Union[int, str] |
| 707 | |
| 708 | - When comparing unions, the argument order is ignored, e.g.:: |
| 709 | |
| 710 | Union[int, str] == Union[str, int] |
| 711 | |
| 712 | - When two arguments have a subclass relationship, the least |
| 713 | derived argument is kept, e.g.:: |
| 714 | |
| 715 | class Employee: pass |
| 716 | class Manager(Employee): pass |
| 717 | Union[int, Employee, Manager] == Union[int, Employee] |
| 718 | Union[Manager, int, Employee] == Union[int, Employee] |
| 719 | Union[Employee, Manager] == Employee |
| 720 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 721 | - Similar for object:: |
| 722 | |
| 723 | Union[int, object] == object |
| 724 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 725 | - You cannot subclass or instantiate a union. |
| 726 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 727 | - You can use Optional[X] as a shorthand for Union[X, None]. |
| 728 | """ |
| 729 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 730 | __slots__ = ('__parameters__', '__args__', '__origin__', '__tree_hash__') |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 731 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 732 | def __new__(cls, parameters=None, origin=None, *args, _root=False): |
| 733 | self = super().__new__(cls, parameters, origin, *args, _root=_root) |
| 734 | if origin is None: |
| 735 | self.__parameters__ = None |
| 736 | self.__args__ = None |
| 737 | self.__origin__ = None |
| 738 | self.__tree_hash__ = hash(frozenset(('Union',))) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 739 | return self |
| 740 | if not isinstance(parameters, tuple): |
| 741 | raise TypeError("Expected parameters=<tuple>") |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 742 | if origin is Union: |
| 743 | parameters = _remove_dups_flatten(parameters) |
| 744 | # It's not a union if there's only one type left. |
| 745 | if len(parameters) == 1: |
| 746 | return parameters[0] |
| 747 | self.__parameters__ = _type_vars(parameters) |
| 748 | self.__args__ = parameters |
| 749 | self.__origin__ = origin |
| 750 | # Pre-calculate the __hash__ on instantiation. |
| 751 | # This improves speed for complex substitutions. |
| 752 | subs_tree = self._subs_tree() |
| 753 | if isinstance(subs_tree, tuple): |
| 754 | self.__tree_hash__ = hash(frozenset(subs_tree)) |
| 755 | else: |
| 756 | self.__tree_hash__ = hash(subs_tree) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 757 | return self |
| 758 | |
| 759 | def _eval_type(self, globalns, localns): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 760 | if self.__args__ is None: |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 761 | return self |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 762 | ev_args = tuple(_eval_type(t, globalns, localns) for t in self.__args__) |
| 763 | ev_origin = _eval_type(self.__origin__, globalns, localns) |
| 764 | if ev_args == self.__args__ and ev_origin == self.__origin__: |
| 765 | # Everything is already evaluated. |
| 766 | return self |
| 767 | return self.__class__(ev_args, ev_origin, _root=True) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 768 | |
| 769 | def _get_type_vars(self, tvars): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 770 | if self.__origin__ and self.__parameters__: |
| 771 | _get_type_vars(self.__parameters__, tvars) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 772 | |
| 773 | def __repr__(self): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 774 | if self.__origin__ is None: |
| 775 | return super().__repr__() |
| 776 | tree = self._subs_tree() |
| 777 | if not isinstance(tree, tuple): |
| 778 | return repr(tree) |
| 779 | return tree[0]._tree_repr(tree) |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 780 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 781 | def _tree_repr(self, tree): |
| 782 | arg_list = [] |
| 783 | for arg in tree[1:]: |
| 784 | if not isinstance(arg, tuple): |
| 785 | arg_list.append(_type_repr(arg)) |
| 786 | else: |
| 787 | arg_list.append(arg[0]._tree_repr(arg)) |
| 788 | return super().__repr__() + '[%s]' % ', '.join(arg_list) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 789 | |
| 790 | @_tp_cache |
| 791 | def __getitem__(self, parameters): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 792 | if parameters == (): |
| 793 | raise TypeError("Cannot take a Union of no types.") |
| 794 | if not isinstance(parameters, tuple): |
| 795 | parameters = (parameters,) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 796 | if self.__origin__ is None: |
| 797 | msg = "Union[arg, ...]: each arg must be a type." |
| 798 | else: |
| 799 | msg = "Parameters to generic types must be types." |
| 800 | parameters = tuple(_type_check(p, msg) for p in parameters) |
| 801 | if self is not Union: |
| 802 | _check_generic(self, parameters) |
| 803 | return self.__class__(parameters, origin=self, _root=True) |
| 804 | |
| 805 | def _subs_tree(self, tvars=None, args=None): |
| 806 | if self is Union: |
| 807 | return Union # Nothing to substitute |
| 808 | tree_args = _subs_tree(self, tvars, args) |
| 809 | tree_args = _remove_dups_flatten(tree_args) |
| 810 | if len(tree_args) == 1: |
| 811 | return tree_args[0] # Union of a single type is that type |
| 812 | return (Union,) + tree_args |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 813 | |
| 814 | def __eq__(self, other): |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 815 | if isinstance(other, _Union): |
| 816 | return self.__tree_hash__ == other.__tree_hash__ |
| 817 | elif self is not Union: |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 818 | return self._subs_tree() == other |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 819 | else: |
| 820 | return self is other |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 821 | |
| 822 | def __hash__(self): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 823 | return self.__tree_hash__ |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 824 | |
| 825 | def __instancecheck__(self, obj): |
| 826 | raise TypeError("Unions cannot be used with isinstance().") |
| 827 | |
| 828 | def __subclasscheck__(self, cls): |
| 829 | raise TypeError("Unions cannot be used with issubclass().") |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 830 | |
| 831 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 832 | Union = _Union(_root=True) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 833 | |
| 834 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 835 | class _Optional(_FinalTypingBase, _root=True): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 836 | """Optional type. |
| 837 | |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 838 | Optional[X] is equivalent to Union[X, None]. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 839 | """ |
| 840 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 841 | __slots__ = () |
| 842 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 843 | @_tp_cache |
| 844 | def __getitem__(self, arg): |
| 845 | arg = _type_check(arg, "Optional[t] requires a single type.") |
| 846 | return Union[arg, type(None)] |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 847 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 848 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 849 | Optional = _Optional(_root=True) |
| 850 | |
| 851 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 852 | def _gorg(a): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 853 | """Return the farthest origin of a generic class (internal helper).""" |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 854 | assert isinstance(a, GenericMeta) |
| 855 | while a.__origin__ is not None: |
| 856 | a = a.__origin__ |
| 857 | return a |
| 858 | |
| 859 | |
| 860 | def _geqv(a, b): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 861 | """Return whether two generic classes are equivalent (internal helper). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 862 | |
| 863 | The intention is to consider generic class X and any of its |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 864 | parameterized forms (X[T], X[int], etc.) as equivalent. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 865 | |
| 866 | However, X is not equivalent to a subclass of X. |
| 867 | |
| 868 | The relation is reflexive, symmetric and transitive. |
| 869 | """ |
| 870 | assert isinstance(a, GenericMeta) and isinstance(b, GenericMeta) |
| 871 | # Reduce each to its origin. |
| 872 | return _gorg(a) is _gorg(b) |
| 873 | |
| 874 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 875 | def _next_in_mro(cls): |
| 876 | """Helper for Generic.__new__. |
| 877 | |
| 878 | Returns the class after the last occurrence of Generic or |
| 879 | Generic[...] in cls.__mro__. |
| 880 | """ |
| 881 | next_in_mro = object |
| 882 | # Look for the last occurrence of Generic or Generic[...]. |
| 883 | for i, c in enumerate(cls.__mro__[:-1]): |
| 884 | if isinstance(c, GenericMeta) and _gorg(c) is Generic: |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 885 | next_in_mro = cls.__mro__[i + 1] |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 886 | return next_in_mro |
| 887 | |
| 888 | |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 889 | def _make_subclasshook(cls): |
| 890 | """Construct a __subclasshook__ callable that incorporates |
| 891 | the associated __extra__ class in subclass checks performed |
| 892 | against cls. |
| 893 | """ |
| 894 | if isinstance(cls.__extra__, abc.ABCMeta): |
| 895 | # The logic mirrors that of ABCMeta.__subclasscheck__. |
| 896 | # Registered classes need not be checked here because |
| 897 | # cls and its extra share the same _abc_registry. |
| 898 | def __extrahook__(subclass): |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 899 | res = cls.__extra__.__subclasshook__(subclass) |
| 900 | if res is not NotImplemented: |
| 901 | return res |
| 902 | if cls.__extra__ in subclass.__mro__: |
| 903 | return True |
| 904 | for scls in cls.__extra__.__subclasses__(): |
| 905 | if isinstance(scls, GenericMeta): |
| 906 | continue |
| 907 | if issubclass(subclass, scls): |
| 908 | return True |
| 909 | return NotImplemented |
| 910 | else: |
| 911 | # For non-ABC extras we'll just call issubclass(). |
| 912 | def __extrahook__(subclass): |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 913 | if cls.__extra__ and issubclass(subclass, cls.__extra__): |
| 914 | return True |
| 915 | return NotImplemented |
| 916 | return __extrahook__ |
| 917 | |
| 918 | |
Guido van Rossum | 61f0a02 | 2016-11-29 09:46:21 -0800 | [diff] [blame] | 919 | def _no_slots_copy(dct): |
| 920 | """Internal helper: copy class __dict__ and clean slots class variables. |
| 921 | (They will be re-created if necessary by normal class machinery.) |
| 922 | """ |
| 923 | dict_copy = dict(dct) |
| 924 | if '__slots__' in dict_copy: |
| 925 | for slot in dict_copy['__slots__']: |
| 926 | dict_copy.pop(slot, None) |
| 927 | return dict_copy |
| 928 | |
| 929 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 930 | class GenericMeta(TypingMeta, abc.ABCMeta): |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 931 | """Metaclass for generic types. |
| 932 | |
| 933 | This is a metaclass for typing.Generic and generic ABCs defined in |
| 934 | typing module. User defined subclasses of GenericMeta can override |
| 935 | __new__ and invoke super().__new__. Note that GenericMeta.__new__ |
| 936 | has strict rules on what is allowed in its bases argument: |
| 937 | * plain Generic is disallowed in bases; |
| 938 | * Generic[...] should appear in bases at most once; |
| 939 | * if Generic[...] is present, then it should list all type variables |
| 940 | that appear in other bases. |
| 941 | In addition, type of all generic bases is erased, e.g., C[int] is |
| 942 | stripped to plain C. |
| 943 | """ |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 944 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 945 | def __new__(cls, name, bases, namespace, |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 946 | tvars=None, args=None, origin=None, extra=None, orig_bases=None): |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 947 | """Create a new generic class. GenericMeta.__new__ accepts |
| 948 | keyword arguments that are used for internal bookkeeping, therefore |
| 949 | an override should pass unused keyword arguments to super(). |
| 950 | """ |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 951 | if tvars is not None: |
| 952 | # Called from __getitem__() below. |
| 953 | assert origin is not None |
| 954 | assert all(isinstance(t, TypeVar) for t in tvars), tvars |
| 955 | else: |
| 956 | # Called from class statement. |
| 957 | assert tvars is None, tvars |
| 958 | assert args is None, args |
| 959 | assert origin is None, origin |
| 960 | |
| 961 | # Get the full set of tvars from the bases. |
| 962 | tvars = _type_vars(bases) |
| 963 | # Look for Generic[T1, ..., Tn]. |
| 964 | # If found, tvars must be a subset of it. |
| 965 | # If not found, tvars is it. |
| 966 | # Also check for and reject plain Generic, |
| 967 | # and reject multiple Generic[...]. |
| 968 | gvars = None |
| 969 | for base in bases: |
| 970 | if base is Generic: |
| 971 | raise TypeError("Cannot inherit from plain Generic") |
| 972 | if (isinstance(base, GenericMeta) and |
| 973 | base.__origin__ is Generic): |
| 974 | if gvars is not None: |
| 975 | raise TypeError( |
| 976 | "Cannot inherit from Generic[...] multiple types.") |
| 977 | gvars = base.__parameters__ |
| 978 | if gvars is None: |
| 979 | gvars = tvars |
| 980 | else: |
| 981 | tvarset = set(tvars) |
| 982 | gvarset = set(gvars) |
| 983 | if not tvarset <= gvarset: |
| 984 | raise TypeError( |
| 985 | "Some type variables (%s) " |
| 986 | "are not listed in Generic[%s]" % |
| 987 | (", ".join(str(t) for t in tvars if t not in gvarset), |
| 988 | ", ".join(str(g) for g in gvars))) |
| 989 | tvars = gvars |
| 990 | |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 991 | initial_bases = bases |
| 992 | if extra is not None and type(extra) is abc.ABCMeta and extra not in bases: |
| 993 | bases = (extra,) + bases |
| 994 | bases = tuple(_gorg(b) if isinstance(b, GenericMeta) else b for b in bases) |
| 995 | |
| 996 | # remove bare Generic from bases if there are other generic bases |
| 997 | if any(isinstance(b, GenericMeta) and b is not Generic for b in bases): |
| 998 | bases = tuple(b for b in bases if b is not Generic) |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 999 | namespace.update({'__origin__': origin, '__extra__': extra}) |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1000 | self = super().__new__(cls, name, bases, namespace, _root=True) |
| 1001 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1002 | self.__parameters__ = tvars |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1003 | # Be prepared that GenericMeta will be subclassed by TupleMeta |
| 1004 | # and CallableMeta, those two allow ..., (), or [] in __args___. |
| 1005 | self.__args__ = tuple(... if a is _TypingEllipsis else |
| 1006 | () if a is _TypingEmpty else |
| 1007 | a for a in args) if args else None |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1008 | # Speed hack (https://github.com/python/typing/issues/196). |
| 1009 | self.__next_in_mro__ = _next_in_mro(self) |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1010 | # Preserve base classes on subclassing (__bases__ are type erased now). |
| 1011 | if orig_bases is None: |
| 1012 | self.__orig_bases__ = initial_bases |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 1013 | |
| 1014 | # This allows unparameterized generic collections to be used |
| 1015 | # with issubclass() and isinstance() in the same way as their |
| 1016 | # collections.abc counterparts (e.g., isinstance([], Iterable)). |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1017 | if ( |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1018 | '__subclasshook__' not in namespace and extra or |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1019 | # allow overriding |
| 1020 | getattr(self.__subclasshook__, '__name__', '') == '__extrahook__' |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1021 | ): |
Guido van Rossum | e259267 | 2016-10-08 20:27:22 -0700 | [diff] [blame] | 1022 | self.__subclasshook__ = _make_subclasshook(self) |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 1023 | if isinstance(extra, abc.ABCMeta): |
| 1024 | self._abc_registry = extra._abc_registry |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1025 | self._abc_cache = extra._abc_cache |
| 1026 | elif origin is not None: |
| 1027 | self._abc_registry = origin._abc_registry |
| 1028 | self._abc_cache = origin._abc_cache |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1029 | |
| 1030 | if origin and hasattr(origin, '__qualname__'): # Fix for Python 3.2. |
| 1031 | self.__qualname__ = origin.__qualname__ |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1032 | self.__tree_hash__ = (hash(self._subs_tree()) if origin else |
| 1033 | super(GenericMeta, self).__hash__()) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1034 | return self |
| 1035 | |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1036 | # _abc_negative_cache and _abc_negative_cache_version |
| 1037 | # realised as descriptors, since GenClass[t1, t2, ...] always |
| 1038 | # share subclass info with GenClass. |
| 1039 | # This is an important memory optimization. |
| 1040 | @property |
| 1041 | def _abc_negative_cache(self): |
| 1042 | if isinstance(self.__extra__, abc.ABCMeta): |
| 1043 | return self.__extra__._abc_negative_cache |
| 1044 | return _gorg(self)._abc_generic_negative_cache |
| 1045 | |
| 1046 | @_abc_negative_cache.setter |
| 1047 | def _abc_negative_cache(self, value): |
| 1048 | if self.__origin__ is None: |
| 1049 | if isinstance(self.__extra__, abc.ABCMeta): |
| 1050 | self.__extra__._abc_negative_cache = value |
| 1051 | else: |
| 1052 | self._abc_generic_negative_cache = value |
| 1053 | |
| 1054 | @property |
| 1055 | def _abc_negative_cache_version(self): |
| 1056 | if isinstance(self.__extra__, abc.ABCMeta): |
| 1057 | return self.__extra__._abc_negative_cache_version |
| 1058 | return _gorg(self)._abc_generic_negative_cache_version |
| 1059 | |
| 1060 | @_abc_negative_cache_version.setter |
| 1061 | def _abc_negative_cache_version(self, value): |
| 1062 | if self.__origin__ is None: |
| 1063 | if isinstance(self.__extra__, abc.ABCMeta): |
| 1064 | self.__extra__._abc_negative_cache_version = value |
| 1065 | else: |
| 1066 | self._abc_generic_negative_cache_version = value |
| 1067 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1068 | def _get_type_vars(self, tvars): |
| 1069 | if self.__origin__ and self.__parameters__: |
| 1070 | _get_type_vars(self.__parameters__, tvars) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1071 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1072 | def _eval_type(self, globalns, localns): |
| 1073 | ev_origin = (self.__origin__._eval_type(globalns, localns) |
| 1074 | if self.__origin__ else None) |
| 1075 | ev_args = tuple(_eval_type(a, globalns, localns) for a |
| 1076 | in self.__args__) if self.__args__ else None |
| 1077 | if ev_origin == self.__origin__ and ev_args == self.__args__: |
| 1078 | return self |
| 1079 | return self.__class__(self.__name__, |
| 1080 | self.__bases__, |
Guido van Rossum | 61f0a02 | 2016-11-29 09:46:21 -0800 | [diff] [blame] | 1081 | _no_slots_copy(self.__dict__), |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1082 | tvars=_type_vars(ev_args) if ev_args else None, |
| 1083 | args=ev_args, |
| 1084 | origin=ev_origin, |
| 1085 | extra=self.__extra__, |
| 1086 | orig_bases=self.__orig_bases__) |
| 1087 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1088 | def __repr__(self): |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1089 | if self.__origin__ is None: |
| 1090 | return super().__repr__() |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1091 | return self._tree_repr(self._subs_tree()) |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1092 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1093 | def _tree_repr(self, tree): |
| 1094 | arg_list = [] |
| 1095 | for arg in tree[1:]: |
| 1096 | if arg == (): |
| 1097 | arg_list.append('()') |
| 1098 | elif not isinstance(arg, tuple): |
| 1099 | arg_list.append(_type_repr(arg)) |
| 1100 | else: |
| 1101 | arg_list.append(arg[0]._tree_repr(arg)) |
| 1102 | return super().__repr__() + '[%s]' % ', '.join(arg_list) |
| 1103 | |
| 1104 | def _subs_tree(self, tvars=None, args=None): |
| 1105 | if self.__origin__ is None: |
| 1106 | return self |
| 1107 | tree_args = _subs_tree(self, tvars, args) |
| 1108 | return (_gorg(self),) + tuple(tree_args) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1109 | |
| 1110 | def __eq__(self, other): |
| 1111 | if not isinstance(other, GenericMeta): |
| 1112 | return NotImplemented |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1113 | if self.__origin__ is None or other.__origin__ is None: |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1114 | return self is other |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1115 | return self.__tree_hash__ == other.__tree_hash__ |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1116 | |
| 1117 | def __hash__(self): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1118 | return self.__tree_hash__ |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1119 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1120 | @_tp_cache |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1121 | def __getitem__(self, params): |
| 1122 | if not isinstance(params, tuple): |
| 1123 | params = (params,) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1124 | if not params and not _gorg(self) is Tuple: |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1125 | raise TypeError( |
| 1126 | "Parameter list to %s[...] cannot be empty" % _qualname(self)) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1127 | msg = "Parameters to generic types must be types." |
| 1128 | params = tuple(_type_check(p, msg) for p in params) |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1129 | if self is Generic: |
| 1130 | # Generic can only be subscripted with unique type variables. |
| 1131 | if not all(isinstance(p, TypeVar) for p in params): |
| 1132 | raise TypeError( |
| 1133 | "Parameters to Generic[...] must all be type variables") |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1134 | if len(set(params)) != len(params): |
Guido van Rossum | 1b66910 | 2015-09-04 12:15:54 -0700 | [diff] [blame] | 1135 | raise TypeError( |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1136 | "Parameters to Generic[...] must all be unique") |
| 1137 | tvars = params |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1138 | args = params |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1139 | elif self in (Tuple, Callable): |
| 1140 | tvars = _type_vars(params) |
| 1141 | args = params |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1142 | elif self is _Protocol: |
| 1143 | # _Protocol is internal, don't check anything. |
| 1144 | tvars = params |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1145 | args = params |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1146 | elif self.__origin__ in (Generic, _Protocol): |
| 1147 | # Can't subscript Generic[...] or _Protocol[...]. |
| 1148 | raise TypeError("Cannot subscript already-subscripted %s" % |
| 1149 | repr(self)) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1150 | else: |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1151 | # Subscripting a regular Generic subclass. |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1152 | _check_generic(self, params) |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1153 | tvars = _type_vars(params) |
| 1154 | args = params |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1155 | |
| 1156 | prepend = (self,) if self.__origin__ is None else () |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1157 | return self.__class__(self.__name__, |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1158 | prepend + self.__bases__, |
Guido van Rossum | 61f0a02 | 2016-11-29 09:46:21 -0800 | [diff] [blame] | 1159 | _no_slots_copy(self.__dict__), |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1160 | tvars=tvars, |
| 1161 | args=args, |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1162 | origin=self, |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1163 | extra=self.__extra__, |
| 1164 | orig_bases=self.__orig_bases__) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1165 | |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1166 | def __subclasscheck__(self, cls): |
| 1167 | if self.__origin__ is not None: |
| 1168 | if sys._getframe(1).f_globals['__name__'] not in ['abc', 'functools']: |
| 1169 | raise TypeError("Parameterized generics cannot be used with class " |
| 1170 | "or instance checks") |
| 1171 | return False |
| 1172 | if self is Generic: |
| 1173 | raise TypeError("Class %r cannot be used with class " |
| 1174 | "or instance checks" % self) |
| 1175 | return super().__subclasscheck__(cls) |
| 1176 | |
Guido van Rossum | 1b66910 | 2015-09-04 12:15:54 -0700 | [diff] [blame] | 1177 | def __instancecheck__(self, instance): |
| 1178 | # Since we extend ABC.__subclasscheck__ and |
| 1179 | # ABC.__instancecheck__ inlines the cache checking done by the |
| 1180 | # latter, we must extend __instancecheck__ too. For simplicity |
| 1181 | # we just skip the cache check -- instance checks for generic |
| 1182 | # classes are supposed to be rare anyways. |
Guido van Rossum | b47c9d2 | 2016-10-03 08:40:50 -0700 | [diff] [blame] | 1183 | return issubclass(instance.__class__, self) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1184 | |
Guido van Rossum | b7dedc8 | 2016-10-29 12:44:29 -0700 | [diff] [blame] | 1185 | def __copy__(self): |
Guido van Rossum | 61f0a02 | 2016-11-29 09:46:21 -0800 | [diff] [blame] | 1186 | return self.__class__(self.__name__, self.__bases__, |
| 1187 | _no_slots_copy(self.__dict__), |
Guido van Rossum | b7dedc8 | 2016-10-29 12:44:29 -0700 | [diff] [blame] | 1188 | self.__parameters__, self.__args__, self.__origin__, |
| 1189 | self.__extra__, self.__orig_bases__) |
| 1190 | |
Ivan Levkivskyi | abb3b8a | 2017-02-24 04:03:28 +0100 | [diff] [blame] | 1191 | def __setattr__(self, attr, value): |
| 1192 | # We consider all the subscripted genrics as proxies for original class |
Ivan Levkivskyi | 365cb5b | 2017-02-24 18:28:26 +0100 | [diff] [blame] | 1193 | if ( |
| 1194 | attr.startswith('__') and attr.endswith('__') or |
| 1195 | attr.startswith('_abc_') |
| 1196 | ): |
Ivan Levkivskyi | abb3b8a | 2017-02-24 04:03:28 +0100 | [diff] [blame] | 1197 | super(GenericMeta, self).__setattr__(attr, value) |
| 1198 | else: |
| 1199 | super(GenericMeta, _gorg(self)).__setattr__(attr, value) |
| 1200 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1201 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1202 | # Prevent checks for Generic to crash when defining Generic. |
| 1203 | Generic = None |
| 1204 | |
| 1205 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1206 | def _generic_new(base_cls, cls, *args, **kwds): |
| 1207 | # Assure type is erased on instantiation, |
| 1208 | # but attempt to store it in __orig_class__ |
| 1209 | if cls.__origin__ is None: |
| 1210 | return base_cls.__new__(cls) |
| 1211 | else: |
| 1212 | origin = _gorg(cls) |
| 1213 | obj = base_cls.__new__(origin) |
| 1214 | try: |
| 1215 | obj.__orig_class__ = cls |
| 1216 | except AttributeError: |
| 1217 | pass |
| 1218 | obj.__init__(*args, **kwds) |
| 1219 | return obj |
| 1220 | |
| 1221 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1222 | class Generic(metaclass=GenericMeta): |
| 1223 | """Abstract base class for generic types. |
| 1224 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1225 | A generic type is typically declared by inheriting from |
| 1226 | this class parameterized with one or more type variables. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1227 | For example, a generic mapping type might be defined as:: |
| 1228 | |
| 1229 | class Mapping(Generic[KT, VT]): |
| 1230 | def __getitem__(self, key: KT) -> VT: |
| 1231 | ... |
| 1232 | # Etc. |
| 1233 | |
| 1234 | This class can then be used as follows:: |
| 1235 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1236 | def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1237 | try: |
| 1238 | return mapping[key] |
| 1239 | except KeyError: |
| 1240 | return default |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1241 | """ |
| 1242 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1243 | __slots__ = () |
| 1244 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1245 | def __new__(cls, *args, **kwds): |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 1246 | if _geqv(cls, Generic): |
| 1247 | raise TypeError("Type Generic cannot be instantiated; " |
| 1248 | "it can be used only as a base class") |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1249 | return _generic_new(cls.__next_in_mro__, cls, *args, **kwds) |
| 1250 | |
| 1251 | |
| 1252 | class _TypingEmpty: |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1253 | """Internal placeholder for () or []. Used by TupleMeta and CallableMeta |
| 1254 | to allow empty list/tuple in specific places, without allowing them |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1255 | to sneak in where prohibited. |
| 1256 | """ |
| 1257 | |
| 1258 | |
| 1259 | class _TypingEllipsis: |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1260 | """Internal placeholder for ... (ellipsis).""" |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1261 | |
| 1262 | |
| 1263 | class TupleMeta(GenericMeta): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1264 | """Metaclass for Tuple (internal).""" |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1265 | |
| 1266 | @_tp_cache |
| 1267 | def __getitem__(self, parameters): |
| 1268 | if self.__origin__ is not None or not _geqv(self, Tuple): |
| 1269 | # Normal generic rules apply if this is not the first subscription |
| 1270 | # or a subscription of a subclass. |
| 1271 | return super().__getitem__(parameters) |
| 1272 | if parameters == (): |
| 1273 | return super().__getitem__((_TypingEmpty,)) |
| 1274 | if not isinstance(parameters, tuple): |
| 1275 | parameters = (parameters,) |
| 1276 | if len(parameters) == 2 and parameters[1] is ...: |
| 1277 | msg = "Tuple[t, ...]: t must be a type." |
| 1278 | p = _type_check(parameters[0], msg) |
| 1279 | return super().__getitem__((p, _TypingEllipsis)) |
| 1280 | msg = "Tuple[t0, t1, ...]: each t must be a type." |
| 1281 | parameters = tuple(_type_check(p, msg) for p in parameters) |
| 1282 | return super().__getitem__(parameters) |
| 1283 | |
| 1284 | def __instancecheck__(self, obj): |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1285 | if self.__args__ is None: |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1286 | return isinstance(obj, tuple) |
| 1287 | raise TypeError("Parameterized Tuple cannot be used " |
| 1288 | "with isinstance().") |
| 1289 | |
| 1290 | def __subclasscheck__(self, cls): |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1291 | if self.__args__ is None: |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1292 | return issubclass(cls, tuple) |
| 1293 | raise TypeError("Parameterized Tuple cannot be used " |
| 1294 | "with issubclass().") |
| 1295 | |
| 1296 | |
| 1297 | class Tuple(tuple, extra=tuple, metaclass=TupleMeta): |
| 1298 | """Tuple type; Tuple[X, Y] is the cross-product type of X and Y. |
| 1299 | |
| 1300 | Example: Tuple[T1, T2] is a tuple of two elements corresponding |
| 1301 | to type variables T1 and T2. Tuple[int, float, str] is a tuple |
| 1302 | of an int, a float and a string. |
| 1303 | |
| 1304 | To specify a variable-length tuple of homogeneous type, use Tuple[T, ...]. |
| 1305 | """ |
| 1306 | |
| 1307 | __slots__ = () |
| 1308 | |
| 1309 | def __new__(cls, *args, **kwds): |
| 1310 | if _geqv(cls, Tuple): |
| 1311 | raise TypeError("Type Tuple cannot be instantiated; " |
| 1312 | "use tuple() instead") |
| 1313 | return _generic_new(tuple, cls, *args, **kwds) |
| 1314 | |
| 1315 | |
| 1316 | class CallableMeta(GenericMeta): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1317 | """Metaclass for Callable (internal).""" |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1318 | |
| 1319 | def __repr__(self): |
| 1320 | if self.__origin__ is None: |
| 1321 | return super().__repr__() |
| 1322 | return self._tree_repr(self._subs_tree()) |
| 1323 | |
| 1324 | def _tree_repr(self, tree): |
| 1325 | if _gorg(self) is not Callable: |
| 1326 | return super()._tree_repr(tree) |
| 1327 | # For actual Callable (not its subclass) we override |
| 1328 | # super()._tree_repr() for nice formatting. |
| 1329 | arg_list = [] |
| 1330 | for arg in tree[1:]: |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1331 | if not isinstance(arg, tuple): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1332 | arg_list.append(_type_repr(arg)) |
| 1333 | else: |
| 1334 | arg_list.append(arg[0]._tree_repr(arg)) |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1335 | if arg_list[0] == '...': |
| 1336 | return repr(tree[0]) + '[..., %s]' % arg_list[1] |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1337 | return (repr(tree[0]) + |
| 1338 | '[[%s], %s]' % (', '.join(arg_list[:-1]), arg_list[-1])) |
| 1339 | |
| 1340 | def __getitem__(self, parameters): |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1341 | """A thin wrapper around __getitem_inner__ to provide the latter |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1342 | with hashable arguments to improve speed. |
| 1343 | """ |
| 1344 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1345 | if self.__origin__ is not None or not _geqv(self, Callable): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1346 | return super().__getitem__(parameters) |
| 1347 | if not isinstance(parameters, tuple) or len(parameters) != 2: |
| 1348 | raise TypeError("Callable must be used as " |
| 1349 | "Callable[[arg, ...], result].") |
| 1350 | args, result = parameters |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1351 | if args is Ellipsis: |
| 1352 | parameters = (Ellipsis, result) |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1353 | else: |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1354 | if not isinstance(args, list): |
| 1355 | raise TypeError("Callable[args, result]: args must be a list." |
| 1356 | " Got %.100r." % (args,)) |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1357 | parameters = (tuple(args), result) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1358 | return self.__getitem_inner__(parameters) |
| 1359 | |
| 1360 | @_tp_cache |
| 1361 | def __getitem_inner__(self, parameters): |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1362 | args, result = parameters |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1363 | msg = "Callable[args, result]: result must be a type." |
| 1364 | result = _type_check(result, msg) |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1365 | if args is Ellipsis: |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1366 | return super().__getitem__((_TypingEllipsis, result)) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1367 | msg = "Callable[[arg, ...], result]: each arg must be a type." |
| 1368 | args = tuple(_type_check(arg, msg) for arg in args) |
| 1369 | parameters = args + (result,) |
| 1370 | return super().__getitem__(parameters) |
| 1371 | |
| 1372 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1373 | class Callable(extra=collections_abc.Callable, metaclass=CallableMeta): |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1374 | """Callable type; Callable[[int], str] is a function of (int) -> str. |
| 1375 | |
| 1376 | The subscription syntax must always be used with exactly two |
| 1377 | values: the argument list and the return type. The argument list |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1378 | must be a list of types or ellipsis; the return type must be a single type. |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1379 | |
| 1380 | There is no syntax to indicate optional or keyword arguments, |
| 1381 | such function types are rarely used as callback types. |
| 1382 | """ |
| 1383 | |
| 1384 | __slots__ = () |
| 1385 | |
| 1386 | def __new__(cls, *args, **kwds): |
| 1387 | if _geqv(cls, Callable): |
| 1388 | raise TypeError("Type Callable cannot be instantiated; " |
| 1389 | "use a non-abstract subclass instead") |
| 1390 | return _generic_new(cls.__next_in_mro__, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1391 | |
| 1392 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1393 | class _ClassVar(_FinalTypingBase, _root=True): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1394 | """Special type construct to mark class variables. |
| 1395 | |
| 1396 | An annotation wrapped in ClassVar indicates that a given |
| 1397 | attribute is intended to be used as a class variable and |
| 1398 | should not be set on instances of that class. Usage:: |
| 1399 | |
| 1400 | class Starship: |
| 1401 | stats: ClassVar[Dict[str, int]] = {} # class variable |
| 1402 | damage: int = 10 # instance variable |
| 1403 | |
| 1404 | ClassVar accepts only types and cannot be further subscribed. |
| 1405 | |
| 1406 | Note that ClassVar is not a class itself, and should not |
| 1407 | be used with isinstance() or issubclass(). |
| 1408 | """ |
| 1409 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1410 | __slots__ = ('__type__',) |
| 1411 | |
| 1412 | def __init__(self, tp=None, **kwds): |
| 1413 | self.__type__ = tp |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1414 | |
| 1415 | def __getitem__(self, item): |
| 1416 | cls = type(self) |
| 1417 | if self.__type__ is None: |
| 1418 | return cls(_type_check(item, |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1419 | '{} accepts only single type.'.format(cls.__name__[1:])), |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1420 | _root=True) |
| 1421 | raise TypeError('{} cannot be further subscripted' |
| 1422 | .format(cls.__name__[1:])) |
| 1423 | |
| 1424 | def _eval_type(self, globalns, localns): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1425 | new_tp = _eval_type(self.__type__, globalns, localns) |
| 1426 | if new_tp == self.__type__: |
| 1427 | return self |
| 1428 | return type(self)(new_tp, _root=True) |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1429 | |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1430 | def __repr__(self): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1431 | r = super().__repr__() |
| 1432 | if self.__type__ is not None: |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1433 | r += '[{}]'.format(_type_repr(self.__type__)) |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1434 | return r |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1435 | |
| 1436 | def __hash__(self): |
| 1437 | return hash((type(self).__name__, self.__type__)) |
| 1438 | |
| 1439 | def __eq__(self, other): |
| 1440 | if not isinstance(other, _ClassVar): |
| 1441 | return NotImplemented |
| 1442 | if self.__type__ is not None: |
| 1443 | return self.__type__ == other.__type__ |
| 1444 | return self is other |
| 1445 | |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1446 | |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1447 | ClassVar = _ClassVar(_root=True) |
| 1448 | |
| 1449 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1450 | def cast(typ, val): |
| 1451 | """Cast a value to a type. |
| 1452 | |
| 1453 | This returns the value unchanged. To the type checker this |
| 1454 | signals that the return value has the designated type, but at |
| 1455 | runtime we intentionally don't check anything (we want this |
| 1456 | to be as fast as possible). |
| 1457 | """ |
| 1458 | return val |
| 1459 | |
| 1460 | |
| 1461 | def _get_defaults(func): |
| 1462 | """Internal helper to extract the default arguments, by name.""" |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1463 | try: |
| 1464 | code = func.__code__ |
| 1465 | except AttributeError: |
| 1466 | # Some built-in functions don't have __code__, __defaults__, etc. |
| 1467 | return {} |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1468 | pos_count = code.co_argcount |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1469 | arg_names = code.co_varnames |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1470 | arg_names = arg_names[:pos_count] |
| 1471 | defaults = func.__defaults__ or () |
| 1472 | kwdefaults = func.__kwdefaults__ |
| 1473 | res = dict(kwdefaults) if kwdefaults else {} |
| 1474 | pos_offset = pos_count - len(defaults) |
| 1475 | for name, value in zip(arg_names[pos_offset:], defaults): |
| 1476 | assert name not in res |
| 1477 | res[name] = value |
| 1478 | return res |
| 1479 | |
| 1480 | |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1481 | _allowed_types = (types.FunctionType, types.BuiltinFunctionType, |
| 1482 | types.MethodType, types.ModuleType, |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 1483 | WrapperDescriptorType, MethodWrapperType, MethodDescriptorType) |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1484 | |
| 1485 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1486 | def get_type_hints(obj, globalns=None, localns=None): |
| 1487 | """Return type hints for an object. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1488 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1489 | This is often the same as obj.__annotations__, but it handles |
| 1490 | forward references encoded as string literals, and if necessary |
| 1491 | adds Optional[t] if a default value equal to None is set. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1492 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1493 | The argument may be a module, class, method, or function. The annotations |
| 1494 | are returned as a dictionary. For classes, annotations include also |
| 1495 | inherited members. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1496 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1497 | TypeError is raised if the argument is not of a type that can contain |
| 1498 | annotations, and an empty dictionary is returned if no annotations are |
| 1499 | present. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1500 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1501 | BEWARE -- the behavior of globalns and localns is counterintuitive |
| 1502 | (unless you are familiar with how eval() and exec() work). The |
| 1503 | search order is locals first, then globals. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1504 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1505 | - If no dict arguments are passed, an attempt is made to use the |
| 1506 | globals from obj, and these are also used as the locals. If the |
| 1507 | object does not appear to have globals, an exception is raised. |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1508 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1509 | - If one dict argument is passed, it is used for both globals and |
| 1510 | locals. |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1511 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1512 | - If two dict arguments are passed, they specify globals and |
| 1513 | locals, respectively. |
| 1514 | """ |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1515 | |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1516 | if getattr(obj, '__no_type_check__', None): |
| 1517 | return {} |
| 1518 | if globalns is None: |
| 1519 | globalns = getattr(obj, '__globals__', {}) |
| 1520 | if localns is None: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1521 | localns = globalns |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1522 | elif localns is None: |
| 1523 | localns = globalns |
| 1524 | # Classes require a special treatment. |
| 1525 | if isinstance(obj, type): |
| 1526 | hints = {} |
| 1527 | for base in reversed(obj.__mro__): |
| 1528 | ann = base.__dict__.get('__annotations__', {}) |
| 1529 | for name, value in ann.items(): |
| 1530 | if value is None: |
| 1531 | value = type(None) |
| 1532 | if isinstance(value, str): |
| 1533 | value = _ForwardRef(value) |
| 1534 | value = _eval_type(value, globalns, localns) |
| 1535 | hints[name] = value |
| 1536 | return hints |
| 1537 | hints = getattr(obj, '__annotations__', None) |
| 1538 | if hints is None: |
| 1539 | # Return empty annotations for something that _could_ have them. |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1540 | if isinstance(obj, _allowed_types): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1541 | return {} |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 1542 | else: |
| 1543 | raise TypeError('{!r} is not a module, class, method, ' |
| 1544 | 'or function.'.format(obj)) |
| 1545 | defaults = _get_defaults(obj) |
| 1546 | hints = dict(hints) |
| 1547 | for name, value in hints.items(): |
| 1548 | if value is None: |
| 1549 | value = type(None) |
| 1550 | if isinstance(value, str): |
| 1551 | value = _ForwardRef(value) |
| 1552 | value = _eval_type(value, globalns, localns) |
| 1553 | if name in defaults and defaults[name] is None: |
| 1554 | value = Optional[value] |
| 1555 | hints[name] = value |
| 1556 | return hints |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1557 | |
| 1558 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1559 | def no_type_check(arg): |
| 1560 | """Decorator to indicate that annotations are not type hints. |
| 1561 | |
| 1562 | The argument must be a class or function; if it is a class, it |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1563 | applies recursively to all methods and classes defined in that class |
| 1564 | (but not to methods defined in its superclasses or subclasses). |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1565 | |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1566 | This mutates the function(s) or class(es) in place. |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1567 | """ |
| 1568 | if isinstance(arg, type): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1569 | arg_attrs = arg.__dict__.copy() |
| 1570 | for attr, val in arg.__dict__.items(): |
| 1571 | if val in arg.__bases__: |
| 1572 | arg_attrs.pop(attr) |
| 1573 | for obj in arg_attrs.values(): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1574 | if isinstance(obj, types.FunctionType): |
| 1575 | obj.__no_type_check__ = True |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1576 | if isinstance(obj, type): |
| 1577 | no_type_check(obj) |
| 1578 | try: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1579 | arg.__no_type_check__ = True |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1580 | except TypeError: # built-in classes |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1581 | pass |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1582 | return arg |
| 1583 | |
| 1584 | |
| 1585 | def no_type_check_decorator(decorator): |
| 1586 | """Decorator to give another decorator the @no_type_check effect. |
| 1587 | |
| 1588 | This wraps the decorator with something that wraps the decorated |
| 1589 | function in @no_type_check. |
| 1590 | """ |
| 1591 | |
| 1592 | @functools.wraps(decorator) |
| 1593 | def wrapped_decorator(*args, **kwds): |
| 1594 | func = decorator(*args, **kwds) |
| 1595 | func = no_type_check(func) |
| 1596 | return func |
| 1597 | |
| 1598 | return wrapped_decorator |
| 1599 | |
| 1600 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1601 | def _overload_dummy(*args, **kwds): |
| 1602 | """Helper for @overload to raise when called.""" |
| 1603 | raise NotImplementedError( |
| 1604 | "You should not call an overloaded function. " |
| 1605 | "A series of @overload-decorated functions " |
| 1606 | "outside a stub module should always be followed " |
| 1607 | "by an implementation that is not @overload-ed.") |
| 1608 | |
| 1609 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1610 | def overload(func): |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1611 | """Decorator for overloaded functions/methods. |
| 1612 | |
| 1613 | In a stub file, place two or more stub definitions for the same |
| 1614 | function in a row, each decorated with @overload. For example: |
| 1615 | |
| 1616 | @overload |
| 1617 | def utf8(value: None) -> None: ... |
| 1618 | @overload |
| 1619 | def utf8(value: bytes) -> bytes: ... |
| 1620 | @overload |
| 1621 | def utf8(value: str) -> bytes: ... |
| 1622 | |
| 1623 | In a non-stub file (i.e. a regular .py file), do the same but |
| 1624 | follow it with an implementation. The implementation should *not* |
| 1625 | be decorated with @overload. For example: |
| 1626 | |
| 1627 | @overload |
| 1628 | def utf8(value: None) -> None: ... |
| 1629 | @overload |
| 1630 | def utf8(value: bytes) -> bytes: ... |
| 1631 | @overload |
| 1632 | def utf8(value: str) -> bytes: ... |
| 1633 | def utf8(value): |
| 1634 | # implementation goes here |
| 1635 | """ |
| 1636 | return _overload_dummy |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1637 | |
| 1638 | |
| 1639 | class _ProtocolMeta(GenericMeta): |
| 1640 | """Internal metaclass for _Protocol. |
| 1641 | |
| 1642 | This exists so _Protocol classes can be generic without deriving |
| 1643 | from Generic. |
| 1644 | """ |
| 1645 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1646 | def __instancecheck__(self, obj): |
Guido van Rossum | ca4b252 | 2016-11-19 10:32:41 -0800 | [diff] [blame] | 1647 | if _Protocol not in self.__bases__: |
| 1648 | return super().__instancecheck__(obj) |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1649 | raise TypeError("Protocols cannot be used with isinstance().") |
| 1650 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1651 | def __subclasscheck__(self, cls): |
| 1652 | if not self._is_protocol: |
| 1653 | # No structural checks since this isn't a protocol. |
| 1654 | return NotImplemented |
| 1655 | |
| 1656 | if self is _Protocol: |
| 1657 | # Every class is a subclass of the empty protocol. |
| 1658 | return True |
| 1659 | |
| 1660 | # Find all attributes defined in the protocol. |
| 1661 | attrs = self._get_protocol_attrs() |
| 1662 | |
| 1663 | for attr in attrs: |
| 1664 | if not any(attr in d.__dict__ for d in cls.__mro__): |
| 1665 | return False |
| 1666 | return True |
| 1667 | |
| 1668 | def _get_protocol_attrs(self): |
| 1669 | # Get all Protocol base classes. |
| 1670 | protocol_bases = [] |
| 1671 | for c in self.__mro__: |
| 1672 | if getattr(c, '_is_protocol', False) and c.__name__ != '_Protocol': |
| 1673 | protocol_bases.append(c) |
| 1674 | |
| 1675 | # Get attributes included in protocol. |
| 1676 | attrs = set() |
| 1677 | for base in protocol_bases: |
| 1678 | for attr in base.__dict__.keys(): |
| 1679 | # Include attributes not defined in any non-protocol bases. |
| 1680 | for c in self.__mro__: |
| 1681 | if (c is not base and attr in c.__dict__ and |
| 1682 | not getattr(c, '_is_protocol', False)): |
| 1683 | break |
| 1684 | else: |
| 1685 | if (not attr.startswith('_abc_') and |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1686 | attr != '__abstractmethods__' and |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 1687 | attr != '__annotations__' and |
| 1688 | attr != '__weakref__' and |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1689 | attr != '_is_protocol' and |
| 1690 | attr != '__dict__' and |
| 1691 | attr != '__args__' and |
| 1692 | attr != '__slots__' and |
| 1693 | attr != '_get_protocol_attrs' and |
| 1694 | attr != '__next_in_mro__' and |
| 1695 | attr != '__parameters__' and |
| 1696 | attr != '__origin__' and |
Guido van Rossum | 7ef22d6 | 2016-10-21 14:27:58 -0700 | [diff] [blame] | 1697 | attr != '__orig_bases__' and |
Guido van Rossum | 1cea70f | 2016-05-18 08:35:00 -0700 | [diff] [blame] | 1698 | attr != '__extra__' and |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1699 | attr != '__tree_hash__' and |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1700 | attr != '__module__'): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1701 | attrs.add(attr) |
| 1702 | |
| 1703 | return attrs |
| 1704 | |
| 1705 | |
| 1706 | class _Protocol(metaclass=_ProtocolMeta): |
| 1707 | """Internal base class for protocol classes. |
| 1708 | |
Guido van Rossum | b24569a | 2016-11-20 18:01:29 -0800 | [diff] [blame] | 1709 | This implements a simple-minded structural issubclass check |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1710 | (similar but more general than the one-offs in collections.abc |
| 1711 | such as Hashable). |
| 1712 | """ |
| 1713 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1714 | __slots__ = () |
| 1715 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1716 | _is_protocol = True |
| 1717 | |
| 1718 | |
| 1719 | # Various ABCs mimicking those in collections.abc. |
| 1720 | # A few are simply re-exported for completeness. |
| 1721 | |
| 1722 | Hashable = collections_abc.Hashable # Not generic. |
| 1723 | |
| 1724 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1725 | if hasattr(collections_abc, 'Awaitable'): |
| 1726 | class Awaitable(Generic[T_co], extra=collections_abc.Awaitable): |
| 1727 | __slots__ = () |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 1728 | |
| 1729 | __all__.append('Awaitable') |
| 1730 | |
| 1731 | |
| 1732 | if hasattr(collections_abc, 'Coroutine'): |
| 1733 | class Coroutine(Awaitable[V_co], Generic[T_co, T_contra, V_co], |
| 1734 | extra=collections_abc.Coroutine): |
| 1735 | __slots__ = () |
| 1736 | |
| 1737 | __all__.append('Coroutine') |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 1738 | |
| 1739 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1740 | if hasattr(collections_abc, 'AsyncIterable'): |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 1741 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1742 | class AsyncIterable(Generic[T_co], extra=collections_abc.AsyncIterable): |
| 1743 | __slots__ = () |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 1744 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1745 | class AsyncIterator(AsyncIterable[T_co], |
| 1746 | extra=collections_abc.AsyncIterator): |
| 1747 | __slots__ = () |
| 1748 | |
Guido van Rossum | 62fe1bb | 2016-10-29 16:05:26 -0700 | [diff] [blame] | 1749 | __all__.append('AsyncIterable') |
| 1750 | __all__.append('AsyncIterator') |
Guido van Rossum | f17c200 | 2015-12-03 17:31:24 -0800 | [diff] [blame] | 1751 | |
| 1752 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1753 | class Iterable(Generic[T_co], extra=collections_abc.Iterable): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1754 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1755 | |
| 1756 | |
| 1757 | class Iterator(Iterable[T_co], extra=collections_abc.Iterator): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1758 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1759 | |
| 1760 | |
| 1761 | class SupportsInt(_Protocol): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1762 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1763 | |
| 1764 | @abstractmethod |
| 1765 | def __int__(self) -> int: |
| 1766 | pass |
| 1767 | |
| 1768 | |
| 1769 | class SupportsFloat(_Protocol): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1770 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1771 | |
| 1772 | @abstractmethod |
| 1773 | def __float__(self) -> float: |
| 1774 | pass |
| 1775 | |
| 1776 | |
| 1777 | class SupportsComplex(_Protocol): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1778 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1779 | |
| 1780 | @abstractmethod |
| 1781 | def __complex__(self) -> complex: |
| 1782 | pass |
| 1783 | |
| 1784 | |
| 1785 | class SupportsBytes(_Protocol): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1786 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1787 | |
| 1788 | @abstractmethod |
| 1789 | def __bytes__(self) -> bytes: |
| 1790 | pass |
| 1791 | |
| 1792 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1793 | class SupportsAbs(_Protocol[T_co]): |
| 1794 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1795 | |
| 1796 | @abstractmethod |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1797 | def __abs__(self) -> T_co: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1798 | pass |
| 1799 | |
| 1800 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1801 | class SupportsRound(_Protocol[T_co]): |
| 1802 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1803 | |
| 1804 | @abstractmethod |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1805 | def __round__(self, ndigits: int = 0) -> T_co: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1806 | pass |
| 1807 | |
| 1808 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1809 | if hasattr(collections_abc, 'Reversible'): |
| 1810 | class Reversible(Iterable[T_co], extra=collections_abc.Reversible): |
| 1811 | __slots__ = () |
| 1812 | else: |
| 1813 | class Reversible(_Protocol[T_co]): |
| 1814 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1815 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1816 | @abstractmethod |
| 1817 | def __reversed__(self) -> 'Iterator[T_co]': |
| 1818 | pass |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1819 | |
| 1820 | |
| 1821 | Sized = collections_abc.Sized # Not generic. |
| 1822 | |
| 1823 | |
| 1824 | class Container(Generic[T_co], extra=collections_abc.Container): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1825 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1826 | |
| 1827 | |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 1828 | if hasattr(collections_abc, 'Collection'): |
| 1829 | class Collection(Sized, Iterable[T_co], Container[T_co], |
| 1830 | extra=collections_abc.Collection): |
| 1831 | __slots__ = () |
| 1832 | |
| 1833 | __all__.append('Collection') |
| 1834 | |
| 1835 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1836 | # Callable was defined earlier. |
| 1837 | |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 1838 | if hasattr(collections_abc, 'Collection'): |
| 1839 | class AbstractSet(Collection[T_co], |
| 1840 | extra=collections_abc.Set): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1841 | __slots__ = () |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 1842 | else: |
| 1843 | class AbstractSet(Sized, Iterable[T_co], Container[T_co], |
| 1844 | extra=collections_abc.Set): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1845 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1846 | |
| 1847 | |
| 1848 | class MutableSet(AbstractSet[T], extra=collections_abc.MutableSet): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1849 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1850 | |
| 1851 | |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 1852 | # NOTE: It is only covariant in the value type. |
| 1853 | if hasattr(collections_abc, 'Collection'): |
| 1854 | class Mapping(Collection[KT], Generic[KT, VT_co], |
| 1855 | extra=collections_abc.Mapping): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1856 | __slots__ = () |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 1857 | else: |
| 1858 | class Mapping(Sized, Iterable[KT], Container[KT], Generic[KT, VT_co], |
| 1859 | extra=collections_abc.Mapping): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1860 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1861 | |
| 1862 | |
| 1863 | class MutableMapping(Mapping[KT, VT], extra=collections_abc.MutableMapping): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1864 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1865 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1866 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1867 | if hasattr(collections_abc, 'Reversible'): |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 1868 | if hasattr(collections_abc, 'Collection'): |
| 1869 | class Sequence(Reversible[T_co], Collection[T_co], |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1870 | extra=collections_abc.Sequence): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1871 | __slots__ = () |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 1872 | else: |
| 1873 | class Sequence(Sized, Reversible[T_co], Container[T_co], |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1874 | extra=collections_abc.Sequence): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1875 | __slots__ = () |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1876 | else: |
| 1877 | class Sequence(Sized, Iterable[T_co], Container[T_co], |
| 1878 | extra=collections_abc.Sequence): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1879 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1880 | |
| 1881 | |
| 1882 | class MutableSequence(Sequence[T], extra=collections_abc.MutableSequence): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1883 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1884 | |
| 1885 | |
| 1886 | class ByteString(Sequence[int], extra=collections_abc.ByteString): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1887 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1888 | |
| 1889 | |
Guido van Rossum | 1cea70f | 2016-05-18 08:35:00 -0700 | [diff] [blame] | 1890 | class List(list, MutableSequence[T], extra=list): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1891 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1892 | __slots__ = () |
| 1893 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1894 | def __new__(cls, *args, **kwds): |
| 1895 | if _geqv(cls, List): |
| 1896 | raise TypeError("Type List cannot be instantiated; " |
| 1897 | "use list() instead") |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1898 | return _generic_new(list, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1899 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1900 | |
Raymond Hettinger | 8049052 | 2017-01-16 22:42:37 -0800 | [diff] [blame] | 1901 | class Deque(collections.deque, MutableSequence[T], extra=collections.deque): |
| 1902 | |
| 1903 | __slots__ = () |
| 1904 | |
| 1905 | def __new__(cls, *args, **kwds): |
| 1906 | if _geqv(cls, Deque): |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 1907 | return collections.deque(*args, **kwds) |
Raymond Hettinger | 8049052 | 2017-01-16 22:42:37 -0800 | [diff] [blame] | 1908 | return _generic_new(collections.deque, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1909 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 1910 | |
Guido van Rossum | 1cea70f | 2016-05-18 08:35:00 -0700 | [diff] [blame] | 1911 | class Set(set, MutableSet[T], extra=set): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1912 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1913 | __slots__ = () |
| 1914 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1915 | def __new__(cls, *args, **kwds): |
| 1916 | if _geqv(cls, Set): |
| 1917 | raise TypeError("Type Set cannot be instantiated; " |
| 1918 | "use set() instead") |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1919 | return _generic_new(set, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1920 | |
| 1921 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1922 | class FrozenSet(frozenset, AbstractSet[T_co], extra=frozenset): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1923 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1924 | |
| 1925 | def __new__(cls, *args, **kwds): |
| 1926 | if _geqv(cls, FrozenSet): |
| 1927 | raise TypeError("Type FrozenSet cannot be instantiated; " |
| 1928 | "use frozenset() instead") |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 1929 | return _generic_new(frozenset, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1930 | |
| 1931 | |
| 1932 | class MappingView(Sized, Iterable[T_co], extra=collections_abc.MappingView): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1933 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1934 | |
| 1935 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 1936 | class KeysView(MappingView[KT], AbstractSet[KT], |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1937 | extra=collections_abc.KeysView): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1938 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1939 | |
| 1940 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1941 | class ItemsView(MappingView[Tuple[KT, VT_co]], |
Guido van Rossum | 0e0563c | 2016-04-05 14:54:25 -0700 | [diff] [blame] | 1942 | AbstractSet[Tuple[KT, VT_co]], |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 1943 | Generic[KT, VT_co], |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1944 | extra=collections_abc.ItemsView): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1945 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1946 | |
| 1947 | |
| 1948 | class ValuesView(MappingView[VT_co], extra=collections_abc.ValuesView): |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 1949 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 1950 | |
| 1951 | |
Brett Cannon | f3ad042 | 2016-04-15 10:51:30 -0700 | [diff] [blame] | 1952 | if hasattr(contextlib, 'AbstractContextManager'): |
| 1953 | class ContextManager(Generic[T_co], extra=contextlib.AbstractContextManager): |
| 1954 | __slots__ = () |
Ivan Levkivskyi | 29fda8d | 2017-06-10 21:57:56 +0200 | [diff] [blame^] | 1955 | else: |
| 1956 | class ContextManager(Generic[T_co]): |
| 1957 | __slots__ = () |
| 1958 | |
| 1959 | def __enter__(self): |
| 1960 | return self |
| 1961 | |
| 1962 | @abc.abstractmethod |
| 1963 | def __exit__(self, exc_type, exc_value, traceback): |
| 1964 | return None |
| 1965 | |
| 1966 | @classmethod |
| 1967 | def __subclasshook__(cls, C): |
| 1968 | if cls is ContextManager: |
| 1969 | # In Python 3.6+, it is possible to set a method to None to |
| 1970 | # explicitly indicate that the class does not implement an ABC |
| 1971 | # (https://bugs.python.org/issue25958), but we do not support |
| 1972 | # that pattern here because this fallback class is only used |
| 1973 | # in Python 3.5 and earlier. |
| 1974 | if (any("__enter__" in B.__dict__ for B in C.__mro__) and |
| 1975 | any("__exit__" in B.__dict__ for B in C.__mro__)): |
| 1976 | return True |
| 1977 | return NotImplemented |
| 1978 | |
| 1979 | |
| 1980 | if hasattr(contextlib, 'AbstractAsyncContextManager'): |
| 1981 | class AsyncContextManager(Generic[T_co], |
| 1982 | extra=contextlib.AbstractAsyncContextManager): |
| 1983 | __slots__ = () |
| 1984 | |
| 1985 | __all__.append('AsyncContextManager') |
| 1986 | elif sys.version_info[:2] >= (3, 5): |
| 1987 | exec(""" |
| 1988 | class AsyncContextManager(Generic[T_co]): |
| 1989 | __slots__ = () |
| 1990 | |
| 1991 | async def __aenter__(self): |
| 1992 | return self |
| 1993 | |
| 1994 | @abc.abstractmethod |
| 1995 | async def __aexit__(self, exc_type, exc_value, traceback): |
| 1996 | return None |
| 1997 | |
| 1998 | @classmethod |
| 1999 | def __subclasshook__(cls, C): |
| 2000 | if cls is AsyncContextManager: |
| 2001 | if sys.version_info[:2] >= (3, 6): |
| 2002 | return _collections_abc._check_methods(C, "__aenter__", "__aexit__") |
| 2003 | if (any("__aenter__" in B.__dict__ for B in C.__mro__) and |
| 2004 | any("__aexit__" in B.__dict__ for B in C.__mro__)): |
| 2005 | return True |
| 2006 | return NotImplemented |
| 2007 | |
| 2008 | __all__.append('AsyncContextManager') |
| 2009 | """) |
Brett Cannon | f3ad042 | 2016-04-15 10:51:30 -0700 | [diff] [blame] | 2010 | |
| 2011 | |
Guido van Rossum | 1cea70f | 2016-05-18 08:35:00 -0700 | [diff] [blame] | 2012 | class Dict(dict, MutableMapping[KT, VT], extra=dict): |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2013 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 2014 | __slots__ = () |
| 2015 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2016 | def __new__(cls, *args, **kwds): |
| 2017 | if _geqv(cls, Dict): |
| 2018 | raise TypeError("Type Dict cannot be instantiated; " |
| 2019 | "use dict() instead") |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 2020 | return _generic_new(dict, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2021 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2022 | |
Guido van Rossum | 1cea70f | 2016-05-18 08:35:00 -0700 | [diff] [blame] | 2023 | class DefaultDict(collections.defaultdict, MutableMapping[KT, VT], |
| 2024 | extra=collections.defaultdict): |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 2025 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 2026 | __slots__ = () |
| 2027 | |
Guido van Rossum | bd5b9a0 | 2016-04-05 08:28:52 -0700 | [diff] [blame] | 2028 | def __new__(cls, *args, **kwds): |
| 2029 | if _geqv(cls, DefaultDict): |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2030 | return collections.defaultdict(*args, **kwds) |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 2031 | return _generic_new(collections.defaultdict, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2032 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2033 | |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2034 | class Counter(collections.Counter, Dict[T, int], extra=collections.Counter): |
| 2035 | |
| 2036 | __slots__ = () |
| 2037 | |
| 2038 | def __new__(cls, *args, **kwds): |
| 2039 | if _geqv(cls, Counter): |
| 2040 | return collections.Counter(*args, **kwds) |
| 2041 | return _generic_new(collections.Counter, cls, *args, **kwds) |
| 2042 | |
| 2043 | |
| 2044 | if hasattr(collections, 'ChainMap'): |
| 2045 | # ChainMap only exists in 3.3+ |
| 2046 | __all__.append('ChainMap') |
| 2047 | |
| 2048 | class ChainMap(collections.ChainMap, MutableMapping[KT, VT], |
| 2049 | extra=collections.ChainMap): |
| 2050 | |
| 2051 | __slots__ = () |
| 2052 | |
| 2053 | def __new__(cls, *args, **kwds): |
| 2054 | if _geqv(cls, ChainMap): |
| 2055 | return collections.ChainMap(*args, **kwds) |
| 2056 | return _generic_new(collections.ChainMap, cls, *args, **kwds) |
| 2057 | |
| 2058 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2059 | # Determine what base class to use for Generator. |
| 2060 | if hasattr(collections_abc, 'Generator'): |
| 2061 | # Sufficiently recent versions of 3.5 have a Generator ABC. |
| 2062 | _G_base = collections_abc.Generator |
| 2063 | else: |
| 2064 | # Fall back on the exact type. |
| 2065 | _G_base = types.GeneratorType |
| 2066 | |
| 2067 | |
| 2068 | class Generator(Iterator[T_co], Generic[T_co, T_contra, V_co], |
| 2069 | extra=_G_base): |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2070 | __slots__ = () |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2071 | |
| 2072 | def __new__(cls, *args, **kwds): |
| 2073 | if _geqv(cls, Generator): |
| 2074 | raise TypeError("Type Generator cannot be instantiated; " |
| 2075 | "create a subclass instead") |
Guido van Rossum | 5fc25a8 | 2016-10-29 08:54:56 -0700 | [diff] [blame] | 2076 | return _generic_new(_G_base, cls, *args, **kwds) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2077 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2078 | |
Guido van Rossum | e9ed560 | 2017-01-18 13:10:31 -0800 | [diff] [blame] | 2079 | if hasattr(collections_abc, 'AsyncGenerator'): |
| 2080 | class AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra], |
| 2081 | extra=collections_abc.AsyncGenerator): |
| 2082 | __slots__ = () |
| 2083 | |
| 2084 | __all__.append('AsyncGenerator') |
| 2085 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2086 | |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 2087 | # Internal type variable used for Type[]. |
Guido van Rossum | efa798d | 2016-08-23 11:01:50 -0700 | [diff] [blame] | 2088 | CT_co = TypeVar('CT_co', covariant=True, bound=type) |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 2089 | |
| 2090 | |
Guido van Rossum | b22c708 | 2016-05-26 09:56:19 -0700 | [diff] [blame] | 2091 | # This is not a real generic class. Don't use outside annotations. |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2092 | class Type(Generic[CT_co], extra=type): |
Guido van Rossum | b22c708 | 2016-05-26 09:56:19 -0700 | [diff] [blame] | 2093 | """A special construct usable to annotate class objects. |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 2094 | |
| 2095 | For example, suppose we have the following classes:: |
| 2096 | |
| 2097 | class User: ... # Abstract base for User classes |
| 2098 | class BasicUser(User): ... |
| 2099 | class ProUser(User): ... |
| 2100 | class TeamUser(User): ... |
| 2101 | |
| 2102 | And a function that takes a class argument that's a subclass of |
| 2103 | User and returns an instance of the corresponding class:: |
| 2104 | |
| 2105 | U = TypeVar('U', bound=User) |
| 2106 | def new_user(user_class: Type[U]) -> U: |
| 2107 | user = user_class() |
| 2108 | # (Here we could write the user object to a database) |
| 2109 | return user |
| 2110 | |
| 2111 | joe = new_user(BasicUser) |
| 2112 | |
| 2113 | At this point the type checker knows that joe has type BasicUser. |
| 2114 | """ |
| 2115 | |
Guido van Rossum | 4cefe74 | 2016-09-27 15:20:12 -0700 | [diff] [blame] | 2116 | __slots__ = () |
| 2117 | |
Guido van Rossum | eb9aca3 | 2016-05-24 16:38:22 -0700 | [diff] [blame] | 2118 | |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2119 | def _make_nmtuple(name, types): |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2120 | msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type" |
| 2121 | types = [(n, _type_check(t, msg)) for n, t in types] |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2122 | nm_tpl = collections.namedtuple(name, [n for n, t in types]) |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 2123 | # Prior to PEP 526, only _field_types attribute was assigned. |
| 2124 | # Now, both __annotations__ and _field_types are used to maintain compatibility. |
| 2125 | nm_tpl.__annotations__ = nm_tpl._field_types = collections.OrderedDict(types) |
Guido van Rossum | 557d1eb | 2015-11-19 08:16:31 -0800 | [diff] [blame] | 2126 | try: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2127 | nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__') |
Guido van Rossum | 557d1eb | 2015-11-19 08:16:31 -0800 | [diff] [blame] | 2128 | except (AttributeError, ValueError): |
| 2129 | pass |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2130 | return nm_tpl |
| 2131 | |
| 2132 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2133 | _PY36 = sys.version_info[:2] >= (3, 6) |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2134 | |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2135 | # attributes prohibited to set in NamedTuple class syntax |
| 2136 | _prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__', |
| 2137 | '_fields', '_field_defaults', '_field_types', |
Ivan Levkivskyi | f06e021 | 2017-05-02 19:14:07 +0200 | [diff] [blame] | 2138 | '_make', '_replace', '_asdict', '_source') |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2139 | |
| 2140 | _special = ('__module__', '__name__', '__qualname__', '__annotations__') |
| 2141 | |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2142 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2143 | class NamedTupleMeta(type): |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2144 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2145 | def __new__(cls, typename, bases, ns): |
| 2146 | if ns.get('_root', False): |
| 2147 | return super().__new__(cls, typename, bases, ns) |
| 2148 | if not _PY36: |
| 2149 | raise TypeError("Class syntax for NamedTuple is only supported" |
| 2150 | " in Python 3.6+") |
| 2151 | types = ns.get('__annotations__', {}) |
Guido van Rossum | 3c268be | 2017-01-18 08:03:50 -0800 | [diff] [blame] | 2152 | nm_tpl = _make_nmtuple(typename, types.items()) |
| 2153 | defaults = [] |
| 2154 | defaults_dict = {} |
| 2155 | for field_name in types: |
| 2156 | if field_name in ns: |
| 2157 | default_value = ns[field_name] |
| 2158 | defaults.append(default_value) |
| 2159 | defaults_dict[field_name] = default_value |
| 2160 | elif defaults: |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2161 | raise TypeError("Non-default namedtuple field {field_name} cannot " |
| 2162 | "follow default field(s) {default_names}" |
Guido van Rossum | 3c268be | 2017-01-18 08:03:50 -0800 | [diff] [blame] | 2163 | .format(field_name=field_name, |
| 2164 | default_names=', '.join(defaults_dict.keys()))) |
| 2165 | nm_tpl.__new__.__defaults__ = tuple(defaults) |
| 2166 | nm_tpl._field_defaults = defaults_dict |
Guido van Rossum | 95919c0 | 2017-01-22 17:47:20 -0800 | [diff] [blame] | 2167 | # update from user namespace without overriding special namedtuple attributes |
| 2168 | for key in ns: |
Ivan Levkivskyi | b692dc8 | 2017-02-13 22:50:14 +0100 | [diff] [blame] | 2169 | if key in _prohibited: |
| 2170 | raise AttributeError("Cannot overwrite NamedTuple attribute " + key) |
| 2171 | elif key not in _special and key not in nm_tpl._fields: |
Guido van Rossum | 95919c0 | 2017-01-22 17:47:20 -0800 | [diff] [blame] | 2172 | setattr(nm_tpl, key, ns[key]) |
Guido van Rossum | 3c268be | 2017-01-18 08:03:50 -0800 | [diff] [blame] | 2173 | return nm_tpl |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2174 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2175 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2176 | class NamedTuple(metaclass=NamedTupleMeta): |
| 2177 | """Typed version of namedtuple. |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2178 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2179 | Usage in Python versions >= 3.6:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2180 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2181 | class Employee(NamedTuple): |
| 2182 | name: str |
| 2183 | id: int |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2184 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2185 | This is equivalent to:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2186 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2187 | Employee = collections.namedtuple('Employee', ['name', 'id']) |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2188 | |
Guido van Rossum | 83ec302 | 2017-01-17 20:43:28 -0800 | [diff] [blame] | 2189 | The resulting class has extra __annotations__ and _field_types |
| 2190 | attributes, giving an ordered dict mapping field names to types. |
| 2191 | __annotations__ should be preferred, while _field_types |
| 2192 | is kept to maintain pre PEP 526 compatibility. (The field names |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2193 | are in the _fields attribute, which is part of the namedtuple |
| 2194 | API.) Alternative equivalent keyword syntax is also accepted:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2195 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2196 | Employee = NamedTuple('Employee', name=str, id=int) |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2197 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2198 | In Python versions <= 3.5 use:: |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2199 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2200 | Employee = NamedTuple('Employee', [('name', str), ('id', int)]) |
| 2201 | """ |
| 2202 | _root = True |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2203 | |
Guido van Rossum | 2f84144 | 2016-11-15 09:48:06 -0800 | [diff] [blame] | 2204 | def __new__(self, typename, fields=None, **kwargs): |
| 2205 | if kwargs and not _PY36: |
| 2206 | raise TypeError("Keyword syntax for NamedTuple is only supported" |
| 2207 | " in Python 3.6+") |
| 2208 | if fields is None: |
| 2209 | fields = kwargs.items() |
| 2210 | elif kwargs: |
| 2211 | raise TypeError("Either list of fields or keywords" |
| 2212 | " can be provided to NamedTuple, not both") |
Guido van Rossum | 0a6976d | 2016-09-11 15:34:56 -0700 | [diff] [blame] | 2213 | return _make_nmtuple(typename, fields) |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2214 | |
| 2215 | |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 2216 | def NewType(name, tp): |
| 2217 | """NewType creates simple unique types with almost zero |
| 2218 | runtime overhead. NewType(name, tp) is considered a subtype of tp |
| 2219 | by static type checkers. At runtime, NewType(name, tp) returns |
| 2220 | a dummy function that simply returns its argument. Usage:: |
| 2221 | |
| 2222 | UserId = NewType('UserId', int) |
| 2223 | |
| 2224 | def name_by_id(user_id: UserId) -> str: |
| 2225 | ... |
| 2226 | |
| 2227 | UserId('user') # Fails type check |
| 2228 | |
| 2229 | name_by_id(42) # Fails type check |
| 2230 | name_by_id(UserId(42)) # OK |
| 2231 | |
| 2232 | num = UserId(5) + 1 # type: int |
| 2233 | """ |
| 2234 | |
| 2235 | def new_type(x): |
| 2236 | return x |
| 2237 | |
| 2238 | new_type.__name__ = name |
| 2239 | new_type.__supertype__ = tp |
| 2240 | return new_type |
| 2241 | |
| 2242 | |
Guido van Rossum | 0e0563c | 2016-04-05 14:54:25 -0700 | [diff] [blame] | 2243 | # Python-version-specific alias (Python 2: unicode; Python 3: str) |
| 2244 | Text = str |
| 2245 | |
| 2246 | |
Guido van Rossum | 91185fe | 2016-06-08 11:19:11 -0700 | [diff] [blame] | 2247 | # Constant that's True when type checking, but False here. |
| 2248 | TYPE_CHECKING = False |
| 2249 | |
| 2250 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2251 | class IO(Generic[AnyStr]): |
| 2252 | """Generic base class for TextIO and BinaryIO. |
| 2253 | |
| 2254 | This is an abstract, generic version of the return of open(). |
| 2255 | |
| 2256 | NOTE: This does not distinguish between the different possible |
| 2257 | classes (text vs. binary, read vs. write vs. read/write, |
| 2258 | append-only, unbuffered). The TextIO and BinaryIO subclasses |
| 2259 | below capture the distinctions between text vs. binary, which is |
| 2260 | pervasive in the interface; however we currently do not offer a |
| 2261 | way to track the other distinctions in the type system. |
| 2262 | """ |
| 2263 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2264 | __slots__ = () |
| 2265 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2266 | @abstractproperty |
| 2267 | def mode(self) -> str: |
| 2268 | pass |
| 2269 | |
| 2270 | @abstractproperty |
| 2271 | def name(self) -> str: |
| 2272 | pass |
| 2273 | |
| 2274 | @abstractmethod |
| 2275 | def close(self) -> None: |
| 2276 | pass |
| 2277 | |
| 2278 | @abstractmethod |
| 2279 | def closed(self) -> bool: |
| 2280 | pass |
| 2281 | |
| 2282 | @abstractmethod |
| 2283 | def fileno(self) -> int: |
| 2284 | pass |
| 2285 | |
| 2286 | @abstractmethod |
| 2287 | def flush(self) -> None: |
| 2288 | pass |
| 2289 | |
| 2290 | @abstractmethod |
| 2291 | def isatty(self) -> bool: |
| 2292 | pass |
| 2293 | |
| 2294 | @abstractmethod |
| 2295 | def read(self, n: int = -1) -> AnyStr: |
| 2296 | pass |
| 2297 | |
| 2298 | @abstractmethod |
| 2299 | def readable(self) -> bool: |
| 2300 | pass |
| 2301 | |
| 2302 | @abstractmethod |
| 2303 | def readline(self, limit: int = -1) -> AnyStr: |
| 2304 | pass |
| 2305 | |
| 2306 | @abstractmethod |
| 2307 | def readlines(self, hint: int = -1) -> List[AnyStr]: |
| 2308 | pass |
| 2309 | |
| 2310 | @abstractmethod |
| 2311 | def seek(self, offset: int, whence: int = 0) -> int: |
| 2312 | pass |
| 2313 | |
| 2314 | @abstractmethod |
| 2315 | def seekable(self) -> bool: |
| 2316 | pass |
| 2317 | |
| 2318 | @abstractmethod |
| 2319 | def tell(self) -> int: |
| 2320 | pass |
| 2321 | |
| 2322 | @abstractmethod |
| 2323 | def truncate(self, size: int = None) -> int: |
| 2324 | pass |
| 2325 | |
| 2326 | @abstractmethod |
| 2327 | def writable(self) -> bool: |
| 2328 | pass |
| 2329 | |
| 2330 | @abstractmethod |
| 2331 | def write(self, s: AnyStr) -> int: |
| 2332 | pass |
| 2333 | |
| 2334 | @abstractmethod |
| 2335 | def writelines(self, lines: List[AnyStr]) -> None: |
| 2336 | pass |
| 2337 | |
| 2338 | @abstractmethod |
| 2339 | def __enter__(self) -> 'IO[AnyStr]': |
| 2340 | pass |
| 2341 | |
| 2342 | @abstractmethod |
| 2343 | def __exit__(self, type, value, traceback) -> None: |
| 2344 | pass |
| 2345 | |
| 2346 | |
| 2347 | class BinaryIO(IO[bytes]): |
| 2348 | """Typed version of the return of open() in binary mode.""" |
| 2349 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2350 | __slots__ = () |
| 2351 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2352 | @abstractmethod |
| 2353 | def write(self, s: Union[bytes, bytearray]) -> int: |
| 2354 | pass |
| 2355 | |
| 2356 | @abstractmethod |
| 2357 | def __enter__(self) -> 'BinaryIO': |
| 2358 | pass |
| 2359 | |
| 2360 | |
| 2361 | class TextIO(IO[str]): |
| 2362 | """Typed version of the return of open() in text mode.""" |
| 2363 | |
Guido van Rossum | d70fe63 | 2015-08-05 12:11:06 +0200 | [diff] [blame] | 2364 | __slots__ = () |
| 2365 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2366 | @abstractproperty |
| 2367 | def buffer(self) -> BinaryIO: |
| 2368 | pass |
| 2369 | |
| 2370 | @abstractproperty |
| 2371 | def encoding(self) -> str: |
| 2372 | pass |
| 2373 | |
| 2374 | @abstractproperty |
Guido van Rossum | 991d14f | 2016-11-09 13:12:51 -0800 | [diff] [blame] | 2375 | def errors(self) -> Optional[str]: |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2376 | pass |
| 2377 | |
| 2378 | @abstractproperty |
| 2379 | def line_buffering(self) -> bool: |
| 2380 | pass |
| 2381 | |
| 2382 | @abstractproperty |
| 2383 | def newlines(self) -> Any: |
| 2384 | pass |
| 2385 | |
| 2386 | @abstractmethod |
| 2387 | def __enter__(self) -> 'TextIO': |
| 2388 | pass |
| 2389 | |
| 2390 | |
| 2391 | class io: |
| 2392 | """Wrapper namespace for IO generic classes.""" |
| 2393 | |
| 2394 | __all__ = ['IO', 'TextIO', 'BinaryIO'] |
| 2395 | IO = IO |
| 2396 | TextIO = TextIO |
| 2397 | BinaryIO = BinaryIO |
| 2398 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2399 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2400 | io.__name__ = __name__ + '.io' |
| 2401 | sys.modules[io.__name__] = io |
| 2402 | |
| 2403 | |
| 2404 | Pattern = _TypeAlias('Pattern', AnyStr, type(stdlib_re.compile('')), |
| 2405 | lambda p: p.pattern) |
| 2406 | Match = _TypeAlias('Match', AnyStr, type(stdlib_re.match('', '')), |
| 2407 | lambda m: m.re.pattern) |
| 2408 | |
| 2409 | |
| 2410 | class re: |
| 2411 | """Wrapper namespace for re type aliases.""" |
| 2412 | |
| 2413 | __all__ = ['Pattern', 'Match'] |
| 2414 | Pattern = Pattern |
| 2415 | Match = Match |
| 2416 | |
Guido van Rossum | d7adfe1 | 2017-01-22 17:43:53 -0800 | [diff] [blame] | 2417 | |
Guido van Rossum | 46dbb7d | 2015-05-22 10:14:11 -0700 | [diff] [blame] | 2418 | re.__name__ = __name__ + '.re' |
| 2419 | sys.modules[re.__name__] = re |