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