Skip to content

A class with async "__call__" method fails to work as a middleware #1131

Closed
@chet-manley

Description

@chet-manley

(Filling out the following details about bugs will help us solve your issue sooner.)

Reproducible in:

pip freeze | grep slack
python --version
sw_vers && uname -v # or `ver`

The slack_bolt version

slack-bolt==1.18.1
slack_sdk==3.31.0

Python runtime version

Python 3.11.9

OS info

bash: sw_vers: command not found
❯ uname -a
Linux 49d1cffcb728 6.8.4-200.fc39.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Apr  4 20:45:21 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Steps to reproduce:

(Share the commands to run, source code, and project settings (e.g., setup.py))

from slack_bolt.app.async_app import AsyncApp

class MyCallableMiddleware:

    async def __call__(self, next_) -> None:
        await next_()

app = AsyncApp()
app.middleware(MyCallableMiddleware()) # this raises `ValueError: Async middleware function must be an async function`

Expected result:

AsyncApp.middleware accepts any Callable[..., Awaitable[Any]] object, per signature.

Actual result:

The middleware instance pass the callable check here
https://github.com/slackapi/bolt-python/blob/main/slack_bolt/app/async_app.py#L678
and is thus sent to AsyncCustomMiddleware init here
https://github.com/slackapi/bolt-python/blob/main/slack_bolt/middleware/async_custom_middleware.py#L13
where the signature for func is Callable[..., Awaitable[Any]] (which my middleware instance is).
However, inspect.iscoroutinefunction is used to check func, which does not pass, as it checks for the existence of
CO_COROUTINE flag (see here: CPython source),
despite the instance being both callable and asynchronous.

Requirements

Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy