Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mfinzi opened this issue Sep 20, 2023 · 2 comments
Closed

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

mfinzi opened this issue Sep 20, 2023 · 2 comments

Comments

@mfinzi
Copy link

mfinzi commented Sep 20, 2023

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
Copy link
Member

wesselb commented Sep 20, 2023

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
Copy link
Member

wesselb commented Oct 7, 2023

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

@wesselb wesselb closed this as completed Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants