beartype / plum

Multiple dispatch in Python
https://beartype.github.io/plum
MIT License
497 stars 22 forks source link

Possible bug with covariance of parametric types and dispatch 🐛 #107

Closed mfinzi closed 7 months ago

mfinzi commented 8 months ago

First off, thanks for the amazing library. 🙌

I had some issues come up when trying to dispatch on parametric types.

In the docs it is specified that the parametric types are covariant:

A[T1] is a subtype of A[T2] whenever T1 is a subtype of T2

However, even though issubclass seems to respect this property, dispatch does match on these covariant types as I would expect.

I added a minimal example to reproduce the behavior below:

from plum import parametric, dispatch

@parametric
class A:
    def __init__(self, x):
        self.x = x

class B:
    pass

class C(B):
    pass

@dispatch
def f(x: A[B]):
    return "special"

test_obj = A(C())
f(test_obj)

NotFoundLookupError: For function `f`, `(A(<__main__.C object at 0x7f095719b8e0>),)` could not be resolved.

whereas

issubclass(test_obj, A[B]) returns true

Is this expected? If so, what is the appropriate way of handling this?

wesselb commented 8 months ago

Het @mfinzi! Thanks for opening an issue. :)

This is a massive coincidence, but we discussed exactly this problem here this morning! I think you've very correctly diagnosed the issue. In the PR, we're discussed a possible fix, which might be very simple. Please bear with us! With a bit of luck, this should be fixed shortly.

wesselb commented 7 months ago

This should be fixed with #108, which will be included in the next release! :)