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] Stabilize autofix for redundant-numeric-union (PYI041) #15343

Merged
merged 1 commit into from
Jan 8, 2025
Merged
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 @@ -133,40 +133,39 @@ fn check_annotation<'a>(checker: &mut Checker, annotation: &'a Expr) {
traverse_union(&mut remove_numeric_type, checker.semantic(), annotation);

let mut diagnostic = Diagnostic::new(RedundantNumericUnion { redundancy }, annotation.range());
if checker.settings.preview.is_enabled() {
// Mark [`Fix`] as unsafe when comments are in range.
let applicability = if checker.comment_ranges().intersects(annotation.range()) {
Applicability::Unsafe
} else {
Applicability::Safe
};

// Generate the flattened fix once.
let fix = if let &[edit_expr] = necessary_nodes.as_slice() {
// Generate a [`Fix`] for a single type expression, e.g. `int`.
Some(Fix::applicable_edit(
Edit::range_replacement(checker.generator().expr(edit_expr), annotation.range()),
// Mark [`Fix`] as unsafe when comments are in range.
let applicability = if checker.comment_ranges().intersects(annotation.range()) {
Applicability::Unsafe
} else {
Applicability::Safe
};

// Generate the flattened fix once.
let fix = if let &[edit_expr] = necessary_nodes.as_slice() {
// Generate a [`Fix`] for a single type expression, e.g. `int`.
Some(Fix::applicable_edit(
Edit::range_replacement(checker.generator().expr(edit_expr), annotation.range()),
applicability,
))
} else {
match union_type {
UnionKind::PEP604 => Some(generate_pep604_fix(
checker,
necessary_nodes,
annotation,
applicability,
))
} else {
match union_type {
UnionKind::PEP604 => Some(generate_pep604_fix(
checker,
necessary_nodes,
annotation,
applicability,
)),
UnionKind::TypingUnion => {
generate_union_fix(checker, necessary_nodes, annotation, applicability).ok()
}
)),
UnionKind::TypingUnion => {
generate_union_fix(checker, necessary_nodes, annotation, applicability).ok()
}
};

if let Some(fix) = fix {
diagnostic.set_fix(fix);
}
};

if let Some(fix) = fix {
diagnostic.set_fix(fix);
}

checker.diagnostics.push(diagnostic);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,170 @@
---
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
snapshot_kind: text
---
PYI041.py:22:14: PYI041 Use `float` instead of `int | float`
PYI041.py:22:14: PYI041 [*] Use `float` instead of `int | float`
|
22 | def f0(arg1: float | int) -> None:
| ^^^^^^^^^^^ PYI041
23 | ...
|
= help: Remove redundant type

PYI041.py:26:30: PYI041 Use `complex` instead of `float | complex`
ℹ Safe fix
19 19 | ...
20 20 |
21 21 |
22 |-def f0(arg1: float | int) -> None:
22 |+def f0(arg1: float) -> None:
23 23 | ...
24 24 |
25 25 |

PYI041.py:26:30: PYI041 [*] Use `complex` instead of `float | complex`
|
26 | def f1(arg1: float, *, arg2: float | list[str] | type[bool] | complex) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
27 | ...
|
= help: Remove redundant type

PYI041.py:30:28: PYI041 Use `float` instead of `int | float`
ℹ Safe fix
23 23 | ...
24 24 |
25 25 |
26 |-def f1(arg1: float, *, arg2: float | list[str] | type[bool] | complex) -> None:
26 |+def f1(arg1: float, *, arg2: list[str] | type[bool] | complex) -> None:
27 27 | ...
28 28 |
29 29 |

PYI041.py:30:28: PYI041 [*] Use `float` instead of `int | float`
|
30 | def f2(arg1: int, /, arg2: int | int | float) -> None:
| ^^^^^^^^^^^^^^^^^ PYI041
31 | ...
|
= help: Remove redundant type

PYI041.py:34:26: PYI041 Use `float` instead of `int | float`
ℹ Safe fix
27 27 | ...
28 28 |
29 29 |
30 |-def f2(arg1: int, /, arg2: int | int | float) -> None:
30 |+def f2(arg1: int, /, arg2: float) -> None:
31 31 | ...
32 32 |
33 33 |

PYI041.py:34:26: 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`
ℹ Safe fix
31 31 | ...
32 32 |
33 33 |
34 |-def f3(arg1: int, *args: Union[int | int | float]) -> None:
34 |+def f3(arg1: int, *args: float) -> None:
35 35 | ...
36 36 |
37 37 |

PYI041.py:38:24: PYI041 [*] Use `float` instead of `int | float`
|
38 | async def f4(**kwargs: int | int | float) -> None:
| ^^^^^^^^^^^^^^^^^ PYI041
39 | ...
|
= help: Remove redundant type

PYI041.py:42:26: PYI041 Use `float` instead of `int | float`
ℹ Safe fix
35 35 | ...
36 36 |
37 37 |
38 |-async def f4(**kwargs: int | int | float) -> None:
38 |+async def f4(**kwargs: float) -> None:
39 39 | ...
40 40 |
41 41 |

PYI041.py:42:26: PYI041 [*] Use `float` instead of `int | float`
|
42 | def f5(arg1: int, *args: Union[int, int, float]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^ PYI041
43 | ...
|
= help: Remove redundant type

PYI041.py:46:26: PYI041 Use `float` instead of `int | float`
ℹ Safe fix
39 39 | ...
40 40 |
41 41 |
42 |-def f5(arg1: int, *args: Union[int, int, float]) -> None:
42 |+def f5(arg1: int, *args: float) -> None:
43 43 | ...
44 44 |
45 45 |

PYI041.py:46:26: PYI041 [*] Use `float` instead of `int | float`
|
46 | def f6(arg1: int, *args: Union[Union[int, int, float]]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
47 | ...
|
= help: Remove redundant type

PYI041.py:50:26: PYI041 Use `float` instead of `int | float`
ℹ Safe fix
43 43 | ...
44 44 |
45 45 |
46 |-def f6(arg1: int, *args: Union[Union[int, int, float]]) -> None:
46 |+def f6(arg1: int, *args: float) -> None:
47 47 | ...
48 48 |
49 49 |

PYI041.py:50:26: PYI041 [*] Use `float` instead of `int | float`
|
50 | def f7(arg1: int, *args: Union[Union[Union[int, int, float]]]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
51 | ...
|
= help: Remove redundant type

PYI041.py:54:26: PYI041 Use `float` instead of `int | float`
ℹ Safe fix
47 47 | ...
48 48 |
49 49 |
50 |-def f7(arg1: int, *args: Union[Union[Union[int, int, float]]]) -> None:
50 |+def f7(arg1: int, *args: float) -> None:
51 51 | ...
52 52 |
53 53 |

PYI041.py:54:26: PYI041 [*] Use `float` instead of `int | float`
|
54 | def f8(arg1: int, *args: Union[Union[Union[int | int | float]]]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
55 | ...
|
= help: Remove redundant type

PYI041.py:59:10: PYI041 Use `complex` instead of `int | float | complex`
ℹ Safe fix
51 51 | ...
52 52 |
53 53 |
54 |-def f8(arg1: int, *args: Union[Union[Union[int | int | float]]]) -> None:
54 |+def f8(arg1: int, *args: float) -> None:
55 55 | ...
56 56 |
57 57 |

PYI041.py:59:10: PYI041 [*] Use `complex` instead of `int | float | complex`
|
58 | def f9(
59 | arg: Union[ # comment
Expand All @@ -86,7 +177,19 @@ PYI041.py:59:10: PYI041 Use `complex` instead of `int | float | complex`
|
= help: Remove redundant type

PYI041.py:67:9: PYI041 Use `complex` instead of `int | float | complex`
ℹ Unsafe fix
56 56 |
57 57 |
58 58 | def f9(
59 |- arg: Union[ # comment
60 |- float, # another
61 |- complex, int]
59 |+ arg: complex
62 60 | ) -> None:
63 61 | ...
64 62 |

PYI041.py:67:9: PYI041 [*] Use `complex` instead of `int | float | complex`
|
65 | def f10(
66 | arg: (
Expand All @@ -100,7 +203,17 @@ PYI041.py:67:9: PYI041 Use `complex` instead of `int | float | complex`
|
= help: Remove redundant type

PYI041.py:79:24: PYI041 Use `complex` instead of `int | float | complex`
ℹ Unsafe fix
64 64 |
65 65 | def f10(
66 66 | arg: (
67 |- int | # comment
68 |- float | # another
69 67 | complex
70 68 | )
71 69 | ) -> None:

PYI041.py:79:24: PYI041 [*] Use `complex` instead of `int | float | complex`
|
77 | ...
78 |
Expand All @@ -110,7 +223,17 @@ PYI041.py:79:24: PYI041 Use `complex` instead of `int | float | complex`
|
= help: Remove redundant type

PYI041.py:82:25: PYI041 Use `complex` instead of `int | float | complex`
ℹ Safe fix
76 76 | def good(self, arg: int) -> None:
77 77 | ...
78 78 |
79 |- def bad(self, arg: int | float | complex) -> None:
79 |+ def bad(self, arg: complex) -> None:
80 80 | ...
81 81 |
82 82 | def bad2(self, arg: int | Union[float, complex]) -> None:

PYI041.py:82:25: PYI041 [*] Use `complex` instead of `int | float | complex`
|
80 | ...
81 |
Expand All @@ -120,7 +243,17 @@ PYI041.py:82:25: PYI041 Use `complex` instead of `int | float | complex`
|
= help: Remove redundant type

PYI041.py:85:25: PYI041 Use `complex` instead of `int | float | complex`
ℹ Safe fix
79 79 | def bad(self, arg: int | float | complex) -> None:
80 80 | ...
81 81 |
82 |- def bad2(self, arg: int | Union[float, complex]) -> None:
82 |+ def bad2(self, arg: complex) -> None:
83 83 | ...
84 84 |
85 85 | def bad3(self, arg: Union[Union[float, complex], int]) -> None:

PYI041.py:85:25: PYI041 [*] Use `complex` instead of `int | float | complex`
|
83 | ...
84 |
Expand All @@ -130,7 +263,17 @@ PYI041.py:85:25: PYI041 Use `complex` instead of `int | float | complex`
|
= help: Remove redundant type

PYI041.py:88:25: PYI041 Use `complex` instead of `int | float | complex`
ℹ Safe fix
82 82 | def bad2(self, arg: int | Union[float, complex]) -> None:
83 83 | ...
84 84 |
85 |- def bad3(self, arg: Union[Union[float, complex], int]) -> None:
85 |+ def bad3(self, arg: complex) -> None:
86 86 | ...
87 87 |
88 88 | def bad4(self, arg: Union[float | complex, int]) -> None:

PYI041.py:88:25: PYI041 [*] Use `complex` instead of `int | float | complex`
|
86 | ...
87 |
Expand All @@ -140,7 +283,17 @@ PYI041.py:88:25: PYI041 Use `complex` instead of `int | float | complex`
|
= help: Remove redundant type

PYI041.py:91:25: PYI041 Use `complex` instead of `int | float | complex`
ℹ Safe fix
85 85 | def bad3(self, arg: Union[Union[float, complex], int]) -> None:
86 86 | ...
87 87 |
88 |- def bad4(self, arg: Union[float | complex, int]) -> None:
88 |+ def bad4(self, arg: complex) -> None:
89 89 | ...
90 90 |
91 91 | def bad5(self, arg: int | (float | complex)) -> None:

PYI041.py:91:25: PYI041 [*] Use `complex` instead of `int | float | complex`
|
89 | ...
90 |
Expand All @@ -149,3 +302,11 @@ PYI041.py:91:25: PYI041 Use `complex` instead of `int | float | complex`
92 | ...
|
= help: Remove redundant type

ℹ Safe fix
88 88 | def bad4(self, arg: Union[float | complex, int]) -> None:
89 89 | ...
90 90 |
91 |- def bad5(self, arg: int | (float | complex)) -> None:
91 |+ def bad5(self, arg: complex) -> None:
92 92 | ...
Loading
Loading