Skip to content

Commit d4bf434

Browse files
committed
feat: add return_exceptions parameter to fs.info
1 parent fee83b4 commit d4bf434

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

src/dvc_objects/fs/base.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ def info(
573573
path: AnyFSPath,
574574
callback: fsspec.Callback = ...,
575575
batch_size: Optional[int] = ...,
576+
return_exceptions: Literal[False] = ...,
576577
**kwargs,
577578
) -> "Entry": ...
578579

@@ -582,12 +583,44 @@ def info(
582583
path: list[AnyFSPath],
583584
callback: fsspec.Callback = ...,
584585
batch_size: Optional[int] = ...,
586+
return_exceptions: Literal[False] = ...,
585587
) -> list["Entry"]: ...
586588

587-
def info(self, path, callback=DEFAULT_CALLBACK, batch_size=None, **kwargs):
589+
@overload
590+
def info(
591+
self,
592+
path: AnyFSPath,
593+
callback: fsspec.Callback = ...,
594+
batch_size: Optional[int] = ...,
595+
return_exceptions: Literal[True] = ...,
596+
**kwargs,
597+
) -> Union["Entry", Exception]: ...
598+
599+
@overload
600+
def info(
601+
self,
602+
path: list[AnyFSPath],
603+
callback: fsspec.Callback = ...,
604+
batch_size: Optional[int] = ...,
605+
return_exceptions: Literal[True] = ...,
606+
) -> list[Union["Entry", Exception]]: ...
607+
608+
def info(
609+
self,
610+
path,
611+
callback: fsspec.Callback = DEFAULT_CALLBACK,
612+
batch_size=None,
613+
return_exceptions=False,
614+
**kwargs,
615+
):
588616
if isinstance(path, str):
589-
return self.fs.info(path, **kwargs)
590-
callback.set_size(len(path))
617+
try:
618+
return self.fs.info(path, **kwargs)
619+
except Exception as e:
620+
if return_exceptions:
621+
return e
622+
raise
623+
591624
jobs = batch_size or self.jobs
592625
if self.fs.async_impl:
593626
loop = get_loop()
@@ -596,14 +629,22 @@ def info(self, path, callback=DEFAULT_CALLBACK, batch_size=None, **kwargs):
596629
[self.fs._info(p, **kwargs) for p in path],
597630
batch_size=jobs,
598631
callback=callback,
632+
return_exceptions=return_exceptions,
599633
),
600634
loop,
601635
)
602636
return fut.result()
603637

604-
func = partial(self.fs.info, **kwargs)
638+
def info_func(p):
639+
try:
640+
return self.fs.info(p, **kwargs)
641+
except Exception as e:
642+
if return_exceptions:
643+
return e
644+
raise
645+
605646
with ThreadPoolExecutor(max_workers=jobs, cancel_on_error=True) as executor:
606-
it = executor.map(func, path)
647+
it = executor.map(info_func, path)
607648
return list(callback.wrap(it))
608649

609650
def mkdir(

0 commit comments

Comments
 (0)