Skip to content

Commit 016c96d

Browse files
Fix join_path type inconsistency: return Path when Path inputs are provided (NVIDIA#33)
## Summary `LocalBackend.join_path` accepted `Union[str, Path]` inputs but always returned `str` (via `os.path.join`), even when `Path` objects were passed. This violated the type contract and could cause `AttributeError` downstream. ## Changes - **local_backend.py**: Now checks if any input is a `Path` and returns `Path(result)` accordingly. Removed the stale TODO that acknowledged this issue. - **base_backend.py, easy_io.py, file_client.py**: Updated return type from `str` to `Union[str, Path]`. - **boto3_backend.py, msc_backend.py, http_backend.py**: Updated return type signature for consistency with the abstract base class. ## Related Issue Closes NVIDIA#32 Co-authored-by: Maosheng Liao <maoshengl@nvidia.com>
1 parent bbda321 commit 016c96d

7 files changed

Lines changed: 15 additions & 13 deletions

File tree

cosmos_framework/utils/easy_io/backends/base_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def isfile(self, filepath: Union[str, Path]) -> bool:
7070
pass
7171

7272
@abstractmethod
73-
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> str:
73+
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> Union[str, Path]:
7474
pass
7575

7676
@abstractmethod

cosmos_framework/utils/easy_io/backends/boto3_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def join_path(
284284
self,
285285
filepath: Union[str, Path],
286286
*filepaths: Union[str, Path],
287-
) -> str:
287+
) -> Union[str, Path]:
288288
r"""Concatenate all file paths.
289289
290290
Join one or more filepath components intelligently. The return value
@@ -294,7 +294,7 @@ def join_path(
294294
filepath (str or Path): Path to be concatenated.
295295
296296
Returns:
297-
str: The result after concatenation.
297+
str or Path: The result after concatenation.
298298
299299
Examples:
300300
>>> backend = Boto3Backend()

cosmos_framework/utils/easy_io/backends/http_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def isdir(self, filepath: Union[str, Path]) -> bool:
112112
def isfile(self, filepath: Union[str, Path]) -> bool:
113113
raise NotImplementedError(f"isfile not supported in {self.name}")
114114

115-
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> str:
115+
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> Union[str, Path]:
116116
raise NotImplementedError(f"join_path not supported in {self.name}")
117117

118118
@contextmanager

cosmos_framework/utils/easy_io/backends/local_backend.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def isfile(self, filepath: Union[str, Path]) -> bool:
187187
"""
188188
return osp.isfile(filepath)
189189

190-
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> str:
190+
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> Union[str, Path]:
191191
r"""Concatenate all file paths.
192192
193193
Join one or more filepath components intelligently. The return value
@@ -197,7 +197,7 @@ def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) ->
197197
filepath (str or Path): Path to be concatenated.
198198
199199
Returns:
200-
str: The result of concatenation.
200+
str or Path: The result of concatenation. Returns a Path if any input is a Path.
201201
202202
Examples:
203203
>>> backend = LocalBackend()
@@ -207,8 +207,10 @@ def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) ->
207207
>>> backend.join_path(filepath1, filepath2, filepath3)
208208
'/path/of/dir/dir2/path/of/file'
209209
"""
210-
# TODO, if filepath or filepaths are Path, should return Path
211-
return osp.join(filepath, *filepaths)
210+
result = osp.join(filepath, *filepaths)
211+
if isinstance(filepath, Path) or any(isinstance(p, Path) for p in filepaths):
212+
return Path(result)
213+
return result
212214

213215
@contextmanager
214216
def get_local_path(

cosmos_framework/utils/easy_io/backends/msc_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def join_path(
554554
self,
555555
filepath: Union[str, Path],
556556
*filepaths: Union[str, Path],
557-
) -> str:
557+
) -> Union[str, Path]:
558558
r"""Concatenate all file paths.
559559
560560
Join one or more filepath components intelligently. The return value
@@ -564,7 +564,7 @@ def join_path(
564564
filepath (str or Path): Path to be concatenated.
565565
566566
Returns:
567-
str: The result after concatenation.
567+
str or Path: The result after concatenation.
568568
569569
Examples:
570570
>>> backend = MSCBackend()

cosmos_framework/utils/easy_io/easy_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def join_path(
424424
backend_key (str, optional): The key to get the backend from register.
425425
426426
Returns:
427-
str: The result of concatenation.
427+
str or Path: The result of concatenation. Returns a Path if any input is a Path.
428428
429429
Examples:
430430
>>> filepath1 = '/path/of/dir1'

cosmos_framework/utils/easy_io/file_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def isfile(self, filepath: Union[str, Path]) -> bool:
375375
"""
376376
return self.client.isfile(filepath)
377377

378-
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> str:
378+
def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) -> Union[str, Path]:
379379
r"""Concatenate all file paths.
380380
381381
Join one or more filepath components intelligently. The return value
@@ -385,7 +385,7 @@ def join_path(self, filepath: Union[str, Path], *filepaths: Union[str, Path]) ->
385385
filepath (str or Path): Path to be concatenated.
386386
387387
Returns:
388-
str: The result of concatenation.
388+
str or Path: The result of concatenation. Returns a Path if any input is a Path.
389389
"""
390390
return self.client.join_path(filepath, *filepaths)
391391

0 commit comments

Comments
 (0)