Skip to content

Commit

Permalink
Merge pull request #6 from ffekirnew/fix/thread-safe
Browse files Browse the repository at this point in the history
fix(type): Fix type issues to allow for union types
  • Loading branch information
ffekirnew authored Oct 8, 2024
2 parents c7c30fb + 22c4764 commit a015890
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/rmediator/decorators/request.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from types import UnionType
from typing import Annotated, Union

from typing_extensions import Doc


def request(
response_type: Annotated[
Union[type, None],
Union[type, UnionType, None],
Doc(
"The type of the response expected from the request, or None if no response is expected."
),
Expand Down
3 changes: 2 additions & 1 deletion src/rmediator/decorators/request_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
from types import UnionType
from typing import Annotated, Union

from typing_extensions import Doc
Expand All @@ -9,7 +10,7 @@ def request_handler(
type, Doc("The type of the request that the handler should process.")
],
response_type: Annotated[
Union[type, None],
Union[type, UnionType, None],
Doc(
"The type of the response that the handler should return, or None if no response is expected."
),
Expand Down
17 changes: 9 additions & 8 deletions src/rmediator/mediator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from threading import Lock
from typing import Annotated, Dict
from typing import Annotated

from typing_extensions import Doc

Expand Down Expand Up @@ -43,14 +43,15 @@ class Mediator(metaclass=SingletonMeta):
Mediator class to handle the registration and dispatching of requests to their corresponding handlers.
Attributes:
__request_handlers (Dict[type, RequestHandler]): A dictionary mapping request types to their handlers.
__request_handlers (dict[type, RequestHandler]): A dictionary mapping request types to their handlers.
"""

def __init__(self) -> None:
"""
Initializes a new instance of the Mediator class.
"""
self.__request_handlers: Dict[type, RequestHandler] = {}
self._request_handlers: dict[type, RequestHandler] = {}
self._lock = Lock()

def send(
self,
Expand All @@ -71,7 +72,7 @@ def send(
Raises:
ValueError: If no handler has been registered for the type of the request.
"""
handler = self.__request_handlers.get(type(request))
handler = self._request_handlers.get(type(request))
if not handler:
raise ValueError(
f"Request cannot be handled; no handler has been registered for {type(request)}."
Expand Down Expand Up @@ -109,15 +110,15 @@ def register_handler(
f"Handler cannot be registered; handler response {handler._response} does not match request response {request_type._response}."
)

if request_type in self.__request_handlers:
old_handler = self.__request_handlers[request_type]
if request_type in self._request_handlers:
old_handler = self._request_handlers[request_type]

if type(handler) is not type(old_handler):
raise ValueError(
f"Handler cannot be registered; another handler for request type {request_type} has already been registered. Check {type(self.__request_handlers[request_type])}."
f"Handler cannot be registered; another handler for request type {request_type} has already been registered. Check {type(self._request_handlers[request_type])}."
)

self.__request_handlers[request_type] = handler
self._request_handlers[request_type] = handler

def __check_request_validity(self, request_type: type) -> None:
"""
Expand Down

0 comments on commit a015890

Please sign in to comment.