From c8799790d607fce11ea13e0ca427c81d3f57b744 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 18 Feb 2023 11:03:24 -0500 Subject: [PATCH] PathLike support 3.8 --- pathspec/pathspec.py | 16 +++++++++++----- pathspec/util.py | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/pathspec/pathspec.py b/pathspec/pathspec.py index 291ff15..1f3a1c1 100644 --- a/pathspec/pathspec.py +++ b/pathspec/pathspec.py @@ -3,6 +3,7 @@ of files. """ +import sys from collections.abc import ( Collection as CollectionType) from itertools import ( @@ -30,6 +31,11 @@ match_file, normalize_file) +if sys.version_info >= (3,9): + StrPath = Union[str, PathLike[str]] +else: + StrPath = Union[str, PathLike] + Self = TypeVar("Self", bound="PathSpec") """ :class:`PathSpec` self type hint to support Python v<3.11 using PEP 673 @@ -164,7 +170,7 @@ def match_entries( def match_file( self, - file: Union[str, PathLike[str]], + file: StrPath, separators: Optional[Collection[str]] = None, ) -> bool: """ @@ -184,9 +190,9 @@ def match_file( def match_files( self, - files: Iterable[Union[str, PathLike[str]]], + files: Iterable[StrPath], separators: Optional[Collection[str]] = None, - ) -> Iterator[Union[str, PathLike[str]]]: + ) -> Iterator[StrPath]: """ Matches the files to this path-spec. @@ -213,7 +219,7 @@ def match_files( def match_tree_entries( self, - root: Union[str, PathLike[str]], + root: StrPath, on_error: Optional[Callable] = None, follow_links: Optional[bool] = None, ) -> Iterator[TreeEntry]: @@ -240,7 +246,7 @@ def match_tree_entries( def match_tree_files( self, - root: Union[str, PathLike[str]], + root: StrPath, on_error: Optional[Callable] = None, follow_links: Optional[bool] = None, ) -> Iterator[str]: diff --git a/pathspec/util.py b/pathspec/util.py index b9ca887..fd209ab 100644 --- a/pathspec/util.py +++ b/pathspec/util.py @@ -7,6 +7,7 @@ import pathlib import posixpath import stat +import sys import warnings from collections.abc import ( Collection as CollectionType, @@ -30,6 +31,11 @@ from .pattern import ( Pattern) +if sys.version_info >= (3,9): + StrPath = Union[str, PathLike[str]] +else: + StrPath = Union[str, PathLike] + NORMALIZE_PATH_SEPS = [ __sep for __sep in [os.sep, os.altsep] @@ -141,7 +147,7 @@ def _is_iterable(value: Any) -> bool: def iter_tree_entries( - root: Union[str, PathLike[str]], + root: StrPath, on_error: Optional[Callable] = None, follow_links: Optional[bool] = None, ) -> Iterator['TreeEntry']: @@ -257,7 +263,7 @@ def _iter_tree_entries_next( def iter_tree_files( - root: Union[str, PathLike[str]], + root: StrPath, on_error: Optional[Callable] = None, follow_links: Optional[bool] = None, ) -> Iterator[str]: @@ -365,7 +371,7 @@ def match_files( def normalize_file( - file: Union[str, PathLike[str]], + file: StrPath, separators: Optional[Collection[str]] = None, ) -> str: """ @@ -405,9 +411,9 @@ def normalize_file( def normalize_files( - files: Iterable[Union[str, PathLike[str]]], + files: Iterable[StrPath], separators: Optional[Collection[str]] = None, -) -> Dict[str, List[Union[str, PathLike[str]]]]: +) -> Dict[str, List[StrPath]]: """ DEPRECATED: This function is no longer used. Use the :func:`.normalize_file` function with a loop for better results.