Compare commits

...

6 Commits

Author SHA1 Message Date
Charlie Marsh
b974d1a746 Move gate 2024-07-08 19:30:59 -07:00
Charlie Marsh
b9438eb57a Merge branch 'main' into 20240708-1515 2024-07-08 19:21:48 -07:00
epenet
75e65254e5 Update snap 2024-07-08 16:10:14 +02:00
epenet
225a4169c4 Remove duplicate code 2024-07-08 16:10:08 +02:00
epenet
dc3773b878 Update snap 2024-07-08 15:57:49 +02:00
epenet
60f18bd0ef [flake8-return] Exempt properties (RET501) 2024-07-08 13:43:41 +00:00
4 changed files with 32 additions and 5 deletions

View File

@@ -12,3 +12,8 @@ class BaseCache:
def get(self, key: str) -> None:
print(f"{key} not found")
return None
@property
def prop(self) -> None:
print("Property not found")
return None

View File

@@ -223,7 +223,12 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
Rule::SuperfluousElseContinue,
Rule::SuperfluousElseBreak,
]) {
flake8_return::rules::function(checker, body, returns.as_ref().map(AsRef::as_ref));
flake8_return::rules::function(
checker,
body,
decorator_list,
returns.as_ref().map(AsRef::as_ref),
);
}
if checker.enabled(Rule::UselessReturn) {
pylint::rules::useless_return(

View File

@@ -9,7 +9,7 @@ use ruff_python_ast::helpers::{is_const_false, is_const_true};
use ruff_python_ast::stmt_if::elif_else_range;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::whitespace::indentation;
use ruff_python_ast::{self as ast, ElifElseClause, Expr, Stmt};
use ruff_python_ast::{self as ast, Decorator, ElifElseClause, Expr, Stmt};
use ruff_python_codegen::Stylist;
use ruff_python_index::Indexer;
use ruff_python_semantic::SemanticModel;
@@ -731,7 +731,12 @@ fn superfluous_elif_else(checker: &mut Checker, stack: &Stack) {
}
/// Run all checks from the `flake8-return` plugin.
pub(crate) fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Expr>) {
pub(crate) fn function(
checker: &mut Checker,
body: &[Stmt],
decorator_list: &[Decorator],
returns: Option<&Expr>,
) {
// Find the last statement in the function.
let Some(last_stmt) = body.last() else {
// Skip empty functions.
@@ -787,6 +792,15 @@ pub(crate) fn function(checker: &mut Checker, body: &[Stmt], returns: Option<&Ex
if checker.enabled(Rule::UnnecessaryReturnNone) {
// Skip functions that have a return annotation that is not `None`.
if returns.map_or(true, Expr::is_none_literal_expr) {
// Skip properties.
if decorator_list.iter().any(|decorator| {
checker
.semantic()
.match_builtin_expr(&decorator.expression, "property")
}) {
return;
}
unnecessary_return_none(checker, &stack);
}
}

View File

@@ -26,6 +26,8 @@ RET501.py:14:9: RET501 [*] Do not explicitly `return None` in function if it is
13 | print(f"{key} not found")
14 | return None
| ^^^^^^^^^^^ RET501
15 |
16 | @property
|
= help: Remove explicit `return None`
@@ -35,5 +37,6 @@ RET501.py:14:9: RET501 [*] Do not explicitly `return None` in function if it is
13 13 | print(f"{key} not found")
14 |- return None
14 |+ return
15 15 |
16 16 | @property
17 17 | def prop(self) -> None: