@@ -125,7 +125,7 @@ def find_module(self, name, path=None):
125
125
co = _read_pyc (fn_pypath , pyc , state .trace )
126
126
if co is None :
127
127
state .trace ("rewriting %r" % (fn ,))
128
- source_stat , co = _rewrite_test (state , fn_pypath )
128
+ source_stat , co = _rewrite_test (self . config , fn_pypath )
129
129
if co is None :
130
130
# Probably a SyntaxError in the test.
131
131
return None
@@ -252,8 +252,9 @@ def _write_pyc(state, co, source_stat, pyc):
252
252
cookie_re = re .compile (r"^[ \t\f]*#.*coding[:=][ \t]*[-\w.]+" )
253
253
BOM_UTF8 = '\xef \xbb \xbf '
254
254
255
- def _rewrite_test (state , fn ):
255
+ def _rewrite_test (config , fn ):
256
256
"""Try to read and rewrite *fn* and return the code object."""
257
+ state = config ._assertstate
257
258
try :
258
259
stat = fn .stat ()
259
260
source = fn .read ("rb" )
@@ -298,7 +299,7 @@ def _rewrite_test(state, fn):
298
299
# Let this pop up again in the real import.
299
300
state .trace ("failed to parse: %r" % (fn ,))
300
301
return None , None
301
- rewrite_asserts (tree )
302
+ rewrite_asserts (tree , fn , config )
302
303
try :
303
304
co = compile (tree , fn .strpath , "exec" )
304
305
except SyntaxError :
@@ -354,9 +355,9 @@ def _read_pyc(source, pyc, trace=lambda x: None):
354
355
return co
355
356
356
357
357
- def rewrite_asserts (mod ):
358
+ def rewrite_asserts (mod , module_path = None , config = None ):
358
359
"""Rewrite the assert statements in mod."""
359
- AssertionRewriter ().run (mod )
360
+ AssertionRewriter (module_path , config ).run (mod )
360
361
361
362
362
363
def _saferepr (obj ):
@@ -543,6 +544,11 @@ class AssertionRewriter(ast.NodeVisitor):
543
544
544
545
"""
545
546
547
+ def __init__ (self , module_path , config ):
548
+ super (AssertionRewriter , self ).__init__ ()
549
+ self .module_path = module_path
550
+ self .config = config
551
+
546
552
def run (self , mod ):
547
553
"""Find all assert statements in *mod* and rewrite them."""
548
554
if not mod .body :
@@ -683,6 +689,10 @@ def visit_Assert(self, assert_):
683
689
the expression is false.
684
690
685
691
"""
692
+ if isinstance (assert_ .test , ast .Tuple ) and self .config is not None :
693
+ fslocation = (self .module_path , assert_ .lineno )
694
+ self .config .warn ('R1' , 'assertion is always true, perhaps '
695
+ 'remove parentheses?' , fslocation = fslocation )
686
696
self .statements = []
687
697
self .variables = []
688
698
self .variable_counter = itertools .count ()
0 commit comments