Skip to content

Commit

Permalink
Fixed misinterpreted modulo sign for consider-using-f-string (#6914)
Browse files Browse the repository at this point in the history
* Fixed a false positive in consider-using-f-string if the left side of a % is not a string.

* consider-using-f-string: if left side of a \% does not have the attr value it can't be a str
  • Loading branch information
d1gl3 authored and Pierre-Sassoulas committed Jun 15, 2022
1 parent e9683c4 commit c742f8f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ What's New in Pylint 2.14.2?
----------------------------
Release date: TBA

* Fixed a false positive in ``consider-using-f-string`` if the left side of a ``%`` is not a string.

Closes #6689

* Fixed a false positive in ``unnecessary-list-index-lookup`` and ``unnecessary-dict-index-lookup``
when the subscript is updated in the body of a nested loop.

Expand Down
6 changes: 6 additions & 0 deletions pylint/checkers/refactoring/recommendation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,12 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
if "\\" in node.parent.right.as_string():
return

# If % applied to another type than str, it's modulo and can't be replaced by formatting
if not hasattr(node.parent.left, "value") or not isinstance(
node.parent.left.value, str
):
return

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

# If dicts or lists of length > 1 are used
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/c/consider/consider_using_f_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def statement_good():
"{}".format("\n".join(i for i in "string"))
"%s" % "\n"
"%s" % "\n".join(i for i in "string")
1 % "str"
(1, 2) % 'garbage'


def statement_bad():
"String %f" % PARAM_1 # [consider-using-f-string]
Expand Down
36 changes: 18 additions & 18 deletions tests/functional/c/consider/consider_using_f_string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ consider-using-f-string:51:10:51:21:print_bad:Formatting a regular string which
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:77:4:77:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:78:4:78:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:79:4:79:22:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:80:4:80:11:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:81:4:81:20: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:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:84:4:84:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:85:4:85:18:statement_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: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:86:4:86:15:statement_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:109:8:109:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:110:8:110:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:111:8:111:26:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:112:8:112:15:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:113:8:113:24: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:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:116:8:116:19:assignment_bad:Formatting a regular string which could be a f-string:UNDEFINED
consider-using-f-string:117:8:117:22:assignment_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: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: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

0 comments on commit c742f8f

Please sign in to comment.