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

Avoid filtering out un-representable types in return annotation #8881

Merged
merged 1 commit into from
Nov 28, 2023
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 @@ -30,3 +30,20 @@ def func(x: int):

def func(x: int):
return 1 + 2.5 if x > 0 else 1.5 or "str"


def func(x: int):
if not x:
return None
return {"foo": 1}


def func(x: int):
return {"foo": 1}


def func():
if not x:
return 1
else:
return True
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/flake8_annotations/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub(crate) fn auto_return_type(
let names = python_types
.iter()
.sorted_unstable()
.filter_map(|python_type| type_expr(*python_type))
.collect::<Vec<_>>();
.map(|python_type| type_expr(*python_type))
.collect::<Option<Vec<_>>>()?;

// Wrap in a bitwise union (e.g., `int | float`).
Some(pep_604_union(&names))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,43 @@ auto_return_type.py:31:5: ANN201 [*] Missing return type annotation for public f
31 |-def func(x: int):
31 |+def func(x: int) -> str | float:
32 32 | return 1 + 2.5 if x > 0 else 1.5 or "str"
33 33 |
34 34 |

auto_return_type.py:35:5: ANN201 Missing return type annotation for public function `func`
|
35 | def func(x: int):
| ^^^^ ANN201
36 | if not x:
37 | return None
|
= help: Add return type annotation

auto_return_type.py:41:5: ANN201 Missing return type annotation for public function `func`
|
41 | def func(x: int):
| ^^^^ ANN201
42 | return {"foo": 1}
|
= help: Add return type annotation

auto_return_type.py:45:5: ANN201 [*] Missing return type annotation for public function `func`
|
45 | def func():
| ^^^^ ANN201
46 | if not x:
47 | return 1
|
= help: Add return type annotation: `int`

ℹ Unsafe fix
42 42 | return {"foo": 1}
43 43 |
44 44 |
45 |-def func():
45 |+def func() -> int:
46 46 | if not x:
47 47 | return 1
48 48 | else:


Loading