-
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.
- Loading branch information
Showing
11 changed files
with
192 additions
and
39 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
crates/ruff/resources/test/fixtures/pylint/return_in_init.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,41 @@ | ||
def a(): | ||
return | ||
|
||
def __init__(): | ||
return | ||
|
||
class A: | ||
def __init__(self): | ||
return | ||
|
||
|
||
class B: | ||
def __init__(self): | ||
return 3 | ||
|
||
def gen(self): | ||
return 5 | ||
|
||
class MyClass: | ||
|
||
def __init__(self): | ||
return 1 | ||
|
||
class MyClass2: | ||
"""dummy class""" | ||
|
||
def __init__(self): | ||
return | ||
|
||
|
||
class MyClass3: | ||
"""dummy class""" | ||
|
||
def __init__(self): | ||
return None | ||
|
||
class MyClass5: | ||
"""dummy class""" | ||
|
||
def __init__(self): | ||
self.callable = lambda: (yield None) |
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,38 @@ | ||
use crate::ast::function_type; | ||
use crate::ast::function_type::FunctionType; | ||
use crate::{ | ||
ast::types::{FunctionDef, ScopeKind}, | ||
checkers::ast::Checker, | ||
}; | ||
|
||
pub fn in_dunder_init(checker: &mut Checker) -> bool { | ||
let scope = checker.current_scope(); | ||
let ScopeKind::Function(FunctionDef { | ||
name, | ||
decorator_list, | ||
.. | ||
}) = &scope.kind else { | ||
return false; | ||
}; | ||
if *name != "__init__" { | ||
return false; | ||
} | ||
let Some(parent) = checker.current_scope_parent() else { | ||
return false; | ||
}; | ||
|
||
if !matches!( | ||
function_type::classify( | ||
checker, | ||
parent, | ||
name, | ||
decorator_list, | ||
&checker.settings.pep8_naming.classmethod_decorators, | ||
&checker.settings.pep8_naming.staticmethod_decorators, | ||
), | ||
FunctionType::Method | ||
) { | ||
return false; | ||
} | ||
true | ||
} |
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,72 @@ | ||
use rustpython_parser::ast::{Constant, ExprKind, Stmt, StmtKind}; | ||
|
||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::Diagnostic; | ||
use crate::rules::pylint::helpers::in_dunder_init; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
/// ## What it does | ||
/// Checks for `__init__` methods that return values. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `__init__` method is the constructor for a given Python class, | ||
/// responsible for initializing, rather than creating, new objects. | ||
/// | ||
/// The `__init__` method has to return `None`. Returning any value from | ||
/// an `__init__` method will result in a runtime error. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Example: | ||
/// def __init__(self): | ||
/// return [] | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Example: | ||
/// def __init__(self): | ||
/// self.value = [] | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// * [CodeQL: `py-explicit-return-in-init`](https://codeql.github.com/codeql-query-help/python/py-explicit-return-in-init/) | ||
pub struct ReturnInInit; | ||
); | ||
impl Violation for ReturnInInit { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Explicit return in `__init__`") | ||
} | ||
} | ||
|
||
/// PLE0101 | ||
pub fn return_in_init(checker: &mut Checker, stmt: &Stmt) { | ||
if let StmtKind::Return { value } = &stmt.node { | ||
if let Some(expr) = value { | ||
if matches!( | ||
expr.node, | ||
ExprKind::Constant { | ||
value: Constant::None, | ||
.. | ||
} | ||
) { | ||
// Explicit `return None`. | ||
return; | ||
} | ||
} else { | ||
// Implicit `return`. | ||
return; | ||
} | ||
} | ||
|
||
if in_dunder_init(checker) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(ReturnInInit, Range::from_located(stmt))); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
...uff/src/rules/pylint/snapshots/ruff__rules__pylint__tests__PLE0101_return_in_init.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,25 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
ReturnInInit: ~ | ||
location: | ||
row: 14 | ||
column: 8 | ||
end_location: | ||
row: 14 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
ReturnInInit: ~ | ||
location: | ||
row: 22 | ||
column: 8 | ||
end_location: | ||
row: 22 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
|
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 |
---|---|---|
|
@@ -1778,6 +1778,7 @@ | |
"PLE01", | ||
"PLE010", | ||
"PLE0100", | ||
"PLE0101", | ||
"PLE011", | ||
"PLE0117", | ||
"PLE0118", | ||
|