Skip to content

Commit 7b761f9

Browse files
committed
feat: remove patch_callable and add comprehensive tests for FunctionPatcher and ModulePatcher
1 parent 80e424a commit 7b761f9

File tree

6 files changed

+193
-242
lines changed

6 files changed

+193
-242
lines changed

src/awepatch/__init__.py

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,15 @@
11
from __future__ import annotations
22

3-
from typing import Any, TypeAlias
4-
53
from awepatch._function import FunctionPatcher
64
from awepatch._module import ModulePatcher
7-
from awepatch._utils import AbstractPatcher, Ident, Patch
5+
from awepatch._utils import AbstractPatcher, Ident
86
from awepatch._version import __commit_id__, __version__, __version_tuple__
97

10-
TYPE_CHECKING = False
11-
12-
if TYPE_CHECKING:
13-
from collections.abc import Callable
14-
15-
16-
def _check_patches(patch: Any) -> list[Patch]: # noqa: ANN401
17-
"""Check and normalize the patches input.
18-
19-
Args:
20-
patch: A single Patch or a list of Patch objects.
21-
22-
Returns:
23-
A list of Patch objects.
24-
25-
"""
26-
if isinstance(patch, Patch):
27-
return [patch]
28-
elif isinstance(patch, list) and all(isinstance(p, Patch) for p in patch): # pyright: ignore[reportUnknownVariableType]
29-
return patch # pyright: ignore[reportUnknownVariableType]
30-
else:
31-
raise TypeError("patch must be a Patch or a list of Patch objects")
32-
33-
34-
def patch_callable(
35-
func: Callable[..., Any],
36-
/,
37-
patch: Patch | list[Patch],
38-
) -> AbstractPatcher:
39-
"""Patch a callable using AST manipulation.
40-
41-
Args:
42-
func (Callable[..., Any]): The function to patch.
43-
patch (Patch | list[Patch]): Patch or list of Patch objects for applying
44-
multiple patches.
45-
46-
Examples:
47-
>>> from awepatch import Patch, patch_callable
48-
>>> def my_function(x):
49-
... return x + 1
50-
>>> with patch_callable(my_function, Patch("x + 1", "x + 2", "replace")):
51-
... assert my_function(3) == 5
52-
53-
"""
54-
55-
collector = FunctionPatcher()
56-
patches = _check_patches(patch)
57-
for p in patches:
58-
collector.add_patch(func, p.target, p.content, p.mode)
59-
return collector
60-
61-
62-
# compatibility alias
63-
CallablePatcher: TypeAlias = AbstractPatcher
64-
65-
668
__all__ = (
679
"__commit_id__",
6810
"__version__",
6911
"__version_tuple__",
70-
"CallablePatcher",
71-
"Patch",
7212
"Ident",
73-
"patch_callable",
7413
"AbstractPatcher",
7514
"ModulePatcher",
7615
"FunctionPatcher",

src/awepatch/_utils.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,6 @@ class Ident:
3636
Mode: TypeAlias = Literal["before", "after", "replace"]
3737

3838

39-
@dataclass(slots=True)
40-
class Patch:
41-
"""A single patch operation.
42-
43-
Attributes:
44-
target: The target pattern to search for in the source code.
45-
patch: The patch code or AST statements.
46-
mode: The mode of patching (before/after/replace).
47-
48-
"""
49-
50-
target: IdentType | tuple[IdentType, ...]
51-
content: str | Sequence[ast.stmt]
52-
mode: Mode = "before"
53-
54-
5539
@dataclass(slots=True)
5640
class CompiledIdent:
5741
"""Compiled identifier for locating target AST nodes.

0 commit comments

Comments
 (0)