Skip to content

Commit

Permalink
[Backport maintenance/2.17.x] Avoid consider-using-f-string on modu…
Browse files Browse the repository at this point in the history
…los with brackets in template … (#8836)

* Avoid `consider-using-f-string` on modulos with brackets in template (#8720)

Brackets can be inconvenient in f-string expressions.

Co-authored-by: Val Lorentz <[email protected]>
Co-authored-by: Pierre Sassoulas <[email protected]>
  • Loading branch information
3 people authored Jul 8, 2023
1 parent d690dd8 commit 1928589
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8720.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Avoid ``consider-using-f-string`` on modulos with brackets in template.

Closes #8720.
4 changes: 4 additions & 0 deletions pylint/checkers/refactoring/recommendation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
):
return

# Brackets can be inconvenient in f-string expressions
if "{" in node.parent.left.value or "}" in node.parent.left.value:
return

inferred_right = utils.safe_infer(node.parent.right)

# If dicts or lists of length > 1 are used
Expand Down
1 change: 1 addition & 0 deletions tests/functional/c/consider/consider_using_f_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def print_good():
print("{}".format("\n".join(i for i in "string")))
print("%s" % "\n")
print("%s" % "\n".join(i for i in "string"))
print("{%s}%s" % (PARAM_1, PARAM_2))


def print_bad():
Expand Down
42 changes: 21 additions & 21 deletions tests/functional/c/consider/consider_using_f_string.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
consider-using-f-string:45:10:45:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:46:10:46:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:47:10:47:28:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:48:10:48:17:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:49:10:49:26:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:50:10:50:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:47:10:47:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:48:10:48:28:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:49:10:49:17:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:50:10:50:26:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:51:10:51:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:52:10:52:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:53:10:53:24:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:54:10:54:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:80:4:80:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:53:10:53:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:54:10:54:24:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:55:10:55:21:print_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:81:4:81:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:82:4:82:22:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:83:4:83:11:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:84:4:84:20:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:85:4:85:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:82:4:82:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:83:4:83:22:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:84:4:84:11:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:85:4:85:20:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:86:4:86:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:87:4:87:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:88:4:88:18:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:89:4:89:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:112:8:112:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:88:4:88:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:89:4:89:18:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:90:4:90:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:113:8:113:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:114:8:114:26:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:115:8:115:15:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:116:8:116:24:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:117:8:117:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:114:8:114:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:115:8:115:26:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:116:8:116:15:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:117:8:117:24:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:118:8:118:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:119:8:119:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:120:8:120:22:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:121:8:121:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:120:8:120:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:121:8:121:22:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:122:8:122:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
2 changes: 1 addition & 1 deletion tests/functional/r/regression_02/regression_8109.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class Number:
round: int = 2

def __str__(self):
number_format = "{:,.%sf}" % self.round # [consider-using-f-string]
number_format = "{:,.%sf}" % self.round
return number_format.format(self.amount).rstrip("0").rstrip(".")
1 change: 0 additions & 1 deletion tests/functional/r/regression_02/regression_8109.txt

This file was deleted.

1 change: 0 additions & 1 deletion tests/test_epylint.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def test_epylint_good_command(example_path: PosixPath) -> None:
def test_epylint_strange_command(example_path: PosixPath) -> None:
with pytest.warns(DeprecationWarning):
out, _ = lint.py_run(
# pylint: disable-next=consider-using-f-string
"%s -E --disable=E1111 --msg-template={category} {module} {obj} {line} {column} {msg}"
% example_path,
return_std=True,
Expand Down

0 comments on commit 1928589

Please sign in to comment.