Prefer Never to NoReturn in auto-typing (#9213)
Closes https://github.com/astral-sh/ruff/issues/9212.
This commit is contained in:
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user