Skip to content
Merged
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
14 changes: 9 additions & 5 deletions .rules/python-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ def func(x):
def func(x):
if x > 0:
return x
return 0
return
```

Ensure all branches explicitly return a value if any branch does.
Ensure all branches return explicitly if any branch returns a value. Use a bare
`return` when the other result is `None`, as required by R501.
Comment on lines +40 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Replace bare returns in mixed-value examples

When one branch returns x, a bare return is precisely what RET502 rejects: running ruff check --isolated --select RET501,RET502,RET503 on this example reports “RET502 Do not implicitly return None in function able to return non-None value.” R501 only removes explicit None when None is the function's only possible result, so both new “GOOD” examples fail the configured RET lint; use return None or another explicit value instead.

AGENTS.md reference: AGENTS.md:L199-L200

Useful? React with 👍 / 👎.


______________________________________________________________________

## R503 — Add an Explicit Return at the End
## R503 — Add a Terminal Return When Another Path Returns a Value

```python
# BAD:
Expand All @@ -54,10 +55,13 @@ def func(x):
def func(x):
if x > 0:
return x
return -1
return
```

Don't rely on implicit `None`—always return something at the end.
Don't rely on implicit `None` if the function may return a value
elsewhere—always return something at the end. This requirement does not apply
to functions whose only possible result is `None`; they do not need a
redundant bare `return` at the end.

______________________________________________________________________

Expand Down
Loading