Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c453f59

Browse files
committedFeb 21, 2025·
relaxed io _open helper type constraints
1 parent 1273260 commit c453f59

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed
 

‎mir_eval/io.py

+18-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import re
66
import warnings
77
import scipy.io.wavfile
8-
from pathlib import Path
98

109
from . import util
1110
from . import key
@@ -16,19 +15,18 @@
1615
def _open(file_or_path, **kwargs):
1716
"""Either open a file handle, or use an existing file-like object.
1817

19-
This will behave as the `open` function if `file_or_path` is a string or `pathlib.Path`.
20-
2118
If `file_or_path` has the `read` attribute, it will return `file_or_path`.
2219

23-
Otherwise, an `IOError` is raised.
20+
Otherwise, it will attempt to open the file at the specified location.
2421
"""
2522
if hasattr(file_or_path, "read"):
2623
yield file_or_path
27-
elif isinstance(file_or_path, (str, Path)):
28-
with open(file_or_path, **kwargs) as file_desc:
29-
yield file_desc
3024
else:
31-
raise IOError(f"Invalid file-or-path object: {file_or_path}")
25+
try:
26+
with open(file_or_path, **kwargs) as file_desc:
27+
yield file_desc
28+
except TypeError as exc:
29+
raise IOError(f"Invalid file-or-path object: {file_or_path}") from exc
3230

3331

3432
def load_delimited(filename, converters, delimiter=r"\s+", comment="#"):
@@ -44,7 +42,7 @@ def load_delimited(filename, converters, delimiter=r"\s+", comment="#"):
4442

4543
Parameters
4644
----------
47-
filename : str or `pathlib.Path`
45+
filename : str or `os.Pathlike`
4846
Path to the annotation file
4947

5048
converters : list of functions
@@ -130,7 +128,7 @@ def load_events(filename, delimiter=r"\s+", comment="#"):
130128

131129
Parameters
132130
----------
133-
filename : str or `pathlib.Path`
131+
filename : str or `os.Pathlike`
134132
Path to the annotation file
135133

136134
delimiter : str
@@ -170,7 +168,7 @@ def load_labeled_events(filename, delimiter=r"\s+", comment="#"):
170168

171169
Parameters
172170
----------
173-
filename : str or `pathlib.Path`
171+
filename : str or `os.Pathlike`
174172
Path to the annotation file
175173

176174
delimiter : str
@@ -213,7 +211,7 @@ def load_intervals(filename, delimiter=r"\s+", comment="#"):
213211

214212
Parameters
215213
----------
216-
filename : str or `pathlib.Path`
214+
filename : str or `os.Pathlike`
217215
Path to the annotation file
218216

219217
delimiter : str
@@ -256,7 +254,7 @@ def load_labeled_intervals(filename, delimiter=r"\s+", comment="#"):
256254

257255
Parameters
258256
----------
259-
filename : str or `pathlib.Path`
257+
filename : str or `os.Pathlike`
260258
Path to the annotation file
261259

262260
delimiter : str
@@ -299,7 +297,7 @@ def load_time_series(filename, delimiter=r"\s+", comment="#"):
299297

300298
Parameters
301299
----------
302-
filename : str or `pathlib.Path`
300+
filename : str or `os.Pathlike`
303301
Path to the annotation file
304302

305303
delimiter : str
@@ -340,7 +338,7 @@ def load_patterns(filename):
340338

341339
Parameters
342340
----------
343-
filename : str or `pathlib.Path`
341+
filename : str or `os.Pathlike`
344342
The input file path containing the patterns of a given piece using the
345343
MIREX 2013 format.
346344

@@ -418,7 +416,7 @@ def load_wav(path, mono=True):
418416

419417
Parameters
420418
----------
421-
path : str or `pathlib.Path`
419+
path : str or `os.Pathlike`
422420
Path to a .wav file
423421
mono : bool
424422
If the provided .wav has more than one channel, it will be
@@ -457,7 +455,7 @@ def load_valued_intervals(filename, delimiter=r"\s+", comment="#"):
457455

458456
Parameters
459457
----------
460-
filename : str or `pathlib.Path`
458+
filename : str or `os.Pathlike`
461459
Path to the annotation file
462460

463461
delimiter : str
@@ -504,7 +502,7 @@ def load_key(filename, delimiter=r"\s+", comment="#"):
504502

505503
Parameters
506504
----------
507-
filename : str or `pathlib.Path`
505+
filename : str or `os.Pathlike`
508506
Path to the annotation file
509507

510508
delimiter : str
@@ -550,7 +548,7 @@ def load_tempo(filename, delimiter=r"\s+", comment="#"):
550548

551549
Parameters
552550
----------
553-
filename : str or `pathlib.Path`
551+
filename : str or `os.Pathlike`
554552
Path to the annotation file
555553

556554
delimiter : str
@@ -614,7 +612,7 @@ def load_ragged_time_series(
614612

615613
Parameters
616614
----------
617-
filename : str or `pathlib.Path`
615+
filename : str or `os.Pathlike`
618616
Path to the annotation file
619617

620618
dtype : function

0 commit comments

Comments
 (0)
Please sign in to comment.