This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: singledispatchmethod should expose registry of all known overloads
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, Ilya.Kulakov, hongweipeng, lukasz.langa, methane, ncoghlan
Priority: normal Keywords: patch

Created on 2021-01-16 21:46 by Ilya.Kulakov, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 30007 open hongweipeng, 2021-12-09 15:14
Messages (1)
msg385151 - (view) Author: Ilya Kulakov (Ilya.Kulakov) * Date: 2021-01-16 21:46
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()))`.
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87109
2021-12-09 15:14:16hongweipengsetkeywords: + patch
nosy: + hongweipeng

pull_requests: + pull_request28231
stage: patch review
2021-11-04 20:24:04AlexWaygoodsetnosy: + AlexWaygood

versions: + Python 3.11, - Python 3.10
2021-01-16 21:46:19Ilya.Kulakovcreate