bpo-28556: Updates to typing module (#2076) · python/cpython@29fda8d (original) (raw)

`@@ -1552,6 +1552,12 @@ def anext(self) -> T_a:

`

1552

1552

` return data

`

1553

1553

` else:

`

1554

1554

` raise StopAsyncIteration

`

``

1555

+

``

1556

`+

class ACM:

`

``

1557

`+

async def aenter(self) -> int:

`

``

1558

`+

return 42

`

``

1559

`+

async def aexit(self, etype, eval, tb):

`

``

1560

`+

return None

`

1555

1561

`"""

`

1556

1562

``

1557

1563

`if ASYNCIO:

`

`@@ -1562,12 +1568,13 @@ def anext(self) -> T_a:

`

1562

1568

`else:

`

1563

1569

`# fake names for the sake of static analysis

`

1564

1570

`asyncio = None

`

1565

``

`-

AwaitableWrapper = AsyncIteratorWrapper = object

`

``

1571

`+

AwaitableWrapper = AsyncIteratorWrapper = ACM = object

`

1566

1572

``

1567

1573

`PY36 = sys.version_info[:2] >= (3, 6)

`

1568

1574

``

1569

1575

`PY36_TESTS = """

`

1570

1576

`from test import ann_module, ann_module2, ann_module3

`

``

1577

`+

from typing import AsyncContextManager

`

1571

1578

``

1572

1579

`class A:

`

1573

1580

` y: float

`

`@@ -1604,6 +1611,16 @@ def str(self):

`

1604

1611

` return f'{self.x} -> {self.y}'

`

1605

1612

` def add(self, other):

`

1606

1613

` return 0

`

``

1614

+

``

1615

`+

async def g_with(am: AsyncContextManager[int]):

`

``

1616

`+

x: int

`

``

1617

`+

async with am as x:

`

``

1618

`+

return x

`

``

1619

+

``

1620

`+

try:

`

``

1621

`+

g_with(ACM()).send(None)

`

``

1622

`+

except StopIteration as e:

`

``

1623

`+

assert e.args[0] == 42

`

1607

1624

`"""

`

1608

1625

``

1609

1626

`if PY36:

`

`@@ -2156,8 +2173,6 @@ class B: ...

`

2156

2173

``

2157

2174

`class OtherABCTests(BaseTestCase):

`

2158

2175

``

2159

``

`-

@skipUnless(hasattr(typing, 'ContextManager'),

`

2160

``

`-

'requires typing.ContextManager')

`

2161

2176

`def test_contextmanager(self):

`

2162

2177

`@contextlib.contextmanager

`

2163

2178

`def manager():

`

`@@ -2167,6 +2182,24 @@ def manager():

`

2167

2182

`self.assertIsInstance(cm, typing.ContextManager)

`

2168

2183

`self.assertNotIsInstance(42, typing.ContextManager)

`

2169

2184

``

``

2185

`+

@skipUnless(ASYNCIO, 'Python 3.5 required')

`

``

2186

`+

def test_async_contextmanager(self):

`

``

2187

`+

class NotACM:

`

``

2188

`+

pass

`

``

2189

`+

self.assertIsInstance(ACM(), typing.AsyncContextManager)

`

``

2190

`+

self.assertNotIsInstance(NotACM(), typing.AsyncContextManager)

`

``

2191

`+

@contextlib.contextmanager

`

``

2192

`+

def manager():

`

``

2193

`+

yield 42

`

``

2194

+

``

2195

`+

cm = manager()

`

``

2196

`+

self.assertNotIsInstance(cm, typing.AsyncContextManager)

`

``

2197

`+

self.assertEqual(typing.AsyncContextManager[int].args, (int,))

`

``

2198

`+

with self.assertRaises(TypeError):

`

``

2199

`+

isinstance(42, typing.AsyncContextManager[int])

`

``

2200

`+

with self.assertRaises(TypeError):

`

``

2201

`+

typing.AsyncContextManager[int, str]

`

``

2202

+

2170

2203

``

2171

2204

`class TypeTests(BaseTestCase):

`

2172

2205

``