mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:23:11 +00:00
[syntax-errors] Async comprehension in sync comprehension (#17177)
Summary -- Detect async comprehensions nested in sync comprehensions in async functions before Python 3.11, when this was [changed]. The actual logic of this rule is very straightforward, but properly tracking the async scopes took a bit of work. An alternative to the current approach is to offload the `in_async_context` check into the `SemanticSyntaxContext` trait, but that actually required much more extensive changes to the `TestContext` and also to ruff's semantic model, as you can see in the changes up to 31554b473507034735bd410760fde6341d54a050. This version has the benefit of mostly centralizing the state tracking in `SemanticSyntaxChecker`, although there was some subtlety around deferred function body traversal that made the changes to `Checker` more intrusive too (hence the new linter test). The `Checkpoint` struct/system is obviously overkill for now since it's only tracking a single `bool`, but I thought it might be more useful later. [changed]: https://github.com/python/cpython/issues/77527 Test Plan -- New inline tests and a new linter integration test.
This commit is contained in:
parent
dc02732d4d
commit
058439d5d3
18 changed files with 2076 additions and 28 deletions
|
@ -0,0 +1,2 @@
|
|||
# parse_options: {"target-version": "3.10"}
|
||||
async def test(): return [[x async for x in elements(n)] async for n in range(3)]
|
|
@ -0,0 +1,9 @@
|
|||
# parse_options: {"target-version": "3.10"}
|
||||
# this case fails if exit_expr doesn't run
|
||||
async def f():
|
||||
[_ for n in range(3)]
|
||||
[_ async for n in range(3)]
|
||||
# and this fails without exit_stmt
|
||||
async def f():
|
||||
def g(): ...
|
||||
[_ async for n in range(3)]
|
|
@ -0,0 +1,4 @@
|
|||
# parse_options: {"target-version": "3.11"}
|
||||
async def f(): return [[x async for x in foo(n)] for n in range(3)] # list
|
||||
async def g(): return [{x: 1 async for x in foo(n)} for n in range(3)] # dict
|
||||
async def h(): return [{x async for x in foo(n)} for n in range(3)] # set
|
Loading…
Add table
Add a link
Reference in a new issue