[flake8-pyi] Implement PYI058#9313
Merged
charliermarsh merged 4 commits intoastral-sh:mainfrom Jan 1, 2024
Merged
Conversation
Contributor
|
Member
Author
|
Thanks for the review! I think I tackled all your points :D |
charliermarsh
pushed a commit
that referenced
this pull request
Jan 3, 2024
## Summary This PR adds an autofix for the newly added PYI058 rule (added in #9313). ~~The PR's current implementation is that the fix is only available if the fully qualified name of `Generator` or `AsyncGenerator` is being used:~~ - ~~`-> typing.Generator` is converted to `-> typing.Iterator`;~~ - ~~`-> collections.abc.AsyncGenerator[str, Any]` is converted to `-> collections.abc.AsyncIterator[str]`;~~ - ~~but `-> Generator` is _not_ converted to `-> Iterator`. (It would require more work to figure out if `Iterator` was already imported or not. And if it wasn't, where should we import it from? `typing`, `typing_extensions`, or `collections.abc`? It seems much more complicated.)~~ The fix is marked as always safe for `__iter__` or `__aiter__` methods in `.pyi` files, but unsafe for all such methods in `.py` files that have more than one statement in the method body. This felt slightly fiddly to accomplish, but I couldn't _see_ any utilities in https://github.com/astral-sh/ruff/tree/main/crates/ruff_linter/src/fix that would have made it simpler to implement. Lmk if I'm missing something, though -- my first time implementing an autofix! :) ## Test Plan `cargo test` / `cargo insta review`.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR implements Y058 from flake8-pyi -- this is a new flake8-pyi rule that was released as part of
flake8-pyi 23.11.0. I've followed the Python implementation as closely as possible (see PyCQA/flake8-pyi@858c091), except that for the Ruff version, the rule also applies to.pyfiles as well as for.pyifiles. (For.pyfiles, we only emit the diagnostic in very specific situations, however, as there's a much higher likelihood of emitting false positives when applying this rule to a.pyfile.)Test Plan
cargo test/cargo insta review