summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Martin <tim@asymptotic.co.uk>2021-10-06 20:56:32 +0100
committerGitHub <noreply@github.com>2021-10-06 21:56:32 +0200
commitf963868f6b46d31ef7ae1bd1196eafe50bc4bad7 (patch)
treebf03bfc20aa347f2ffa3266f816b2b4e8e51bdf7
parent6ad9d6ce38988c55fa8d2c0cccfe03df8a08de54 (diff)
downloadpylint-git-f963868f6b46d31ef7ae1bd1196eafe50bc4bad7.tar.gz
Fix a crash if ``str.format`` is referenced without being called (#5109)
Closes #5058 Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r--CONTRIBUTORS.txt2
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/refactoring/recommendation_checker.py4
-rw-r--r--tests/functional/c/consider/consider_using_f_string.py8
4 files changed, 16 insertions, 2 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 34da2a803..4f4164cfa 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -546,3 +546,5 @@ contributors:
* Jeroen Seegers (jeroenseegers): contributor
- Fixed `toml` dependency issue
+
+* Tim Martin: contributor
diff --git a/ChangeLog b/ChangeLog
index e01a402a7..a4a650fd6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -58,6 +58,10 @@ Release date: TBA
Closes #3614
+* Fixed crash in ``consider-using-f-string`` if ``format`` is not called
+
+ Closes #5058
+
* Fix crash with ``AssignAttr`` in ``if TYPE_CHECKING`` blocks.
Closes #5111
diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py
index 81a7dd7b1..3bccd5fcb 100644
--- a/pylint/checkers/refactoring/recommendation_checker.py
+++ b/pylint/checkers/refactoring/recommendation_checker.py
@@ -341,8 +341,8 @@ class RecommendationChecker(checkers.BaseChecker):
isinstance(node.parent, nodes.Attribute)
and node.parent.attrname == "format"
):
- # Allow assigning .format to a variable
- if isinstance(node.parent.parent, nodes.Assign):
+ # Don't warn on referencing / assigning .format without calling it
+ if not isinstance(node.parent.parent, nodes.Call):
return
if node.parent.parent.args:
diff --git a/tests/functional/c/consider/consider_using_f_string.py b/tests/functional/c/consider/consider_using_f_string.py
index 1d5400d5a..f6801989e 100644
--- a/tests/functional/c/consider/consider_using_f_string.py
+++ b/tests/functional/c/consider/consider_using_f_string.py
@@ -116,3 +116,11 @@ def assignment_bad():
h = "String %s" % (PARAM_1) # [consider-using-f-string]
i = "String %s %s" % (PARAM_1, PARAM_2) # [consider-using-f-string]
j = "String %s" % (PARAM_LIST_SINGLE) # [consider-using-f-string]
+
+
+def regression_tests():
+ # Referencing .format in a kwarg should not be warned
+ def wrap_print(value):
+ print(value)
+
+ wrap_print(value="{}".format)