-
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 PYI041
#5722
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,47 @@ | ||
from typing import ( | ||
Union, | ||
) | ||
|
||
from typing_extensions import ( | ||
TypeAlias, | ||
) | ||
|
||
TA0: TypeAlias = int | ||
TA1: TypeAlias = int | float | bool | ||
TA2: TypeAlias = Union[int, float, bool] | ||
|
||
|
||
def good1(arg: int) -> int | bool: | ||
... | ||
|
||
|
||
def good2(arg: int, arg2: int | bool) -> None: | ||
... | ||
|
||
|
||
def f0(arg1: float | int) -> None: | ||
... | ||
|
||
|
||
def f1(arg1: float, *, arg2: float | list[str] | type[bool] | complex) -> None: | ||
... | ||
|
||
|
||
def f2(arg1: int, /, arg2: int | int | float) -> None: | ||
... | ||
|
||
|
||
def f3(arg1: int, *args: Union[int | int | float]) -> None: | ||
... | ||
|
||
|
||
async def f4(**kwargs: int | int | float) -> None: | ||
... | ||
|
||
|
||
class Foo: | ||
def good(self, arg: int) -> None: | ||
... | ||
|
||
def bad(self, arg: int | float | complex) -> 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,39 @@ | ||
from typing import ( | ||
Union, | ||
) | ||
|
||
from typing_extensions import ( | ||
TypeAlias, | ||
) | ||
|
||
# Type aliases not flagged | ||
TA0: TypeAlias = int | ||
TA1: TypeAlias = int | float | bool | ||
TA2: TypeAlias = Union[int, float, bool] | ||
|
||
|
||
def good1(arg: int) -> int | bool: ... | ||
|
||
|
||
def good2(arg: int, arg2: int | bool) -> None: ... | ||
|
||
|
||
def f0(arg1: float | int) -> None: ... # PYI041 | ||
|
||
|
||
def f1(arg1: float, *, arg2: float | list[str] | type[bool] | complex) -> None: ... # PYI041 | ||
|
||
|
||
def f2(arg1: int, /, arg2: int | int | float) -> None: ... # PYI041 | ||
|
||
|
||
def f3(arg1: int, *args: Union[int | int | float]) -> None: ... # PYI041 | ||
|
||
|
||
async def f4(**kwargs: int | int | float) -> None: ... # PYI041 | ||
|
||
|
||
class Foo: | ||
def good(self, arg: int) -> None: ... | ||
|
||
def bad(self, arg: int | float | complex) -> None: ... # PYI041 |
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
133 changes: 133 additions & 0 deletions
133
crates/ruff/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use rustpython_parser::ast::{Arguments, Expr, Ranged}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::rules::flake8_pyi::helpers::traverse_union; | ||
|
||
/// ## What it does | ||
/// Checks for union annotations that contain redundant numeric types (e.g., | ||
/// `int | float`). | ||
/// | ||
/// ## Why is this bad? | ||
/// In Python, `int` is a subtype of `float`, and `float` is a subtype of | ||
/// `complex`. As such, a union that includes both `int` and `float` is | ||
/// redundant, as it is equivalent to a union that only includes `float`. | ||
/// | ||
/// For more, see [PEP 3141], which defines Python's "numeric tower". | ||
/// | ||
/// Unions with redundant elements are less readable than unions without them. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// def foo(x: float | int) -> None: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def foo(x: float) -> None: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: The numeric tower](https://docs.python.org/3/library/numbers.html#the-numeric-tower) | ||
/// | ||
/// [PEP 3141]: https://peps.python.org/pep-3141/ | ||
#[violation] | ||
pub struct RedundantNumericUnion { | ||
redundancy: Redundancy, | ||
} | ||
|
||
impl Violation for RedundantNumericUnion { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let (subtype, supertype) = match self.redundancy { | ||
Redundancy::FloatComplex => ("float", "complex"), | ||
Redundancy::IntComplex => ("int", "complex"), | ||
Redundancy::IntFloat => ("int", "float"), | ||
}; | ||
format!("Use `{supertype}` instead of `{subtype} | {supertype}`") | ||
} | ||
} | ||
|
||
/// PYI041 | ||
pub(crate) fn redundant_numeric_union(checker: &mut Checker, args: &Arguments) { | ||
for annotation in args | ||
.args | ||
.iter() | ||
.chain(args.posonlyargs.iter()) | ||
.chain(args.kwonlyargs.iter()) | ||
.filter_map(|arg| arg.def.annotation.as_ref()) | ||
{ | ||
check_annotation(checker, annotation); | ||
} | ||
|
||
// If annotations on `args` or `kwargs` are flagged by this rule, the annotations themselves | ||
// are not accurate, but check them anyway. It's possible that flagging them will help the user | ||
// realize they're incorrect. | ||
for annotation in args | ||
.vararg | ||
.iter() | ||
.chain(args.kwarg.iter()) | ||
.filter_map(|arg| arg.annotation.as_ref()) | ||
{ | ||
check_annotation(checker, annotation); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
enum Redundancy { | ||
FloatComplex, | ||
IntComplex, | ||
IntFloat, | ||
} | ||
|
||
fn check_annotation(checker: &mut Checker, annotation: &Expr) { | ||
let mut has_float = false; | ||
let mut has_complex = false; | ||
let mut has_int = false; | ||
|
||
let mut func = |expr: &Expr, _parent: Option<&Expr>| { | ||
let Some(call_path) = checker.semantic().resolve_call_path(expr) else { | ||
return; | ||
}; | ||
|
||
match call_path.as_slice() { | ||
["" | "builtins", "int"] => has_int = true, | ||
["" | "builtins", "float"] => has_float = true, | ||
["" | "builtins", "complex"] => has_complex = true, | ||
_ => (), | ||
} | ||
}; | ||
|
||
traverse_union(&mut func, checker.semantic(), annotation, None); | ||
|
||
if has_complex { | ||
if has_float { | ||
checker.diagnostics.push(Diagnostic::new( | ||
RedundantNumericUnion { | ||
redundancy: Redundancy::FloatComplex, | ||
}, | ||
annotation.range(), | ||
)); | ||
} | ||
|
||
if has_int { | ||
checker.diagnostics.push(Diagnostic::new( | ||
RedundantNumericUnion { | ||
redundancy: Redundancy::IntComplex, | ||
}, | ||
annotation.range(), | ||
)); | ||
} | ||
} else if has_float && has_int { | ||
checker.diagnostics.push(Diagnostic::new( | ||
RedundantNumericUnion { | ||
redundancy: Redundancy::IntFloat, | ||
}, | ||
annotation.range(), | ||
)); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI041_PYI041.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,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
|
44 changes: 44 additions & 0 deletions
44
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI041_PYI041.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,44 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI041.pyi:21:14: PYI041 Use `float` instead of `int | float` | ||
| | ||
21 | def f0(arg1: float | int) -> None: ... # PYI041 | ||
| ^^^^^^^^^^^ PYI041 | ||
| | ||
|
||
PYI041.pyi:24:30: PYI041 Use `complex` instead of `float | complex` | ||
| | ||
24 | def f1(arg1: float, *, arg2: float | list[str] | type[bool] | complex) -> None: ... # PYI041 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041 | ||
| | ||
|
||
PYI041.pyi:27:28: PYI041 Use `float` instead of `int | float` | ||
| | ||
27 | def f2(arg1: int, /, arg2: int | int | float) -> None: ... # PYI041 | ||
| ^^^^^^^^^^^^^^^^^ PYI041 | ||
| | ||
|
||
PYI041.pyi:33:24: PYI041 Use `float` instead of `int | float` | ||
| | ||
33 | async def f4(**kwargs: int | int | float) -> None: ... # PYI041 | ||
| ^^^^^^^^^^^^^^^^^ PYI041 | ||
| | ||
|
||
PYI041.pyi:39:24: PYI041 Use `complex` instead of `float | complex` | ||
| | ||
37 | def good(self, arg: int) -> None: ... | ||
38 | | ||
39 | def bad(self, arg: int | float | complex) -> None: ... # PYI041 | ||
| ^^^^^^^^^^^^^^^^^^^^^ PYI041 | ||
| | ||
|
||
PYI041.pyi:39:24: PYI041 Use `complex` instead of `int | complex` | ||
| | ||
37 | def good(self, arg: int) -> None: ... | ||
38 | | ||
39 | def bad(self, arg: int | float | complex) -> None: ... # PYI041 | ||
| ^^^^^^^^^^^^^^^^^^^^^ PYI041 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I think we should document the limitation that this rule won't check for type aliases.
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.
Added a comment. Thanks!