diff --git a/path/__init__.py b/path/__init__.py index 30c1494..942cd7b 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -25,23 +25,22 @@ from __future__ import annotations import builtins -import sys -import warnings -import os +import contextlib +import datetime +import errno import fnmatch +import functools import glob -import shutil import hashlib -import errno -import tempfile -import functools -import re -import contextlib import importlib import itertools -import datetime +import os +import re +import shutil +import sys +import tempfile +import warnings from numbers import Number -from typing import Union with contextlib.suppress(ImportError): import win32security @@ -60,33 +59,28 @@ TextIOWrapper, ) from typing import ( + IO, + TYPE_CHECKING, Any, BinaryIO, Callable, - IO, Iterator, - Optional, overload, ) -from typing import TYPE_CHECKING - if TYPE_CHECKING: from _typeshed import ( OpenBinaryMode, - OpenBinaryModeUpdating, OpenBinaryModeReading, + OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode, ) from typing_extensions import Literal -from . import matchers -from . import masks -from . import classes +from . import classes, masks, matchers from .compat.py38 import removesuffix - __all__ = ['Path', 'TempDir'] @@ -100,7 +94,7 @@ _default_linesep = object() -def _make_timestamp_ns(value: Union[Number, datetime.datetime]) -> Number: +def _make_timestamp_ns(value: Number | datetime.datetime) -> Number: timestamp_s = value if isinstance(value, Number) else value.timestamp() return int(timestamp_s * 10**9) @@ -203,7 +197,7 @@ def _next_class(cls): # --- Special Python methods. def __repr__(self): - return '{}({})'.format(type(self).__name__, super().__repr__()) + return f'{type(self).__name__}({super().__repr__()})' # Adding a Path and a string yields a Path. def __add__(self, more): @@ -689,11 +683,11 @@ def open( self, mode: OpenTextMode = ..., buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ..., + opener: Callable[[str, int], int] | None = ..., ) -> TextIOWrapper: ... @overload @@ -701,9 +695,9 @@ def open( self, mode: OpenBinaryMode, buffering: Literal[0], - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., opener: Callable[[str, int], int] = ..., ) -> FileIO: ... @@ -713,9 +707,9 @@ def open( self, mode: OpenBinaryModeUpdating, buffering: Literal[-1, 1] = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., opener: Callable[[str, int], int] = ..., ) -> BufferedRandom: ... @@ -725,9 +719,9 @@ def open( self, mode: OpenBinaryModeReading, buffering: Literal[-1, 1] = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., opener: Callable[[str, int], int] = ..., ) -> BufferedReader: ... @@ -737,9 +731,9 @@ def open( self, mode: OpenBinaryModeWriting, buffering: Literal[-1, 1] = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., opener: Callable[[str, int], int] = ..., ) -> BufferedWriter: ... @@ -749,9 +743,9 @@ def open( self, mode: OpenBinaryMode, buffering: int, - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., opener: Callable[[str, int], int] = ..., ) -> BinaryIO: ... @@ -761,9 +755,9 @@ def open( self, mode: str, buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., opener: Callable[[str, int], int] = ..., ) -> IO[Any]: ... @@ -787,11 +781,11 @@ def chunks( size: int, mode: OpenTextMode = ..., buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ..., + opener: Callable[[str, int], int] | None = ..., ) -> Iterator[str]: ... @overload @@ -800,11 +794,11 @@ def chunks( size: int, mode: OpenBinaryMode, buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ..., + opener: Callable[[str, int], int] | None = ..., ) -> Iterator[builtins.bytes]: ... @overload @@ -813,12 +807,12 @@ def chunks( size: int, mode: str, buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., closefd: bool = ..., - opener: Optional[Callable[[str, int], int]] = ..., - ) -> Iterator[Union[str, builtins.bytes]]: ... + opener: Callable[[str, int], int] | None = ..., + ) -> Iterator[str | builtins.bytes]: ... def chunks(self, size, *args, **kwargs): """Returns a generator yielding chunks of the file, so it can @@ -877,9 +871,9 @@ def text(self, encoding=None, errors='strict'): def write_text( self, text: str, - encoding: Optional[str] = ..., + encoding: str | None = ..., errors: str = ..., - linesep: Optional[str] = ..., + linesep: str | None = ..., append: bool = ..., ) -> None: ... @@ -889,7 +883,7 @@ def write_text( text: builtins.bytes, encoding: None = ..., errors: str = ..., - linesep: Optional[str] = ..., + linesep: str | None = ..., append: bool = ..., ) -> None: ... diff --git a/path/__init__.pyi b/path/__init__.pyi index 2e8a926..95ea632 100644 --- a/path/__init__.pyi +++ b/path/__init__.pyi @@ -6,19 +6,14 @@ import os import sys from types import ModuleType, TracebackType from typing import ( + IO, Any, AnyStr, Callable, Generator, Iterable, Iterator, - IO, - List, Optional, - Set, - Tuple, - Type, - Union, ) from _typeshed import ( @@ -28,7 +23,7 @@ from _typeshed import ( from . import classes # Type for the match argument for several methods -_Match = Optional[Union[str, Callable[[str], bool], Callable[[Path], bool]]] +_Match = Optional[str | Callable[[str], bool] | Callable[[Path], bool]] class TreeWalkWarning(Warning): pass @@ -39,7 +34,7 @@ class Traversal: def __init__(self, follow: Callable[[Path], bool]): ... def __call__( self, - walker: Generator[Path, Optional[Callable[[], bool]], None], + walker: Generator[Path, Callable[[], bool] | None, None], ) -> Iterator[Path]: ... class Path(str): @@ -47,10 +42,10 @@ class Path(str): def __init__(self, other: Any = ...) -> None: ... @classmethod - def using_module(cls, module: ModuleType) -> Type[Path]: ... + def using_module(cls, module: ModuleType) -> type[Path]: ... @classes.ClassProperty @classmethod - def _next_class(cls: Type[Self]) -> Type[Self]: ... + def _next_class(cls: type[Self]) -> type[Self]: ... def __repr__(self) -> str: ... def __add__(self: Self, more: str) -> Self: ... def __radd__(self: Self, other: str) -> Self: ... @@ -61,12 +56,12 @@ class Path(str): def __enter__(self: Self) -> Self: ... def __exit__( self, - exc_type: Optional[type[BaseException]], - exc_val: Optional[BaseException], - exc_tb: Optional[TracebackType], + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, ) -> None: ... @classmethod - def getcwd(cls: Type[Self]) -> Self: ... + def getcwd(cls: type[Self]) -> Self: ... def abspath(self: Self) -> Self: ... def normcase(self: Self) -> Self: ... def normpath(self: Self) -> Self: ... @@ -87,29 +82,29 @@ class Path(str): def parent(self: Self) -> Self: ... @property def name(self: Self) -> Self: ... - def splitpath(self: Self) -> Tuple[Self, str]: ... - def splitdrive(self: Self) -> Tuple[Self, Self]: ... - def splitext(self: Self) -> Tuple[Self, str]: ... + def splitpath(self: Self) -> tuple[Self, str]: ... + def splitdrive(self: Self) -> tuple[Self, Self]: ... + def splitext(self: Self) -> tuple[Self, str]: ... def stripext(self: Self) -> Self: ... @classes.multimethod def joinpath(cls: Self, first: str, *others: str) -> Self: ... - def splitall(self: Self) -> List[Union[Self, str]]: ... - def parts(self: Self) -> Tuple[Union[Self, str], ...]: ... - def _parts(self: Self) -> Iterator[Union[Self, str]]: ... - def _parts_iter(self: Self) -> Iterator[Union[Self, str]]: ... + def splitall(self: Self) -> list[Self | str]: ... + def parts(self: Self) -> tuple[Self | str, ...]: ... + def _parts(self: Self) -> Iterator[Self | str]: ... + def _parts_iter(self: Self) -> Iterator[Self | str]: ... def relpath(self: Self, start: str = ...) -> Self: ... def relpathto(self: Self, dest: str) -> Self: ... # --- Listing, searching, walking, and matching def iterdir(self: Self, match: _Match = ...) -> Iterator[Self]: ... - def listdir(self: Self, match: _Match = ...) -> List[Self]: ... - def dirs(self: Self, match: _Match = ...) -> List[Self]: ... - def files(self: Self, match: _Match = ...) -> List[Self]: ... + def listdir(self: Self, match: _Match = ...) -> list[Self]: ... + def dirs(self: Self, match: _Match = ...) -> list[Self]: ... + def files(self: Self, match: _Match = ...) -> list[Self]: ... def walk( self: Self, match: _Match = ..., errors: str = ..., - ) -> Generator[Self, Optional[Callable[[], bool]], None]: ... + ) -> Generator[Self, Callable[[], bool] | None, None]: ... def walkdirs( self: Self, match: _Match = ..., @@ -122,30 +117,30 @@ class Path(str): ) -> Iterator[Self]: ... def fnmatch( self, - pattern: Union[Path, str], - normcase: Optional[Callable[[str], str]] = ..., + pattern: Path | str, + normcase: Callable[[str], str] | None = ..., ) -> bool: ... - def glob(self: Self, pattern: str) -> List[Self]: ... + def glob(self: Self, pattern: str) -> list[Self]: ... def iglob(self: Self, pattern: str) -> Iterator[Self]: ... def bytes(self) -> builtins.bytes: ... def write_bytes(self, bytes: builtins.bytes, append: bool = ...) -> None: ... def read_text( - self, encoding: Optional[str] = ..., errors: Optional[str] = ... + self, encoding: str | None = ..., errors: str | None = ... ) -> str: ... def read_bytes(self) -> builtins.bytes: ... - def text(self, encoding: Optional[str] = ..., errors: str = ...) -> str: ... + def text(self, encoding: str | None = ..., errors: str = ...) -> str: ... def lines( self, - encoding: Optional[str] = ..., - errors: Optional[str] = ..., + encoding: str | None = ..., + errors: str | None = ..., retain: bool = ..., - ) -> List[str]: ... + ) -> list[str]: ... def write_lines( self, - lines: List[str], - encoding: Optional[str] = ..., + lines: list[str], + encoding: str | None = ..., errors: str = ..., - linesep: Optional[str] = ..., + linesep: str | None = ..., append: bool = ..., ) -> None: ... def read_md5(self) -> builtins.bytes: ... @@ -176,7 +171,7 @@ class Path(str): self, mode: int, *, - dir_fd: Optional[int] = ..., + dir_fd: int | None = ..., effective_ids: bool = ..., follow_symlinks: bool = ..., ) -> bool: ... @@ -188,22 +183,20 @@ class Path(str): if sys.platform != 'win32': def statvfs(self) -> os.statvfs_result: ... - def pathconf(self, name: Union[str, int]) -> int: ... + def pathconf(self, name: str | int) -> int: ... def utime( self, - times: Union[Tuple[int, int], Tuple[float, float], None] = ..., + times: tuple[int, int] | tuple[float, float] | None = ..., *, - ns: Tuple[int, int] = ..., - dir_fd: Optional[int] = ..., + ns: tuple[int, int] = ..., + dir_fd: int | None = ..., follow_symlinks: bool = ..., ) -> Path: ... - def chmod(self: Self, mode: Union[str, int]) -> Self: ... + def chmod(self: Self, mode: str | int) -> Self: ... if sys.platform != 'win32': - def chown( - self: Self, uid: Union[int, str] = ..., gid: Union[int, str] = ... - ) -> Self: ... + def chown(self: Self, uid: int | str = ..., gid: int | str = ...) -> Self: ... def rename(self: Self, new: str) -> Self: ... def renames(self: Self, new: str) -> Self: ... @@ -221,7 +214,7 @@ class Path(str): def unlink(self: Self) -> Self: ... def unlink_p(self: Self) -> Self: ... def link(self: Self, newpath: str) -> Self: ... - def symlink(self: Self, newlink: Optional[str] = ...) -> Self: ... + def symlink(self: Self, newlink: str | None = ...) -> Self: ... def readlink(self: Self) -> Self: ... def readlinkabs(self: Self) -> Self: ... def copyfile(self, dst: str, *, follow_symlinks: bool = ...) -> str: ... @@ -233,7 +226,7 @@ class Path(str): self, dst: str, symlinks: bool = ..., - ignore: Optional[Callable[[str, list[str]], Iterable[str]]] = ..., + ignore: Callable[[str, list[str]], Iterable[str]] | None = ..., copy_function: Callable[[str, str], None] = ..., ignore_dangling_symlinks: bool = ..., dirs_exist_ok: bool = ..., @@ -244,7 +237,7 @@ class Path(str): def rmtree( self, ignore_errors: bool = ..., - onerror: Optional[Callable[[Any, Any, Any], Any]] = ..., + onerror: Callable[[Any, Any, Any], Any] | None = ..., ) -> None: ... def rmtree_p(self: Self) -> Self: ... def chdir(self) -> None: ... @@ -255,28 +248,28 @@ class Path(str): symlinks: bool = ..., *, copy_function: Callable[[str, str], None] = ..., - ignore: Callable[[Any, List[str]], Union[List[str], Set[str]]] = ..., + ignore: Callable[[Any, list[str]], list[str] | set[str]] = ..., ) -> None: ... if sys.platform != 'win32': def chroot(self) -> None: ... if sys.platform == 'win32': - def startfile(self: Self, operation: Optional[str] = ...) -> Self: ... + def startfile(self: Self, operation: str | None = ...) -> Self: ... @contextlib.contextmanager def in_place( self, mode: str = ..., buffering: int = ..., - encoding: Optional[str] = ..., - errors: Optional[str] = ..., - newline: Optional[str] = ..., - backup_extension: Optional[str] = ..., - ) -> Iterator[Tuple[IO[Any], IO[Any]]]: ... + encoding: str | None = ..., + errors: str | None = ..., + newline: str | None = ..., + backup_extension: str | None = ..., + ) -> Iterator[tuple[IO[Any], IO[Any]]]: ... @classes.ClassProperty @classmethod - def special(cls) -> Callable[[Optional[str]], SpecialResolver]: ... + def special(cls) -> Callable[[str | None], SpecialResolver]: ... class DirectoryNotEmpty(OSError): @staticmethod @@ -298,9 +291,9 @@ class SpecialResolver: def __init__( self, path_class: type, - appname: Optional[str] = ..., - appauthor: Optional[str] = ..., - version: Optional[str] = ..., + appname: str | None = ..., + appauthor: str | None = ..., + version: str | None = ..., roaming: bool = ..., multipath: bool = ..., ): ... @@ -309,13 +302,13 @@ class SpecialResolver: class Multi: @classmethod - def for_class(cls, path_cls: type) -> Type[MultiPathType]: ... + def for_class(cls, path_cls: type) -> type[MultiPathType]: ... @classmethod def detect(cls, input: str) -> MultiPathType: ... def __iter__(self) -> Iterator[Path]: ... @classes.ClassProperty @classmethod - def _next_class(cls) -> Type[Path]: ... + def _next_class(cls) -> type[Path]: ... class MultiPathType(Multi, Path): pass @@ -323,24 +316,22 @@ class MultiPathType(Multi, Path): class TempDir(Path): @classes.ClassProperty @classmethod - def _next_class(cls) -> Type[Path]: ... + def _next_class(cls) -> type[Path]: ... def __new__( - cls: Type[Self], - suffix: Optional[AnyStr] = ..., - prefix: Optional[AnyStr] = ..., - dir: Optional[Union[AnyStr, os.PathLike[AnyStr]]] = ..., + cls: type[Self], + suffix: AnyStr | None = ..., + prefix: AnyStr | None = ..., + dir: AnyStr | os.PathLike[AnyStr] | None = ..., ) -> Self: ... def __init__(self) -> None: ... def __enter__(self) -> Path: ... # type: ignore def __exit__( self, - exc_type: Optional[type[BaseException]], - exc_val: Optional[BaseException], - exc_tb: Optional[TracebackType], + exc_type: type[BaseException] | None, + exc_val: BaseException | None, + exc_tb: TracebackType | None, ) -> None: ... class Handlers: @classmethod - def _resolve( - cls, param: Union[str, Callable[[str], None]] - ) -> Callable[[str], None]: ... + def _resolve(cls, param: str | Callable[[str], None]) -> Callable[[str], None]: ... diff --git a/path/classes.pyi b/path/classes.pyi index 2878c48..0e119d0 100644 --- a/path/classes.pyi +++ b/path/classes.pyi @@ -1,8 +1,8 @@ from typing import Any, Callable, Optional class ClassProperty(property): - def __get__(self, cls: Any, owner: Optional[type] = ...) -> Any: ... + def __get__(self, cls: Any, owner: type | None = ...) -> Any: ... class multimethod: def __init__(self, func: Callable[..., Any]): ... - def __get__(self, instance: Any, owner: Optional[type]) -> Any: ... + def __get__(self, instance: Any, owner: type | None) -> Any: ... diff --git a/path/compat/py38.py b/path/compat/py38.py index a5f0fc1..ed7bd0a 100644 --- a/path/compat/py38.py +++ b/path/compat/py38.py @@ -1,6 +1,5 @@ import sys - if sys.version_info < (3, 9): def removesuffix(self, suffix): diff --git a/path/masks.py b/path/masks.py index 13ae00a..f65a170 100644 --- a/path/masks.py +++ b/path/masks.py @@ -1,8 +1,7 @@ -import re import functools -import operator import itertools - +import operator +import re # from jaraco.functools from typing import Any, Callable diff --git a/path/matchers.py b/path/matchers.py index 1c02389..20ca92e 100644 --- a/path/matchers.py +++ b/path/matchers.py @@ -1,9 +1,7 @@ from __future__ import annotations -import ntpath import fnmatch - - +import ntpath from typing import Any, overload diff --git a/test_path.py b/test_path.py index 6b6f316..3cc6f0d 100644 --- a/test_path.py +++ b/test_path.py @@ -13,31 +13,27 @@ time on files. """ -import os -import sys -import shutil -import time -import types +import contextlib +import datetime +import importlib import ntpath -import posixpath -import textwrap +import os import platform -import importlib -import datetime -import subprocess +import posixpath import re -import contextlib +import shutil import stat +import subprocess +import sys +import textwrap +import time +import types import pytest from more_itertools import ilen import path -from path import Path -from path import TempDir -from path import matchers -from path import SpecialResolver -from path import Multi +from path import Multi, Path, SpecialResolver, TempDir, matchers def os_choose(**choices): @@ -1092,7 +1088,7 @@ def unicode_name_in_tmpdir(self, tmpdir): Path(tmpdir).joinpath('☃').mkdir() def test_walkdirs_with_unicode_name(self, tmpdir): - for res in Path(tmpdir).walkdirs(): + for _res in Path(tmpdir).walkdirs(): pass