Skip to content

Commit

Permalink
A new check f-string-without-interpolation was added
Browse files Browse the repository at this point in the history
Close #3190
  • Loading branch information
PCManticore committed Oct 16, 2019
1 parent 462a72d commit 141873a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Release date: TBA
* ``inspect.getargvalues`` is no longer marked as deprecated.


* A new check ``f-string-without-interpolation`` was added

Close #3190


What's New in Pylint 2.4.3?
===========================
Expand Down
7 changes: 6 additions & 1 deletion doc/whatsnew/2.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Summary -- Release highlights
New checkers
============

* A new check ``f-string-without-interpolation`` was added.

This check is emitted whenever **pylint** detects the use of an
f-string without having any interpolated values in it, which means
that the f-string can be a normal string.


Other Changes
=============
Expand All @@ -24,4 +30,3 @@ Other Changes
A pyproject.toml file must prepend section names with ``tool.pylint.``,
for example ``[tool.pylint.'MESSAGES CONTROL']``.
These files can also be passed in on the command line.

15 changes: 15 additions & 0 deletions pylint/checkers/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
"Used when we detect that a string formatting is "
"repeating an argument instead of using named string arguments",
),
"W1309": (
"Using an f-string that does not have any interpolated variables",
"f-string-without-interpolation",
"Used when we detect an f-string that does not use any interpolation variables, "
"in which case it can be either a normal string or a bug in the code.",
),
}

OTHER_NODES = (
Expand Down Expand Up @@ -333,6 +339,15 @@ def visit_binop(self, node):
args=(arg_type.pytype(), format_type),
)

@check_messages("f-string-without-interpolation")
def visit_joinedstr(self, node):
if isinstance(node.parent, astroid.FormattedValue):
return
for value in node.values:
if isinstance(value, astroid.FormattedValue):
return
self.add_message("f-string-without-interpolation", node=node)

@check_messages(*MSGS)
def visit_call(self, node):
func = utils.safe_infer(node.func)
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/f/f_string_without_interpolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# pylint: disable=missing-module-docstring
import math

VALUE = 1

print(f"some value {VALUE}")
print(f'pi: {math.pi:.3f}')

print(f"ERROR") # [f-string-without-interpolation]
2 changes: 2 additions & 0 deletions tests/functional/f/f_string_without_interpolation.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.6
1 change: 1 addition & 0 deletions tests/functional/f/f_string_without_interpolation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f-string-without-interpolation:9::Using an f-string that does not have any interpolated variables

0 comments on commit 141873a

Please sign in to comment.