Skip to content

(🎁) Mypy too conservative with base class compatibility (Definition of in base class is incompatible) #12314

Closed as not planned
@KotlinIsland

Description

@KotlinIsland
class L:
    a: int

class R:
    b: int

class Both(L, R):
    pass


class A:
    @property
    def x(self) -> L: ...

class B:
    @property
    def x(self) -> R: ...

class C(A, B):  # error: Definition of "i" in base class "A" is incompatible with definition in base class "B"
    pass

both: L = Both()
assert isinstance(both, R)
reveal_type(both)  # Revealed type is "<subclass of "L" and "R">"
c: A = C()
assert isinstance(c, B)  # error: Subclass of "A" and "B" cannot exist: would have incompatible method signatures
reveal_type(c)  # expected "subclass of A and B", actual "A"
reveal_type(c.x)  # expected "subclass of L and R", actual "L"

L and R are compatible (Both is an implementation), I would expect mypy to work here

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions