Skip to content

PERF402 triggers, incorrectly, on async for loops #10787

@sliedes

Description

@sliedes

Searched keywords: PERF402. #10322 is similar, but more complicated (and closed by the submitter).

This code triggers PERF402 on ruff 0.3.5:

from typing import AsyncIterator


async def some_async_iterable() -> AsyncIterator[int]:
    for i in range(10):
        yield i


async def foo() -> None:
    values: list[int] = []
    async for i in some_async_iterable():
        values.append(i)

Result from running ruff check --isolated --select PERF402:

bug.py:12:9: PERF402 Use `list` or `list.copy` to create a copy of a list
Found 1 error.

But list and list.copy cannot be used on an async iterator. #10322 suggested using an async list comprehension, but I think that's a different animal; in the very least the error message is wrong, and a check that would suggest using a list comprehension instead of a loop would be rather different in spirit.

Metadata

Metadata

Assignees

Labels

ruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions