Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions upath/_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from upath.types import PathInfo

if TYPE_CHECKING:
from upath import UPath


__all__ = [
"UPathInfo",
]


class UPathInfo(PathInfo):
"""Path info for UPath objects."""

def __init__(self, path: UPath) -> None:
self._path = path.path
self._fs = path.fs

def exists(self, *, follow_symlinks=True) -> bool:
return self._fs.exists(self._path)

def is_dir(self, *, follow_symlinks=True) -> bool:
return self._fs.isdir(self._path)

def is_file(self, *, follow_symlinks=True) -> bool:
return self._fs.isfile(self._path)

def is_symlink(self) -> bool:
return False
3 changes: 2 additions & 1 deletion upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from upath._flavour import WrappedFileSystemFlavour
from upath._flavour import upath_get_kwargs_from_url
from upath._flavour import upath_urijoin
from upath._info import UPathInfo
from upath._protocol import compatible_protocol
from upath._protocol import get_upath_protocol
from upath._stat import UPathStatResult
Expand Down Expand Up @@ -574,7 +575,7 @@ def parents(self) -> Sequence[Self]:

@property
def info(self) -> PathInfo:
_raise_unsupported(type(self).__name__, "info")
return UPathInfo(self)

def iterdir(self) -> Iterator[Self]:
sep = self.parser.sep
Expand Down
13 changes: 13 additions & 0 deletions upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,16 @@ def test_samefile(self):
assert f1.samefile(f2.path) is False
assert f1.samefile(f1) is True
assert f1.samefile(f1.path) is True

def test_info(self):
p0 = self.path.joinpath("file1.txt")
p1 = self.path.joinpath("folder1")

assert p0.info.exists() is True
assert p0.info.is_file() is True
assert p0.info.is_dir() is False
assert p0.info.is_symlink() is False
assert p1.info.exists() is True
assert p1.info.is_file() is False
assert p1.info.is_dir() is True
assert p1.info.is_symlink() is False
8 changes: 8 additions & 0 deletions upath/tests/implementations/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,11 @@ def test_samefile(self):
f1 = self.path

assert f1.samefile(f1) is True

def test_info(self):
p0 = self.path

assert p0.info.exists() is True
assert p0.info.is_file() is True
assert p0.info.is_dir() is False
assert p0.info.is_symlink() is False
15 changes: 15 additions & 0 deletions upath/tests/implementations/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ def test_rename2(self):
def test_stat_dir_st_mode(self):
super().test_stat_dir_st_mode()

def test_info(self):
p0 = self.path.joinpath("file1.txt")
p1 = self.path.joinpath("folder1")

assert p0.info.exists() is True
assert p0.info.is_file() is True
assert p0.info.is_dir() is False
assert p0.info.is_symlink() is False
assert p1.info.exists() is True
assert (
p1.info.is_file() is True
) # Weird quirk of how directories work in http fsspec
assert p1.info.is_dir() is True
assert p1.info.is_symlink() is False


@pytest.mark.parametrize(
"args,parts",
Expand Down