-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
The idea of this file is to make sure that no debugging "print" statements | ||
make it into production. | ||
""" | ||
import ast | ||
import os | ||
|
||
import superduperreload | ||
|
||
join = os.path.join | ||
root = join(os.curdir, superduperreload.__name__) | ||
|
||
|
||
_EXCEPTED_FILES = { | ||
join(root, "_version.py"), | ||
join(join(root, "kernel"), "install.py"), | ||
} | ||
|
||
|
||
class ContainsPrintVisitor(ast.NodeVisitor): | ||
def __init__(self): | ||
self._found_print_call = False | ||
|
||
def __call__(self, filename: str) -> bool: | ||
with open(filename, "r") as f: | ||
self.visit(ast.parse(f.read())) | ||
ret = self._found_print_call | ||
self._found_print_call = False | ||
return ret | ||
|
||
def visit_Call(self, node: ast.Call): | ||
self.generic_visit(node) | ||
if isinstance(node.func, ast.Name) and node.func.id == "print": | ||
self._found_print_call = True | ||
|
||
|
||
def test_no_prints(): | ||
contains_print = ContainsPrintVisitor() | ||
for path, _, files in os.walk(root): | ||
for filename in files: | ||
if not filename.endswith(".py") or filename in _EXCEPTED_FILES: | ||
continue | ||
filename = os.path.join(path, filename) | ||
if filename in _EXCEPTED_FILES: | ||
continue | ||
assert not contains_print( | ||
filename | ||
), f"file {filename} had a print statement!" |