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

Support types with custom __instancecheck__ method #248

Closed
sobolevn opened this issue Jul 5, 2021 · 0 comments
Closed

Support types with custom __instancecheck__ method #248

sobolevn opened this issue Jul 5, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@sobolevn
Copy link
Member

sobolevn commented Jul 5, 2021

This code does not work:

from classes import typeclass

class Meta(type):
    def __instancecheck__(self, other) -> bool:
        return other == 1

class Some(object, metaclass=Meta):
    ...

@typeclass
def some(instance) -> bool:
    ...


@some.instance(Some)
def _some_some(instance: Some) -> bool:
    return True


print(some(1))
# NotImplementedError: Missing matched typeclass instance for type: int

But, it should! For example, phantom-types use this method: https://github.com/antonagestam/phantom-types/blob/main/phantom/base.py#L28-L43

If we want to support them - we would need this feature.

Solution

I suggest that we need to add types with __instancecheck__ defined to both ._instances and ._protocols.
This way both of these cases would work:

print(some(Some()))
print(some(1))
@sobolevn sobolevn added the enhancement New feature or request label Jul 5, 2021
sobolevn added a commit that referenced this issue Jul 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant