Prefer Never to NoReturn in auto-typing (#9213)

Closes https://github.com/astral-sh/ruff/issues/9212.
This commit is contained in:
Charlie Marsh
2023-12-20 09:36:01 -05:00
committed by GitHub
parent 07b293d949
commit 29846f5b09
2 changed files with 14 additions and 7 deletions

View File

@@ -62,7 +62,7 @@ pub(crate) fn auto_return_type(function: &ast::StmtFunctionDef) -> Option<AutoPy
// If every control flow path raises an exception, return `NoReturn`.
if terminal == Some(Terminal::Raise) {
return Some(AutoPythonType::NoReturn);
return Some(AutoPythonType::Never);
}
// Determine the return type of the first `return` statement.
@@ -102,7 +102,7 @@ pub(crate) fn auto_return_type(function: &ast::StmtFunctionDef) -> Option<AutoPy
#[derive(Debug)]
pub(crate) enum AutoPythonType {
NoReturn,
Never,
Atom(PythonType),
Union(FxHashSet<PythonType>),
}
@@ -120,10 +120,17 @@ impl AutoPythonType {
target_version: PythonVersion,
) -> Option<(Expr, Vec<Edit>)> {
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,
)

View File

@@ -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: