Skip to content

Commit

Permalink
Avoid filtering out un-representable types in return annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 28, 2023
1 parent 9dee188 commit 2ae845d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ 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}
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,24 @@ 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


0 comments on commit 2ae845d

Please sign in to comment.