diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3abc593..a70e65c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,8 +1,7 @@ name: Release on: pull_request: - branches: - - main + branches: [main] types: [closed] permissions: contents: write @@ -11,10 +10,16 @@ jobs: if: github.event.pull_request.merged == true runs-on: ubuntu-latest steps: + - name: Create GitHub App token + id: app-token + uses: actions/create-github-app-token@v2 + with: + app-id: ${{ vars.RELEASE_APP_ID }} + private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} - uses: actions/checkout@v4 with: fetch-depth: 0 - token: ${{ secrets.PERSONAL_ACCESS_TOKEN }} + token: ${{ steps.app-token.outputs.token }} - name: Install uv uses: astral-sh/setup-uv@v6 - name: Install the project diff --git a/packages/asyncfast/src/asyncfast/__init__.py b/packages/asyncfast/src/asyncfast/__init__.py index 26dbe82..2c74dcd 100644 --- a/packages/asyncfast/src/asyncfast/__init__.py +++ b/packages/asyncfast/src/asyncfast/__init__.py @@ -54,6 +54,12 @@ _PARAMETER_PATTERN = re.compile(r"{(.*)}") +class InvalidChannelDefinitionError(ValueError): + """ + Raised when a channel or message handler is defined with an invalid shape. + """ + + async def _send_message(send: AMGISendCallable, message: Mapping[str, Any]) -> None: message_send_event: MessageSendEvent = { "type": "message.send", @@ -323,13 +329,17 @@ def _add_channel( elif _is_message(message_sender_type): messages = [message_sender_type] - assert len(payloads) <= 1, "Channel must have no more than 1 payload" + if len(payloads) > 1: + raise InvalidChannelDefinitionError( + "Channel must have no more than 1 payload" + ) payload = payloads[0] if len(payloads) == 1 else None - assert ( - len(message_senders) <= 1 - ), "Channel must have no more than 1 message sender" + if len(message_senders) > 1: + raise InvalidChannelDefinitionError( + "Channel must have no more than 1 message sender" + ) message_sender = message_senders[0] if len(message_senders) == 1 else None diff --git a/packages/asyncfast/tests_asyncfast/test_errors.py b/packages/asyncfast/tests_asyncfast/test_errors.py new file mode 100644 index 0000000..310beeb --- /dev/null +++ b/packages/asyncfast/tests_asyncfast/test_errors.py @@ -0,0 +1,29 @@ +from typing import Any + +import pytest +from asyncfast import AsyncFast +from asyncfast import InvalidChannelDefinitionError +from asyncfast import MessageSender + + +async def test_only_one_payload() -> None: + app = AsyncFast() + + with pytest.raises(InvalidChannelDefinitionError): + + @app.channel("topic") + async def topic_handler(payload1: int, payload2: int) -> None: + pass # pragma: no cover + + +async def test_only_one_message_sender() -> None: + app = AsyncFast() + + with pytest.raises(InvalidChannelDefinitionError): + + @app.channel("topic") + async def topic_handler( + message_sender1: MessageSender[dict[str, Any]], + message_sender2: MessageSender[dict[str, Any]], + ) -> None: + pass # pragma: no cover