Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: accept any os.PathLike in dump() and load(), not just pathlib.Path - #1812

Merged
Nanored4498 merged 4 commits into
joblib:mainfrom
RavSinghChandan:fix/dump-accept-os-pathlike
Jul 15, 2026
Merged

fix: accept any os.PathLike in dump() and load(), not just pathlib.Path#1812
Nanored4498 merged 4 commits into
joblib:mainfrom
RavSinghChandan:fix/dump-accept-os-pathlike

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

Fixes #1784.

dump() converted the filename to a string only when isinstance(filename, pathlib.Path). Any other path-like object — such as BIDSPath from mne-bids, or any custom class implementing __fspath__ — fell through to the is_filename check, failed it, and raised:

ValueError: Second argument should be a filename or a file-like object, ...

This was inconsistent with load(), which passed non-Path path-likes straight to open(), which accepts any os.PathLike.

Fix: Replace both isinstance(filename, Path) guards with isinstance(filename, os.PathLike) and convert via os.fspath(). This is the standard Python protocol for path-like objects. Remove the now-unused from pathlib import Path import.

What changed:

  • joblib/numpy_pickle.py — two isinstance(filename, Path)isinstance(filename, os.PathLike) with os.fspath(); removed unused Path import
  • joblib/test/test_numpy_pickle.py — added test_dump_and_load_accept_os_pathlike using a minimal custom os.PathLike class (no external dependency needed)

Both test_pathlib (existing) and the new test pass.

dump() checked isinstance(filename, Path) before converting to str, so
any path-like object that is not a pathlib.Path subclass (e.g. BIDSPath
from mne-bids, or any custom os.PathLike) raised a ValueError even though
load() already accepted it via open().

Replace both isinstance checks with os.PathLike so dump and load behave
consistently for all path-like objects. Remove the now-unused
pathlib.Path import.

Adds test_dump_and_load_accept_os_pathlike to cover this path.

fixes joblib#1784

@Nanored4498 Nanored4498 left a comment

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.

Thanks for the PR. Just few things before merging.

Comment thread joblib/logger.py

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.

Could you reverse these changes that will be merged in your other PR #1811? :)

Comment thread joblib/numpy_pickle.py Outdated

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.

Suggested change
filename: str, os.PathLike, or file object.

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.10%. Comparing base (b030e4e) to head (f7aa523).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1812      +/-   ##
==========================================
- Coverage   94.15%   94.10%   -0.05%     
==========================================
  Files          46       46              
  Lines        7970     7979       +9     
==========================================
+ Hits         7504     7509       +5     
- Misses        466      470       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Done — reverted the logger.py docstrings so they only land in #1811, and applied the os.PathLike wording to both dump() and load(). PR is just the pathlike change + test now.

@Nanored4498
Nanored4498 merged commit f6c91e3 into joblib:main Jul 15, 2026
34 of 37 checks passed
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.

BIDSPath not accepted by dump() but valid for load()

2 participants