[New-bugs-announce] [issue42943] singledispatchmethod should expose registry of all known overloads

Ilya Kulakov report at bugs.python.org
Sat Jan 16 16:46:19 EST 2021


New submission from Ilya Kulakov <kulakov.ilya at gmail.com>:

The wrapper created by singledispatchmethod does not (trivially) expose registry of all known overloads.
Consider the following example:


    @singledispatchmethod
    def on_message(message):
        raise NotImplementedError

    @on_message.register
    def _(message: MessageA): ...

    @on_message.register
    def _(message: MessageB): ...

    loop = ...
    condition = ...
    messages = []

    def receive_message_on_thread(message):
        if ...:  # only signal to asyncio if message can be handled
            messages.append(message)
            loop.call_soon_threadsafe(condition.notify_all)
        else:
            ...

    async def process_messages_on_asyncio():
        while True:
            await condition.wait_for(lambda: len(messages) > 0)

            while len(messages):
                m = messages.pop(0)
                ...


Here messages are delivered outside of asyncio in a separate thread and thus they need to be forwarded into asyncio for further processing in the app. If the flow of messages is high it is desirable to filter inside the thread handler before signaling asyncio.

There are multiple approaches to describe which messages can be handled by the asyncio processor, but singledispatchmethod is the most elegant as it allows to keep everything in one place without if/else or code duplication.

Having a registry (trivially) exposed would allow to write the condition as `isinstance(message, tuple(on_message.registry.keys()))`.

----------
components: Library (Lib)
messages: 385151
nosy: Ilya.Kulakov, lukasz.langa, methane, ncoghlan
priority: normal
severity: normal
status: open
title: singledispatchmethod should expose registry of all known overloads
type: enhancement
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42943>
_______________________________________


More information about the New-bugs-announce mailing list