|
13 | 13 | zip_longest) |
14 | 14 | from typing import ( |
15 | 15 | Callable, # Replaced by `collections.abc.Callable` in 3.9.2. |
| 16 | + Generic, |
| 17 | + Literal, |
16 | 18 | Optional, # Replaced by `X | None` in 3.10. |
17 | 19 | TypeVar, |
18 | 20 | Union, # Replaced by `X | Y` in 3.10. |
19 | | - cast) |
| 21 | + cast, |
| 22 | + overload) |
20 | 23 |
|
21 | 24 | Self = TypeVar("Self", bound='PathSpec') |
22 | 25 | """ |
|
32 | 35 | make_pathspec_backend) |
33 | 36 | from pathspec.pattern import ( |
34 | 37 | Pattern) |
| 38 | +from pathspec.patterns.gitignore.basic import ( |
| 39 | + GitIgnoreBasicPattern) |
35 | 40 | from pathspec._typing import ( |
36 | 41 | AnyStr, # Removed in 3.18. |
37 | 42 | deprecated) # Added in 3.13. |
38 | 43 | from pathspec.util import ( |
39 | 44 | CheckResult, |
40 | 45 | StrPath, |
| 46 | + TPattern, |
41 | 47 | TStrPath, |
42 | 48 | TreeEntry, |
43 | 49 | _is_iterable, |
44 | 50 | normalize_file) |
45 | 51 |
|
46 | 52 |
|
47 | | -class PathSpec(object): |
| 53 | +class PathSpec(Generic[TPattern]): |
48 | 54 | """ |
49 | 55 | The :class:`PathSpec` class is a wrapper around a list of compiled |
50 | 56 | :class:`.Pattern` instances. |
51 | 57 | """ |
52 | 58 |
|
53 | 59 | def __init__( |
54 | 60 | self, |
55 | | - patterns: Union[Sequence[Pattern], Iterable[Pattern]], |
| 61 | + patterns: Union[Sequence[TPattern], Iterable[TPattern]], |
56 | 62 | *, |
57 | 63 | backend: Union[BackendNamesHint, str, None] = None, |
58 | | - _test_backend_factory: Optional[Callable[[Sequence[Pattern]], _Backend]] = None, |
| 64 | + _test_backend_factory: Optional[Callable[[Sequence[TPattern]], _Backend]] = None, |
59 | 65 | ) -> None: |
60 | 66 | """ |
61 | 67 | Initializes the :class:`.PathSpec` instance. |
@@ -92,7 +98,7 @@ def __init__( |
92 | 98 | *_backend_name* (:class:`str`) is the name of backend to use. |
93 | 99 | """ |
94 | 100 |
|
95 | | - self.patterns: Sequence[Pattern] = patterns |
| 101 | + self.patterns: Sequence[TPattern] = patterns |
96 | 102 | """ |
97 | 103 | *patterns* (:class:`~collections.abc.Sequence` of :class:`.Pattern`) |
98 | 104 | contains the compiled patterns. |
@@ -225,6 +231,42 @@ def check_tree_files( |
225 | 231 | files = util.iter_tree_files(root, on_error=on_error, follow_links=follow_links) |
226 | 232 | yield from self.check_files(files) |
227 | 233 |
|
| 234 | + @overload |
| 235 | + @classmethod |
| 236 | + def from_lines( |
| 237 | + cls: type[PathSpec], |
| 238 | + pattern_factory: Literal['gitignore'], |
| 239 | + lines: Iterable[AnyStr], |
| 240 | + *, |
| 241 | + backend: Union[BackendNamesHint, str, None] = None, |
| 242 | + _test_backend_factory: Optional[Callable[[Sequence[Pattern]], _Backend]] = None, |
| 243 | + ) -> PathSpec[GitIgnoreBasicPattern]: |
| 244 | + ... |
| 245 | + |
| 246 | + @overload |
| 247 | + @classmethod |
| 248 | + def from_lines( |
| 249 | + cls: type[PathSpec], |
| 250 | + pattern_factory: Callable[[AnyStr], TPattern], |
| 251 | + lines: Iterable[AnyStr], |
| 252 | + *, |
| 253 | + backend: Union[BackendNamesHint, str, None] = None, |
| 254 | + _test_backend_factory: Optional[Callable[[Sequence[TPattern]], _Backend]] = None, |
| 255 | + ) -> PathSpec[TPattern]: |
| 256 | + ... |
| 257 | + |
| 258 | + @overload |
| 259 | + @classmethod |
| 260 | + def from_lines( |
| 261 | + cls: type[PathSpec], |
| 262 | + pattern_factory: Union[str, Callable[[AnyStr], Pattern]], |
| 263 | + lines: Iterable[AnyStr], |
| 264 | + *, |
| 265 | + backend: Union[BackendNamesHint, str, None] = None, |
| 266 | + _test_backend_factory: Optional[Callable[[Sequence[Pattern]], _Backend]] = None, |
| 267 | + ) -> PathSpec[Pattern]: |
| 268 | + ... |
| 269 | + |
228 | 270 | @classmethod |
229 | 271 | def from_lines( |
230 | 272 | cls: type[Self], |
|
0 commit comments