Skip to content

Does plum dispatch tighter types priorer than looser types? #214

@roastduck

Description

@roastduck

Hi there,

I'm wondering wheter plum dispatch tighter types priorer than looser types? For example, when I have a dispatch with type A, and another with A | B, will A goes to A but B gose to A | B?

I tested some cases, some behaves like above, but not all the cases. The following int and bool case seems to make plum always choosing the same branch:

import plum

@plum.dispatch
def f(x: int):
    print("f(int)")

@plum.dispatch
def f(x: int | bool):
    print("f(int | bool)")

f(1)
f(True)

The example above prints

f(int | bool)
f(int | bool)

but I expect the output to be

f(int)
f(int | bool)

Even if there is a precedence, plum will still choose the second dispatch:

import plum

@plum.dispatch(precedence=1)
def g(x: int):
    print("g(int)")

@plum.dispatch(precedence=0)
def g(x: int | bool):
    print("g(int | bool)")

g(1)
g(True)

This outputs

g(int | bool)
g(int | bool)

Plum will choose the first dispatch only if the second dispatch does not exist:

import plum

@plum.dispatch
def h(x: int):
    print("h(int)")

h(1)

This outputs:

h(int)

So if there is a rule for the cases that one dispatch has a type that is a superset of the other dispatch's type?

Tested with plum-dispatch==2.5.7, beartype==0.21.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions