From 168d87ba3eaa70a2b70f2e20f84215c59a5a6701 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 13 May 2024 13:01:41 -0400 Subject: [PATCH 1/2] typing tests: remove some unnecessary uses of `exec()` --- Lib/test/test_typing.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index f10b0aea3cd7b9..6bc84be33e050f 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7060,24 +7060,16 @@ def test_iterator(self): self.assertNotIsInstance(42, typing.Iterator) def test_awaitable(self): - ns = {} - exec( - "async def foo() -> typing.Awaitable[int]:\n" - " return await AwaitableWrapper(42)\n", - globals(), ns) - foo = ns['foo'] + async def foo() -> typing.Awaitable[int]: + return await AwaitableWrapper(42) g = foo() self.assertIsInstance(g, typing.Awaitable) self.assertNotIsInstance(foo, typing.Awaitable) g.send(None) # Run foo() till completion, to avoid warning. def test_coroutine(self): - ns = {} - exec( - "async def foo():\n" - " return\n", - globals(), ns) - foo = ns['foo'] + async def foo(): + return g = foo() self.assertIsInstance(g, typing.Coroutine) with self.assertRaises(TypeError): @@ -7352,10 +7344,9 @@ def test_no_generator_instantiation(self): typing.Generator[int, int, int]() def test_async_generator(self): - ns = {} - exec("async def f():\n" - " yield 42\n", globals(), ns) - g = ns['f']() + async def f(): + yield 42 + g = f() self.assertIsSubclass(type(g), typing.AsyncGenerator) def test_no_async_generator_instantiation(self): @@ -7442,9 +7433,9 @@ def asend(self, value): def athrow(self, typ, val=None, tb=None): pass - ns = {} - exec('async def g(): yield 0', globals(), ns) - g = ns['g'] + async def g(): yield 0 + g = g() + self.assertIsSubclass(G, typing.AsyncGenerator) self.assertIsSubclass(G, typing.AsyncIterable) self.assertIsSubclass(G, collections.abc.AsyncGenerator) From 6f25644db6b13d00809d260792df8cdca5a00563 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 13 May 2024 13:35:58 -0400 Subject: [PATCH 2/2] address review --- Lib/test/test_typing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 6bc84be33e050f..64c4c497eb8934 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7434,7 +7434,6 @@ def athrow(self, typ, val=None, tb=None): pass async def g(): yield 0 - g = g() self.assertIsSubclass(G, typing.AsyncGenerator) self.assertIsSubclass(G, typing.AsyncIterable)