-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-pyi
] Implement PYI029 (#4851)
- Loading branch information
Showing
10 changed files
with
270 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,57 @@ | ||
import builtins | ||
from abc import abstractmethod | ||
|
||
|
||
def __repr__(self) -> str: | ||
... | ||
|
||
|
||
def __str__(self) -> builtins.str: | ||
... | ||
|
||
|
||
def __repr__(self, /, foo) -> str: | ||
... | ||
|
||
|
||
def __repr__(self, *, foo) -> str: | ||
... | ||
|
||
|
||
class ShouldRemoveSingle: | ||
def __str__(self) -> builtins.str: | ||
... | ||
|
||
|
||
class ShouldRemove: | ||
def __repr__(self) -> str: | ||
... | ||
|
||
def __str__(self) -> builtins.str: | ||
... | ||
|
||
|
||
class NoReturnSpecified: | ||
def __str__(self): | ||
... | ||
|
||
def __repr__(self): | ||
... | ||
|
||
|
||
class NonMatchingArgs: | ||
def __str__(self, *, extra) -> builtins.str: | ||
... | ||
|
||
def __repr__(self, /, extra) -> str: | ||
... | ||
|
||
|
||
class MatchingArgsButAbstract: | ||
@abstractmethod | ||
def __str__(self) -> builtins.str: | ||
... | ||
|
||
@abstractmethod | ||
def __repr__(self) -> str: | ||
... |
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,28 @@ | ||
import builtins | ||
from abc import abstractmethod | ||
|
||
def __repr__(self) -> str: ... | ||
def __str__(self) -> builtins.str: ... | ||
def __repr__(self, /, foo) -> str: ... | ||
def __repr__(self, *, foo) -> str: ... | ||
|
||
class ShouldRemoveSingle: | ||
def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
|
||
class ShouldRemove: | ||
def __repr__(self) -> str: ... # Error: PYI029 | ||
def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
|
||
class NoReturnSpecified: | ||
def __str__(self): ... | ||
def __repr__(self): ... | ||
|
||
class NonMatchingArgs: | ||
def __str__(self, *, extra) -> builtins.str: ... | ||
def __repr__(self, /, extra) -> str: ... | ||
|
||
class MatchingArgsButAbstract: | ||
@abstractmethod | ||
def __str__(self) -> builtins.str: ... | ||
@abstractmethod | ||
def __repr__(self) -> str: ... |
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
110 changes: 110 additions & 0 deletions
110
crates/ruff/src/rules/flake8_pyi/rules/str_or_repr_defined_in_stub.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,110 @@ | ||
use rustpython_parser::ast; | ||
use rustpython_parser::ast::Stmt; | ||
|
||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::identifier_range; | ||
use ruff_python_semantic::analyze::visibility::is_abstract; | ||
|
||
use crate::autofix::edits::delete_stmt; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::AsRule; | ||
|
||
/// ## What it does | ||
/// Checks for redundant definitions of `__str__` or `__repr__` in stubs. | ||
/// | ||
/// ## Why is this bad? | ||
/// Defining `__str__` or `__repr__` in a stub is almost always redundant, | ||
/// as the signatures are almost always identical to those of the default | ||
/// equivalent, `object.__str__` and `object.__repr__`, respectively. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Foo: | ||
/// def __repr__(self) -> str: | ||
/// ... | ||
/// ``` | ||
#[violation] | ||
pub struct StrOrReprDefinedInStub { | ||
name: String, | ||
} | ||
|
||
impl AlwaysAutofixableViolation for StrOrReprDefinedInStub { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let StrOrReprDefinedInStub { name } = self; | ||
format!("Defining `{name}` in a stub is almost always redundant") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
let StrOrReprDefinedInStub { name } = self; | ||
format!("Remove definition of `{name}`") | ||
} | ||
} | ||
|
||
/// PYI029 | ||
pub(crate) fn str_or_repr_defined_in_stub(checker: &mut Checker, stmt: &Stmt) { | ||
let Stmt::FunctionDef(ast::StmtFunctionDef { | ||
name, | ||
decorator_list, | ||
returns, | ||
args, | ||
.. | ||
}) = stmt else { | ||
return | ||
}; | ||
|
||
let Some(returns) = returns else { | ||
return; | ||
}; | ||
|
||
if !matches!(name.as_str(), "__str__" | "__repr__") { | ||
return; | ||
} | ||
|
||
if !checker.semantic_model().scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
// It is a violation only if the method signature matches that of `object.__str__` | ||
// or `object.__repr__` exactly and the method is not decorated as abstract. | ||
if !args.kwonlyargs.is_empty() || (args.args.len() + args.posonlyargs.len()) > 1 { | ||
return; | ||
} | ||
|
||
if is_abstract(checker.semantic_model(), decorator_list) { | ||
return; | ||
} | ||
|
||
if checker | ||
.semantic_model() | ||
.resolve_call_path(returns) | ||
.map_or(true, |call_path| { | ||
!matches!(call_path.as_slice(), ["" | "builtins", "str"]) | ||
}) | ||
{ | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new( | ||
StrOrReprDefinedInStub { | ||
name: name.to_string(), | ||
}, | ||
identifier_range(stmt, checker.locator), | ||
); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
let stmt = checker.semantic_model().stmt(); | ||
let parent = checker.semantic_model().stmt_parent(); | ||
let edit = delete_stmt( | ||
stmt, | ||
parent, | ||
checker.locator, | ||
checker.indexer, | ||
checker.stylist, | ||
); | ||
diagnostic.set_fix( | ||
Fix::automatic(edit).isolate(checker.isolation(checker.semantic_model().stmt_parent())), | ||
); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} |
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI029_PYI029.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 | ||
--- | ||
|
62 changes: 62 additions & 0 deletions
62
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI029_PYI029.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,62 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI029.pyi:10:9: PYI029 [*] Defining `__str__` in a stub is almost always redundant | ||
| | ||
10 | class ShouldRemoveSingle: | ||
11 | def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
| ^^^^^^^ PYI029 | ||
12 | | ||
13 | class ShouldRemove: | ||
| | ||
= help: Remove definition of `str` | ||
|
||
ℹ Fix | ||
7 7 | def __repr__(self, *, foo) -> str: ... | ||
8 8 | | ||
9 9 | class ShouldRemoveSingle: | ||
10 |- def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
10 |+ pass # Error: PYI029 | ||
11 11 | | ||
12 12 | class ShouldRemove: | ||
13 13 | def __repr__(self) -> str: ... # Error: PYI029 | ||
|
||
PYI029.pyi:13:9: PYI029 [*] Defining `__repr__` in a stub is almost always redundant | ||
| | ||
13 | class ShouldRemove: | ||
14 | def __repr__(self) -> str: ... # Error: PYI029 | ||
| ^^^^^^^^ PYI029 | ||
15 | def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
| | ||
= help: Remove definition of `repr` | ||
|
||
ℹ Fix | ||
10 10 | def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
11 11 | | ||
12 12 | class ShouldRemove: | ||
13 |- def __repr__(self) -> str: ... # Error: PYI029 | ||
14 13 | def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
15 14 | | ||
16 15 | class NoReturnSpecified: | ||
|
||
PYI029.pyi:14:9: PYI029 [*] Defining `__str__` in a stub is almost always redundant | ||
| | ||
14 | class ShouldRemove: | ||
15 | def __repr__(self) -> str: ... # Error: PYI029 | ||
16 | def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
| ^^^^^^^ PYI029 | ||
17 | | ||
18 | class NoReturnSpecified: | ||
| | ||
= help: Remove definition of `str` | ||
|
||
ℹ Fix | ||
11 11 | | ||
12 12 | class ShouldRemove: | ||
13 13 | def __repr__(self) -> str: ... # Error: PYI029 | ||
14 |- def __str__(self) -> builtins.str: ... # Error: PYI029 | ||
15 14 | | ||
16 15 | class NoReturnSpecified: | ||
17 16 | def __str__(self): ... | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.