Skip to content

Commit

Permalink
upath: typing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-- committed Apr 1, 2024
1 parent 8acf527 commit 23f08e5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
15 changes: 0 additions & 15 deletions upath/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,6 @@ def parts(self):
else:
return tuple(self._tail)

def joinpath(self, *pathsegments):
return self.with_segments(self, *pathsegments)

def __truediv__(self, key):
try:
return self.joinpath(key)
except TypeError:
return NotImplemented

def __rtruediv__(self, key):
try:
return self.with_segments(key, self)
except TypeError:
return NotImplemented

@property
def parent(self):
drv = self.drive
Expand Down
33 changes: 27 additions & 6 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
from typing import overload
from urllib.parse import urlsplit

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


from fsspec.registry import get_filesystem_class
from fsspec.spec import AbstractFileSystem

Expand Down Expand Up @@ -529,13 +535,28 @@ def __reduce__(self):
}
return _make_instance, (type(self), args, kwargs)

def with_segments(self, *pathsegments):
def with_segments(self, *pathsegments: str | os.PathLike[str]) -> Self:
return type(self)(
*pathsegments,
protocol=self._protocol,
**self._storage_options,
)

def joinpath(self, *pathsegments: str | os.PathLike[str]) -> Self:
return self.with_segments(self, *pathsegments)

def __truediv__(self, key: str | os.PathLike[str]) -> Self:
try:
return self.joinpath(key)
except TypeError:
return NotImplemented

def __rtruediv__(self, key: str | os.PathLike[str]) -> Self:
try:
return self.with_segments(key, self)
except TypeError:
return NotImplemented

# === upath.UPath non-standard changes ============================

# NOTE:
Expand Down Expand Up @@ -642,13 +663,13 @@ def __bytes__(self):
warnings.warn(msg, PendingDeprecationWarning, stacklevel=2)
return os.fsencode(self)

def as_uri(self):
def as_uri(self) -> str:
return str(self)

def is_reserved(self):
def is_reserved(self) -> bool:
return False

def __eq__(self, other):
def __eq__(self, other: object) -> bool:
"""UPaths are considered equal if their protocol, path and
storage_options are equal."""
if not isinstance(other, UPath):
Expand All @@ -659,7 +680,7 @@ def __eq__(self, other):
and self.storage_options == other.storage_options
)

def __hash__(self):
def __hash__(self) -> int:
"""The returned hash is based on the protocol and path only.
Note: in the future, if hash collisions become an issue, we
Expand All @@ -681,7 +702,7 @@ def is_relative_to(self, other, /, *_deprecated):
return super().is_relative_to(other, *_deprecated)

@property
def name(self):
def name(self) -> str:
tail = self._tail
if not tail:
return ""
Expand Down

0 comments on commit 23f08e5

Please sign in to comment.