Commit ea2a5163 authored by Tom Verhoeff's avatar Tom Verhoeff
Browse files

Added `nb_mypy` line magic with one argument: '', 'On', 'Off', 'DebugOn', 'DebugOff'.

Empty argument prints current state.  In user notebook, type checking can be enabled safely by:
`if 'nb_mypy' in get_ipython().magics_manager.magics.get('line'): %nb_mypy On`.
Default state is now Off, DebugOff.
parent 2b246db9
......@@ -147,7 +147,7 @@ class __MyPyIPython:
self.mypy_names = set()
mypy_shell = get_ipython()
mypy_tmp_func = mypy_shell.run_cell
self.mypy_typecheck = True
self.mypy_typecheck = False
self.debug = False
def first_none_whitspace(s: str) -> int:
......@@ -158,7 +158,7 @@ class __MyPyIPython:
i += 1
else:
break
return i
......@@ -218,7 +218,7 @@ class __MyPyIPython:
traceback.print_exception(exc_type, exc_value, exc_traceback,limit=0)
# logger.exception("SyntaxError")
return ExecutionResult(e)
getCell = NamesLister()
getCell.visit(cell_p)
newnames = getCell.names
......@@ -264,6 +264,11 @@ class __MyPyIPython:
mypy_shell.run_cell = mypy_tmp
def state(self):
on_off = {True: 'On', False: 'Off'}
debug_on_off = {True: 'DebugOn', False: 'DebugOff'}
logger.info(f"nb_mypy state: {on_off[self.mypy_typecheck]} {debug_on_off[self.debug]}")
def stop(self):
self.mypy_typecheck = False
......@@ -303,4 +308,20 @@ def turnOffTyDebug(line):
__TypeChecker.debugOff()
logger.info(f"typecheck.py version {__version__}")
\ No newline at end of file
@register_line_magic
def nb_mypy(line):
"""Inspect or modify mypy autochecking state.
"""
switcher = {
'': __TypeChecker.state,
'On': __TypeChecker.start,
'Off': __TypeChecker.stop,
'DebugOn': __TypeChecker.debugOn,
'DebugOff': __TypeChecker.debugOff,
}
# logger.info(f"line magic argument: {line!r}")
unknown = lambda: logger.error(f"nb_mypy: Unknown argument\nValid arguments: {list(switcher.keys())!r}")
switcher.get(line,unknown)()
logger.info(f"typecheck.py version {__version__}")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment