-
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 PYI032
rule with autofix
#4695
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5231dff
Add boilerplate and fixtures
qdegraaf 591d82c
Add docs, add registry, add first steps to functionality
qdegraaf 5d234dd
Fix violation logic
qdegraaf b7e4585
Add autofix logic
qdegraaf 4049c30
cargo insta review
qdegraaf 20d39ba
Update ruff.schema.json
qdegraaf 6b00fc0
Add `...` to docs
qdegraaf 7f69c5b
Add indent to `...`
qdegraaf 236fd55
Add \n to docs
qdegraaf 3e0e6c8
Tweak docs; shuffle around some calls
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Any | ||
import typing | ||
|
||
|
||
class Bad: | ||
def __eq__(self, other: Any) -> bool: ... # Fine because not a stub file | ||
def __ne__(self, other: typing.Any) -> typing.Any: ... # Fine because not a stub file | ||
|
||
|
||
class Good: | ||
def __eq__(self, other: object) -> bool: ... | ||
|
||
def __ne__(self, obj: object) -> int: ... | ||
|
||
|
||
class WeirdButFine: | ||
def __eq__(self, other: Any, strange_extra_arg: list[str]) -> Any: ... | ||
def __ne__(self, *, kw_only_other: Any) -> bool: ... | ||
|
||
|
||
class Unannotated: | ||
def __eq__(self) -> Any: ... | ||
def __ne__(self) -> bool: ... | ||
|
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Any | ||
import typing | ||
|
||
|
||
class Bad: | ||
def __eq__(self, other: Any) -> bool: ... # Y032 | ||
def __ne__(self, other: typing.Any) -> typing.Any: ... # Y032 | ||
|
||
|
||
class Good: | ||
def __eq__(self, other: object) -> bool: ... | ||
|
||
def __ne__(self, obj: object) -> int: ... | ||
|
||
|
||
class WeirdButFine: | ||
def __eq__(self, other: Any, strange_extra_arg: list[str]) -> Any: ... | ||
def __ne__(self, *, kw_only_other: Any) -> bool: ... | ||
|
||
|
||
class Unannotated: | ||
def __eq__(self) -> Any: ... | ||
def __ne__(self) -> bool: ... | ||
|
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
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
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
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
94 changes: 94 additions & 0 deletions
94
crates/ruff/src/rules/flake8_pyi/rules/any_eq_ne_annotation.rs
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use rustpython_parser::ast::Arguments; | ||
|
||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::registry::AsRule; | ||
|
||
/// ## What it does | ||
/// Checks for `Any` type annotations for the second parameter in `__ne__` and `__eq__` methods | ||
/// | ||
/// ## Why is this bad? | ||
/// From the Python docs: `Use object to indicate that a value could be any type in a typesafe | ||
/// manner. Use Any to indicate that a value is dynamically typed.` For `__eq__` and `__ne__` method | ||
/// we want to do the former. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Foo: | ||
/// def __eq__(self, obj: Any): | ||
/// def __ne__(self, obj: typing.Any): | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Foo: | ||
/// def __eq__(self, obj: object): | ||
/// def __ne__(self, obj: object): | ||
/// ``` | ||
/// ## References | ||
/// - [Python Docs](https://docs.python.org/3/library/typing.html#the-any-type) | ||
/// - [Mypy Docs](https://mypy.readthedocs.io/en/latest/dynamic_typing.html#any-vs-object) | ||
#[violation] | ||
pub struct AnyEqNeAnnotation { | ||
method_name: String, | ||
} | ||
|
||
impl AlwaysAutofixableViolation for AnyEqNeAnnotation { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let AnyEqNeAnnotation { method_name } = self; | ||
format!("Prefer `object` to `Any` for the second parameter in {method_name}") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
format!("Replace `object` with `Any`") | ||
} | ||
} | ||
|
||
/// PYI032 | ||
pub(crate) fn any_eq_ne_annotation(checker: &mut Checker, name: &str, args: &Arguments) { | ||
if !checker.semantic_model().scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
// Ignore non `__eq__` and non `__ne__` methods | ||
if name != "__eq__" && name != "__ne__" { | ||
return; | ||
} | ||
|
||
// Different numbers of arguments than 2 are handled by other rules | ||
if args.args.len() == 2 { | ||
if let Some(annotation) = &args.args[1].annotation { | ||
if checker | ||
.semantic_model() | ||
.match_typing_expr(annotation, "Any") | ||
{ | ||
let mut diagnostic = Diagnostic::new( | ||
AnyEqNeAnnotation { | ||
method_name: name.to_string(), | ||
}, | ||
args.args[1].range, | ||
); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
// def __eq__(self, arg2: Any): ... | ||
if let Some(name) = annotation.as_name_expr() { | ||
diagnostic.set_fix(Fix::automatic(Edit::range_replacement( | ||
format!("object"), | ||
name.range, | ||
))); | ||
} | ||
// def __eq__(self, arg2: typing.Any): ... | ||
if let Some(attr) = annotation.as_attribute_expr() { | ||
diagnostic.set_fix(Fix::automatic(Edit::range_replacement( | ||
format!("object"), | ||
attr.range, | ||
))); | ||
} | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
} | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI032_PYI032.py.snap
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
|
42 changes: 42 additions & 0 deletions
42
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI032_PYI032.pyi.snap
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI032.pyi:6:22: PYI032 [*] Prefer `object` to `Any` for the second parameter in __eq__ | ||
| | ||
6 | class Bad: | ||
7 | def __eq__(self, other: Any) -> bool: ... # Y032 | ||
| ^^^^^^^^^^ PYI032 | ||
8 | def __ne__(self, other: typing.Any) -> typing.Any: ... # Y032 | ||
| | ||
= help: Replace `object` with `Any` | ||
|
||
ℹ Fix | ||
3 3 | | ||
4 4 | | ||
5 5 | class Bad: | ||
6 |- def __eq__(self, other: Any) -> bool: ... # Y032 | ||
6 |+ def __eq__(self, other: object) -> bool: ... # Y032 | ||
7 7 | def __ne__(self, other: typing.Any) -> typing.Any: ... # Y032 | ||
8 8 | | ||
9 9 | | ||
|
||
PYI032.pyi:7:22: PYI032 [*] Prefer `object` to `Any` for the second parameter in __ne__ | ||
| | ||
7 | class Bad: | ||
8 | def __eq__(self, other: Any) -> bool: ... # Y032 | ||
9 | def __ne__(self, other: typing.Any) -> typing.Any: ... # Y032 | ||
| ^^^^^^^^^^^^^^^^^ PYI032 | ||
| | ||
= help: Replace `object` with `Any` | ||
|
||
ℹ Fix | ||
4 4 | | ||
5 5 | class Bad: | ||
6 6 | def __eq__(self, other: Any) -> bool: ... # Y032 | ||
7 |- def __ne__(self, other: typing.Any) -> typing.Any: ... # Y032 | ||
7 |+ def __ne__(self, other: object) -> typing.Any: ... # Y032 | ||
8 8 | | ||
9 9 | | ||
10 10 | class Good: | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I tried combining this logic with the the checker logic from lines 64-66 in a more efficient and elegant manner. Did not succeed in finding a better way, though I believe there are many ways to Rome here, so welcome any advice on making this patch more compact.
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.
Hmm -- is there any issue with just using the range of the annotation? Why do we need to match on the "kind"?
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.
Ah I see what I missed now. Tried that first, but could not get it to work. But I forgot to import:
use ruff_python_ast::prelude::Ranged;
Thanks for edits!