Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix partially unknown PathLike type #75

Merged
merged 2 commits into from
Feb 21, 2023
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
26 changes: 16 additions & 10 deletions pathspec/pathspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
of files.
"""

import sys
from collections.abc import (
Collection as CollectionType)
from itertools import (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -164,13 +170,13 @@ def match_entries(

def match_file(
self,
file: Union[str, PathLike],
file: StrPath,
separators: Optional[Collection[str]] = None,
) -> bool:
"""
Matches the file to this path-spec.

*file* (:class:`str` or :class:`os.PathLike`) is the file path to be
*file* (:class:`str` or :class:`os.PathLike[str]`) is the file path to be
matched against :attr:`self.patterns <PathSpec.patterns>`.

*separators* (:class:`~collections.abc.Collection` of :class:`str`)
Expand All @@ -184,14 +190,14 @@ def match_file(

def match_files(
self,
files: Iterable[Union[str, PathLike]],
files: Iterable[StrPath],
separators: Optional[Collection[str]] = None,
) -> Iterator[Union[str, PathLike]]:
) -> Iterator[StrPath]:
"""
Matches the files to this path-spec.

*files* (:class:`~collections.abc.Iterable` of :class:`str` or
:class:`os.PathLike`) contains the file paths to be matched against
:class:`os.PathLike[str]`) contains the file paths to be matched against
:attr:`self.patterns <PathSpec.patterns>`.

*separators* (:class:`~collections.abc.Collection` of :class:`str`;
Expand All @@ -200,7 +206,7 @@ def match_files(
information.

Returns the matched files (:class:`~collections.abc.Iterator` of
:class:`str` or :class:`os.PathLike`).
:class:`str` or :class:`os.PathLike[str]`).
"""
if not _is_iterable(files):
raise TypeError(f"files:{files!r} is not an iterable.")
Expand All @@ -213,15 +219,15 @@ def match_files(

def match_tree_entries(
self,
root: Union[str, PathLike],
root: StrPath,
on_error: Optional[Callable] = None,
follow_links: Optional[bool] = None,
) -> Iterator[TreeEntry]:
"""
Walks the specified root path for all files and matches them to this
path-spec.

*root* (:class:`str` or :class:`os.PathLike`) is the root directory
*root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory
to search.

*on_error* (:class:`~collections.abc.Callable` or :data:`None`)
Expand All @@ -240,15 +246,15 @@ def match_tree_entries(

def match_tree_files(
self,
root: Union[str, PathLike],
root: StrPath,
on_error: Optional[Callable] = None,
follow_links: Optional[bool] = None,
) -> Iterator[str]:
"""
Walks the specified root path for all files and matches them to this
path-spec.

*root* (:class:`str` or :class:`os.PathLike`) is the root directory
*root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory
to search for files.

*on_error* (:class:`~collections.abc.Callable` or :data:`None`)
Expand Down
26 changes: 16 additions & 10 deletions pathspec/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
import posixpath
import stat
import sys
import warnings
from collections.abc import (
Collection as CollectionType,
Expand All @@ -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]
Expand Down Expand Up @@ -141,14 +147,14 @@ def _is_iterable(value: Any) -> bool:


def iter_tree_entries(
root: Union[str, PathLike],
root: StrPath,
on_error: Optional[Callable] = None,
follow_links: Optional[bool] = None,
) -> Iterator['TreeEntry']:
"""
Walks the specified directory for all files and directories.

*root* (:class:`str` or :class:`os.PathLike`) is the root directory to
*root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory to
search.

*on_error* (:class:`~collections.abc.Callable` or :data:`None`)
Expand Down Expand Up @@ -257,14 +263,14 @@ def _iter_tree_entries_next(


def iter_tree_files(
root: Union[str, PathLike],
root: StrPath,
on_error: Optional[Callable] = None,
follow_links: Optional[bool] = None,
) -> Iterator[str]:
"""
Walks the specified directory for all files.

*root* (:class:`str` or :class:`os.PathLike`) is the root directory to
*root* (:class:`str` or :class:`os.PathLike[str]`) is the root directory to
search for files.

*on_error* (:class:`~collections.abc.Callable` or :data:`None`)
Expand Down Expand Up @@ -365,14 +371,14 @@ def match_files(


def normalize_file(
file: Union[str, PathLike],
file: StrPath,
separators: Optional[Collection[str]] = None,
) -> str:
"""
Normalizes the file path to use the POSIX path separator (i.e.,
:data:`'/'`), and make the paths relative (remove leading :data:`'/'`).

*file* (:class:`str` or :class:`os.PathLike`) is the file path.
*file* (:class:`str` or :class:`os.PathLike[str]`) is the file path.

*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
:data:`None`) optionally contains the path separators to normalize.
Expand Down Expand Up @@ -405,25 +411,25 @@ def normalize_file(


def normalize_files(
files: Iterable[Union[str, PathLike]],
files: Iterable[StrPath],
separators: Optional[Collection[str]] = None,
) -> Dict[str, List[Union[str, PathLike]]]:
) -> Dict[str, List[StrPath]]:
"""
DEPRECATED: This function is no longer used. Use the :func:`.normalize_file`
function with a loop for better results.

Normalizes the file paths to use the POSIX path separator.

*files* (:class:`~collections.abc.Iterable` of :class:`str` or
:class:`os.PathLike`) contains the file paths to be normalized.
:class:`os.PathLike[str]`) contains the file paths to be normalized.

*separators* (:class:`~collections.abc.Collection` of :class:`str`; or
:data:`None`) optionally contains the path separators to normalize.
See :func:`normalize_file` for more information.

Returns a :class:`dict` mapping each normalized file path (:class:`str`)
to the original file paths (:class:`list` of :class:`str` or
:class:`os.PathLike`).
:class:`os.PathLike[str]`).
"""
warnings.warn((
"util.normalize_files() is deprecated. Use util.normalize_file() "
Expand Down