Skip to content

Invalid revealed type for ABC usage #259

@sobolevn

Description

@sobolevn

Here's the code:

from abc import ABCMeta, abstractmethod

from classes import typeclass


@typeclass
def my_typeclass(instance) -> int:
    """Example typeclass."""


class _MyABC(object, metaclass=ABCMeta):
    @abstractmethod
    def get_number(self) -> int:
        """Example abstract method."""


class _MyConcrete(_MyABC):
    def get_number(self) -> int:
        """Concrete method."""
        return 1


def _my_abc(instance: _MyABC) -> int:
    return instance.get_number()


my_typeclass.instance(_MyABC)(_my_abc)
assert my_typeclass(_MyConcrete()) == 1

This works in runtime, but mypy reveals invalid type:

reveal_type(my_typeclass)

Reveals:

ex.py:48: note: Revealed type is "classes._typeclass._TypeClass[ex._MyABC, def (instance: ex._MyABC) -> builtins.int, <nothing>, Literal['ex.my_typeclass']]"

It should be:

ex.py:48: note: Revealed type is "classes._typeclass._TypeClass[ex._MyABC, def (instance: Any) -> builtins.int, <nothing>, Literal['ex.my_typeclass']]"

For some reason, first argument of our initial signature gets mutated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions