summaryrefslogtreecommitdiff
path: root/_pytest/assertion
diff options
context:
space:
mode:
authorBruno Oliveira <nicoddemus@gmail.com>2016-06-26 16:09:15 -0300
committerGitHub <noreply@github.com>2016-06-26 16:09:15 -0300
commit5891061ac1cb248ff6b9f57f41bd1eddb26deddf (patch)
tree1721cf91f08c7eae8d7cbe4780b1a13726400a33 /_pytest/assertion
parent48ac1a0986e105dc95dbed194019ae9a1eb3b5ab (diff)
parent6d4cee21598f35100b46c5d892a8b3af99ec8087 (diff)
downloadpytest-5891061ac1cb248ff6b9f57f41bd1eddb26deddf.tar.gz
Merge pull request #1675 from kvas-it/issue-1562
Add warning for assertions on tuples #1562
Diffstat (limited to '_pytest/assertion')
-rw-r--r--_pytest/assertion/rewrite.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py
index fd4f66cd0..921d17a10 100644
--- a/_pytest/assertion/rewrite.py
+++ b/_pytest/assertion/rewrite.py
@@ -125,7 +125,7 @@ class AssertionRewritingHook(object):
co = _read_pyc(fn_pypath, pyc, state.trace)
if co is None:
state.trace("rewriting %r" % (fn,))
- source_stat, co = _rewrite_test(state, fn_pypath)
+ source_stat, co = _rewrite_test(self.config, fn_pypath)
if co is None:
# Probably a SyntaxError in the test.
return None
@@ -252,8 +252,9 @@ N = "\n".encode("utf-8")
cookie_re = re.compile(r"^[ \t\f]*#.*coding[:=][ \t]*[-\w.]+")
BOM_UTF8 = '\xef\xbb\xbf'
-def _rewrite_test(state, fn):
+def _rewrite_test(config, fn):
"""Try to read and rewrite *fn* and return the code object."""
+ state = config._assertstate
try:
stat = fn.stat()
source = fn.read("rb")
@@ -298,7 +299,7 @@ def _rewrite_test(state, fn):
# Let this pop up again in the real import.
state.trace("failed to parse: %r" % (fn,))
return None, None
- rewrite_asserts(tree)
+ rewrite_asserts(tree, fn, config)
try:
co = compile(tree, fn.strpath, "exec")
except SyntaxError:
@@ -354,9 +355,9 @@ def _read_pyc(source, pyc, trace=lambda x: None):
return co
-def rewrite_asserts(mod):
+def rewrite_asserts(mod, module_path=None, config=None):
"""Rewrite the assert statements in mod."""
- AssertionRewriter().run(mod)
+ AssertionRewriter(module_path, config).run(mod)
def _saferepr(obj):
@@ -543,6 +544,11 @@ class AssertionRewriter(ast.NodeVisitor):
"""
+ def __init__(self, module_path, config):
+ super(AssertionRewriter, self).__init__()
+ self.module_path = module_path
+ self.config = config
+
def run(self, mod):
"""Find all assert statements in *mod* and rewrite them."""
if not mod.body:
@@ -683,6 +689,10 @@ class AssertionRewriter(ast.NodeVisitor):
the expression is false.
"""
+ if isinstance(assert_.test, ast.Tuple) and self.config is not None:
+ fslocation = (self.module_path, assert_.lineno)
+ self.config.warn('R1', 'assertion is always true, perhaps '
+ 'remove parentheses?', fslocation=fslocation)
self.statements = []
self.variables = []
self.variable_counter = itertools.count()