-
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
Autofix for none-not-at-end-of-union (RUF036)
#15139
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,58 +2,246 @@ | |
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
snapshot_kind: text | ||
--- | ||
RUF036.py:4:16: RUF036 `None` not at the end of the type annotation. | ||
RUF036.py:4:16: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
4 | def func1(arg: None | int): | ||
| ^^^^ RUF036 | ||
5 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
RUF036.py:8:16: RUF036 `None` not at the end of the type annotation. | ||
ℹ Safe fix | ||
1 1 | from typing import Union as U | ||
2 2 | | ||
3 3 | | ||
4 |-def func1(arg: None | int): | ||
4 |+def func1(arg: int | None): | ||
5 5 | ... | ||
6 6 | | ||
7 7 | | ||
|
||
RUF036.py:8:16: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
8 | def func2() -> None | int: | ||
| ^^^^ RUF036 | ||
9 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
5 5 | ... | ||
6 6 | | ||
7 7 | | ||
8 |-def func2() -> None | int: | ||
8 |+def func2() -> int | None: | ||
9 9 | ... | ||
10 10 | | ||
11 11 | | ||
|
||
RUF036.py:12:16: RUF036 `None` not at the end of the type annotation. | ||
| | ||
12 | def func3(arg: None | None | int): | ||
| ^^^^ RUF036 | ||
13 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
RUF036.py:12:23: RUF036 `None` not at the end of the type annotation. | ||
RUF036.py:12:23: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
12 | def func3(arg: None | None | int): | ||
| ^^^^ RUF036 | ||
13 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
9 9 | ... | ||
10 10 | | ||
11 11 | | ||
12 |-def func3(arg: None | None | int): | ||
12 |+def func3(arg: int | 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. We should avoid removing duplicate |
||
13 13 | ... | ||
14 14 | | ||
15 15 | | ||
|
||
RUF036.py:16:18: RUF036 `None` not at the end of the type annotation. | ||
RUF036.py:16:18: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
16 | def func4(arg: U[None, int]): | ||
| ^^^^ RUF036 | ||
17 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
RUF036.py:20:18: RUF036 `None` not at the end of the type annotation. | ||
ℹ Safe fix | ||
13 13 | ... | ||
14 14 | | ||
15 15 | | ||
16 |-def func4(arg: U[None, int]): | ||
16 |+def func4(arg: U[int, None]): | ||
17 17 | ... | ||
18 18 | | ||
19 19 | | ||
|
||
RUF036.py:20:18: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
20 | def func5() -> U[None, int]: | ||
| ^^^^ RUF036 | ||
21 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
17 17 | ... | ||
18 18 | | ||
19 19 | | ||
20 |-def func5() -> U[None, int]: | ||
20 |+def func5() -> U[int, None]: | ||
21 21 | ... | ||
22 22 | | ||
23 23 | | ||
|
||
RUF036.py:24:18: RUF036 `None` not at the end of the type annotation. | ||
| | ||
24 | def func6(arg: U[None, None, int]): | ||
| ^^^^ RUF036 | ||
25 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
RUF036.py:24:24: RUF036 `None` not at the end of the type annotation. | ||
RUF036.py:24:24: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
24 | def func6(arg: U[None, None, int]): | ||
| ^^^^ RUF036 | ||
25 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
21 21 | ... | ||
22 22 | | ||
23 23 | | ||
24 |-def func6(arg: U[None, None, int]): | ||
24 |+def func6(arg: U[int, None]): | ||
25 25 | ... | ||
26 26 | | ||
27 27 | | ||
|
||
RUF036.py:29:5: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
28 | def func7() -> U[ | ||
29 | None, | ||
| ^^^^ RUF036 | ||
30 | # comment | ||
31 | int | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Unsafe fix | ||
26 26 | | ||
27 27 | | ||
28 28 | def func7() -> U[ | ||
29 |- None, | ||
30 |- # comment | ||
31 |- int | ||
29 |+ int, None | ||
32 30 | ]: | ||
33 31 | ... | ||
34 32 | | ||
|
||
RUF036.py:36:14: RUF036 `None` not at the end of the type annotation. | ||
| | ||
36 | def func8(x: None | U[None ,int]): | ||
| ^^^^ RUF036 | ||
37 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
RUF036.py:36:23: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
36 | def func8(x: None | U[None ,int]): | ||
| ^^^^ RUF036 | ||
37 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
33 33 | ... | ||
34 34 | | ||
35 35 | | ||
36 |-def func8(x: None | U[None ,int]): | ||
36 |+def func8(x: int | None): | ||
37 37 | ... | ||
38 38 | | ||
39 39 | | ||
|
||
RUF036.py:40:27: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
40 | def func9(x: int | (str | None) | list): | ||
| ^^^^ RUF036 | ||
41 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
37 37 | ... | ||
38 38 | | ||
39 39 | | ||
40 |-def func9(x: int | (str | None) | list): | ||
40 |+def func9(x: int | str | list | None): | ||
41 41 | ... | ||
42 42 | | ||
43 43 | | ||
|
||
RUF036.py:44:24: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
44 | def func10(x: U[int, U[None, list | set]]): | ||
| ^^^^ RUF036 | ||
45 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
41 41 | ... | ||
42 42 | | ||
43 43 | | ||
44 |-def func10(x: U[int, U[None, list | set]]): | ||
44 |+def func10(x: U[int, list, set, None]): | ||
Comment on lines
+207
to
+208
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. The fix here looks incorrect. It flattens the nested 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. @MichaReiser Do we need to fix this case? Can we only generate a fix when the type annotation has a single 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. I don't think we necessarily have to but we at least shouldn't provide a fix for those cases. |
||
45 45 | ... | ||
46 46 | | ||
47 47 | | ||
|
||
RUF036.py:48:15: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
48 | def func11(x: None | int) -> None | int: | ||
| ^^^^ RUF036 | ||
49 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
45 45 | ... | ||
46 46 | | ||
47 47 | | ||
48 |-def func11(x: None | int) -> None | int: | ||
48 |+def func11(x: int | None) -> None | int: | ||
49 49 | ... | ||
50 50 | | ||
51 51 | | ||
|
||
RUF036.py:48:30: RUF036 [*] `None` not at the end of the type annotation. | ||
| | ||
48 | def func11(x: None | int) -> None | int: | ||
| ^^^^ RUF036 | ||
49 | ... | ||
| | ||
= help: Move `None` to the end of the type annotation | ||
|
||
ℹ Safe fix | ||
45 45 | ... | ||
46 46 | | ||
47 47 | | ||
48 |-def func11(x: None | int) -> None | int: | ||
48 |+def func11(x: None | int) -> int | None: | ||
49 49 | ... | ||
50 50 | | ||
51 51 | |
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 set the fix to all the diagnostics?
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.
Can you tell me a bit more why you think we should (or shouldn't) set the fix for all violations?
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.
@MichaReiser For example, this part looks like the same fix is set to all the diagnostics:
ruff/crates/ruff_linter/src/rules/flake8_pyi/rules/duplicate_union_member.rs
Lines 126 to 129 in 8b9c843
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'm actually leaning towards only emitting a single diagnostic rather than emitting multiple diagnostics. This should also simplify the code a bit because we can change
none_exprs
to anOption
only tracking thelast_none
rather than allNone
valuesThere 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.
A single diagnostic makes sense.