-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ruff
] Add never-union
rule to detect redundant typing.NoReturn
and typing.Never
#9217
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import Never, NoReturn, Union | ||
|
||
Union[Never, int] | ||
Union[NoReturn, int] | ||
Never | int | ||
NoReturn | int | ||
Union[Union[Never, int], Union[NoReturn, int]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 5 additions & 4 deletions
9
crates/ruff_linter/src/rules/flake8_pyi/rules/duplicate_union_member.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_literal_union.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
crates/ruff_linter/src/rules/flake8_pyi/rules/redundant_numeric_union.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::Expr; | ||
use ruff_python_semantic::analyze::typing::traverse_union; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `typing.NoReturn` and `typing.Never` in union types. | ||
/// | ||
/// ## Why is this bad? | ||
/// `typing.NoReturn` and `typing.Never` are special types, used to indicate | ||
/// that a function never returns, or that a type has no values. | ||
/// | ||
/// Including `typing.NoReturn` or `typing.Never` in a union type is redundant, | ||
/// as, e.g., `typing.Never | T` is equivalent to `T`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from typing import Never | ||
/// | ||
/// | ||
/// def func() -> Never | int: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def func() -> int: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - [Python documentation: `typing.Never`](https://docs.python.org/3/library/typing.html#typing.Never) | ||
/// - [Python documentation: `typing.NoReturn`](https://docs.python.org/3/library/typing.html#typing.NoReturn) | ||
#[violation] | ||
pub struct NeverUnion { | ||
never_like: NeverLike, | ||
union_like: UnionLike, | ||
} | ||
|
||
impl Violation for NeverUnion { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Self { | ||
never_like, | ||
union_like, | ||
} = self; | ||
match union_like { | ||
UnionLike::BinOp => { | ||
format!("`{never_like} | T` is equivalent to `T`") | ||
} | ||
UnionLike::TypingUnion => { | ||
format!("`Union[{never_like}, T]` is equivalent to `T`") | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// RUF020 | ||
pub(crate) fn never_union<'a>(checker: &mut Checker, expr: &'a Expr) { | ||
let mut expressions: Vec<(NeverLike, UnionLike, &'a Expr)> = Vec::new(); | ||
let mut rest: Vec<&'a Expr> = Vec::new(); | ||
|
||
// Find all `typing.Never` and `typing.NoReturn` expressions. | ||
let semantic = checker.semantic(); | ||
let mut collect_never = |expr: &'a Expr, parent: Option<&'a Expr>| { | ||
if let Some(call_path) = semantic.resolve_call_path(expr) { | ||
let union_like = if parent.is_some_and(Expr::is_bin_op_expr) { | ||
UnionLike::BinOp | ||
} else { | ||
UnionLike::TypingUnion | ||
}; | ||
|
||
let never_like = if semantic.match_typing_call_path(&call_path, "NoReturn") { | ||
Some(NeverLike::NoReturn) | ||
} else if semantic.match_typing_call_path(&call_path, "Never") { | ||
Some(NeverLike::Never) | ||
} else { | ||
None | ||
}; | ||
|
||
if let Some(never_like) = never_like { | ||
expressions.push((never_like, union_like, expr)); | ||
return; | ||
} | ||
} | ||
|
||
rest.push(expr); | ||
}; | ||
|
||
traverse_union(&mut collect_never, checker.semantic(), expr, None); | ||
|
||
// Create a diagnostic for each `typing.Never` and `typing.NoReturn` expression. | ||
for (never_like, union_like, child) in expressions { | ||
let diagnostic = Diagnostic::new( | ||
NeverUnion { | ||
never_like, | ||
union_like, | ||
}, | ||
child.range(), | ||
); | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
enum UnionLike { | ||
/// E.g., `typing.Union[int, str]` | ||
TypingUnion, | ||
/// E.g., `int | str` | ||
BinOp, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
enum NeverLike { | ||
/// E.g., `typing.NoReturn` | ||
NoReturn, | ||
/// E.g., `typing.Never` | ||
Never, | ||
} | ||
|
||
impl std::fmt::Display for NeverLike { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
NeverLike::NoReturn => f.write_str("NoReturn"), | ||
NeverLike::Never => f.write_str("Never"), | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR: Hmm, one downside of quoting runtime only type annotations is that it makes all lint-rules useless because we don't parse string annotations...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually do!