-
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
[flake8-pyi
] Implement PYI055
#6316
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
daaef76
Implement PYI055
LaBatata101 c7bf5d3
Update documentation
LaBatata101 b823ca2
Merge branch 'main' into PYI055
LaBatata101 532c7cc
Fix formatting and update snap test
LaBatata101 b2c8728
Tweaks
LaBatata101 7915289
Merge branch 'main' into PYI055
charliermarsh edb291e
Tweak tests
charliermarsh 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
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,19 @@ | ||
import builtins | ||
from typing import Union | ||
|
||
|
||
w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
x: type[int] | type[str] | type[float] | ||
y: builtins.type[int] | type[str] | builtins.type[complex] | ||
z: Union[type[float], type[complex]] | ||
|
||
|
||
def func(arg: type[int] | str | type[float]) -> None: ... | ||
|
||
# OK | ||
x: type[int, str, float] | ||
y: builtins.type[int, str, complex] | ||
z: Union[float, complex] | ||
|
||
|
||
def func(arg: type[int, float] | str) -> None: ... |
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,19 @@ | ||
import builtins | ||
from typing import Union | ||
|
||
|
||
w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
x: type[int] | type[str] | type[float] | ||
y: builtins.type[int] | type[str] | builtins.type[complex] | ||
z: Union[type[float], type[complex]] | ||
|
||
|
||
def func(arg: type[int] | str | type[float]) -> None: ... | ||
|
||
# OK | ||
x: type[int, str, float] | ||
y: builtins.type[int, str, complex] | ||
z: Union[float, complex] | ||
|
||
|
||
def func(arg: type[int, float] | str) -> None: ... |
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
82 changes: 82 additions & 0 deletions
82
crates/ruff/src/rules/flake8_pyi/rules/unnecessary_type_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{Expr, Ranged}; | ||
|
||
use crate::{checkers::ast::Checker, rules::flake8_pyi::helpers::traverse_union}; | ||
|
||
/// ## What it does | ||
/// Checks for the presence of multiple `type`s in a union. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `type` built-in function accepts unions, and it is | ||
/// clearer to explicitly specify them as a single `type`. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// field: type[int] | type[float] | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// field: type[int | float] | ||
/// ``` | ||
#[violation] | ||
pub struct UnnecessaryTypeUnion { | ||
members: Vec<String>, | ||
is_pep604_union: bool, | ||
} | ||
|
||
impl Violation for UnnecessaryTypeUnion { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let union_str = if self.is_pep604_union { | ||
format!("{}", self.members.join(" | ")) | ||
} else { | ||
format!("Union[{}]", self.members.join(", ")) | ||
}; | ||
|
||
format!( | ||
"Multiple `type` members in a union. Combine them into one, e.g. `type[{union_str}]`" | ||
) | ||
} | ||
} | ||
|
||
/// PYI055 | ||
pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr) { | ||
let mut type_exprs = Vec::new(); | ||
|
||
// Check if `union` is a PEP604 union (e.g. `float | int`) or a `typing.Union[float, int]` | ||
let is_pep604_union = !union.as_subscript_expr().is_some_and(|subscript| { | ||
checker | ||
.semantic() | ||
.match_typing_expr(&subscript.value, "Union") | ||
}); | ||
|
||
let mut collect_type_exprs = |expr: &'a Expr, _| { | ||
let Some(subscript) = expr.as_subscript_expr() else { | ||
return; | ||
}; | ||
let Some(call_path) = checker.semantic().resolve_call_path(subscript.value.as_ref()) else { | ||
return; | ||
}; | ||
|
||
if matches!(call_path.as_slice(), ["" | "builtins", "type"]) { | ||
type_exprs.push(&subscript.slice); | ||
} | ||
}; | ||
|
||
traverse_union(&mut collect_type_exprs, checker.semantic(), union, None); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How was using this helper? I wrote it and would appreciate any feedback :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was very easy to use, good job! |
||
|
||
if type_exprs.len() > 1 { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UnnecessaryTypeUnion { | ||
members: type_exprs | ||
.into_iter() | ||
.map(|type_expr| checker.locator().slice(type_expr.range()).to_string()) | ||
.collect(), | ||
is_pep604_union, | ||
}, | ||
union.range(), | ||
)); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI055_PYI055.py.snap
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,46 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI055.py:5:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | str | complex]` | ||
| | ||
5 | w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
6 | x: type[int] | type[str] | type[float] | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
| | ||
|
||
PYI055.py:6:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | str | float]` | ||
| | ||
5 | w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
6 | x: type[int] | type[str] | type[float] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
8 | z: Union[type[float], type[complex]] | ||
| | ||
|
||
PYI055.py:7:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | str | complex]` | ||
| | ||
5 | w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
6 | x: type[int] | type[str] | type[float] | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
8 | z: Union[type[float], type[complex]] | ||
| | ||
|
||
PYI055.py:8:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[Union[float, complex]]` | ||
| | ||
6 | x: type[int] | type[str] | type[float] | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
8 | z: Union[type[float], type[complex]] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
| | ||
|
||
PYI055.py:11:15: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | float]` | ||
| | ||
11 | def func(arg: type[int] | str | type[float]) -> None: ... | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
12 | | ||
13 | # OK | ||
| | ||
|
||
|
46 changes: 46 additions & 0 deletions
46
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI055_PYI055.pyi.snap
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,46 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI055.pyi:5:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | str | complex]` | ||
| | ||
5 | w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
6 | x: type[int] | type[str] | type[float] | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
| | ||
|
||
PYI055.pyi:6:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | str | float]` | ||
| | ||
5 | w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
6 | x: type[int] | type[str] | type[float] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
8 | z: Union[type[float], type[complex]] | ||
| | ||
|
||
PYI055.pyi:7:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | str | complex]` | ||
| | ||
5 | w: builtins.type[int] | builtins.type[str] | builtins.type[complex] | ||
6 | x: type[int] | type[str] | type[float] | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
8 | z: Union[type[float], type[complex]] | ||
| | ||
|
||
PYI055.pyi:8:4: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[Union[float, complex]]` | ||
| | ||
6 | x: type[int] | type[str] | type[float] | ||
7 | y: builtins.type[int] | type[str] | builtins.type[complex] | ||
8 | z: Union[type[float], type[complex]] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
| | ||
|
||
PYI055.pyi:11:15: PYI055 Multiple `type` members in a union. Combine them into one, e.g. `type[int | float]` | ||
| | ||
11 | def func(arg: type[int] | str | type[float]) -> None: ... | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI055 | ||
12 | | ||
13 | # OK | ||
| | ||
|
||
|
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.
Should we consider moving this check into a shared block like we do for the
Union[...]
case above?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.
That would be nice, removing the duplicated code. Does the variable name
is_unchecked_union
fit in here as well?