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

BUG: Fix bug with Path casting #12878

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions mne/io/fiff/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,12 @@ def read_raw_fif(

def _path_from_fname(fname) -> Path | None:
if not isinstance(fname, Path):
# Try to get a filename from the file-like object
try:
fname = Path(fname.name)
except Exception:
fname = None
if isinstance(fname, str):
fname = Path(fname)
else:
# Try to get a filename from the file-like object
try:
fname = Path(fname.name)
except Exception:
fname = None
return fname
9 changes: 9 additions & 0 deletions mne/io/fiff/tests/test_raw_fiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -2180,3 +2180,12 @@ def test_expand_user(tmp_path, monkeypatch):

raw = read_raw_fif(fname=path_home, preload=True)
raw.save(fname=path_home, overwrite=True)


@pytest.mark.parametrize("cast", [pathlib.Path, str])
def test_init_kwargs(cast):
"""Test for pull/12843#issuecomment-2380491528."""
raw = read_raw_fif(cast(test_fif_fname))
raw2 = read_raw_fif(**raw._init_kwargs)
for r in (raw, raw2):
assert isinstance(r._init_kwargs["fname"], pathlib.Path)
Loading