]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46348: modernize `test_typing` (GH-30547)
authorNikita Sobolev <mail@sobolevn.me>
Wed, 12 Jan 2022 16:48:10 +0000 (19:48 +0300)
committerGitHub <noreply@github.com>
Wed, 12 Jan 2022 16:48:10 +0000 (08:48 -0800)
Lib/test/mod_generics_cache.py
Lib/test/test_typing.py

index 6d35c58396d424ccb6151908b8510001a77d4b8a..9d8b56cf03c364867cba8a4b56f68245cd977121 100644 (file)
@@ -1,53 +1,21 @@
 """Module for testing the behavior of generics across different modules."""
 
-import sys
-from textwrap import dedent
 from typing import TypeVar, Generic, Optional
 
+default_a: Optional['A'] = None
+default_b: Optional['B'] = None
 
-if sys.version_info[:2] >= (3, 6):
-    exec(dedent("""
-    default_a: Optional['A'] = None
-    default_b: Optional['B'] = None
+T = TypeVar('T')
 
-    T = TypeVar('T')
 
-
-    class A(Generic[T]):
-        some_b: 'B'
-
-
-    class B(Generic[T]):
-        class A(Generic[T]):
-            pass
-
-        my_inner_a1: 'B.A'
-        my_inner_a2: A
-        my_outer_a: 'A'  # unless somebody calls get_type_hints with localns=B.__dict__
-    """))
-else:  # This should stay in sync with the syntax above.
-    __annotations__ = dict(
-        default_a=Optional['A'],
-        default_b=Optional['B'],
-    )
-    default_a = None
-    default_b = None
-
-    T = TypeVar('T')
+class A(Generic[T]):
+    some_b: 'B'
 
 
+class B(Generic[T]):
     class A(Generic[T]):
-        __annotations__ = dict(
-            some_b='B'
-        )
-
-
-    class B(Generic[T]):
-        class A(Generic[T]):
-            pass
+        pass
 
-        __annotations__ = dict(
-            my_inner_a1='B.A',
-            my_inner_a2=A,
-            my_outer_a='A'  # unless somebody calls get_type_hints with localns=B.__dict__
-        )
+    my_inner_a1: 'B.A'
+    my_inner_a2: A
+    my_outer_a: 'A'  # unless somebody calls get_type_hints with localns=B.__dict__
index a94d77d4edf4be725630349f4ab93edc1c585a29..af5b1df6b04ca8822161c0424859fe8fdb3cab3c 100644 (file)
@@ -2938,7 +2938,9 @@ class OverloadTests(BaseTestCase):
         blah()
 
 
-ASYNCIO_TESTS = """
+# Definitions needed for features introduced in Python 3.6
+
+from test import ann_module, ann_module2, ann_module3, ann_module5, ann_module6
 import asyncio
 
 T_a = TypeVar('T_a')
@@ -2972,19 +2974,6 @@ class ACM:
         return 42
     async def __aexit__(self, etype, eval, tb):
         return None
-"""
-
-try:
-    exec(ASYNCIO_TESTS)
-except ImportError:
-    ASYNCIO = False  # multithreading is not enabled
-else:
-    ASYNCIO = True
-
-# Definitions needed for features introduced in Python 3.6
-
-from test import ann_module, ann_module2, ann_module3, ann_module5, ann_module6
-from typing import AsyncContextManager
 
 class A:
     y: float
@@ -3044,7 +3033,7 @@ class HasForeignBaseClass(mod_generics_cache.A):
     some_xrepr: 'XRepr'
     other_a: 'mod_generics_cache.A'
 
-async def g_with(am: AsyncContextManager[int]):
+async def g_with(am: typing.AsyncContextManager[int]):
     x: int
     async with am as x:
         return x
@@ -3386,7 +3375,6 @@ class CollectionsAbcTests(BaseTestCase):
         self.assertIsInstance(it, typing.Iterator)
         self.assertNotIsInstance(42, typing.Iterator)
 
-    @skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
     def test_awaitable(self):
         ns = {}
         exec(
@@ -3399,7 +3387,6 @@ class CollectionsAbcTests(BaseTestCase):
         self.assertNotIsInstance(foo, typing.Awaitable)
         g.send(None)  # Run foo() till completion, to avoid warning.
 
-    @skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
     def test_coroutine(self):
         ns = {}
         exec(
@@ -3417,7 +3404,6 @@ class CollectionsAbcTests(BaseTestCase):
         except StopIteration:
             pass
 
-    @skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
     def test_async_iterable(self):
         base_it = range(10)  # type: Iterator[int]
         it = AsyncIteratorWrapper(base_it)
@@ -3425,7 +3411,6 @@ class CollectionsAbcTests(BaseTestCase):
         self.assertIsInstance(it, typing.AsyncIterable)
         self.assertNotIsInstance(42, typing.AsyncIterable)
 
-    @skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
     def test_async_iterator(self):
         base_it = range(10)  # type: Iterator[int]
         it = AsyncIteratorWrapper(base_it)
@@ -3580,7 +3565,6 @@ class CollectionsAbcTests(BaseTestCase):
         self.assertIsSubclass(MyOrdDict, collections.OrderedDict)
         self.assertNotIsSubclass(collections.OrderedDict, MyOrdDict)
 
-    @skipUnless(sys.version_info >= (3, 3), 'ChainMap was added in 3.3')
     def test_chainmap_instantiation(self):
         self.assertIs(type(typing.ChainMap()), collections.ChainMap)
         self.assertIs(type(typing.ChainMap[KT, VT]()), collections.ChainMap)
@@ -3588,7 +3572,6 @@ class CollectionsAbcTests(BaseTestCase):
         class CM(typing.ChainMap[KT, VT]): ...
         self.assertIs(type(CM[int, str]()), CM)
 
-    @skipUnless(sys.version_info >= (3, 3), 'ChainMap was added in 3.3')
     def test_chainmap_subclass(self):
 
         class MyChainMap(typing.ChainMap[str, int]):
@@ -3852,7 +3835,6 @@ class OtherABCTests(BaseTestCase):
         self.assertIsInstance(cm, typing.ContextManager)
         self.assertNotIsInstance(42, typing.ContextManager)
 
-    @skipUnless(ASYNCIO, 'Python 3.5 required')
     def test_async_contextmanager(self):
         class NotACM:
             pass