Skip to content

Commit 72edee1

Browse files
committed
OPT: use self.__combadge_backend__ instead of capturing backend from outside a bound method ♻️
1 parent b15990a commit 72edee1

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

combadge/core/binder.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class BoundService(BaseBoundService, from_protocol): # type: ignore[misc, valid
5353
signature = Signature.from_method(method)
5454
bound_method: ServiceMethod = bind_method(signature) # generate implementation by the backend
5555
update_wrapper(bound_method, method)
56-
bound_method = _wrap(bound_method, signature.method_markers) # apply user decorators
56+
bound_method = _wrap(bound_method, signature.method_markers)
5757
bound_method = override(bound_method) # no functional change, just possibly setting `__override__`
5858
setattr(BoundService, name, bound_method)
5959

@@ -63,6 +63,15 @@ class BoundService(BaseBoundService, from_protocol): # type: ignore[misc, valid
6363

6464

6565
def _wrap(method: Callable[..., Any], with_markers: Iterable[MethodMarker]) -> Callable[..., Any]:
66+
"""
67+
Apply method markers.
68+
69+
Method markers may wrap or modify the bound method.
70+
71+
Args:
72+
method: bound method in the protocol implementation
73+
with_markers: method markers to apply
74+
"""
6675
for marker in with_markers:
6776
method = marker.wrap(method)
6877
return method

combadge/support/httpx/backends/async_.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,20 @@ def __init__(
3838
ServiceContainer.__init__(self)
3939

4040
def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # noqa: D102
41-
backend = self
4241
response_type: TypeAdapter[Any] = TypeAdapter(signature.return_type)
4342

4443
async def bound_method(self: BaseBoundService[HttpxBackend], *args: Any, **kwargs: Any) -> Any:
4544
request = signature.build_request(Request, self, args, kwargs)
4645
with BackendError:
47-
response: Response = await backend._client.request(
46+
response: Response = await self.__combadge_backend__._client.request(
4847
request.get_method(),
4948
request.get_url_path(),
5049
json=request.payload,
5150
data=request.form_data,
5251
params=request.query_params,
5352
headers=request.http_headers,
5453
)
55-
payload = backend._parse_payload(response)
54+
payload = self.__combadge_backend__._parse_payload(response)
5655
return signature.apply_response_markers(response, payload, response_type)
5756

5857
return bound_method # type: ignore[return-value]

combadge/support/httpx/backends/sync.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,20 @@ def __init__(
3838
ServiceContainer.__init__(self)
3939

4040
def bind_method(self, signature: Signature) -> ServiceMethod[HttpxBackend]: # noqa: D102
41-
backend = self
4241
response_type: TypeAdapter[Any] = TypeAdapter(signature.return_type)
4342

4443
def bound_method(self: BaseBoundService[HttpxBackend], *args: Any, **kwargs: Any) -> Any:
4544
request = signature.build_request(Request, self, args, kwargs)
4645
with BackendError:
47-
response: Response = backend._client.request(
46+
response: Response = self.__combadge_backend__._client.request(
4847
request.get_method(),
4948
request.get_url_path(),
5049
json=request.payload,
5150
data=request.form_data,
5251
params=request.query_params,
5352
headers=request.http_headers,
5453
)
55-
payload = backend._parse_payload(response)
54+
payload = self.__combadge_backend__._parse_payload(response)
5655
return signature.apply_response_markers(response, payload, response_type)
5756

5857
return bound_method # type: ignore[return-value]

combadge/support/zeep/backends/async_.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,14 @@ def __init__(
9999

100100
def bind_method(self, signature: Signature) -> ServiceMethod[ZeepBackend]: # noqa: D102
101101
response_type, fault_type = self._adapt_response_type(signature.return_type)
102-
backend = self
103102

104103
async def bound_method(self: BaseBoundService[ZeepBackend], *args: Any, **kwargs: Any) -> Any:
105104
request = signature.build_request(Request, self, args, kwargs)
106-
operation = backend._get_operation(request.get_operation_name())
105+
operation = self.__combadge_backend__._get_operation(request.get_operation_name())
107106
try:
108107
response = await operation(**(request.payload or {}), _soapheaders=request.soap_header)
109108
except Fault as e:
110-
return backend._parse_soap_fault(e, fault_type)
109+
return self.__combadge_backend__._parse_soap_fault(e, fault_type)
111110
except Exception as e:
112111
raise BackendError(e) from e
113112
else:

combadge/support/zeep/backends/sync.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ def __init__(
8181

8282
def bind_method(self, signature: Signature) -> ServiceMethod[ZeepBackend]: # noqa: D102
8383
response_type, fault_type = self._adapt_response_type(signature.return_type)
84-
backend = self
8584

8685
def bound_method(self: BaseBoundService[ZeepBackend], *args: Any, **kwargs: Any) -> Any:
8786
request = signature.build_request(Request, self, args, kwargs)
88-
operation = backend._get_operation(request.get_operation_name())
87+
operation = self.__combadge_backend__._get_operation(request.get_operation_name())
8988
try:
9089
response = operation(**(request.payload or {}), _soapheaders=request.soap_header)
9190
except Fault as e:
92-
return backend._parse_soap_fault(e, fault_type)
91+
return self.__combadge_backend__._parse_soap_fault(e, fault_type)
9392
except Exception as e:
9493
raise BackendError(e) from e
9594
else:

0 commit comments

Comments
 (0)