-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[flake8-pyi] Add autofix for PYI058 #9355
Merged
+845
−226
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
de8f9cd
[flake8-pyi] Add autofix for PYI058
AlexWaygood adef1d7
Address reviews
AlexWaygood 7271472
Merge branch 'main' into pyi058-autofix
charliermarsh 79746d5
Misc. minor tweaks
charliermarsh c2fabf3
Use enum types
charliermarsh 2745f77
Update fixtures
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
214 changes: 153 additions & 61 deletions
214
crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI058.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,174 @@ | ||
import collections.abc | ||
import typing | ||
from collections.abc import AsyncGenerator, Generator | ||
from typing import Any | ||
def scope(): | ||
from collections.abc import Generator | ||
|
||
class IteratorReturningSimpleGenerator1: | ||
def __iter__(self) -> Generator: # PYI058 (use `Iterator`) | ||
return (x for x in range(42)) | ||
class IteratorReturningSimpleGenerator1: | ||
def __iter__(self) -> Generator: | ||
... # PYI058 (use `Iterator`) | ||
|
||
class IteratorReturningSimpleGenerator2: | ||
def __iter__(self, /) -> collections.abc.Generator[str, Any, None]: # PYI058 (use `Iterator`) | ||
"""Fully documented, because I'm a runtime function!""" | ||
yield from "abcdefg" | ||
return None | ||
|
||
class IteratorReturningSimpleGenerator3: | ||
def __iter__(self, /) -> collections.abc.Generator[str, None, typing.Any]: # PYI058 (use `Iterator`) | ||
yield "a" | ||
yield "b" | ||
yield "c" | ||
return | ||
def scope(): | ||
import typing | ||
|
||
class AsyncIteratorReturningSimpleAsyncGenerator1: | ||
def __aiter__(self) -> typing.AsyncGenerator: pass # PYI058 (Use `AsyncIterator`) | ||
class IteratorReturningSimpleGenerator2: | ||
def __iter__(self) -> typing.Generator: | ||
... # PYI058 (use `Iterator`) | ||
|
||
class AsyncIteratorReturningSimpleAsyncGenerator2: | ||
def __aiter__(self, /) -> collections.abc.AsyncGenerator[str, Any]: ... # PYI058 (Use `AsyncIterator`) | ||
|
||
class AsyncIteratorReturningSimpleAsyncGenerator3: | ||
def __aiter__(self, /) -> collections.abc.AsyncGenerator[str, None]: pass # PYI058 (Use `AsyncIterator`) | ||
def scope(): | ||
import collections.abc | ||
|
||
class CorrectIterator: | ||
def __iter__(self) -> Iterator[str]: ... # OK | ||
class IteratorReturningSimpleGenerator3: | ||
def __iter__(self) -> collections.abc.Generator: | ||
... # PYI058 (use `Iterator`) | ||
|
||
class CorrectAsyncIterator: | ||
def __aiter__(self) -> collections.abc.AsyncIterator[int]: ... # OK | ||
|
||
class Fine: | ||
def __iter__(self) -> typing.Self: ... # OK | ||
def scope(): | ||
import collections.abc | ||
from typing import Any | ||
|
||
class StrangeButWeWontComplainHere: | ||
def __aiter__(self) -> list[bytes]: ... # OK | ||
class IteratorReturningSimpleGenerator4: | ||
def __iter__(self, /) -> collections.abc.Generator[str, Any, None]: | ||
... # PYI058 (use `Iterator`) | ||
|
||
def __iter__(self) -> Generator: ... # OK (not in class scope) | ||
def __aiter__(self) -> AsyncGenerator: ... # OK (not in class scope) | ||
|
||
class IteratorReturningComplexGenerator: | ||
def __iter__(self) -> Generator[str, int, bytes]: ... # OK | ||
def scope(): | ||
import collections.abc | ||
import typing | ||
|
||
class AsyncIteratorReturningComplexAsyncGenerator: | ||
def __aiter__(self) -> AsyncGenerator[str, int]: ... # OK | ||
class IteratorReturningSimpleGenerator5: | ||
def __iter__(self, /) -> collections.abc.Generator[str, None, typing.Any]: | ||
... # PYI058 (use `Iterator`) | ||
|
||
class ClassWithInvalidAsyncAiterMethod: | ||
async def __aiter__(self) -> AsyncGenerator: ... # OK | ||
|
||
class IteratorWithUnusualParameters1: | ||
def __iter__(self, foo) -> Generator: ... # OK | ||
def scope(): | ||
from collections.abc import Generator | ||
|
||
class IteratorWithUnusualParameters2: | ||
def __iter__(self, *, bar) -> Generator: ... # OK | ||
class IteratorReturningSimpleGenerator6: | ||
def __iter__(self, /) -> Generator[str, None, None]: | ||
... # PYI058 (use `Iterator`) | ||
|
||
class IteratorWithUnusualParameters3: | ||
def __iter__(self, *args) -> Generator: ... # OK | ||
|
||
class IteratorWithUnusualParameters4: | ||
def __iter__(self, **kwargs) -> Generator: ... # OK | ||
def scope(): | ||
import typing_extensions | ||
|
||
class IteratorWithIterMethodThatReturnsThings: | ||
def __iter__(self) -> Generator: # OK | ||
yield | ||
return 42 | ||
class AsyncIteratorReturningSimpleAsyncGenerator1: | ||
def __aiter__( | ||
self, | ||
) -> typing_extensions.AsyncGenerator: | ||
... # PYI058 (Use `AsyncIterator`) | ||
|
||
class IteratorWithIterMethodThatReceivesThingsFromSend: | ||
def __iter__(self) -> Generator: # OK | ||
x = yield 42 | ||
|
||
class IteratorWithNonTrivialIterBody: | ||
def __iter__(self) -> Generator: # OK | ||
foo, bar, baz = (1, 2, 3) | ||
yield foo | ||
yield bar | ||
yield baz | ||
def scope(): | ||
import collections.abc | ||
|
||
class AsyncIteratorReturningSimpleAsyncGenerator2: | ||
def __aiter__(self, /) -> collections.abc.AsyncGenerator[str, Any]: | ||
... # PYI058 (Use `AsyncIterator`) | ||
|
||
|
||
def scope(): | ||
import collections.abc | ||
|
||
class AsyncIteratorReturningSimpleAsyncGenerator3: | ||
def __aiter__(self, /) -> collections.abc.AsyncGenerator[str, None]: | ||
... # PYI058 (Use `AsyncIterator`) | ||
|
||
|
||
def scope(): | ||
from typing import Iterator | ||
|
||
class CorrectIterator: | ||
def __iter__(self) -> Iterator[str]: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
import collections.abc | ||
|
||
class CorrectAsyncIterator: | ||
def __aiter__(self) -> collections.abc.AsyncIterator[int]: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
import typing | ||
|
||
class Fine: | ||
def __iter__(self) -> typing.Self: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
class StrangeButWeWontComplainHere: | ||
def __aiter__(self) -> list[bytes]: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
from collections.abc import Generator | ||
|
||
def __iter__(self) -> Generator: | ||
... # OK (not in class scope) | ||
|
||
|
||
def scope(): | ||
from collections.abc import AsyncGenerator | ||
|
||
def __aiter__(self) -> AsyncGenerator: | ||
... # OK (not in class scope) | ||
|
||
|
||
def scope(): | ||
from collections.abc import Generator | ||
|
||
class IteratorReturningComplexGenerator: | ||
def __iter__(self) -> Generator[str, int, bytes]: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
from collections.abc import AsyncGenerator | ||
|
||
class AsyncIteratorReturningComplexAsyncGenerator: | ||
def __aiter__(self) -> AsyncGenerator[str, int]: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
from collections.abc import AsyncGenerator | ||
|
||
class ClassWithInvalidAsyncAiterMethod: | ||
async def __aiter__(self) -> AsyncGenerator: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
from collections.abc import Generator | ||
|
||
class IteratorWithUnusualParameters1: | ||
def __iter__(self, foo) -> Generator: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
from collections.abc import Generator | ||
|
||
class IteratorWithUnusualParameters2: | ||
def __iter__(self, *, bar) -> Generator: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
from collections.abc import Generator | ||
|
||
class IteratorWithUnusualParameters3: | ||
def __iter__(self, *args) -> Generator: | ||
... # OK | ||
|
||
|
||
def scope(): | ||
from collections.abc import Generator | ||
|
||
class IteratorWithUnusualParameters4: | ||
def __iter__(self, **kwargs) -> Generator: | ||
... # OK |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I had to scope each test here into its own function so that they can reason about the imports independently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, that makes sense, was wondering if that would be an issue