Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow notebooks to exit early (e.g. with sys.exit(0) or pytest.skip(reason, allow_module_level=True)) #134

Open
tswast opened this issue Jan 29, 2025 · 1 comment

Comments

@tswast
Copy link

tswast commented Jan 29, 2025

Is your feature request related to a problem? Please describe.

I have some notebooks that I test with multiple versions of Python, but they are not compatible with Python 3.13. I would love to be able to exit early from these notebooks without causing the test to be reported as a failure.

Describe the solution you'd like

I have tried the following cells:

import sys

# Python 3.13 is not yet a supported runtime.
if sys.version_info >= (3, 13, 0):
    sys.exit(0)

also

import sys

# Python 3.13 is not yet a supported runtime.
if sys.version_info >= (3, 13, 0):
    import pytest

    pytest.skip("Python 3.13 not supported", allow_module_level=True)

I would expect at least the latter to work, if not both. Note: one can disambiguate if SystemExit exception is "ok" / intended from a failure by looking at SystemExit.code == 0.

Describe alternatives you've considered

  • I've also tried creating an autouse fixture in a conftext.py file that wraps the yield in a try / except block to capture these expected exceptions, but it seems conftest.py fixtures don't run. Perhaps an alternative to consider to allow such flexibility.

  • I've tried the "metadata": { "tags": [ "raises-exception" ] } on the cell, but this actually continues execution after the failed cell. I suspect this is desirable to have code cells that demonstrate exceptions, but it wasn't the right solution to be able to exit early.

Additional context

N/A

@tswast
Copy link
Author

tswast commented Jan 30, 2025

I think the solution will end up being somewhere around here:

nbmake/src/nbmake/nb_run.py

Lines 117 to 138 in 5de6e18

error = self._get_error(nb)
except CellTimeoutError as err:
trace = err.args[0]
error = NotebookError(
summary=trace.split("\n")[0],
trace=trace,
failing_cell_index=self._get_timeout_cell(nb),
)
except Exception as err:
# if at top causes https://github.com/jupyter/nbclient/issues/128
# from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel
# trace=f"{summary}\n\nInstalled Kernels: {str(KernelSpecManager().find_kernel_specs())}", # noqa
# https://github.com/treebeardtech/nbmake/runs/1536896858?check_suite_focus=true
if str(type(err)) != "<class 'jupyter_client.kernelspec.NoSuchKernel'>":
raise err
summary = f"Error - No such kernel: '{err.name}'" # type: ignore
error = NotebookError(
summary=summary,
trace=f"{summary}",
failing_cell_index=0,
)

We could explicitly catch SystemExit (if exc.code == 0: pass; else error = NotebookError(...);) / the pytest.skip.Skipped exception. Though, maybe those end up being wrapped by one of those CellExecutionErrors?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant