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
16 changes: 13 additions & 3 deletions .rules/python-return.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ Ensure all branches explicitly return a value if any branch does.

______________________________________________________________________

## R503 — Add an Explicit Return at the End
## R503 — Add an Explicit Return at the End When a Function May Return a Value

```python
# BAD:
def func(x):
if x > 0:
return x
# no return (bad)
# missing terminal return (bad)


# GOOD:
Expand All @@ -60,7 +60,17 @@ def func(x):
return -1
```

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.

Functions whose only possible result is `None` do not need a final bare `return`:

```python
# GOOD:
def func():
do_something()
# implicit None is fine here
```

______________________________________________________________________

Expand Down
Loading