summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-09-21 09:01:31 +0200
committerGitHub <noreply@github.com>2021-09-21 09:01:31 +0200
commit95b530d5b776c7716eb81125e065cf4eadd7ff7c (patch)
tree67511a5888758011ef3579c4e2578a77da774703
parentb3336c77378f88eb1428d6ff5b6e78bba1177f40 (diff)
downloadpylint-git-95b530d5b776c7716eb81125e065cf4eadd7ff7c.tar.gz
Make ``consider-using-f-string`` skip `format()` with backslash (#5055)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r--ChangeLog2
-rw-r--r--pylint/checkers/refactoring/recommendation_checker.py7
-rw-r--r--tests/functional/c/consider/consider_using_f_string.py13
-rw-r--r--tests/functional/c/consider/consider_using_f_string.txt48
4 files changed, 45 insertions, 25 deletions
diff --git a/ChangeLog b/ChangeLog
index c078c2ae2..4b2a26008 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -29,6 +29,8 @@ Release date: TBA
Closes #5029
+* Don't emit ``consider-using-f-string`` if the variables to be interpolated include a backslash
+
* Fixed false positive for ``cell-var-from-loop`` when variable is used as the default
value for a keyword-only parameter.
diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py
index b22f18acd..7e8abcb7e 100644
--- a/pylint/checkers/refactoring/recommendation_checker.py
+++ b/pylint/checkers/refactoring/recommendation_checker.py
@@ -355,6 +355,9 @@ class RecommendationChecker(checkers.BaseChecker):
and len(inferred.elts) > 1
):
return
+ # Backslashes can't be in f-string expressions
+ if "\\" in arg.as_string():
+ return
elif node.parent.parent.keywords:
keyword_args = [
@@ -381,6 +384,10 @@ class RecommendationChecker(checkers.BaseChecker):
)
elif isinstance(node.parent, nodes.BinOp) and node.parent.op == "%":
+ # Backslashes can't be in f-string expressions
+ if "\\" in node.parent.right.as_string():
+ return
+
inferred_right = utils.safe_infer(node.parent.right)
# If dicts or lists of length > 1 are used
diff --git a/tests/functional/c/consider/consider_using_f_string.py b/tests/functional/c/consider/consider_using_f_string.py
index 825f3517c..1d5400d5a 100644
--- a/tests/functional/c/consider/consider_using_f_string.py
+++ b/tests/functional/c/consider/consider_using_f_string.py
@@ -1,6 +1,6 @@
"""Test to see if a f-string would be possible and consider-using-f-string should be raised"""
# pylint: disable=unused-variable, invalid-name, missing-function-docstring, pointless-statement
-# pylint: disable=expression-not-assigned, repeated-keyword
+# pylint: disable=expression-not-assigned, repeated-keyword, too-many-locals
PARAM_1 = PARAM_2 = PARAM_3 = 1
PARAM_LIST = [PARAM_1, PARAM_2, PARAM_3]
@@ -35,6 +35,11 @@ def print_good():
print("%(Param_1)s %(Param_2)s" % PARAM_DICT)
print("%(Param_1)s %(Param_2)s" % return_dict())
print("{a[Param_1]}{a[Param_2]}".format(a=PARAM_DICT))
+ print("{}".format("\n"))
+ print("{}".format("\n".join(i for i in "string")))
+ print("%s" % "\n")
+ print("%s" % "\n".join(i for i in "string"))
+
def print_bad():
print("String %f" % PARAM_1) # [consider-using-f-string]
@@ -63,6 +68,10 @@ def statement_good():
"%(Param_1)s %(Param_2)s" % PARAM_DICT
"%(Param_1)s %(Param_2)s" % return_dict()
"{a[Param_1]}{a[Param_2]}".format(a=PARAM_DICT)
+ "{}".format("\n")
+ "{}".format("\n".join(i for i in "string"))
+ "%s" % "\n"
+ "%s" % "\n".join(i for i in "string")
def statement_bad():
"String %f" % PARAM_1 # [consider-using-f-string]
@@ -92,6 +101,8 @@ def assignment_good():
L = "%(Param_1)s %(Param_2)s" % return_dict()
M = "{a[Param_1]}{a[Param_2]}".format(a=PARAM_DICT)
N = "{Param}".format
+ O = "%s" % "\n"
+ P = "%s" % "\n".join(i for i in "string")
def assignment_bad():
diff --git a/tests/functional/c/consider/consider_using_f_string.txt b/tests/functional/c/consider/consider_using_f_string.txt
index db8f98112..65363cea4 100644
--- a/tests/functional/c/consider/consider_using_f_string.txt
+++ b/tests/functional/c/consider/consider_using_f_string.txt
@@ -1,30 +1,30 @@
-consider-using-f-string:40:10:print_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:41:10:print_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:42:10:print_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:43:10:print_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:44:10:print_bad:Formatting a regular string which could be a f-string:HIGH
consider-using-f-string:45:10:print_bad:Formatting a regular string which could be a f-string:HIGH
consider-using-f-string:46:10:print_bad:Formatting a regular string which could be a f-string:HIGH
consider-using-f-string:47:10:print_bad:Formatting a regular string which could be a f-string:HIGH
consider-using-f-string:48:10:print_bad:Formatting a regular string which could be a f-string:HIGH
consider-using-f-string:49:10:print_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:68:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:69:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:70:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:71:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:72:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:73:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:74:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:75:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:76:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:50:10:print_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:51:10:print_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:52:10:print_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:53:10:print_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:54:10:print_bad:Formatting a regular string which could be a f-string:HIGH
consider-using-f-string:77:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:98:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:99:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:100:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:101:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:102:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:103:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:104:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:105:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:106:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
-consider-using-f-string:107:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:78:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:79:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:80:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:81:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:82:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:83:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:84:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:85:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:86:4:statement_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:109:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:110:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:111:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:112:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:113:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:114:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:115:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:116:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:117:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH
+consider-using-f-string:118:8:assignment_bad:Formatting a regular string which could be a f-string:HIGH