Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-return] Exempt cached properties and other property-like decorators from explicit return rule (RET501) #12563

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_return/RET501.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ def get(self, key: str) -> None:
def prop(self) -> None:
print("Property not found")
return None


from functools import cached_property


class BaseCache2:
@cached_property
def prop(self) -> None:
print("Property not found")
return None
22 changes: 16 additions & 6 deletions crates/ruff_linter/src/rules/flake8_return/rules/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use ruff_diagnostics::{AlwaysFixableViolation, FixAvailability, Violation};
use ruff_diagnostics::{Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::{is_const_false, is_const_true};
use ruff_python_ast::name::QualifiedName;
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, Decorator, ElifElseClause, Expr, Stmt};
use ruff_python_codegen::Stylist;
use ruff_python_index::Indexer;
use ruff_python_semantic::analyze::visibility::is_property;
use ruff_python_semantic::SemanticModel;
use ruff_python_trivia::{is_python_whitespace, SimpleTokenKind, SimpleTokenizer};
use ruff_source_file::Locator;
Expand Down Expand Up @@ -373,12 +375,20 @@ fn unnecessary_return_none(checker: &mut Checker, decorator_list: &[Decorator],
continue;
}

// Skip properties.
if decorator_list.iter().any(|decorator| {
checker
.semantic()
.match_builtin_expr(&decorator.expression, "property")
}) {
let extra_property_decorators = checker
.settings
.pydocstyle
.property_decorators
.iter()
.map(|decorator| QualifiedName::from_dotted_name(decorator))
.collect::<Vec<QualifiedName>>();

// Skip property functions
if is_property(
decorator_list,
&extra_property_decorators,
checker.semantic(),
) {
return;
}

Expand Down
Loading