Skip to content

Commit

Permalink
BUG: Fix bug with Path casting (#12878)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored Sep 30, 2024
1 parent 5492174 commit 8379668
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
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)

0 comments on commit 8379668

Please sign in to comment.