8000 bpo-46348: modernize `test_typing` (GH-30547) · python/cpython@e2a9c8e · GitHub
[go: up one dir, main page]

Skip to content

Commit e2a9c8e

Browse files
authored
bpo-46348: modernize test_typing (GH-30547)
1 parent 43839ba commit e2a9c8e

File tree

2 files changed

+14
-64
lines changed

2 files changed

+14
-64
lines changed

Lib/test/mod_generics_cache.py

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,21 @@
11
"""Module for testing the behavior of generics across different modules."""
22

3-
import sys
4-
from textwrap import dedent
53
from typing import TypeVar, Generic, Optional
64

5+
default_a: Optional['A'] = None
6+
default_b: Optional['B'] = None
77

8-
if sys.version_info[:2] >= (3, 6):
9-
exec(dedent("""
10-
default_a: Optional['A'] = None
11-
default_b: Optional['B'] = None
8+
T = TypeVar('T')
129

13-
T = TypeVar('T')
1410

15-
16-
class A(Generic[T]):
17-
some_b: 'B'
18-
19-
20-
class B(Generic[T]):
21-
class A(Generic[T]):
22-
pass
23-
24-
my_inner_a1: 'B.A'
25-
my_inner_a2: A
26-
my_outer_a: 'A' # unless somebody calls get_type_hints with localns=B.__dict__
27-
"""))
28-
else: # This should stay in sync with the syntax above.
29-
__annotations__ = dict(
30-
default_a=Optional['A'],
31-
default_b=Optional['B'],
32-
)
33-
default_a = None
34-
default_b = None
35-
36-
T = TypeVar('T')
11+
class A(Generic[T]):
12+
some_b: 'B'
3713

3814

15+
class B(Generic[T]):
3916
class A(Generic[T]):
40-
__annotations__ = dict(
41-
some_b='B'
42-
)
43-
44-
45-
class B(Generic[T]):
46-
class A(Generic[T]):
47-
pass
17+
pass
4818

49-
__annotations__ = dict(
50-
my_inner_a1='B.A',
51-
my_inner_a2=A,
52-
my_outer_a='A' # unless somebody calls get_type_hints with localns=B.__dict__
53-
)
19+
my_inner_a1: 'B.A'
20+
my_inner_a2: A
21+
my_outer_a: 'A' # unless somebody calls get_type_hints with localns=B.__dict__

Lib/test/test_typing.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,7 +2938,9 @@ def blah():
29382938
blah()
29392939

29402940

2941-
ASYNCIO_TESTS = """
2941+
# Definitions needed for features introduced in Python 3.6
2942+
2943+
from test import ann_module, ann_module2, ann_module3, ann_module5, ann_module6
29422944
import asyncio
29432945

29442946
T_a = TypeVar('T_a')
@@ -2972,19 +2974,6 @@ async def __aenter__(self) -> int:
29722974
return 42
29732975
async def __aexit__(self, etype, eval, tb):
29742976
return None
2975-
"""
2976-
2977-
try:
2978-
exec(ASYNCIO_TESTS)
2979-
except ImportError:
2980-
ASYNCIO = False # multithreading is not enabled
2981-
else:
2982-
ASYNCIO = True
2983-
2984-
# Definitions needed for features introduced in Python 3.6
2985-
2986-
from test import ann_module, ann_module2, ann_module3, ann_module5, ann_module6
2987-
from typing import AsyncContextManager
29882977

29892978
class A:
29902979
y: float
@@ -3044,7 +3033,7 @@ class HasForeignBaseClass(mod_generics_cache.A):
30443033
some_xrepr: 'XRepr'
30453034
other_a: 'mod_generics_cache.A'
30463035

3047-
async def g_with(am: AsyncContextManager[int]):
3036+
async def g_with(am: typing.AsyncContextManager[int]):
30483037
x: int
30493038
async with am as x:
30503039
return x
@@ -3386,7 +3375,6 @@ def test_iterator(self):
33863375
self.assertIsInstance(it, typing.Iterator)
33873376
self.assertNotIsInstance(42, typing.Iterator)
33883377

3389-
@skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
33903378
def test_awaitable(self):
33913379
ns = {}
33923380
exec(
@@ -3399,7 +3387,6 @@ def test_awaitable(self):
33993387
self.assertNotIsInstance(foo, typing.Awaitable)
34003388
g.send(None) # Run foo() till completion, to avoid warning.
34013389

3402-
@skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
34033390
def test_coroutine(self):
34043391
ns = {}
34053392
exec(
@@ -3417,15 +3404,13 @@ def test_coroutine(self):
34173404
except StopIteration:
34183405
pass
34193406

3420-
@skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
34213407
def test_async_iterable(self):
34223408
base_it = range(10) # type: Iterator[int]
34233409
it = AsyncIteratorWrapper(base_it)
34243410
self.assertIsInstance(it, typing.AsyncIterable)
34253411
self.assertIsInstance(it, typing.AsyncIterable)
34263412
self.assertNotIsInstance(42, typing.AsyncIterable)
34273413

3428-
@skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
34293414
def test_async_iterator(self):
34303415
base_it = range(10) # type: Iterator[int]
34313416
it = AsyncIteratorWrapper(base_it)
@@ -3580,15 +3565,13 @@ class MyOrdDict(typing.OrderedDict[str, int]):
35803565
self.assertIsSubclass(MyOrdDict, collections.OrderedDict)
35813566
self.assertNotIsSubclass(collections.OrderedDict, MyOrdDict)
35823567

3583-
@skipUnless(sys.version_info >= (3, 3), 'ChainMap was added in 3.3')
35843568
def test_chainmap_instantiation(self):
35853569
self.assertIs(type(typing.ChainMap()), collections.ChainMap)
35863570
self.assertIs(type(typing.ChainMap[KT, VT]()), collections.ChainMap)
35873571
self.assertIs(type(typing.ChainMap[str, int]()), collections.ChainMap)
35883572
class CM(typing.ChainMap[KT, VT]): ...
35893573
self.assertIs(type(CM[int, str]()), CM)
35903574

3591-
@skipUnless(sys.version_info >= (3, 3), 'ChainMap was added in 3.3')
35923575
def test_chainmap_subclass(self):
35933576

35943577
class MyChainMap(typing.ChainMap[str, int]):
@@ -3852,7 +3835,6 @@ def manager():
38523835
self.assertIsInstance(cm, typing.ContextManager)
38533836
self.assertNotIsInstance(42, typing.ContextManager)
38543837

3855-
@skipUnless(ASYNCIO, 'Python 3.5 required')
38563838
def test_async_contextmanager(self):
38573839
class NotACM:
38583840
pass

0 commit comments

Comments
 (0)
0