Skip to content

Commit

Permalink
upath: ensure support for __fspath__ args
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-- committed Mar 3, 2024
1 parent ed4946b commit ec0645f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ def __init__(
if isinstance(args0, UPath):
self._storage_options = {**args0.storage_options, **storage_options}
else:
if hasattr(args0, "__fspath__"):
_args0 = args0.__fspath__()
else:
_args0 = str(args0)
self._storage_options = type(self)._parse_storage_options(
str(args0), protocol, storage_options
_args0, protocol, storage_options
)
else:
self._storage_options = storage_options.copy()
Expand Down
11 changes: 11 additions & 0 deletions upath/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ def test_compare_to_pathlib_path_ne():
assert pathlib.Path("/bucket/folder") == UPath("/bucket/folder")


def test_handle_fspath_args():
class X:
def __str__(self):
raise ValueError("should not be called")

def __fspath__(self):
return "/some/path"

assert UPath(X()).path == "/some/path"


@pytest.mark.parametrize(
"urlpath",
[
Expand Down

0 comments on commit ec0645f

Please sign in to comment.