Skip to content

Load bundled themes with importlib.resources so zipapp installs work - #15478

Draft
xh123637 wants to merge 3 commits into
ipython:mainfrom
xh123637:fix/15477-zipapp-themes
Draft

xh123637 wants to merge 3 commits into
ipython:mainfrom
xh123637:fix/15477-zipapp-themes

Conversation

@xh123637

Copy link
Copy Markdown

Fixes #15477 🤖🤖

Bundled themes were read with open() on a path under the package directory, which fails with NotADirectoryError when IPython is imported from a zip file (for example a zipapp), since the theme JSON files have no real directory there.

ThemeTable now reads shipped themes through importlib.resources, the same approach already used for entry-point themes. Themes from the IPython directory and Theme.from_file are unchanged.

Adds a regression test that zips the package, imports IPython from the zip, and loads and formats with the linux theme.

AI usage

Made with Muse (Muse Spark). I do not know the effort (reasoning) level it ran at. Human review: No — no human has reviewed this change.

@gpotter2

Copy link
Copy Markdown
Contributor

@xh123637 Tests don't pass.

Comment thread IPython/utils/PyColorize.py Outdated
Comment on lines +507 to +519
@staticmethod
def _from_builtin(filename: str) -> Theme:
"""Read a theme IPython ships, from the package rather than the disk.

`importlib.resources` reads the JSON whether the package is a plain
directory or inside a zip file, so this also works for zipapp installs.
"""
from importlib.resources import files

data = json.loads(
(files("IPython.utils") / "themes" / filename).read_text(encoding="utf-8")
)
return Theme.from_dict(data)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't need a function if you only use it once.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in da2ad5b — inlined _from_builtin into __getitem__, keeping the importlib.resources read and the zipapp explanation as a comment. Re-ran tests/test_themes.py and tests/test_pycolorize.py locally: 96 passed; ruff format --check and ruff check are clean on the file.

@xh123637

Copy link
Copy Markdown
Author

@gpotter2 On the failing check: I re-checked the CI on the previous head (2daed4a). The only failing check was the build job of the Build docs workflow, which failed at the "Install Graphviz" setup step — it ran for ~10 minutes and failed before "Install Python dependencies" and "Build docs" ran, so the docs were never built and no project code ran in that job. The other 7 recent runs of that workflow all passed, so this looks like a one-off environment flake rather than a regression from this PR.

All 16 test-matrix jobs (ubuntu/macos/windows × Python 3.11–3.15, test and test_extra), oldest-deps, formatting, zizmor, and the other build jobs were green on that head. The one cancelled run (test (macos-14, 3.13)) was cancelled 1 second after starting, not a test failure.

The latest push (da2ad5b, inlining _from_builtin per your other comment) re-triggers CI, so the docs job will get a fresh run.

This branch has not been deployed

No deployments
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.

Use resources for IPython themes for ZipApp

2 participants