Skip to content

Commit

Permalink
Prefer Never to NoReturn in auto-typing (#9213)
Browse files Browse the repository at this point in the history
Closes #9212.
  • Loading branch information
charliermarsh committed Dec 20, 2023
1 parent 07b293d commit 29846f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 11 additions & 4 deletions crates/ruff_linter/src/rules/flake8_annotations/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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>),
}
Expand All @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down

0 comments on commit 29846f5

Please sign in to comment.