From 5a1fde8fd262c95377489c850584edb03dd12dda Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 20 Dec 2023 09:26:05 -0500 Subject: [PATCH] Prefer Never to NoReturn in auto-typing --- .../src/rules/flake8_annotations/helpers.rs | 15 +++++++++++---- ...ake8_annotations__tests__auto_return_type.snap | 6 +++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs index baf316357da55..0ef1bcf262c0f 100644 --- a/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_annotations/helpers.rs @@ -62,7 +62,7 @@ pub(crate) fn auto_return_type(function: &ast::StmtFunctionDef) -> Option Option), } @@ -120,10 +120,17 @@ impl AutoPythonType { target_version: PythonVersion, ) -> Option<(Expr, Vec)> { match self { - AutoPythonType::NoReturn => { + AutoPythonType::Never => { let (no_return_edit, binding) = importer .get_or_import_symbol( - &ImportRequest::import_from("typing", "NoReturn"), + &ImportRequest::import_from( + "typing", + if target_version >= PythonVersion::Py311 { + "Never" + } else { + "NoReturn" + }, + ), at, semantic, ) 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 1dc85efd74b0d..d374776949219 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 @@ -540,13 +540,13 @@ auto_return_type.py:203:5: ANN201 [*] Missing return type annotation for public 204 | if not x: 205 | raise ValueError | - = help: Add return type annotation: `NoReturn` + = help: Add return type annotation: `Never` ℹ Unsafe fix 151 151 | 152 152 | import abc 153 153 | from abc import abstractmethod - 154 |+from typing import NoReturn + 154 |+from typing import Never 154 155 | 155 156 | 156 157 | class Foo(abc.ABC): @@ -555,7 +555,7 @@ auto_return_type.py:203:5: ANN201 [*] Missing return type annotation for public 201 202 | 202 203 | 203 |-def func(x: int): - 204 |+def func(x: int) -> NoReturn: + 204 |+def func(x: int) -> Never: 204 205 | if not x: 205 206 | raise ValueError 206 207 | else: