From 48cb29f1081d6b04216505e0f461e3ec8b4a4813 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 28 Nov 2023 13:01:52 -0800 Subject: [PATCH] Avoid filtering out un-representable types in return annotation --- .../flake8_annotations/auto_return_type.py | 17 +++++++++ .../src/rules/flake8_annotations/helpers.rs | 4 +- ..._annotations__tests__auto_return_type.snap | 38 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_annotations/auto_return_type.py b/crates/ruff_linter/resources/test/fixtures/flake8_annotations/auto_return_type.py index b7d58a6f94302..32d0cc2094523 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_annotations/auto_return_type.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_annotations/auto_return_type.py @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs index 318ebf053fac9..8308d9a06569a 100644 --- a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs @@ -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::>(); + .map(|python_type| type_expr(*python_type)) + .collect::>>()?; // Wrap in a bitwise union (e.g., `int | float`). Some(pep_604_union(&names)) diff --git a/crates/ruff_linter/src/rules/flake8_annotations/snapshots/ruff_linter__rules__flake8_annotations__tests__auto_return_type.snap b/crates/ruff_linter/src/rules/flake8_annotations/snapshots/ruff_linter__rules__flake8_annotations__tests__auto_return_type.snap index 108a1483004c7..c5508c652a894 100644 --- a/crates/ruff_linter/src/rules/flake8_annotations/snapshots/ruff_linter__rules__flake8_annotations__tests__auto_return_type.snap +++ b/crates/ruff_linter/src/rules/flake8_annotations/snapshots/ruff_linter__rules__flake8_annotations__tests__auto_return_type.snap @@ -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: