Skip to content
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
6 changes: 5 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/ruff/RUF036.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def func14(arg: None | int | str | bytes):
...


# Preserve token boundaries when fixing non-annotation expressions.
print(None | (int)and 2)
print(2 or(None) | int)


# Ok
def good_func1(arg: int | None):
...
Expand All @@ -85,4 +90,3 @@ def good_func4(arg: U[None]):

def good_func5(arg: U[int]):
...

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ruff_python_semantic::analyze::typing::traverse_union;
use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
use crate::fix::edits::pad;
use crate::{Edit, Fix, FixAvailability, Violation};

/// ## What it does
Expand Down Expand Up @@ -184,6 +185,13 @@ fn generate_fix(
})
};

let edit = Edit::range_replacement(checker.generator().expr(&new_expr), annotation.range());
let edit = Edit::range_replacement(
pad(
checker.generator().expr(&new_expr),
annotation.range(),
checker.locator(),
),
annotation.range(),
);
Some(Fix::applicable_edit(edit, applicability))
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,40 @@ help: Move `None` to the end of the type union
65 + def func14(arg: int | str | bytes | None):
66 | ...
67 |
68 |
68 |

RUF036 [*] `None` not at the end of the type union.
--> RUF036.py:70:7
|
69 | # Preserve token boundaries when fixing non-annotation expressions.
70 | print(None | (int)and 2)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I'm surprised that the rule applies to cases outside of annotations. The docs make it sound like it only applies to annotations, which is what I always expected. Should we change that instead (or in addition)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is necessary regardless, but yeah, it should be limited to type contexts.

| ^^^^^^^^^^^^
71 | print(2 or(None) | int)
|
help: Move `None` to the end of the type union
67 |
68 |
69 | # Preserve token boundaries when fixing non-annotation expressions.
- print(None | (int)and 2)
70 + print(int | None and 2)
71 | print(2 or(None) | int)
72 |
73 |

RUF036 [*] `None` not at the end of the type union.
--> RUF036.py:71:11
|
69 | # Preserve token boundaries when fixing non-annotation expressions.
70 | print(None | (int)and 2)
71 | print(2 or(None) | int)
| ^^^^^^^^^^^^
|
help: Move `None` to the end of the type union
68 |
69 | # Preserve token boundaries when fixing non-annotation expressions.
70 | print(None | (int)and 2)
- print(2 or(None) | int)
71 + print(2 or int | None)
72 |
73 |
74 | # Ok