Skip to content

Commit

Permalink
Add required space to UP006 and UP007 fixes (#7202)
Browse files Browse the repository at this point in the history
We're removing a set of brackets here so we need to pad the fix.

Closes #7201.
  • Loading branch information
charliermarsh authored Sep 6, 2023
1 parent 171b66c commit c1af1c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP007.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,8 @@ class ServiceRefOrValue:
list[ServiceSpecificationRef]
| list[ServiceSpecification]
] = None


# Regression test for: https://github.com/astral-sh/ruff/issues/7201
class ServiceRefOrValue:
service_specification: Optional[str]is not True = None
19 changes: 16 additions & 3 deletions crates/ruff/src/rules/pyupgrade/rules/use_pep604_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ruff_python_ast::{self as ast, Constant, Expr, ExprContext, Operator};
use ruff_python_semantic::analyze::typing::Pep604Operator;
use ruff_text_size::{Ranged, TextRange};

use crate::autofix::edits::pad;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;

Expand Down Expand Up @@ -79,7 +80,11 @@ pub(crate) fn use_pep604_annotation(
}
_ => {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
checker.generator().expr(&optional(slice)),
pad(
checker.generator().expr(&optional(slice)),
expr.range(),
checker.locator(),
),
expr.range(),
)));
}
Expand All @@ -96,14 +101,22 @@ pub(crate) fn use_pep604_annotation(
}
Expr::Tuple(ast::ExprTuple { elts, .. }) => {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
checker.generator().expr(&union(elts)),
pad(
checker.generator().expr(&union(elts)),
expr.range(),
checker.locator(),
),
expr.range(),
)));
}
_ => {
// Single argument.
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
checker.locator().slice(slice).to_string(),
pad(
checker.locator().slice(slice).to_string(),
expr.range(),
checker.locator(),
),
expr.range(),
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,24 @@ UP007.py:102:28: UP007 [*] Use `X | Y` for type annotations
104 |- | list[ServiceSpecification]
105 |- ] = None
102 |+ service_specification: list[ServiceSpecificationRef] | list[ServiceSpecification] | None = None
106 103 |
107 104 |
108 105 | # Regression test for: https://github.com/astral-sh/ruff/issues/7201

UP007.py:110:28: UP007 [*] Use `X | Y` for type annotations
|
108 | # Regression test for: https://github.com/astral-sh/ruff/issues/7201
109 | class ServiceRefOrValue:
110 | service_specification: Optional[str]is not True = None
| ^^^^^^^^^^^^^ UP007
|
= help: Convert to `X | Y`

Suggested fix
107 107 |
108 108 | # Regression test for: https://github.com/astral-sh/ruff/issues/7201
109 109 | class ServiceRefOrValue:
110 |- service_specification: Optional[str]is not True = None
110 |+ service_specification: str | None is not True = None


0 comments on commit c1af1c2

Please sign in to comment.