Skip to content

Commit 29846f5

Browse files
Prefer Never to NoReturn in auto-typing (#9213)
Closes #9212.
1 parent 07b293d commit 29846f5

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

crates/ruff_linter/src/rules/flake8_annotations/helpers.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn auto_return_type(function: &ast::StmtFunctionDef) -> Option<AutoPy
6262

6363
// If every control flow path raises an exception, return `NoReturn`.
6464
if terminal == Some(Terminal::Raise) {
65-
return Some(AutoPythonType::NoReturn);
65+
return Some(AutoPythonType::Never);
6666
}
6767

6868
// Determine the return type of the first `return` statement.
@@ -102,7 +102,7 @@ pub(crate) fn auto_return_type(function: &ast::StmtFunctionDef) -> Option<AutoPy
102102

103103
#[derive(Debug)]
104104
pub(crate) enum AutoPythonType {
105-
NoReturn,
105+
Never,
106106
Atom(PythonType),
107107
Union(FxHashSet<PythonType>),
108108
}
@@ -120,10 +120,17 @@ impl AutoPythonType {
120120
target_version: PythonVersion,
121121
) -> Option<(Expr, Vec<Edit>)> {
122122
match self {
123-
AutoPythonType::NoReturn => {
123+
AutoPythonType::Never => {
124124
let (no_return_edit, binding) = importer
125125
.get_or_import_symbol(
126-
&ImportRequest::import_from("typing", "NoReturn"),
126+
&ImportRequest::import_from(
127+
"typing",
128+
if target_version >= PythonVersion::Py311 {
129+
"Never"
130+
} else {
131+
"NoReturn"
132+
},
133+
),
127134
at,
128135
semantic,
129136
)

crates/ruff_linter/src/rules/flake8_annotations/snapshots/ruff_linter__rules__flake8_annotations__tests__auto_return_type.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,13 @@ auto_return_type.py:203:5: ANN201 [*] Missing return type annotation for public
540540
204 | if not x:
541541
205 | raise ValueError
542542
|
543-
= help: Add return type annotation: `NoReturn`
543+
= help: Add return type annotation: `Never`
544544

545545
ℹ Unsafe fix
546546
151 151 |
547547
152 152 | import abc
548548
153 153 | from abc import abstractmethod
549-
154 |+from typing import NoReturn
549+
154 |+from typing import Never
550550
154 155 |
551551
155 156 |
552552
156 157 | class Foo(abc.ABC):
@@ -555,7 +555,7 @@ auto_return_type.py:203:5: ANN201 [*] Missing return type annotation for public
555555
201 202 |
556556
202 203 |
557557
203 |-def func(x: int):
558-
204 |+def func(x: int) -> NoReturn:
558+
204 |+def func(x: int) -> Never:
559559
204 205 | if not x:
560560
205 206 | raise ValueError
561561
206 207 | else:

0 commit comments

Comments
 (0)