user45245
user45245

Reputation: 905

@singledispatchmethod is not working correctly

@singledispatchmethod is not working correctly. I have this code

class Stream:
    @singledispatchmethod
    def subscribe(self, arg_1, arg_2):
        pass

    @subscribe.register
    def _(self, msg_type: type, action: Callable):
        pass

    @subscribe.register
    def _(self, msg_type: type, context: bool):
        pass


def aaa() -> None:
    pass


@pytest.mark.asyncio
async def test_can_subscribe_to_specific_event_types():
    stream = Stream()
    stream.subscribe(type(str), aaa)

I want to call an overload of a method that takes

def _(self, msg_type: type, action: Callable):

but python calls

def _(self, msg_type: type, context: bool):

What am I doing wrong?

Upvotes: 0

Views: 808

Answers (1)

buran
buran

Reputation: 14273

From the docs

When defining a function using @singledispatchmethod, note that the dispatch happens on the type of the first non-self or non-cls argument

In your case it is msg_type.

Upvotes: 2

Related Questions