Skip to content

Commit

Permalink
Allow EM fixes even if msg variable is defined (#9059)
Browse files Browse the repository at this point in the history
This PR updates the `EM` rules to generate the auto-fix even if the
`msg` variable is defined in the current scope.

As discussed in #9052.
  • Loading branch information
dhruvmanila committed Dec 8, 2023
1 parent e043bd4 commit b7dd2b5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def f_ok():
raise RuntimeError(msg)


def f_unfixable():
def f_msg_defined():
msg = "hello"
raise RuntimeError("This is an example exception")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,13 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
if let Some(indentation) =
whitespace::indentation(checker.locator(), stmt)
{
if checker.semantic().is_available("msg") {
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.locator(),
));
}
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.locator(),
));
}
checker.diagnostics.push(diagnostic);
}
Expand All @@ -211,15 +209,13 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
let mut diagnostic = Diagnostic::new(FStringInException, first.range());
if let Some(indentation) = whitespace::indentation(checker.locator(), stmt)
{
if checker.semantic().is_available("msg") {
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.locator(),
));
}
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.locator(),
));
}
checker.diagnostics.push(diagnostic);
}
Expand All @@ -236,15 +232,13 @@ pub(crate) fn string_in_exception(checker: &mut Checker, stmt: &Stmt, exc: &Expr
if let Some(indentation) =
whitespace::indentation(checker.locator(), stmt)
{
if checker.semantic().is_available("msg") {
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.locator(),
));
}
diagnostic.set_fix(generate_fix(
stmt,
first,
indentation,
checker.stylist(),
checker.locator(),
));
}
checker.diagnostics.push(diagnostic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,26 @@ EM.py:22:24: EM103 [*] Exception must not use a `.format()` string directly, ass
24 25 |
25 26 | def f_ok():

EM.py:32:24: EM101 Exception must not use a string literal, assign to variable first
EM.py:32:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
30 | def f_unfixable():
30 | def f_msg_defined():
31 | msg = "hello"
32 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

Unsafe fix
29 29 |
30 30 | def f_msg_defined():
31 31 | msg = "hello"
32 |- raise RuntimeError("This is an example exception")
32 |+ msg = "This is an example exception"
33 |+ raise RuntimeError(msg)
33 34 |
34 35 |
35 36 | def f_msg_in_nested_scope():

EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
37 | msg = "hello"
Expand All @@ -88,14 +99,25 @@ EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variab
41 42 |
42 43 | def f_msg_in_parent_scope():

EM.py:46:28: EM101 Exception must not use a string literal, assign to variable first
EM.py:46:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
45 | def nested():
46 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

Unsafe fix
43 43 | msg = "hello"
44 44 |
45 45 | def nested():
46 |- raise RuntimeError("This is an example exception")
46 |+ msg = "This is an example exception"
47 |+ raise RuntimeError(msg)
47 48 |
48 49 |
49 50 | def f_fix_indentation_check(foo):

EM.py:51:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
49 | def f_fix_indentation_check(foo):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,26 @@ EM.py:22:24: EM103 [*] Exception must not use a `.format()` string directly, ass
24 25 |
25 26 | def f_ok():

EM.py:32:24: EM101 Exception must not use a string literal, assign to variable first
EM.py:32:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
30 | def f_unfixable():
30 | def f_msg_defined():
31 | msg = "hello"
32 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

Unsafe fix
29 29 |
30 30 | def f_msg_defined():
31 31 | msg = "hello"
32 |- raise RuntimeError("This is an example exception")
32 |+ msg = "This is an example exception"
33 |+ raise RuntimeError(msg)
33 34 |
34 35 |
35 36 | def f_msg_in_nested_scope():

EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variable first
|
37 | msg = "hello"
Expand All @@ -126,14 +137,25 @@ EM.py:39:24: EM101 [*] Exception must not use a string literal, assign to variab
41 42 |
42 43 | def f_msg_in_parent_scope():

EM.py:46:28: EM101 Exception must not use a string literal, assign to variable first
EM.py:46:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
45 | def nested():
46 | raise RuntimeError("This is an example exception")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EM101
|
= help: Assign to variable; remove string literal

Unsafe fix
43 43 | msg = "hello"
44 44 |
45 45 | def nested():
46 |- raise RuntimeError("This is an example exception")
46 |+ msg = "This is an example exception"
47 |+ raise RuntimeError(msg)
47 48 |
48 49 |
49 50 | def f_fix_indentation_check(foo):

EM.py:51:28: EM101 [*] Exception must not use a string literal, assign to variable first
|
49 | def f_fix_indentation_check(foo):
Expand Down

0 comments on commit b7dd2b5

Please sign in to comment.