Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Support types with custom __instancecheck__ method #248

Copy link
Copy link

Description

@sobolevn
Issue body actions

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))
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.