Skip to content

Commit

Permalink
Merge pull request #6 from JBKahn/python3
Browse files Browse the repository at this point in the history
attempt to make compatible with python3
  • Loading branch information
JBKahn committed May 19, 2015
2 parents 27dc162 + cb3b575 commit 3f94fe7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: python
python:
- "2.7"
# command to run tests
script: python setup.py test
- "2.6"
- "3.4"

install: "python setup.py install"
script: "nosetests"
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Testing with `flake8==2.2.1`, and `2.2.4`. There was a bug in flake8 that was su
Changes
-------

1.6 - 2015-05-18
````````````````
* Added proper support for python3 and testing for python 2.6, 2.7 and 3.4

1.5 - 2014-11-04
````````````````
* Added python2.6 support. Thanks @zoidbergwill
Expand Down
10 changes: 5 additions & 5 deletions flake8_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from sys import stdin

__version__ = '1.5.0'
__version__ = '1.6.0'

PRINT_ERROR_CODE = 'T001'
PRINT_ERROR_MESSAGE = 'print statement found.'
Expand Down Expand Up @@ -31,9 +31,9 @@ def run(self):


def get_noqa_lines(code):
tokens = tokenize.generate_tokens(lambda L=iter(code): next(L))
noqa = [token[2][0] for token in tokens if token[0] == tokenize.COMMENT and (token[1].endswith('noqa') or (isinstance(token[0], str) and token[0].endswith('noqa')))]
return noqa
tokens = tokenize.generate_tokens(lambda L=iter(code): next(L))
noqa = [token[2][0] for token in tokens if token[0] == tokenize.COMMENT and (token[1].endswith('noqa') or (isinstance(token[0], str) and token[0].endswith('noqa')))]
return noqa


def check_code_for_print_statements(code):
Expand All @@ -45,7 +45,7 @@ def check_code_for_print_statements(code):
def check_tree_for_print_statements(tree, noqa):
errors = []
for node in ast.walk(tree):
if isinstance(node, ast.Print) and node.lineno not in noqa:
if ((isinstance(node, ast.Call) and node.func.id == 'print') or (hasattr(ast, 'Print') and isinstance(node, ast.Print) and node.lineno not in noqa)) and node.lineno not in noqa:
errors.append({
"message": '{0} {1}'.format(PRINT_ERROR_CODE, PRINT_ERROR_MESSAGE),
"line": node.lineno,
Expand Down
34 changes: 25 additions & 9 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from flake8_print import check_code_for_print_statements, PRINT_ERROR_CODE, PRINT_ERROR_MESSAGE
from nose.tools import assert_equal
from nose.tools import assert_equal, assert_true


# Python 3 detects the col as the column with the execution brackets rather than the function name. So we will attempt both when necessary.


class Flake8PrintTestCases(object):
Expand All @@ -9,20 +12,30 @@ def generate_error_statement(self, line, col):

class TestGenericCases(Flake8PrintTestCases):
def test_skips_noqa(self):
result = check_code_for_print_statements('print 4 # noqa')
result = check_code_for_print_statements('print(4) # noqa')
assert_equal(result, list())

def test_skips_noqa_line_only(self):
result = check_code_for_print_statements('print 4 # noqa\nprint 5\n # noqa')
assert_equal(result, [{'col': 0, 'line': 2, 'message': 'T001 print statement found.'}])
result = check_code_for_print_statements('print(4) # noqa\nprint(5)\n # noqa')
try:
assert_equal(result, [{'col': 0, 'line': 2, 'message': 'T001 print statement found.'}])
except AssertionError:
assert_equal(result, [{'col': 5, 'line': 2, 'message': 'T001 print statement found.'}])

def test_catches_simple_print_python2(self):
result = check_code_for_print_statements('print 4')
assert_equal(result, [{'col': 0, 'line': 1, 'message': 'T001 print statement found.'}])
try:
result = check_code_for_print_statements('print 4')
assert_equal(result, [{'col': 0, 'line': 1, 'message': 'T001 print statement found.'}])
except SyntaxError:
from sys import version
assert_true(float(version[:3]) >= 3.0)

def test_catches_simple_print_python3(self):
result = check_code_for_print_statements('print(4)')
assert_equal(result, [{'col': 0, 'line': 1, 'message': 'T001 print statement found.'}])
try:
assert_equal(result, [{'col': 0, 'line': 1, 'message': 'T001 print statement found.'}])
except AssertionError:
assert_equal(result, [{'col': 5, 'line': 1, 'message': 'T001 print statement found.'}])


class TestComments(Flake8PrintTestCases):
Expand All @@ -31,8 +44,11 @@ def test_print_in_inline_comment_is_not_a_false_positive(self):
assert_equal(result, list())

def test_print_same_line_as_comment(self):
result = check_code_for_print_statements('print 5 # what should I do with 5 ?')
assert_equal(result, [{'col': 0, 'line': 1, 'message': 'T001 print statement found.'}])
result = check_code_for_print_statements('print(5) # what should I do with 5 ?')
try:
assert_equal(result, [{'col': 0, 'line': 1, 'message': 'T001 print statement found.'}])
except AssertionError:
assert_equal(result, [{'col': 5, 'line': 1, 'message': 'T001 print statement found.'}])


class TestSingleQuotes(Flake8PrintTestCases):
Expand Down

0 comments on commit 3f94fe7

Please sign in to comment.