File Lib/test/test_typing.py changed (mode: 100644) (index 20fc2219f7..33d553ed6e) |
... |
... |
import sys |
6 |
6 |
from unittest import TestCase, main, skipUnless, SkipTest |
from unittest import TestCase, main, skipUnless, SkipTest |
7 |
7 |
from copy import copy, deepcopy |
from copy import copy, deepcopy |
8 |
8 |
|
|
9 |
|
from typing import Any |
|
|
9 |
|
from typing import Any, NoReturn |
10 |
10 |
from typing import TypeVar, AnyStr |
from typing import TypeVar, AnyStr |
11 |
11 |
from typing import T, KT, VT # Not in __all__. |
from typing import T, KT, VT # Not in __all__. |
12 |
12 |
from typing import Union, Optional |
from typing import Union, Optional |
|
... |
... |
class AnyTests(BaseTestCase): |
102 |
102 |
with self.assertRaises(TypeError): |
with self.assertRaises(TypeError): |
103 |
103 |
type(Any)() |
type(Any)() |
104 |
104 |
|
|
105 |
|
def test_cannot_subscript(self): |
|
106 |
|
with self.assertRaises(TypeError): |
|
107 |
|
Any[int] |
|
108 |
|
|
|
109 |
105 |
def test_any_works_with_alias(self): |
def test_any_works_with_alias(self): |
110 |
106 |
# These expressions must simply not fail. |
# These expressions must simply not fail. |
111 |
107 |
typing.Match[Any] |
typing.Match[Any] |
|
... |
... |
class AnyTests(BaseTestCase): |
113 |
109 |
typing.IO[Any] |
typing.IO[Any] |
114 |
110 |
|
|
115 |
111 |
|
|
|
112 |
|
class NoReturnTests(BaseTestCase): |
|
113 |
|
|
|
114 |
|
def test_noreturn_instance_type_error(self): |
|
115 |
|
with self.assertRaises(TypeError): |
|
116 |
|
isinstance(42, NoReturn) |
|
117 |
|
|
|
118 |
|
def test_noreturn_subclass_type_error(self): |
|
119 |
|
with self.assertRaises(TypeError): |
|
120 |
|
issubclass(Employee, NoReturn) |
|
121 |
|
with self.assertRaises(TypeError): |
|
122 |
|
issubclass(NoReturn, Employee) |
|
123 |
|
|
|
124 |
|
def test_repr(self): |
|
125 |
|
self.assertEqual(repr(NoReturn), 'typing.NoReturn') |
|
126 |
|
|
|
127 |
|
def test_not_generic(self): |
|
128 |
|
with self.assertRaises(TypeError): |
|
129 |
|
NoReturn[int] |
|
130 |
|
|
|
131 |
|
def test_cannot_subclass(self): |
|
132 |
|
with self.assertRaises(TypeError): |
|
133 |
|
class A(NoReturn): |
|
134 |
|
pass |
|
135 |
|
with self.assertRaises(TypeError): |
|
136 |
|
class A(type(NoReturn)): |
|
137 |
|
pass |
|
138 |
|
|
|
139 |
|
def test_cannot_instantiate(self): |
|
140 |
|
with self.assertRaises(TypeError): |
|
141 |
|
NoReturn() |
|
142 |
|
with self.assertRaises(TypeError): |
|
143 |
|
type(NoReturn)() |
|
144 |
|
|
|
145 |
|
|
116 |
146 |
class TypeVarTests(BaseTestCase): |
class TypeVarTests(BaseTestCase): |
117 |
147 |
|
|
118 |
148 |
def test_basic_plain(self): |
def test_basic_plain(self): |
|
... |
... |
class XMethBad(NamedTuple): |
2273 |
2303 |
return 'no chance for this' |
return 'no chance for this' |
2274 |
2304 |
""") |
""") |
2275 |
2305 |
|
|
|
2306 |
|
with self.assertRaises(AttributeError): |
|
2307 |
|
exec(""" |
|
2308 |
|
class XMethBad2(NamedTuple): |
|
2309 |
|
x: int |
|
2310 |
|
def _source(self): |
|
2311 |
|
return 'no chance for this as well' |
|
2312 |
|
""") |
|
2313 |
|
|
2276 |
2314 |
@skipUnless(PY36, 'Python 3.6 required') |
@skipUnless(PY36, 'Python 3.6 required') |
2277 |
2315 |
def test_namedtuple_keyword_usage(self): |
def test_namedtuple_keyword_usage(self): |
2278 |
2316 |
LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int) |
LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int) |
|
... |
... |
class AllTests(BaseTestCase): |
2420 |
2458 |
self.assertNotIn('sys', a) |
self.assertNotIn('sys', a) |
2421 |
2459 |
# Check that Text is defined. |
# Check that Text is defined. |
2422 |
2460 |
self.assertIn('Text', a) |
self.assertIn('Text', a) |
|
2461 |
|
# Check previously missing classes. |
|
2462 |
|
self.assertIn('SupportsBytes', a) |
|
2463 |
|
self.assertIn('SupportsComplex', a) |
2423 |
2464 |
|
|
2424 |
2465 |
|
|
2425 |
2466 |
if __name__ == '__main__': |
if __name__ == '__main__': |
File Lib/typing.py changed (mode: 100644) (index 9a0f49099a..645bc6f8ae) |
... |
... |
try: |
11 |
11 |
except ImportError: |
except ImportError: |
12 |
12 |
import collections as collections_abc # Fallback for PY3.2. |
import collections as collections_abc # Fallback for PY3.2. |
13 |
13 |
try: |
try: |
14 |
|
from types import SlotWrapperType, MethodWrapperType, MethodDescriptorType |
|
|
14 |
|
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType |
15 |
15 |
except ImportError: |
except ImportError: |
16 |
|
SlotWrapperType = type(object.__init__) |
|
|
16 |
|
WrapperDescriptorType = type(object.__init__) |
17 |
17 |
MethodWrapperType = type(object().__str__) |
MethodWrapperType = type(object().__str__) |
18 |
18 |
MethodDescriptorType = type(str.join) |
MethodDescriptorType = type(str.join) |
19 |
19 |
|
|
|
... |
... |
__all__ = [ |
63 |
63 |
# Structural checks, a.k.a. protocols. |
# Structural checks, a.k.a. protocols. |
64 |
64 |
'Reversible', |
'Reversible', |
65 |
65 |
'SupportsAbs', |
'SupportsAbs', |
|
66 |
|
'SupportsBytes', |
|
67 |
|
'SupportsComplex', |
66 |
68 |
'SupportsFloat', |
'SupportsFloat', |
67 |
69 |
'SupportsInt', |
'SupportsInt', |
68 |
70 |
'SupportsRound', |
'SupportsRound', |
|
... |
... |
class _Any(_FinalTypingBase, _root=True): |
420 |
422 |
Any = _Any(_root=True) |
Any = _Any(_root=True) |
421 |
423 |
|
|
422 |
424 |
|
|
|
425 |
|
class _NoReturn(_FinalTypingBase, _root=True): |
|
426 |
|
"""Special type indicating functions that never return. |
|
427 |
|
Example:: |
|
428 |
|
|
|
429 |
|
from typing import NoReturn |
|
430 |
|
|
|
431 |
|
def stop() -> NoReturn: |
|
432 |
|
raise Exception('no way') |
|
433 |
|
|
|
434 |
|
This type is invalid in other positions, e.g., ``List[NoReturn]`` |
|
435 |
|
will fail in static type checkers. |
|
436 |
|
""" |
|
437 |
|
|
|
438 |
|
__slots__ = () |
|
439 |
|
|
|
440 |
|
def __instancecheck__(self, obj): |
|
441 |
|
raise TypeError("NoReturn cannot be used with isinstance().") |
|
442 |
|
|
|
443 |
|
def __subclasscheck__(self, cls): |
|
444 |
|
raise TypeError("NoReturn cannot be used with issubclass().") |
|
445 |
|
|
|
446 |
|
|
|
447 |
|
NoReturn = _NoReturn(_root=True) |
|
448 |
|
|
|
449 |
|
|
423 |
450 |
class TypeVar(_TypingBase, _root=True): |
class TypeVar(_TypingBase, _root=True): |
424 |
451 |
"""Type variable. |
"""Type variable. |
425 |
452 |
|
|
|
... |
... |
def _get_defaults(func): |
1450 |
1477 |
|
|
1451 |
1478 |
_allowed_types = (types.FunctionType, types.BuiltinFunctionType, |
_allowed_types = (types.FunctionType, types.BuiltinFunctionType, |
1452 |
1479 |
types.MethodType, types.ModuleType, |
types.MethodType, types.ModuleType, |
1453 |
|
SlotWrapperType, MethodWrapperType, MethodDescriptorType) |
|
|
1480 |
|
WrapperDescriptorType, MethodWrapperType, MethodDescriptorType) |
1454 |
1481 |
|
|
1455 |
1482 |
|
|
1456 |
1483 |
def get_type_hints(obj, globalns=None, localns=None): |
def get_type_hints(obj, globalns=None, localns=None): |
|
... |
... |
_PY36 = sys.version_info[:2] >= (3, 6) |
2051 |
2078 |
# attributes prohibited to set in NamedTuple class syntax |
# attributes prohibited to set in NamedTuple class syntax |
2052 |
2079 |
_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__', |
_prohibited = ('__new__', '__init__', '__slots__', '__getnewargs__', |
2053 |
2080 |
'_fields', '_field_defaults', '_field_types', |
'_fields', '_field_defaults', '_field_types', |
2054 |
|
'_make', '_replace', '_asdict') |
|
|
2081 |
|
'_make', '_replace', '_asdict', '_source') |
2055 |
2082 |
|
|
2056 |
2083 |
_special = ('__module__', '__name__', '__qualname__', '__annotations__') |
_special = ('__module__', '__name__', '__qualname__', '__annotations__') |
2057 |
2084 |
|
|