Skip to content

Commit

Permalink
Add a check for asserts on string literals (#3346)
Browse files Browse the repository at this point in the history
This check is emitted whenever **pylint** finds an assert statement
with a string literal as its first argument. Such assert statements
are probably unintended as they will always pass.

Close #3284
  • Loading branch information
anubh-v authored and PCManticore committed Jan 14, 2020
1 parent be87624 commit f2f4e6f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ What's New in Pylint 2.5.0?

Release date: TBA

* Add a check for asserts on string literals.

Close #3284

* Clean up plugin HOWTO documentation.

* `not in` is considered iterating context for some of the Python 3 porting checkers.
Expand Down
6 changes: 6 additions & 0 deletions 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 ``assert-on-string-literal`` was added.

This check is emitted whenever **pylint** finds an assert statement
with a string literal as its first argument. Such assert statements
are probably unintended as they will always pass.

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

This check is emitted whenever **pylint** detects the use of an
Expand Down
13 changes: 11 additions & 2 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,12 @@ class BasicChecker(_BasicChecker):
'print("value: {}".format(123)). This might not be what the user '
"intended to do.",
),
"W0129": (
"Assert statement has a string literal as its first argument. The assert will never fail.",
"assert-on-string-literal",
"Used when an assert statement has a string literal as its first argument, which will "
"cause the assert to always pass.",
),
}

reports = (("RP0101", "Statistics by type", report_by_type_stats),)
Expand Down Expand Up @@ -1377,16 +1383,19 @@ def visit_call(self, node):
elif name == "eval":
self.add_message("eval-used", node=node)

@utils.check_messages("assert-on-tuple")
@utils.check_messages("assert-on-tuple", "assert-on-string-literal")
def visit_assert(self, node):
"""check the use of an assert statement on a tuple."""
"""check whether assert is used on a tuple or string literal."""
if (
node.fail is None
and isinstance(node.test, astroid.Tuple)
and len(node.test.elts) == 2
):
self.add_message("assert-on-tuple", node=node)

if isinstance(node.test, astroid.Const) and isinstance(node.test.value, str):
self.add_message("assert-on-string-literal", node=node)

@utils.check_messages("duplicate-key")
def visit_dict(self, node):
"""check duplicate key in dictionary"""
Expand Down
3 changes: 3 additions & 0 deletions tests/functional/a/assert_on_string_literal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pylint: disable=missing-module-docstring, undefined-variable
assert [foo, bar], "No AssertionError"
assert "There is an AssertionError" # [assert-on-string-literal]
1 change: 1 addition & 0 deletions tests/functional/a/assert_on_string_literal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert-on-string-literal:3::Assert statement has a string literal as its first argument. The assert will never fail.

0 comments on commit f2f4e6f

Please sign in to comment.