Skip to content

Commit

Permalink
Add some clean-up annotations
Browse files Browse the repository at this point in the history
Added annotations to functions that did not strictly need them but
adding them improves editor highlighing.
  • Loading branch information
SethMMorton committed Dec 23, 2024
1 parent 26819d2 commit d7e6e20
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@

_Match = str | Callable[[str], bool] | None
_CopyFn = Callable[[str, str], object]
_IgnoreFn = Callable[[str, list[str]], Iterable[str]]
_OnErrorCallback = Callable[[Callable[..., Any], str, ExcInfo], object]
_OnExcCallback = Callable[[Callable[..., Any], str, BaseException], object]

Expand Down Expand Up @@ -174,13 +175,13 @@ class Path(str):
the Path instance.
"""

module: Any = os.path
module: ModuleType = os.path
""" The path module to use for path operations.
.. seealso:: :mod:`os.path`
"""

def __new__(cls, other='.'):
def __new__(cls, other: Any = '.') -> Self:
return super().__new__(cls, other)

def __init__(self, other: Any = '.') -> None:
Expand Down Expand Up @@ -312,7 +313,7 @@ def stem(self) -> str:
>>> Path('/home/guido/python.tar.gz').stem
'python.tar'
"""
base, ext = self.module.splitext(self.name)
base, _ = self.module.splitext(self.name)
return base

def with_stem(self, stem: str) -> Self:
Expand All @@ -326,7 +327,7 @@ def with_stem(self, stem: str) -> Self:
@property
def suffix(self) -> Self:
"""The file extension, for example ``'.py'``."""
f, suffix = self.module.splitext(self)
_, suffix = self.module.splitext(self)
return suffix

def with_suffix(self, suffix: str) -> Self:
Expand All @@ -345,7 +346,6 @@ def with_suffix(self, suffix: str) -> Self:
"""
if not suffix.startswith('.'):
raise ValueError(f"Invalid suffix {suffix!r}")

return self.stripext() + suffix

@property
Expand All @@ -354,7 +354,7 @@ def drive(self) -> Self:
This is always empty on systems that don't use drive specifiers.
"""
drive, r = self.module.splitdrive(self)
drive, _ = self.module.splitdrive(self)
return self._next_class(drive)

@property
Expand Down Expand Up @@ -1310,10 +1310,10 @@ def chown(self, uid: str | int = -1, gid: str | int = -1) -> Self:
.. seealso:: :func:`os.chown`
"""

def resolve_uid(uid):
def resolve_uid(uid: str | int) -> int:
return uid if isinstance(uid, int) else pwd.getpwnam(uid).pw_uid

def resolve_gid(gid):
def resolve_gid(gid: str | int) -> int:
return gid if isinstance(gid, int) else grp.getgrnam(gid).gr_gid

os.chown(self, resolve_uid(uid), resolve_gid(gid))
Expand Down Expand Up @@ -1525,7 +1525,7 @@ def copytree(
self,
dst: str,
symlinks: bool = False,
ignore: None | Callable[[str, list[str]], Iterable[str]] = None,
ignore: _IgnoreFn | None = None,
copy_function: _CopyFn = shutil.copy2,
ignore_dangling_symlinks: bool = False,
dirs_exist_ok: bool = False,
Expand Down Expand Up @@ -1640,8 +1640,7 @@ def merge_tree(
symlinks: bool = False,
*,
copy_function: _CopyFn = shutil.copy2,
ignore: Callable[[Any, list[str]], list[str] | set[str]] = lambda dir,
contents: [],
ignore: _IgnoreFn = lambda dir, contents: [],
):
"""
Copy entire contents of self to dst, overwriting existing
Expand All @@ -1660,9 +1659,9 @@ def merge_tree(
dst_path.makedirs_p()

sources = list(self.iterdir())
_ignored = ignore(self, [item.name for item in sources])
_ignored = set(ignore(self, [item.name for item in sources]))

def ignored(item):
def ignored(item: Self) -> bool:
return item.name in _ignored

for source in itertools.filterfalse(ignored, sources):
Expand Down Expand Up @@ -1844,18 +1843,20 @@ def translate() -> Iterator[None]:
raise


def only_newer(copy_func: Callable[[str, str], None]) -> Callable[[str, str], None]:
def only_newer(copy_func: _CopyFn) -> _CopyFn:
"""
Wrap a copy function (like shutil.copy2) to return
the dst if it's newer than the source.
"""

@functools.wraps(copy_func)
def wrapper(src, dst, *args, **kwargs):
is_newer_dst = dst.exists() and dst.getmtime() >= src.getmtime()
def wrapper(src: str, dst: str):
src_p = Path(src)
dst_p = Path(dst)
is_newer_dst = dst_p.exists() and dst_p.getmtime() >= src_p.getmtime()
if is_newer_dst:
return dst
return copy_func(src, dst, *args, **kwargs)
return copy_func(src, dst)

return wrapper

Expand Down

0 comments on commit d7e6e20

Please sign in to comment.