diff --git a/guppylang/decorator.py b/guppylang/decorator.py index 0e2b627f..2a4244ad 100644 --- a/guppylang/decorator.py +++ b/guppylang/decorator.py @@ -33,7 +33,11 @@ from guppylang.definition.struct import RawStructDef from guppylang.definition.ty import OpaqueTypeDef, TypeDef from guppylang.error import MissingModuleError, pretty_errors -from guppylang.ipython_inspect import get_ipython_globals, is_running_ipython +from guppylang.ipython_inspect import ( + get_ipython_globals, + is_ipython_dummy_file, + is_running_ipython, +) from guppylang.module import ( GuppyModule, PyClass, @@ -154,10 +158,8 @@ def _get_python_caller(self, fn: PyFunc | None = None) -> ModuleIdentifier: # Jupyter notebook cells all get different dummy filenames. However, # we want the whole notebook to correspond to a single implicit # Guppy module. - # TODO: Find a better way to detect if `filename` is a dummy name - # generated by Jupyter filename = info.filename - if is_running_ipython() and not module and "ipykernel" in filename: + if is_running_ipython() and not module and is_ipython_dummy_file(filename): filename = _JUPYTER_NOTEBOOK_MODULE module_path = Path(filename) return ModuleIdentifier( diff --git a/guppylang/ipython_inspect.py b/guppylang/ipython_inspect.py index 9726f7f0..5622c73c 100644 --- a/guppylang/ipython_inspect.py +++ b/guppylang/ipython_inspect.py @@ -12,6 +12,18 @@ def is_running_ipython() -> bool: return False +def is_ipython_dummy_file(filename: str) -> bool: + """Checks whether a given filename is a dummy name generated for an IPython cell.""" + # TODO: The approach below is false-positive prone. Figure out if there is a better + # way to do this. + return ( + # IPython cells have filenames like "" + filename.startswith(" list[str]: """Returns the source code of all cells in the running IPython session.