Skip to content

Commit 781caff

Browse files
authored
When copying a ForStmt or WithStmt, copy is_async flag (python#9268)
(The test only tests 'async with', but 'async for' had the same bug.) Fixes python#9261.
1 parent c5814e5 commit 781caff

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

mypy/treetransform.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ def visit_for_stmt(self, node: ForStmt) -> ForStmt:
250250
self.block(node.body),
251251
self.optional_block(node.else_body),
252252
self.optional_type(node.unanalyzed_index_type))
253+
new.is_async = node.is_async
253254
new.index_type = self.optional_type(node.index_type)
254255
return new
255256

@@ -293,6 +294,7 @@ def visit_with_stmt(self, node: WithStmt) -> WithStmt:
293294
self.optional_expressions(node.target),
294295
self.block(node.body),
295296
self.optional_type(node.unanalyzed_type))
297+
new.is_async = node.is_async
296298
new.analyzed_types = [self.type(typ) for typ in node.analyzed_types]
297299
return new
298300

test-data/unit/check-async-await.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,16 @@ def t() -> None:
745745

746746
[builtins fixtures/async_await.pyi]
747747
[typing fixtures/typing-async.pyi]
748+
749+
[case testAsyncWithInGenericClass]
750+
from typing import Generic, AsyncContextManager, TypeVar
751+
752+
T = TypeVar('T', str, int)
753+
754+
class Foo(Generic[T]):
755+
async def foo(self, manager: AsyncContextManager):
756+
async with manager:
757+
pass
758+
759+
[builtins fixtures/async_await.pyi]
760+
[typing fixtures/typing-async.pyi]

test-data/unit/fixtures/typing-async.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,8 @@ class ContextManager(Generic[T]):
118118
def __enter__(self) -> T: pass
119119
# Use Any because not all the precise types are in the fixtures.
120120
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: pass
121+
122+
class AsyncContextManager(Generic[T]):
123+
def __aenter__(self) -> Awaitable[T]: pass
124+
# Use Any because not all the precise types are in the fixtures.
125+
def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Awaitable[Any]: pass

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy