dry-python/classes

delegate fails for `Literal` type

q0w opened this issue · 2 comments

q0w commented

Bug report

What's wrong

from classes import typeclass
from typing import Literal


class _Car(type):
    def __instancecheck__(self, instance) -> bool:
        return isinstance(instance, str) and instance == "car"


class Car(str, metaclass=_Car):
    ...


class _Pony(type):
    def __instancecheck__(self, instance) -> bool:
        return isinstance(instance, str) and instance == "pony"


class Pony(str, metaclass=_Pony):
    ...


@typeclass
def get_repr(type, name: str) -> str:
    pass


@get_repr.instance(delegate=Pony) # error: Instance "Literal['pony']" does not match inferred type "t.Pony*"
def _pony(type: Literal["pony"], name: str) -> str:
    return f"Pony {name}"


@get_repr.instance(delegate=Car) # error: Instance "Literal['car']" does not match inferred type "t.Car*"
def _car(type: Literal["car"], name: str) -> str:
    return f"Car {name}"


if __name__ == "__main__":
    print(get_repr('car', 'test')) # error: Argument 1 to "get_repr" has incompatible type "str"; expected <nothing>

How is that should be

System information

  • python version: 3.9.6
  • classes version: 0.4.0
  • mypy version: 0.910

Thanks for your issue, Literal is not supported yet!

Related #274

I guess we can close it in favor of #274 🙂