Override a class attribute of the type of a generic type via a mixin so mypy won’t say it’s incompatible

64 views Asked by At

I have a generic base class A that has an attribute containing the generic type TK.

import typing


class K1:
    pass


TK = typing.TypeVar("TK", bound=K1)


class A(typing.Generic[TK]):
    k: type[TK]

Then I have a mixin B that should be applied on it in combination of a specific TK which is K1.

class B:
    k = K1

Then I try to apply it to A like this:

class AB(B, A[K1]):
    pass

The code works, but mypy complains:

error: Definition of "k" in base class "B" is incompatible with definition in base class "A"

The issue won’t happen when directly inheriting it and doing the same assignment:

class C(A[K1]):
    k = K1

Is there any way to make the mixin compatible with A[K1]? Notably the issue won’t happen when A.k isn’t annotated with the typevar, therefore I find it a bit odd.

0

There are 0 answers