Skip to content

Commit

Permalink
implement assignment changes in autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharsadhwani committed May 7, 2024
1 parent 7e566f5 commit aec54e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,17 @@ pub(crate) fn redundant_final_literal(checker: &mut Checker, ann_assign: &ast::S
},
ann_assign.range(),
)
.with_fix(generate_fix(annotation, assign_value.as_deref(), literal)),
.with_fix(generate_fix(
checker,
annotation,
assign_value.as_deref(),
literal,
)),
);
}

fn generate_fix(
checker: &Checker,
annotation: &ast::Expr,
assign_value: Option<&ast::Expr>,
literal: &ast::Expr,
Expand All @@ -98,13 +104,22 @@ fn generate_fix(
let insertion = Edit::insertion(format!("Final"), annotation.start());

let Some(assign_value) = assign_value else {
// TODO: modify the assign side
return Fix::safe_edits(deletion, [insertion]);
// If no assignment exists, add our own, same as the literal value.
let literal_source = checker.locator().slice(literal.range());
let assignment = Edit::insertion(format!(" = {literal_source}"), annotation.end());
return Fix::safe_edits(deletion, [insertion, assignment]);
};

if ComparableExpr::from(assign_value) != ComparableExpr::from(literal) {
// TODO: modify the assign side
return Fix::unsafe_edits(deletion, [insertion]);
// In this case, assume that the value in the literal annotation
// is the correct one.
let literal_source = checker.locator().slice(literal.range());
let assign_replacement = Edit::replacement(
literal_source.to_string(),
assign_value.start(),
assign_value.end(),
);
return Fix::unsafe_edits(deletion, [insertion, assign_replacement]);
}

Fix::safe_edits(deletion, [insertion])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ PYI064.py:10:1: PYI064 [*] `Final[Literal[123]]` can be replaced with a bare `Fi
8 8 |
9 9 | # This should be fixable, and marked as safe
10 |-w1: Final[Literal[123]] # PYI064
10 |+w1: Final # PYI064
10 |+w1: Final = 123 # PYI064
11 11 |
12 12 | # This should be fixable, but marked as unsafe
13 13 | w2: Final[Literal[123]] = "random value" # PYI064
Expand All @@ -101,7 +101,7 @@ PYI064.py:13:1: PYI064 [*] `Final[Literal[123]]` can be replaced with a bare `Fi
11 11 |
12 12 | # This should be fixable, but marked as unsafe
13 |-w2: Final[Literal[123]] = "random value" # PYI064
13 |+w2: Final = "random value" # PYI064
13 |+w2: Final = 123 # PYI064
14 14 |
15 15 | n1: Final[Literal[True, False]] = True # No issue here
16 16 | n2: Literal[True] = True # No issue here
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ PYI064.pyi:3:1: PYI064 [*] `Final[Literal[True]]` can be replaced with a bare `F
1 1 | from typing import Final, Literal
2 2 |
3 |-x: Final[Literal[True]] # PYI064
3 |+x: Final # PYI064
3 |+x: Final = True # PYI064
4 4 | y: Final[Literal[None]] = None # PYI064
5 5 | z: Final[Literal["this is a really long literal, that won't be rendered in the issue text"]] # PYI064
6 6 |
Expand Down Expand Up @@ -56,7 +56,7 @@ PYI064.pyi:5:1: PYI064 [*] `Final[Literal[...]] can be replaced with a bare `Fin
3 3 | x: Final[Literal[True]] # PYI064
4 4 | y: Final[Literal[None]] = None # PYI064
5 |-z: Final[Literal["this is a really long literal, that won't be rendered in the issue text"]] # PYI064
5 |+z: Final # PYI064
5 |+z: Final = "this is a really long literal, that won't be rendered in the issue text" # PYI064
6 6 |
7 7 | # This should be fixable, and marked as safe
8 8 | w1: Final[Literal[123]] # PYI064
Expand All @@ -76,7 +76,7 @@ PYI064.pyi:8:1: PYI064 [*] `Final[Literal[123]]` can be replaced with a bare `Fi
6 6 |
7 7 | # This should be fixable, and marked as safe
8 |-w1: Final[Literal[123]] # PYI064
8 |+w1: Final # PYI064
8 |+w1: Final = 123 # PYI064
9 9 |
10 10 | # This should be fixable, but marked as unsafe
11 11 | w2: Final[Literal[123]] = "random value" # PYI064
Expand All @@ -96,7 +96,7 @@ PYI064.pyi:11:1: PYI064 [*] `Final[Literal[123]]` can be replaced with a bare `F
9 9 |
10 10 | # This should be fixable, but marked as unsafe
11 |-w2: Final[Literal[123]] = "random value" # PYI064
11 |+w2: Final = "random value" # PYI064
11 |+w2: Final = 123 # PYI064
12 12 |
13 13 | n1: Final[Literal[True, False]] # No issue here
14 14 | n2: Literal[True] # No issue here

0 comments on commit aec54e8

Please sign in to comment.