10000 Support type comments in async for/with by afrieder · Pull Request #2858 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Support type comments in async for/with #2858

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
Feb 13, 2017
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
18 changes: 14 additions & 4 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,18 @@ def visit_For(self, n: ast3.For) -> ForStmt:
self.as_block(n.orelse, n.lineno),
target_type)

# AsyncFor(expr target, expr iter, stmt* body, stmt* orelse)
# AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
@with_line
def visit_AsyncFor(self, n: ast3.AsyncFor) -> ForStmt:
if n.type_comment is not None:
target_type = parse_type_comment(n.type_comment, n.lineno, self.errors)
else:
target_type = None
r = ForStmt(self.visit(n.target),
self.visit(n.iter),
self.as_block(n.body, n.lineno),
self.as_block(n.orelse, n.lineno))
self.as_block(n.orelse, n.lineno),
target_type)
r.is_async = True
return r

Expand Down Expand Up @@ -568,12 +573,17 @@ def visit_With(self, n: ast3.With) -> WithStmt:
self.as_block(n.body, n.lineno),
target_type)

# AsyncWith(withitem* items, stmt* body)
# AsyncWith(withitem* items, stmt* body, string? type_comment)
@with_line
def visit_AsyncWith(self, n: ast3.AsyncWith) -> WithStmt:
if n.type_comment is not None:
target_type = parse_type_comment(n.type_comment, n.lineno, self.errors)
else:
target_type = None
r = WithStmt([self.visit(i.context_expr) for i in n.items],
[self.visit(i.optional_vars) for i in n.items],
self.as_block(n.body, n.lineno))
self.as_block(n.body, n.lineno),
target_type)
r.is_async = True
return r

Expand Down
34 changes: 33 additions & 1 deletion test-data/unit/check-async-await.test
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ async def f() -> None:
main:4: error: AsyncIterable expected
main:4: error: List[int] has no attribute "__aiter__"

[case testAsyncForTypeComments]
# flags: --fast-parser
from typing import AsyncIterator, Union
class C(AsyncIterator[int]):
async def __anext__(self) -> int: return 0
async def f() -> None:
async for x in C(): # type: str # E: Incompatible types in assignment (expression has type "int", variable has type "str")
pass

async for y in C(): # type: int
pass

async for z in C(): # type: Union[int, str]
reveal_type(z) # E: Revealed type is 'Union[builtins.int, builtins.str]'
[builtins fixtures/async_await.pyi]

[case testAsyncWith]
# flags: --fast-parser
class C:
Expand All @@ -161,7 +177,7 @@ async def f() -> None:
async with C() as x:
reveal_type(x) # E: Revealed type is 'builtins.int*'
[builtins fixtures/async_await.pyi]
[out]


[case testAsyncWithError]
# flags: --fast-parser
Expand Down Expand Up @@ -220,6 +236,22 @@ async def f() -> None:
[builtins fixtures/async_await.pyi]
[out]

[case testAsyncWithTypeComments]
# flags: --fast-parser
class C:
async def __aenter__(self) -> int: pass
async def __aexit__(self, x, y, z) -> None: pass
async def f() -> None:
async with C() as x: # type: int
pass

async with C() as y, C() as z: # type: str, int # E: Incompatible types in assignment (expression has type "int", variable has type "str")
pass

async with C() as a: # type: int, int # E: Invalid tuple literal type
pass
[builtins fixtures/async_await.pyi]

[case testNoYieldInAsyncDef]
# flags: --fast-parser
async def f():
Expand Down
0