Skip to content
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] Implement PYI058 #9313

Merged
merged 4 commits into from
Jan 1, 2024
Merged

Conversation

AlexWaygood
Copy link
Member

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 .py files as well as for .pyi files. (For .py files, 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 .py file.)

Test Plan

cargo test/cargo insta review

Copy link
Contributor

github-actions bot commented Dec 29, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels Dec 31, 2023
@AlexWaygood
Copy link
Member Author

AlexWaygood commented Jan 1, 2024

Thanks for the review! I think I tackled all your points :D

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

@charliermarsh charliermarsh merged commit 15f6213 into astral-sh:main Jan 1, 2024
17 checks passed
@AlexWaygood AlexWaygood deleted the pyi058 branch January 1, 2024 11:39
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants