-
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.
[pylint] Implement
eq-without-hash
rule (PLW1641)
- Loading branch information
Showing
8 changed files
with
113 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,75 @@ | ||
use rustpython_parser::ast::{self, Ranged, Stmt}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks that a class that implements a `__eq__` also implements `__hash__`. | ||
/// | ||
/// ## Why is this bad? | ||
/// A class that overrides eq() and does not define hash() will have its hash() | ||
/// implicitly set to None. | ||
/// | ||
/// ## 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 !hash_eq_without_hash(body) { | ||
return; | ||
} | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(EqWithoutHash, name.range())); | ||
} | ||
|
||
fn hash_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; | ||
}; | ||
if name == "__hash__" { | ||
has_hash = true; | ||
} else if name == "__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.