bpo-28556: Routine updates to typing (#1366)

- Add NoReturn type
- Use WrapperDescriptorType (original PR by Jim Fasarakis-Hilliard)
- Minor bug-fixes
This commit is contained in:
Ivan Levkivskyi 2017-05-02 19:14:07 +02:00 committed by Mariatta
parent 495b5021e7
commit f06e0218ef
3 changed files with 81 additions and 9 deletions

View file

@ -6,7 +6,7 @@ import sys
from unittest import TestCase, main, skipUnless, SkipTest
from copy import copy, deepcopy
from typing import Any
from typing import Any, NoReturn
from typing import TypeVar, AnyStr
from typing import T, KT, VT # Not in __all__.
from typing import Union, Optional
@ -102,10 +102,6 @@ class AnyTests(BaseTestCase):
with self.assertRaises(TypeError):
type(Any)()
def test_cannot_subscript(self):
with self.assertRaises(TypeError):
Any[int]
def test_any_works_with_alias(self):
# These expressions must simply not fail.
typing.Match[Any]
@ -113,6 +109,40 @@ class AnyTests(BaseTestCase):
typing.IO[Any]
class NoReturnTests(BaseTestCase):
def test_noreturn_instance_type_error(self):
with self.assertRaises(TypeError):
isinstance(42, NoReturn)
def test_noreturn_subclass_type_error(self):
with self.assertRaises(TypeError):
issubclass(Employee, NoReturn)
with self.assertRaises(TypeError):
issubclass(NoReturn, Employee)
def test_repr(self):
self.assertEqual(repr(NoReturn), 'typing.NoReturn')
def test_not_generic(self):
with self.assertRaises(TypeError):
NoReturn[int]
def test_cannot_subclass(self):
with self.assertRaises(TypeError):
class A(NoReturn):
pass
with self.assertRaises(TypeError):
class A(type(NoReturn)):
pass
def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
NoReturn()
with self.assertRaises(TypeError):
type(NoReturn)()
class TypeVarTests(BaseTestCase):
def test_basic_plain(self):
@ -2273,6 +2303,14 @@ class XMethBad(NamedTuple):
return 'no chance for this'
""")
with self.assertRaises(AttributeError):
exec("""
class XMethBad2(NamedTuple):
x: int
def _source(self):
return 'no chance for this as well'
""")
@skipUnless(PY36, 'Python 3.6 required')
def test_namedtuple_keyword_usage(self):
LocalEmployee = NamedTuple("LocalEmployee", name=str, age=int)
@ -2420,6 +2458,9 @@ class AllTests(BaseTestCase):
self.assertNotIn('sys', a)
# Check that Text is defined.
self.assertIn('Text', a)
# Check previously missing classes.
self.assertIn('SupportsBytes', a)
self.assertIn('SupportsComplex', a)
if __name__ == '__main__':