Add required space to UP006 and UP007 fixes (#7202)

We're removing a set of brackets here so we need to pad the fix.

Closes https://github.com/astral-sh/ruff/issues/7201.
This commit is contained in:
Charlie Marsh 2023-09-06 18:06:02 +02:00 committed by GitHub
parent 171b66cb43
commit c1af1c291d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 3 deletions

View file

@ -103,3 +103,8 @@ class ServiceRefOrValue:
list[ServiceSpecificationRef] list[ServiceSpecificationRef]
| list[ServiceSpecification] | list[ServiceSpecification]
] = None ] = None
# Regression test for: https://github.com/astral-sh/ruff/issues/7201
class ServiceRefOrValue:
service_specification: Optional[str]is not True = None

View file

@ -4,6 +4,7 @@ use ruff_python_ast::{self as ast, Constant, Expr, ExprContext, Operator};
use ruff_python_semantic::analyze::typing::Pep604Operator; use ruff_python_semantic::analyze::typing::Pep604Operator;
use ruff_text_size::{Ranged, TextRange}; use ruff_text_size::{Ranged, TextRange};
use crate::autofix::edits::pad;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
use crate::registry::AsRule; use crate::registry::AsRule;
@ -79,7 +80,11 @@ pub(crate) fn use_pep604_annotation(
} }
_ => { _ => {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement( 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(), expr.range(),
))); )));
} }
@ -96,14 +101,22 @@ pub(crate) fn use_pep604_annotation(
} }
Expr::Tuple(ast::ExprTuple { elts, .. }) => { Expr::Tuple(ast::ExprTuple { elts, .. }) => {
diagnostic.set_fix(Fix::suggested(Edit::range_replacement( 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(), expr.range(),
))); )));
} }
_ => { _ => {
// Single argument. // Single argument.
diagnostic.set_fix(Fix::suggested(Edit::range_replacement( 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(), expr.range(),
))); )));
} }

View file

@ -391,5 +391,24 @@ UP007.py:102:28: UP007 [*] Use `X | Y` for type annotations
104 |- | list[ServiceSpecification] 104 |- | list[ServiceSpecification]
105 |- ] = None 105 |- ] = None
102 |+ service_specification: list[ServiceSpecificationRef] | list[ServiceSpecification] | None = 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