question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HKT subtype method signature "incompatible with supertype"

See original GitHub issue

Bug report

I’m following your docs for HKTs. Thanks for the new feature btw. I’m seeing type errors that I don’t understand

from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Mapping, Iterator, Callable
from returns.primitives.hkt import Kind1, kinded, SupportsKind1

T_co = TypeVar("T_co", covariant=True)
U = TypeVar("U")
V = TypeVar("V")
G = TypeVar("G", bound="Grouping")


class Grouping(ABC, Generic[T_co]):
    @kinded
    @abstractmethod
    def map(self: G, f: Callable[[T_co], U]) -> Kind1[G, U]:
        ...

    @kinded
    @abstractmethod
    def zip_with(self: G, other: Kind1[G, U], f: Callable[[T_co, U], V]) -> Kind1[G, V]:
        ...


class One(Grouping[T_co], SupportsKind1["One", T_co]):
    def __init__(self, o: T_co):
        self._o = o

    def get(self) -> T_co:
        return self._o

    def map(self, f: Callable[[T_co], U]) -> One[U]:
        return One(f(self._o))

    def zip_with(self, other: One[U], f: Callable[[T_co, U], V]) -> One[V]:
        return One(f(self.get(), other.get()))

I’m seeing

grouping.py:31: error: Signature of "map" incompatible with supertype "Grouping"
grouping.py:34: error: Signature of "zip_with" incompatible with supertype "Grouping"

Can you help explain what’s wrong? In the docs you use

class Kind(Generic[_InstanceType, _FirstTypeArgType]):
    """Used for HKT emulation."""

whereas I use Kind1. Am I doing that right?

On a separate but related note, I was also seeing, for map signature on Grouping

def map(self: Kind1[G, T_co], f: Callable[[T_co], U]) -> Kind1[G, U]:

the error

grouping.py:15: error: The erased type of self "returns.primitives.hkt.KindN[G`-1, T_co`1, Any, Any]" is not a supertype of its class "grouping.Grouping[T_co`1]"

but it’s not entirely clear to me whether I should use self: G instead, or make Grouping subclass SupportsKind1["Grouping", T_co]

System information

  • python version: 3.8
  • returns version: 0.15.0
  • mypy version: 0.782

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
joelberkeleycommented, Oct 31, 2020

ok, thanks. I can then get it to pass using dekind within the method.

A bit of feedback: It would be really nice if there was some way of being able to specify One[U] in the function arguments rather than Kind[One, U], as it’s much more readable and is standard type hint style. I appreciate we can’t do this when One is replaced by a generic type constructor e.g. G, but it would be great if it could be universally possible with concrete container types like One

0reactions
sobolevncommented, Oct 31, 2020

oh ok, why’s that? the return type on the implementations aren’t Kind1, so why is @kinded necessary?

My bad, I am reading this in bed 😆

supertype defines the argument type as “KindN[One[T_co], U, Any, Any]”

Here’s how to fix it: https://github.com/dry-python/returns/blob/master/returns/io.py#L130

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python 3.6: Signature of {method} incompatible with super ...
This declaration doesn't mean subclasses can give deliver any signature they want. Subclass deliver methods must be ready to accept any ...
Read more >
Why is "incompatible with supertype" an error? #1237 - GitHub
It violates the Liskov Substitution Principle. You may want to Google that. In particular the problem is that if we have a Foo...
Read more >
python/typing - Gitter
I have an abstract class and a derived class, for which mypy reports an error "signature incompatible with supertype". A minimal example is...
Read more >
Java: how can I make the return type, of an inherited method ...
The simplest solution to this problem is to have a constructor defined in your subclass that makes a Position2D object out of a...
Read more >
Higher Kinded Types in Python - sobolevn.me
We have to manually list all possible cases in a function's signature. This works for cases when all possible arguments and outcomes are ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found