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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI041.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,41 @@ async def f4(**kwargs: int | int | float) -> None:
...


def f5(
arg: Union[ # comment
float, # another
complex, int]
) -> None:
...

def f6(
arg: (
int | # comment
float | # another
complex
)
) -> None:
...


class Foo:
def good(self, arg: int) -> None:
...

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

def bad2(self, arg: int | Union[float, complex]) -> None:
...

def bad3(self, arg: Union[Union[float, complex], int]) -> None:
...

def bad4(self, arg: Union[float | complex, int]) -> None:
...

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

def bad6(self) -> int | (float | complex):
...
23 changes: 23 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI041.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,31 @@ def f3(arg1: int, *args: Union[int | int | float]) -> None: ... # PYI041

async def f4(**kwargs: int | int | float) -> None: ... # PYI041

def f5(
arg: Union[ # comment
float, # another
complex, int]
) -> None: ... # PYI041

def f6(
arg: (
int | # comment
float | # another
complex
)
) -> None: ... # PYI041

class Foo:
def good(self, arg: int) -> None: ...

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

def bad2(self, arg: int | Union[float, complex]) -> None: ... # PYI041

def bad3(self, arg: Union[Union[float, complex], int]) -> None: ... # PYI041

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 @@ -80,6 +80,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
Rule::DuplicateUnionMember,
Rule::RedundantLiteralUnion,
Rule::UnnecessaryTypeUnion,
Rule::RedundantNumericUnion,
]) {
// Avoid duplicate checks if the parent is a union, since these rules already
// traverse nested unions.
Expand All @@ -96,6 +97,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);
}
}
}

Expand Down Expand Up @@ -1273,6 +1277,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);
}
}
}
Expr::UnaryOp(
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 @@ -184,9 +184,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::PrePep570PositionalArgument) {
flake8_pyi::rules::pre_pep570_positional_argument(checker, function_def);
}
Expand Down
Loading
Loading