Debian Codegraph @

pathspec/backend.py

raw
1 """
2 This module defines the necessary classes and type hints for exposing the bare
3 minimum of the internal implementations for the pattern (regular expression)
4 matching backends. The exact structure of the backends is not solidified and is
5 subject to change.
6 """
7
8 from collections.abc import (
9 Sequence)
10 from typing import (
11 Callable, # Replaced by `collections.abc.Callable` in 3.9.2.
12 Literal,
13 Optional) # Replaced by `X | None` in 3.10.
14
15 from .pattern import (
16 Pattern)
17
18 BackendNamesHint = Literal['best', 'hyperscan', 're2', 'simple']
19 """
20 The supported backend values.
21 """
22
23 _TestBackendFactoryHint = Optional[Callable[[Sequence[Pattern]], '_Backend']]
24 """
25 Type hint for the test backend factory argument.
26 """
27
28
29 class _Backend(object):
30 """
31 .. warning:: This class is not part of the public API. It is subject to
32 change.
33
34 The :class:`_Backend` class is the abstract base class defining how to match
35 files against patterns.
36 """
37
38 def match_file(self, file: str) -> tuple[Optional[bool], Optional[int]]:
39 """
40 Check the file against the patterns.
41
42 *file* (:class:`str`) is the normalized file path to check.
43
44 Returns a :class:`tuple` containing whether to include *file* (:class:`bool`
45 or :data:`None`), and the index of the last matched pattern (:class:`int` or
46 :data:`None`).
47 """
48 raise NotImplementedError((
49 f"{self.__class__.__module__}.{self.__class__.__qualname__}.match_file() "
50 f"must be implemented."
51 )) # NotImplementedError