Skip to content

refactor: one isnotebook() for the whole package - #916

Merged
petercorke merged 2 commits into
mainfrom
refactor/one-isnotebook
Nov 1, 2026
Merged

petercorke merged 2 commits into
mainfrom
refactor/one-isnotebook

Conversation

@petercorke

Copy link
Copy Markdown
Owner

PyPlot.py and mobile/Vehicle.py each had a private _isnotebook(), and they disagreed. Vehicle's looked up get_ipython with globals().get(...), but IPython puts that function in builtins, not in a module's globals, so its shell test never ran and it fell back on "ipykernel is in sys.modules". That is also true in a terminal IPython that imported ipykernel, which PyPlot correctly treats as not a notebook.

The version in PyPlot.py, which handles JupyterLite, Google Colab and a Jupyter kernel, moves to roboticstoolbox/tools/plot.py as isnotebook(). Both modules import it as _isnotebook, so the other imports of that name (PyPlot2, BaseRobot) and the tests that patch it are unchanged.

Does this change numerical results or public behaviour? No numerical results change. The mobile module's notebook detection now matches the rest of the package: a terminal IPython that has ipykernel loaded is no longer treated as a notebook there.

spatialmath's environment() and isnotebook() (rai-opensource/spatialmath-python#271, open) would replace this once released. This PR leaves RTB with one copy to replace instead of two.

Tested: the full suite passes (2435 tests), mypy 2.4.1 is clean. New tests check that PyPlot and Vehicle use the same function, that a Jupyter kernel is a notebook, and that a terminal IPython is not one even when ipykernel is loaded. They use mocks, not a real notebook frontend.

This change was written with Claude Code (Claude Sonnet 5.5).

🤖 Generated with Claude Code

PyPlot.py and mobile/Vehicle.py each had their own notebook check, and they
disagreed. Vehicle's looked up get_ipython in the module globals, where IPython
does not put it, so its shell test never ran and it fell back on "ipykernel is
loaded", true in a terminal IPython that imported it. Move the one in PyPlot.py,
which handles JupyterLite, Colab and a Jupyter kernel, to tools/plot.py and use it
from both. PyPlot.py and Vehicle.py still call it _isnotebook, so existing uses
and tests that patch it are unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 4 complexity · 0 duplication

Metric Results
Complexity 4
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 684f355 into main Nov 1, 2026
24 checks passed
@petercorke
petercorke deleted the refactor/one-isnotebook branch November 1, 2026 06:43
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

Successfully merging this pull request may close these issues.

1 participant