Skip to content
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] Apply redundant-numeric-union to more type expressions (PYI041) #14332

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ def bad4(self, arg: Union[float | complex, int]) -> None:

def bad5(self, arg: int | (float | complex)) -> None:
...

def bad6(self) -> int | (float | complex):
...
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ class Foo:
def bad4(self, arg: Union[float | complex, int]) -> None: ... # PYI041

def bad5(self, arg: int | (float | complex)) -> None: ... # PYI041

def bad6(self) -> int | (float | complex): ... # PYI041
7 changes: 7 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
Rule::RedundantLiteralUnion,
Rule::UnnecessaryTypeUnion,
Rule::NoneNotAtEndOfUnion,
Rule::RedundantNumericUnion,
]) {
// Avoid duplicate checks if the parent is a union, since these rules already
// traverse nested unions.
Expand All @@ -97,6 +98,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::UnnecessaryTypeUnion) {
flake8_pyi::rules::unnecessary_type_union(checker, expr);
}
if checker.enabled(Rule::RedundantNumericUnion) {
flake8_pyi::rules::redundant_numeric_union(checker, expr);
}
if checker.enabled(Rule::NoneNotAtEndOfUnion) {
ruff::rules::none_not_at_end_of_union(checker, expr);
}
Expand Down Expand Up @@ -1313,6 +1317,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::RuntimeStringUnion) {
flake8_type_checking::rules::runtime_string_union(checker, expr);
}
if checker.enabled(Rule::RedundantNumericUnion) {
flake8_pyi::rules::redundant_numeric_union(checker, expr);
}
if checker.enabled(Rule::NoneNotAtEndOfUnion) {
ruff::rules::none_not_at_end_of_union(checker, expr);
}
Expand Down
3 changes: 0 additions & 3 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::BadExitAnnotation) {
flake8_pyi::rules::bad_exit_annotation(checker, function_def);
}
if checker.enabled(Rule::RedundantNumericUnion) {
flake8_pyi::rules::redundant_numeric_union(checker, parameters);
}
if checker.enabled(Rule::Pep484StylePositionalOnlyParameter) {
flake8_pyi::rules::pep_484_positional_parameter(checker, function_def);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use anyhow::Result;
use ruff_diagnostics::{Applicability, Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{
name::Name, AnyParameterRef, Expr, ExprBinOp, ExprContext, ExprName, ExprSubscript, ExprTuple,
Operator, Parameters,
name::Name, Expr, ExprBinOp, ExprContext, ExprName, ExprSubscript, ExprTuple, Operator,
};
use ruff_python_semantic::analyze::typing::traverse_union;
use ruff_text_size::{Ranged, TextRange};
Expand Down Expand Up @@ -81,13 +80,7 @@ impl Violation for RedundantNumericUnion {
}

/// PYI041
pub(crate) fn redundant_numeric_union(checker: &mut Checker, parameters: &Parameters) {
for annotation in parameters.iter().filter_map(AnyParameterRef::annotation) {
check_annotation(checker, annotation);
}
}

fn check_annotation<'a>(checker: &mut Checker, annotation: &'a Expr) {
pub(crate) fn redundant_numeric_union<'a>(checker: &mut Checker, annotation: &'a Expr) {
let mut numeric_flags = NumericFlags::empty();

let mut find_numeric_type = |expr: &Expr, _parent: &Expr| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI041.py:10:18: PYI041 Use `float` instead of `int | float`
|
9 | TA0: TypeAlias = int
10 | TA1: TypeAlias = int | float | bool
| ^^^^^^^^^^^^^^^^^^ PYI041
11 | TA2: TypeAlias = Union[int, float, bool]
|
= help: Remove redundant type

PYI041.py:11:18: PYI041 Use `float` instead of `int | float`
|
9 | TA0: TypeAlias = int
10 | TA1: TypeAlias = int | float | bool
11 | TA2: TypeAlias = Union[int, float, bool]
| ^^^^^^^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type

PYI041.py:22:14: PYI041 Use `float` instead of `int | float`
|
22 | def f0(arg1: float | int) -> None:
Expand All @@ -25,6 +43,14 @@ PYI041.py:30:28: PYI041 Use `float` instead of `int | float`
|
= help: Remove redundant type

PYI041.py:34:32: PYI041 Use `float` instead of `int | float`
|
34 | def f3(arg1: int, *args: Union[int | int | float]) -> None:
| ^^^^^^^^^^^^^^^^^ PYI041
35 | ...
|
= help: Remove redundant type

PYI041.py:38:24: PYI041 Use `float` instead of `int | float`
|
38 | async def f4(**kwargs: int | int | float) -> None:
Expand Down Expand Up @@ -109,3 +135,13 @@ PYI041.py:75:25: PYI041 Use `complex` instead of `int | float | complex`
76 | ...
|
= help: Remove redundant type

PYI041.py:78:23: PYI041 Use `complex` instead of `int | float | complex`
|
76 | ...
77 |
78 | def bad6(self) -> int | (float | complex):
| ^^^^^^^^^^^^^^^^^^^^^^^ PYI041
79 | ...
|
= help: Remove redundant type
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
---
PYI041.pyi:11:18: PYI041 Use `float` instead of `int | float`
|
9 | # Type aliases not flagged
10 | TA0: TypeAlias = int
11 | TA1: TypeAlias = int | float | bool
| ^^^^^^^^^^^^^^^^^^ PYI041
12 | TA2: TypeAlias = Union[int, float, bool]
|
= help: Remove redundant type

PYI041.pyi:12:18: PYI041 Use `float` instead of `int | float`
|
10 | TA0: TypeAlias = int
11 | TA1: TypeAlias = int | float | bool
12 | TA2: TypeAlias = Union[int, float, bool]
| ^^^^^^^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type

PYI041.pyi:21:14: PYI041 Use `float` instead of `int | float`
|
21 | def f0(arg1: float | int) -> None: ... # PYI041
Expand All @@ -22,6 +41,13 @@ PYI041.pyi:27:28: PYI041 Use `float` instead of `int | float`
|
= help: Remove redundant type

PYI041.pyi:30:32: PYI041 Use `float` instead of `int | float`
|
30 | def f3(arg1: int, *args: Union[int | int | float]) -> None: ... # PYI041
| ^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type

PYI041.pyi:33:24: PYI041 Use `float` instead of `int | float`
|
33 | async def f4(**kwargs: int | int | float) -> None: ... # PYI041
Expand Down Expand Up @@ -107,5 +133,16 @@ PYI041.pyi:60:25: PYI041 Use `complex` instead of `int | float | complex`
59 |
60 | def bad5(self, arg: int | (float | complex)) -> None: ... # PYI041
| ^^^^^^^^^^^^^^^^^^^^^^^ PYI041
61 |
62 | def bad6(self) -> int | (float | complex): ... # PYI041
|
= help: Remove redundant type

PYI041.pyi:62:23: PYI041 Use `complex` instead of `int | float | complex`
|
60 | def bad5(self, arg: int | (float | complex)) -> None: ... # PYI041
61 |
62 | def bad6(self) -> int | (float | complex): ... # PYI041
| ^^^^^^^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type
Loading