From 77a8d58d8657b4e82f1693cd1c0183baf88c830e Mon Sep 17 00:00:00 2001 From: Voltrex Date: Sun, 13 Sep 2026 16:54:40 -0400 Subject: [PATCH] Support os.PathLike FIT inputs --- fitparse/utils.py | 9 ++------- tests/test_utils.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/fitparse/utils.py b/fitparse/utils.py index 65f424a..3890100 100644 --- a/fitparse/utils.py +++ b/fitparse/utils.py @@ -1,9 +1,8 @@ import io +import os import re from collections.abc import Iterable -from pathlib import PurePath - class FitParseError(ValueError): pass @@ -48,14 +47,10 @@ def fileish_open(fileish, mode): if hasattr(fileish, attr) and hasattr(fileish, 'seek'): # BytesIO-like object return fileish - elif isinstance(fileish, str): + elif isinstance(fileish, (str, os.PathLike)): # file path return open(fileish, mode) - # pathlib obj - if isinstance(fileish, PurePath): - return fileish.open(mode) - # file contents return io.BytesIO(fileish) diff --git a/tests/test_utils.py b/tests/test_utils.py index 9d456aa..916586b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,6 +6,7 @@ from pathlib import Path +from fitparse import FitFile from fitparse.utils import fileish_open, is_iterable import unittest @@ -16,6 +17,15 @@ def testfile(filename): class UtilsTestCase(unittest.TestCase): + def test_fitfile_supports_os_pathlike(self): + class FilePath: + def __fspath__(self): + return testfile('nametest.FIT') + + fit_file = FitFile(FilePath()) + fit_file.parse() + + self.assertTrue(fit_file.messages) def test_fileish_open_read(self): """Test the constructor does the right thing when given different types