Skip to content

Commit 13b6a61

Browse files
committed
NEW: support multiple inner markers in Mixin
1 parent 3170bd4 commit 13b6a61

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

combadge/core/markers/response.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from abc import ABC, abstractmethod
44
from dataclasses import dataclass
5-
from typing import Any, Dict, Mapping, MutableMapping, TypeVar
5+
from typing import Any, Dict, Iterable, Mapping, MutableMapping, TypeVar
66

77
# noinspection PyUnresolvedReferences
88
from typing_extensions import override
@@ -64,16 +64,25 @@ def __call__(self, response: Any, payload: Mapping[Any, Any]) -> Any: # noqa: D
6464
_MutableMappingT = TypeVar("_MutableMappingT", bound=MutableMapping[Any, Any])
6565

6666

67-
@dataclass
67+
@dataclass(init=False)
6868
class Mixin(ResponseMarker):
69-
"""Mix in the inner marker output to the payload."""
69+
"""Mix in the inner marker outputs to the payload."""
7070

71-
inner: ResponseMarker
72-
"""Inner marker to apply to a payload."""
71+
inner: Iterable[ResponseMarker]
7372

7473
__slots__ = ("inner",)
7574

75+
def __init__(self, *inner: ResponseMarker) -> None:
76+
"""
77+
Initialize the marker.
78+
79+
Args:
80+
*inner: inner markers to apply to a payload
81+
"""
82+
self.inner = inner
83+
7684
@override
7785
def __call__(self, response: Any, payload: _MutableMappingT) -> _MutableMappingT: # noqa: D102
78-
payload.update(self.inner(response, payload))
86+
for marker in self.inner:
87+
payload.update(marker(response, payload))
7988
return payload

tests/core/markers/test_response.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ def test_map() -> None:
88

99

1010
def test_mixin() -> None:
11-
class InnerMarker(ResponseMarker):
11+
class InnerMarker1(ResponseMarker):
1212
def __call__(self, response: Any, payload: Any) -> Any:
13-
return {"inner": "foo"}
13+
return {"inner1": "foo"}
1414

15-
assert Mixin(InnerMarker())(..., {"outer": "bar"}) == {
16-
"inner": "foo",
17-
"outer": "bar",
15+
class InnerMarker2(ResponseMarker):
16+
def __call__(self, response: Any, payload: Any) -> Any:
17+
return {"inner2": "bar"}
18+
19+
assert Mixin(InnerMarker1(), InnerMarker2())(..., {"outer": "qux"}) == {
20+
"inner1": "foo",
21+
"inner2": "bar",
22+
"outer": "qux",
1823
}

0 commit comments

Comments
 (0)