Repository: python/mypy

Issue:
Mypy tries checking for __enter__ and __exit__ for "async with" under certain conditions
Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

* Are you reporting a bug, or opening a feature request?

Reporting a bug.

* Please insert below the code you are checking with mypy,
  or a mock-up repro if the source is private. We would appreciate
  if you try to simplify your case to a minimal repro.

```python3
from typing import Generic, AsyncContextManager, TypeVar

T_Item = TypeVar('T_Item', str, int)

class Foo(Generic[T_Item]):
    async def foo(self, manager: AsyncContextManager):
        async with manager:
            pass

```
Strangely, removing the typevar constraints makes the problem go away.

* What is the actual behavior/output?
```
bug.py:7: error: "AsyncContextManager[Any]" has no attribute "__enter__"; maybe "__aenter__"?
bug.py:7: error: "AsyncContextManager[Any]" has no attribute "__exit__"; maybe "__aexit__"?
```
* What is the behavior/output you expect?

No errors.

* What are the versions of mypy and Python you are using?
  Do you see the same issue after installing mypy from Git master?

Mypy 0.782.
Yes, this also happens with master (7938f5d0025bf82c21bfe72b8bcb9add7cd58a77).

* What are the mypy flags you are using? (For example --strict-optional)

No flags used.



Discussion:
Thanks for the report! It's a simple bug, I have a fix coming up. 

Resolving patch:
diff --git a/mypy/treetransform.py b/mypy/treetransform.py
--- a/mypy/treetransform.py
+++ b/mypy/treetransform.py
@@ -250,6 +250,7 @@ def visit_for_stmt(self, node: ForStmt) -> ForStmt:
                       self.block(node.body),
                       self.optional_block(node.else_body),
                       self.optional_type(node.unanalyzed_index_type))
+        new.is_async = node.is_async
         new.index_type = self.optional_type(node.index_type)
         return new
 
@@ -293,6 +294,7 @@ def visit_with_stmt(self, node: WithStmt) -> WithStmt:
                        self.optional_expressions(node.target),
                        self.block(node.body),
                        self.optional_type(node.unanalyzed_type))
+        new.is_async = node.is_async
         new.analyzed_types = [self.type(typ) for typ in node.analyzed_types]
         return new
 


Test patch:
diff --git a/test-data/unit/check-async-await.test b/test-data/unit/check-async-await.test
--- a/test-data/unit/check-async-await.test
+++ b/test-data/unit/check-async-await.test
@@ -745,3 +745,16 @@ def t() -> None:
 
 [builtins fixtures/async_await.pyi]
 [typing fixtures/typing-async.pyi]
+
+[case testAsyncWithInGenericClass]
+from typing import Generic, AsyncContextManager, TypeVar
+
+T = TypeVar('T', str, int)
+
+class Foo(Generic[T]):
+    async def foo(self, manager: AsyncContextManager):
+        async with manager:
+            pass
+
+[builtins fixtures/async_await.pyi]
+[typing fixtures/typing-async.pyi]