Skip to content

Accept pathlib.Path in reading and writing utilities#473

Open
omkar-334 wants to merge 2 commits into
napari:mainfrom
omkar-334:accept-pathlike-paths
Open

Accept pathlib.Path in reading and writing utilities#473
omkar-334 wants to merge 2 commits into
napari:mainfrom
omkar-334:accept-pathlike-paths

Conversation

@omkar-334

@omkar-334 omkar-334 commented Jun 9, 2026

Copy link
Copy Markdown

The reading and writing utilities accepted only str paths. This widens them to also accept pathlib.Path, in addition to str, so callers (napari included) no longer have to stringify paths before calling npe2.

The approach is to accept PathLike (str | pathlib.Path) at the public API and normalise Path to str with os.fspath before any path matching or plugin dispatch. Reader and writer plugins keep receiving str, so existing plugins are unaffected. Remote URLs are passed as str and pass through unchanged.

Changes:

  • types.PathLike is now str | pathlib.Path (was str).
  • io_utils: read, read_get_reader, write, write_get_writer (and the internal _read / _write / _get_compatible_readers_by_choice) accept and normalise Path.
  • PluginManager: iter_compatible_readers and get_writer accept and normalise Path.

Resolves the npe2 side of napari/napari#8586.

Testing strategy

  • Added Path based tests in tests/test__io_utils.py covering read, read_get_reader (single and stacked), write, and write_get_writer.
  • Added a test asserting that reader plugins still receive str when a Path is passed, to lock in backwards compatibility.
  • Added a Path test for PluginManager.iter_compatible_readers in tests/test_plugin_manager.py.
  • Existing str based tests are unchanged and still pass, confirming no behaviour change for current callers.

Run:

uv run --extra testing pytest -q
Screenshot 2026-06-09 at 3 07 36 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant