diff --git a/pathspec/gitignore.py b/pathspec/gitignore.py index 908a9bd..3e0e0dc 100644 --- a/pathspec/gitignore.py +++ b/pathspec/gitignore.py @@ -9,6 +9,7 @@ Collection, Iterable, TYPE_CHECKING, + TypeVar, Union) from .pathspec import ( @@ -22,6 +23,8 @@ from .util import ( _is_iterable) +Self = TypeVar("Self") + class GitIgnoreSpec(PathSpec): """ @@ -29,7 +32,7 @@ class GitIgnoreSpec(PathSpec): replicate *.gitignore* behavior. """ - def __eq__(self, other: 'Self') -> bool: + def __eq__(self, other: object) -> bool: """ Tests the equality of this gitignore-spec with *other* (:class:`GitIgnoreSpec`) by comparing their :attr:`~PathSpec.patterns` @@ -44,10 +47,10 @@ def __eq__(self, other: 'Self') -> bool: @classmethod def from_lines( - cls, + cls: type[Self], lines: Iterable[AnyStr], pattern_factory: Union[str, Callable[[AnyStr], Pattern], None] = None, - ) -> 'Self': + ) -> Self: """ Compiles the pattern lines. @@ -126,13 +129,3 @@ def _match_file( out_priority = priority return out_matched - - -if TYPE_CHECKING: - try: - from typing import Self - except ImportError: - try: - from typing_extensions import Self - except ImportError: - Self = GitIgnoreSpec diff --git a/pathspec/pathspec.py b/pathspec/pathspec.py index d740aa8..f16f45b 100644 --- a/pathspec/pathspec.py +++ b/pathspec/pathspec.py @@ -17,6 +17,7 @@ Iterator, Optional, TYPE_CHECKING, + TypeVar, Union) from . import util @@ -29,6 +30,8 @@ match_file, normalize_file) +Self = TypeVar("Self") + class PathSpec(object): """ @@ -50,7 +53,7 @@ def __init__(self, patterns: Iterable[Pattern]) -> None: contains the compiled patterns. """ - def __eq__(self, other: 'Self') -> bool: + def __eq__(self, other: object) -> bool: """ Tests the equality of this path-spec with *other* (:class:`PathSpec`) by comparing their :attr:`~PathSpec.patterns` attributes. @@ -68,7 +71,7 @@ def __len__(self) -> int: """ return len(self.patterns) - def __add__(self, other: 'Self') -> 'Self': + def __add__(self: Self, other: "PathSpec") -> Self: """ Combines the :attr:`Pathspec.patterns` patterns from two :class:`PathSpec` instances. @@ -78,7 +81,7 @@ def __add__(self, other: 'Self') -> 'Self': else: return NotImplemented - def __iadd__(self, other: 'Self') -> 'Self': + def __iadd__(self: Self, other: "PathSpec") -> Self: """ Adds the :attr:`Pathspec.patterns` patterns from one :class:`PathSpec` instance to this instance. @@ -91,10 +94,10 @@ def __iadd__(self, other: 'Self') -> 'Self': @classmethod def from_lines( - cls, + cls: type[Self], pattern_factory: Union[str, Callable[[AnyStr], Pattern]], lines: Iterable[AnyStr], - ) -> 'Self': + ) -> Self: """ Compiles the pattern lines. @@ -261,13 +264,3 @@ def match_tree_files( # Alias `match_tree_files()` as `match_tree()` for backward # compatibility before v0.3.2. match_tree = match_tree_files - - -if TYPE_CHECKING: - try: - from typing import Self - except ImportError: - try: - from typing_extensions import Self - except ImportError: - Self = PathSpec