Skip to content

Commit

Permalink
denylist collections.abc
Browse files Browse the repository at this point in the history
  • Loading branch information
smacke committed Nov 5, 2023
1 parent d28b533 commit 37bf9e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
7 changes: 3 additions & 4 deletions core/superduperreload/superduperreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

from superduperreload.functional_reload import exec_module_for_new_dict
from superduperreload.patching import IMMUTABLE_PRIMITIVE_TYPES, ObjectPatcher
from superduperreload.utils import print_purple
from superduperreload.utils import print_purple, print_red

if TYPE_CHECKING:
from ipyflow import NotebookFlow
Expand Down Expand Up @@ -107,6 +107,7 @@ def __init__(
"__main__",
"__mp_main__",
"builtins",
"collections.abc",
"numpy",
"os",
"pandas",
Expand Down Expand Up @@ -243,7 +244,6 @@ def _get_modules_maybe_needing_reload(
continue
if mtime <= self.reloaded_mtime.setdefault(modname, mtime):
continue
print(modname, m.__name__, mtime, fname, self.reloaded_mtime.get(modname))
if self.failed.get(fname) == mtime:
continue
modules_needing_reload[modname] = (m, fname, mtime)
Expand Down Expand Up @@ -339,15 +339,14 @@ def check(self, do_reload: bool = True) -> None:
self.reloaded_mtime[modname] = mtime
continue
# If we've reached this point, we should try to reload the module
print("reload", modname, fname, mtime, m)
self._report(f"Reloading '{modname}'.")
try:
self.superduperreload(m)
self.handle_module_refreshed(m, mtime=mtime)
self.failed.pop(fname, None)
self.reloaded_modules.append(modname)
except: # noqa: E722
print(
print_red(
"[autoreload of {} failed: {}]".format(
modname, traceback.format_exc(10)
),
Expand Down
13 changes: 11 additions & 2 deletions core/superduperreload/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
# -*- coding: utf-8 -*-

_PURPLE = "\033[95m"
_RED = "\033[91m"
_RESET = "\033[0m"


def print_purple(text: str) -> None:
# allow exceptions for the test_no_prints test
print_ = print


def print_purple(text: str, **kwargs) -> None:
# The ANSI escape code for purple text is \033[95m
# The \033 is the escape code, and [95m specifies the color (purple)
# Reset code is \033[0m that resets the style to default
print(f"{_PURPLE}{text}{_RESET}")
print_(f"{_PURPLE}{text}{_RESET}", **kwargs)


def print_red(text: str, **kwargs) -> None:
print_(f"{_RED}{text}{_RESET}", **kwargs)


def isinstance2(a, b, typ):
Expand Down
49 changes: 49 additions & 0 deletions core/test/test_no_prints.py
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!"

0 comments on commit 37bf9e1

Please sign in to comment.