-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
198 additions
and
0 deletions.
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, Ranged}; | ||
|
||
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 `__eq__` and `__ne__` implementations that use `typing.Any` as | ||
/// the type annotation for the `obj` parameter. | ||
/// | ||
/// ## Why is this bad? | ||
/// The Python documentation recommends the use of `object` to "indicate that a | ||
/// value could be any type in a typesafe manner", while `Any` should be used to | ||
/// "indicate that a value is dynamically typed." | ||
/// | ||
/// The semantics of `__eq__` and `__ne__` are such that the `obj` parameter | ||
/// should be any type, as opposed to a dynamically typed value. Therefore, the | ||
/// `object` type annotation is more appropriate. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Foo: | ||
/// def __eq__(self, obj: typing.Any): | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Foo: | ||
/// def __eq__(self, obj: object): | ||
/// ... | ||
/// ``` | ||
/// ## References | ||
/// - [Python documentation](https://docs.python.org/3/library/typing.html#the-any-type) | ||
/// - [Mypy documentation](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 to `{method_name}`") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
format!("Replace with `object`") | ||
} | ||
} | ||
|
||
/// PYI032 | ||
pub(crate) fn any_eq_ne_annotation(checker: &mut Checker, name: &str, args: &Arguments) { | ||
if !matches!(name, "__eq__" | "__ne__") { | ||
return; | ||
} | ||
|
||
if args.args.len() != 2 { | ||
return; | ||
} | ||
|
||
let Some(annotation) = &args.args[1].annotation else { | ||
return; | ||
}; | ||
|
||
if !checker.semantic_model().scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
if checker | ||
.semantic_model() | ||
.match_typing_expr(annotation, "Any") | ||
{ | ||
let mut diagnostic = Diagnostic::new( | ||
AnyEqNeAnnotation { | ||
method_name: name.to_string(), | ||
}, | ||
annotation.range(), | ||
); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
// Ex) `def __eq__(self, obj: Any): ...` | ||
if checker.semantic_model().is_builtin("object") { | ||
diagnostic.set_fix(Fix::automatic(Edit::range_replacement( | ||
"object".to_string(), | ||
annotation.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:29: PYI032 [*] Prefer `object` to `Any` for the second parameter to `__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 with `object` | ||
|
||
ℹ 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:29: PYI032 [*] Prefer `object` to `Any` for the second parameter to `__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 with `object` | ||
|
||
ℹ 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.