Skip to content

bpo-46348: modernize test_typing #30547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 10 additions & 42 deletions Lib/test/mod_generics_cache.py
Original file line number Diff line number Diff line change
@@ -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__
26 changes: 4 additions & 22 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2938,7 +2938,9 @@ def blah():
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')
Expand Down Expand Up @@ -2972,19 +2974,6 @@ async def __aenter__(self) -> int:
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3386,7 +3375,6 @@ def test_iterator(self):
self.assertIsInstance(it, typing.Iterator)
self.assertNotIsInstance(42, typing.Iterator)

@skipUnless(ASYNCIO, 'Python 3.5 and multithreading required')
def test_awaitable(self):
ns = {}
exec(
Expand All @@ -3399,7 +3387,6 @@ def test_awaitable(self):
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(
Expand All @@ -3417,15 +3404,13 @@ def test_coroutine(self):
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)
self.assertIsInstance(it, typing.AsyncIterable)
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)
Expand Down Expand Up @@ -3580,15 +3565,13 @@ class MyOrdDict(typing.OrderedDict[str, int]):
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)
self.assertIs(type(typing.ChainMap[str, int]()), collections.ChainMap)
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]):
Expand Down Expand Up @@ -3852,7 +3835,6 @@ def manager():
self.assertIsInstance(cm, typing.ContextManager)
self.assertNotIsInstance(42, typing.ContextManager)

@skipUnless(ASYNCIO, 'Python 3.5 required')
def test_async_contextmanager(self):
class NotACM:
pass
Expand Down