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 3 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from functools import cached_property


def x(y):
if not y:
return
Expand All @@ -17,3 +20,8 @@ def get(self, key: str) -> None:
def prop(self) -> None:
print("Property not found")
return None

@cached_property
def prop(self) -> None:
print("Property not found")
return None
12 changes: 9 additions & 3 deletions crates/ruff_linter/src/rules/flake8_return/rules/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;
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::helpers::{is_const_false, is_const_true, map_callable};
use ruff_python_ast::stmt_if::elif_else_range;
use ruff_python_ast::visitor::Visitor;
use ruff_python_ast::whitespace::indentation;
Expand Down Expand Up @@ -373,11 +373,17 @@ fn unnecessary_return_none(checker: &mut Checker, decorator_list: &[Decorator],
continue;
}

// Skip properties.
// Skip properties and cached properties.
if decorator_list.iter().any(|decorator| {
checker
.semantic()
.match_builtin_expr(&decorator.expression, "property")
.resolve_qualified_name(map_callable(&decorator.expression))
.is_some_and(|qualified_name| {
matches!(
qualified_name.segments(),
["" | "builtins", "property"] | ["functools", "cached_property"]
)
})
}) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
---
source: crates/ruff_linter/src/rules/flake8_return/mod.rs
---
RET501.py:4:5: RET501 [*] Do not explicitly `return None` in function if it is the only possible return value
RET501.py:7:5: RET501 [*] Do not explicitly `return None` in function if it is the only possible return value
|
2 | if not y:
3 | return
4 | return None # error
5 | if not y:
6 | return
7 | return None # error
| ^^^^^^^^^^^ RET501
|
= help: Remove explicit `return None`

ℹ Safe fix
1 1 | def x(y):
2 2 | if not y:
3 3 | return
4 |- return None # error
4 |+ return # error
5 5 |
6 6 |
7 7 | class BaseCache:
4 4 | def x(y):
5 5 | if not y:
6 6 | return
7 |- return None # error
7 |+ return # error
8 8 |
9 9 |
10 10 | class BaseCache:

RET501.py:14:9: RET501 [*] Do not explicitly `return None` in function if it is the only possible return value
RET501.py:17:9: RET501 [*] Do not explicitly `return None` in function if it is the only possible return value
|
12 | def get(self, key: str) -> None:
13 | print(f"{key} not found")
14 | return None
15 | def get(self, key: str) -> None:
16 | print(f"{key} not found")
17 | return None
| ^^^^^^^^^^^ RET501
15 |
16 | @property
18 |
19 | @property
|
= help: Remove explicit `return None`

ℹ Safe fix
11 11 |
12 12 | def get(self, key: str) -> None:
13 13 | print(f"{key} not found")
14 |- return None
14 |+ return
15 15 |
16 16 | @property
17 17 | def prop(self) -> None:
14 14 |
15 15 | def get(self, key: str) -> None:
16 16 | print(f"{key} not found")
17 |- return None
17 |+ return
18 18 |
19 19 | @property
20 20 | def prop(self) -> None:
Loading