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

Handle raises with implicit alternate branches #9377

Merged
merged 2 commits into from
Jan 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,41 @@ def func(x: int):
if x > 0:
return 1
raise ValueError


def func(x: int):
if x > 5:
raise ValueError
else:
pass


def func(x: int):
if x > 5:
raise ValueError
elif x > 10:
pass


def func(x: int):
if x > 5:
raise ValueError
elif x > 10:
return 5


def func():
try:
return 5
except:
pass

raise ValueError


def func(x: int):
match x:
case [1, 2, 3]:
return 1
case y:
return "foo"
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/flake8_annotations/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub(crate) fn auto_return_type(function: &ast::StmtFunctionDef) -> Option<AutoPy
// if x > 0:
// return 1
// ```
if terminal == Terminal::ConditionalReturn || terminal == Terminal::None {
if terminal.has_implicit_return() {
return_type = return_type.union(ResolvedPythonType::Atom(PythonType::None));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ auto_return_type.py:82:5: ANN201 [*] Missing return type annotation for public f
83 | match x:
84 | case [1, 2, 3]:
|
= help: Add return type annotation: `str | int`
= help: Add return type annotation: `str | int | None`

ℹ Unsafe fix
79 79 | return 1
80 80 |
81 81 |
82 |-def func(x: int):
82 |+def func(x: int) -> str | int:
82 |+def func(x: int) -> str | int | None:
83 83 | match x:
84 84 | case [1, 2, 3]:
85 85 | return 1
Expand Down Expand Up @@ -396,14 +396,14 @@ auto_return_type.py:137:5: ANN201 [*] Missing return type annotation for public
138 | try:
139 | return 1
|
= help: Add return type annotation: `int`
= help: Add return type annotation: `int | None`

ℹ Unsafe fix
134 134 | return 2
135 135 |
136 136 |
137 |-def func(x: int):
137 |+def func(x: int) -> int:
137 |+def func(x: int) -> int | None:
138 138 | try:
139 139 | return 1
140 140 | except:
Expand Down Expand Up @@ -674,4 +674,99 @@ auto_return_type.py:262:5: ANN201 [*] Missing return type annotation for public
264 264 | if x > 0:
265 265 | return 1

auto_return_type.py:269:5: ANN201 [*] Missing return type annotation for public function `func`
|
269 | def func(x: int):
| ^^^^ ANN201
270 | if x > 5:
271 | raise ValueError
|
= help: Add return type annotation: `None`

ℹ Unsafe fix
266 266 | raise ValueError
267 267 |
268 268 |
269 |-def func(x: int):
269 |+def func(x: int) -> None:
270 270 | if x > 5:
271 271 | raise ValueError
272 272 | else:

auto_return_type.py:276:5: ANN201 [*] Missing return type annotation for public function `func`
|
276 | def func(x: int):
| ^^^^ ANN201
277 | if x > 5:
278 | raise ValueError
|
= help: Add return type annotation: `None`

ℹ Unsafe fix
273 273 | pass
274 274 |
275 275 |
276 |-def func(x: int):
276 |+def func(x: int) -> None:
277 277 | if x > 5:
278 278 | raise ValueError
279 279 | elif x > 10:

auto_return_type.py:283:5: ANN201 [*] Missing return type annotation for public function `func`
|
283 | def func(x: int):
| ^^^^ ANN201
284 | if x > 5:
285 | raise ValueError
|
= help: Add return type annotation: `int | None`

ℹ Unsafe fix
280 280 | pass
281 281 |
282 282 |
283 |-def func(x: int):
283 |+def func(x: int) -> int | None:
284 284 | if x > 5:
285 285 | raise ValueError
286 286 | elif x > 10:

auto_return_type.py:290:5: ANN201 [*] Missing return type annotation for public function `func`
|
290 | def func():
| ^^^^ ANN201
291 | try:
292 | return 5
|
= help: Add return type annotation: `int`

ℹ Unsafe fix
287 287 | return 5
288 288 |
289 289 |
290 |-def func():
290 |+def func() -> int:
291 291 | try:
292 292 | return 5
293 293 | except:

auto_return_type.py:299:5: ANN201 [*] Missing return type annotation for public function `func`
|
299 | def func(x: int):
| ^^^^ ANN201
300 | match x:
301 | case [1, 2, 3]:
|
= help: Add return type annotation: `str | int`

ℹ Unsafe fix
296 296 | raise ValueError
297 297 |
298 298 |
299 |-def func(x: int):
299 |+def func(x: int) -> str | int:
300 300 | match x:
301 301 | case [1, 2, 3]:
302 302 | return 1


Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ auto_return_type.py:82:5: ANN201 [*] Missing return type annotation for public f
83 | match x:
84 | case [1, 2, 3]:
|
= help: Add return type annotation: `Union[str | int]`
= help: Add return type annotation: `Union[str | int | None]`

ℹ Unsafe fix
1 |+from typing import Union
Expand All @@ -305,7 +305,7 @@ auto_return_type.py:82:5: ANN201 [*] Missing return type annotation for public f
80 81 |
81 82 |
82 |-def func(x: int):
83 |+def func(x: int) -> Union[str | int]:
83 |+def func(x: int) -> Union[str | int | None]:
83 84 | match x:
84 85 | case [1, 2, 3]:
85 86 | return 1
Expand Down Expand Up @@ -446,17 +446,22 @@ auto_return_type.py:137:5: ANN201 [*] Missing return type annotation for public
138 | try:
139 | return 1
|
= help: Add return type annotation: `int`
= help: Add return type annotation: `Optional[int]`

ℹ Unsafe fix
134 134 | return 2
135 135 |
136 136 |
1 |+from typing import Optional
1 2 | def func():
2 3 | return 1
3 4 |
--------------------------------------------------------------------------------
134 135 | return 2
135 136 |
136 137 |
137 |-def func(x: int):
137 |+def func(x: int) -> int:
138 138 | try:
139 139 | return 1
140 140 | except:
138 |+def func(x: int) -> Optional[int]:
138 139 | try:
139 140 | return 1
140 141 | except:

auto_return_type.py:146:5: ANN201 [*] Missing return type annotation for public function `func`
|
Expand Down Expand Up @@ -755,4 +760,117 @@ auto_return_type.py:262:5: ANN201 [*] Missing return type annotation for public
264 264 | if x > 0:
265 265 | return 1

auto_return_type.py:269:5: ANN201 [*] Missing return type annotation for public function `func`
|
269 | def func(x: int):
| ^^^^ ANN201
270 | if x > 5:
271 | raise ValueError
|
= help: Add return type annotation: `None`

ℹ Unsafe fix
266 266 | raise ValueError
267 267 |
268 268 |
269 |-def func(x: int):
269 |+def func(x: int) -> None:
270 270 | if x > 5:
271 271 | raise ValueError
272 272 | else:

auto_return_type.py:276:5: ANN201 [*] Missing return type annotation for public function `func`
|
276 | def func(x: int):
| ^^^^ ANN201
277 | if x > 5:
278 | raise ValueError
|
= help: Add return type annotation: `None`

ℹ Unsafe fix
273 273 | pass
274 274 |
275 275 |
276 |-def func(x: int):
276 |+def func(x: int) -> None:
277 277 | if x > 5:
278 278 | raise ValueError
279 279 | elif x > 10:

auto_return_type.py:283:5: ANN201 [*] Missing return type annotation for public function `func`
|
283 | def func(x: int):
| ^^^^ ANN201
284 | if x > 5:
285 | raise ValueError
|
= help: Add return type annotation: `Optional[int]`

ℹ Unsafe fix
214 214 | return 1
215 215 |
216 216 |
217 |-from typing import overload
217 |+from typing import overload, Optional
218 218 |
219 219 |
220 220 | @overload
--------------------------------------------------------------------------------
280 280 | pass
281 281 |
282 282 |
283 |-def func(x: int):
283 |+def func(x: int) -> Optional[int]:
284 284 | if x > 5:
285 285 | raise ValueError
286 286 | elif x > 10:

auto_return_type.py:290:5: ANN201 [*] Missing return type annotation for public function `func`
|
290 | def func():
| ^^^^ ANN201
291 | try:
292 | return 5
|
= help: Add return type annotation: `int`

ℹ Unsafe fix
287 287 | return 5
288 288 |
289 289 |
290 |-def func():
290 |+def func() -> int:
291 291 | try:
292 292 | return 5
293 293 | except:

auto_return_type.py:299:5: ANN201 [*] Missing return type annotation for public function `func`
|
299 | def func(x: int):
| ^^^^ ANN201
300 | match x:
301 | case [1, 2, 3]:
|
= help: Add return type annotation: `Union[str | int]`

ℹ Unsafe fix
214 214 | return 1
215 215 |
216 216 |
217 |-from typing import overload
217 |+from typing import overload, Union
218 218 |
219 219 |
220 220 | @overload
--------------------------------------------------------------------------------
296 296 | raise ValueError
297 297 |
298 298 |
299 |-def func(x: int):
299 |+def func(x: int) -> Union[str | int]:
300 300 | match x:
301 301 | case [1, 2, 3]:
302 302 | return 1


Loading