Skip to content

Commit

Permalink
OPT: rename BaseBoundService internal attributes to avoid name clas…
Browse files Browse the repository at this point in the history
…hing 🧑‍💻
  • Loading branch information
eigenein committed Nov 17, 2023
1 parent 94f86c2 commit 98529ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion combadge/core/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def bind_class(
class BoundService(BaseBoundService, from_protocol): # type: ignore[misc, valid-type]
"""Bound service class that implements the protocol."""

_protocol = from_protocol
__combadge_protocol__ = from_protocol

for name, method in _enumerate_methods(from_protocol):
signature = Signature.from_method(method)
Expand Down
20 changes: 10 additions & 10 deletions combadge/core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
class BaseBoundService(Generic[BackendT]):
"""Base for dynamically generated service classes."""

_protocol: ClassVar[Type]
__combadge_protocol__: ClassVar[Type]

backend: BackendT
__slots__ = ("backend",)
__combadge_backend__: BackendT
__slots__ = ("__combadge_backend__",)

def __init__(self, backend: BackendT) -> None: # noqa: D107
self.backend = backend
self.__combadge_backend__ = backend

def __enter__(self) -> Self:
self.backend.__enter__() # type: ignore[attr-defined]
self.__combadge_backend__.__enter__() # type: ignore[attr-defined]
return self

def __exit__(
Expand All @@ -28,12 +28,12 @@ def __exit__(
traceback: Optional[TracebackType],
) -> Any:
# Remove the freed instance from the cache:
del self.backend[self._protocol] # type: ignore[attr-defined]
del self.__combadge_backend__[self.__combadge_protocol__] # type: ignore[attr-defined]

return self.backend.__exit__(exc_type, exc_value, traceback) # type: ignore[attr-defined]
return self.__combadge_backend__.__exit__(exc_type, exc_value, traceback) # type: ignore[attr-defined]

async def __aenter__(self) -> Self:
await self.backend.__aenter__() # type: ignore[attr-defined]
await self.__combadge_backend__.__aenter__() # type: ignore[attr-defined]
return self

async def __aexit__(
Expand All @@ -43,6 +43,6 @@ async def __aexit__(
traceback: Optional[TracebackType],
) -> Any:
# Remove the freed instance from the cache:
del self.backend[self._protocol] # type: ignore[attr-defined]
del self.__combadge_backend__[self.__combadge_protocol__] # type: ignore[attr-defined]

return await self.backend.__aexit__(exc_type, exc_value, traceback) # type: ignore[attr-defined]
return await self.__combadge_backend__.__aexit__(exc_type, exc_value, traceback) # type: ignore[attr-defined]
2 changes: 1 addition & 1 deletion tests/core/test_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ServiceProtocol(Protocol):

service = bind(ServiceProtocol, Mock()) # type: ignore[type-abstract]
assert isinstance(service, BaseBoundService)
assert service._protocol is ServiceProtocol
assert service.__combadge_protocol__ is ServiceProtocol


def test_service_type() -> None:
Expand Down

0 comments on commit 98529ac

Please sign in to comment.