-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pylint] Implement
eq-without-hash
rule (PLW1641) (#5955)
Implement https://pylint.pycqa.org/en/latest/user_guide/messages/warning/eq-without-hash.html Issue #970 It's not enabled by default in pylint, so I guess it shouldn't in Ruff either?
- Loading branch information
Showing
8 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
crates/ruff/resources/test/fixtures/pylint/eq_without_hash.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Person: | ||
def __init__(self): | ||
self.name = "monty" | ||
|
||
def __eq__(self, other): | ||
return isinstance(other, Person) and other.name == self.name | ||
|
||
class Language: | ||
def __init__(self): | ||
self.name = "python" | ||
|
||
def __eq__(self, other): | ||
return isinstance(other, Language) and other.name == self.name | ||
|
||
def __hash__(self): | ||
return hash(self.name) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use ruff_python_ast::{self as ast, Ranged, Stmt}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for classes that implement `__eq__` but not `__hash__`. | ||
/// | ||
/// ## Why is this bad? | ||
/// A class that implements `__eq__` but not `__hash__` will have its hash | ||
/// method implicitly set to `None`. This will cause the class to be | ||
/// unhashable, will in turn cause issues when using the class as a key in a | ||
/// dictionary or a member of a set. | ||
/// | ||
/// ## Known problems | ||
/// Does not check for `__hash__` implementations in superclasses. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Person: | ||
/// def __init__(self): | ||
/// self.name = "monty" | ||
/// | ||
/// def __eq__(self, other): | ||
/// return isinstance(other, Person) and other.name == self.name | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Person: | ||
/// def __init__(self): | ||
/// self.name = "monty" | ||
/// | ||
/// def __eq__(self, other): | ||
/// return isinstance(other, Person) and other.name == self.name | ||
/// | ||
/// def __hash__(self): | ||
/// return hash(self.name) | ||
/// ``` | ||
#[violation] | ||
pub struct EqWithoutHash; | ||
|
||
impl Violation for EqWithoutHash { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Object does not implement `__hash__` method") | ||
} | ||
} | ||
|
||
/// W1641 | ||
pub(crate) fn object_without_hash_method( | ||
checker: &mut Checker, | ||
ast::StmtClassDef { name, body, .. }: &ast::StmtClassDef, | ||
) { | ||
if has_eq_without_hash(body) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(EqWithoutHash, name.range())); | ||
} | ||
} | ||
|
||
fn has_eq_without_hash(body: &[Stmt]) -> bool { | ||
let mut has_hash = false; | ||
let mut has_eq = false; | ||
for statement in body { | ||
let Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) = statement else { | ||
continue; | ||
}; | ||
match name.as_str() { | ||
"__hash__" => has_hash = true, | ||
"__eq__" => has_eq = true, | ||
_ => {} | ||
} | ||
} | ||
has_eq && !has_hash | ||
} |
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
12 changes: 12 additions & 0 deletions
12
...ff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLW1641_eq_without_hash.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,12 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
--- | ||
eq_without_hash.py:1:7: PLW1641 Object does not implement `__hash__` method | ||
| | ||
1 | class Person: | ||
| ^^^^^^ PLW1641 | ||
2 | def __init__(self): | ||
3 | self.name = "monty" | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.