Skip to content

Commit

Permalink
update namespace and remove python2 specific code (#55)
Browse files Browse the repository at this point in the history
* update namespace and remove python2 specific code

* remove extra space

Co-authored-by: Joseph Kahn <jkahn@waveapps.com>
  • Loading branch information
JBKahn and Joseph Kahn committed Apr 30, 2022
1 parent e405dd6 commit b33437f
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 287 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,9 +1,9 @@
language: python
python:
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "3.10"

env:
matrix:
Expand Down
17 changes: 10 additions & 7 deletions README.md
Expand Up @@ -29,25 +29,28 @@ Flake8 allows disabling some tests based on the folder:
```
[flake8]
per-file-ignores =
scripts/*: T00
cli.py: T00
scripts/*: T20
cli.py: T20
```


Error codes
-----------

| Error Code | Description |
| ----------- | ------------------------------------ |
| T001 | print found |
| T002 | Python 2.x reserved word print used |
| T003 | pprint found |
| T004 | pprint declared |
| T201 | print found |
| T203 | pprint found |
| T204 | pprint declared |


Changes
-------

##### 5.0.0 - 2022-04-30

* Move namespace from T0* to T2* to avoid collision with other library using same error code.
* Remove python 2 specific code paths, error messages and six usage.

##### 4.0.1 - 2022-04-30

* Fixing bug with noqa detection by removing manual detection and relying on flake8 itself.
Expand Down
31 changes: 11 additions & 20 deletions flake8_print.py
@@ -1,22 +1,21 @@
"""Extension for flake8 that finds usage of print."""
import pycodestyle
import ast
from six import PY2, PY3

try:
from flake8.engine import pep8 as stdin_utils
except ImportError:
from flake8 import utils as stdin_utils

__version__ = "4.0.0"
__version__ = "5.0.0"

PRINT_FUNCTION_NAME = "print"
PPRINT_FUNCTION_NAME = "pprint"
PRINT_FUNCTION_NAMES = [PRINT_FUNCTION_NAME, PPRINT_FUNCTION_NAME]

VIOLATIONS = {
"found": {"print": "T001 print found.", "pprint": "T003 pprint found."},
"declared": {"print": "T002 Python 2.x reserved word print used.", "pprint": "T004 pprint declared"},
"found": {"print": "T201 print found.", "pprint": "T203 pprint found."},
"declared": {"print": "T202 Python 2.x reserved word print used.", "pprint": "T204 pprint declared"},
}


Expand All @@ -26,10 +25,6 @@ def __init__(self, *args, **kwargs):
self.prints_used = {}
self.prints_redefined = {}

def visit_Print(self, node):
"""Only exists in python 2."""
self.prints_used[(node.lineno, node.col_offset)] = VIOLATIONS["found"][PRINT_FUNCTION_NAME]

def visit_Call(self, node):
is_print_function = getattr(node.func, "id", None) in PRINT_FUNCTION_NAMES
is_print_function_attribute = (
Expand All @@ -45,18 +40,14 @@ def visit_Call(self, node):
def visit_FunctionDef(self, node):
if node.name in PRINT_FUNCTION_NAMES:
self.prints_redefined[(node.lineno, node.col_offset)] = VIOLATIONS["declared"][node.name]
if PY2:
for arg in node.args.args:
if arg.id in PRINT_FUNCTION_NAMES:
self.prints_redefined[(node.lineno, node.col_offset)] = VIOLATIONS["declared"][arg.id]
elif PY3:
for arg in node.args.args:
if arg.arg in PRINT_FUNCTION_NAMES:
self.prints_redefined[(node.lineno, node.col_offset)] = VIOLATIONS["declared"][arg.arg]

for arg in node.args.kwonlyargs:
if arg.arg in PRINT_FUNCTION_NAMES:
self.prints_redefined[(node.lineno, node.col_offset)] = VIOLATIONS["declared"][arg.arg]

for arg in node.args.args:
if arg.arg in PRINT_FUNCTION_NAMES:
self.prints_redefined[(node.lineno, node.col_offset)] = VIOLATIONS["declared"][arg.arg]

for arg in node.args.kwonlyargs:
if arg.arg in PRINT_FUNCTION_NAMES:
self.prints_redefined[(node.lineno, node.col_offset)] = VIOLATIONS["declared"][arg.arg]
self.generic_visit(node)

def visit_Name(self, node):
Expand Down

0 comments on commit b33437f

Please sign in to comment.