diff options
author | MacBox7 <ajankit2304@gmail.com> | 2018-03-13 10:42:52 +0530 |
---|---|---|
committer | macbox7 <ajankit2304@gmail.com> | 2018-08-11 12:41:49 +0400 |
commit | 45fc732466056fe35c85936ff25491df7905c597 (patch) | |
tree | 57b6a9d15c5293ac125ce307b276dab9598e7fae | |
parent | e26f3b9fa4bbb8288258dad1c398d4e9026c894f (diff) | |
download | pyflakes-45fc732466056fe35c85936ff25491df7905c597.tar.gz |
checker.py: Check for invalid print syntax
New warning added in messages.py to check for invalid
print syntax
Closes https://github.com/PyCQA/pyflakes/issues/242
-rw-r--r-- | pyflakes/checker.py | 7 | ||||
-rw-r--r-- | pyflakes/messages.py | 4 | ||||
-rw-r--r-- | pyflakes/test/test_other.py | 50 |
3 files changed, 61 insertions, 0 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 2ce6250..c2f8d2e 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -768,6 +768,13 @@ class Checker(object): # iteration continue + if (name == 'print' and + isinstance(scope.get(name, None), Builtin)): + parent = self.getParent(node) + if (isinstance(parent, ast.BinOp) and + isinstance(parent.op, ast.RShift)): + self.report(messages.InvalidPrintSyntax, node) + try: scope[name].used = (self.scope, node) except KeyError: diff --git a/pyflakes/messages.py b/pyflakes/messages.py index bd70693..4756872 100644 --- a/pyflakes/messages.py +++ b/pyflakes/messages.py @@ -250,3 +250,7 @@ class ForwardAnnotationSyntaxError(Message): class RaiseNotImplemented(Message): message = "'raise NotImplemented' should be 'raise NotImplementedError'" + + +class InvalidPrintSyntax(Message): + message = 'use of >> is invalid with print function' diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py index fa093ce..2d6bdac 100644 --- a/pyflakes/test/test_other.py +++ b/pyflakes/test/test_other.py @@ -2034,3 +2034,53 @@ class TestAsyncStatements(TestCase): self.flakes(''' raise NotImplemented ''', m.RaiseNotImplemented) + + +class TestIncompatiblePrintOperator(TestCase): + """ + Tests for warning about invalid use of print function. + """ + + def test_valid_print(self): + self.flakes(''' + print("Hello") + ''') + + def test_invalid_print_when_imported_from_future(self): + exc = self.flakes(''' + from __future__ import print_function + import sys + print >>sys.stderr, "Hello" + ''', m.InvalidPrintSyntax).messages[0] + + self.assertEqual(exc.lineno, 4) + self.assertEqual(exc.col, 0) + + def test_print_function_assignment(self): + """ + A valid assignment, tested for catching false positives. + """ + self.flakes(''' + from __future__ import print_function + log = print + log("Hello") + ''') + + def test_print_in_lambda(self): + self.flakes(''' + from __future__ import print_function + a = lambda: print + ''') + + def test_print_returned_in_function(self): + self.flakes(''' + from __future__ import print_function + def a(): + return print + ''') + + def test_print_as_condition_test(self): + self.flakes(''' + from __future__ import print_function + if print: pass + ''') |