Skip to content

mypy claims compatible types are incompatible when non-related mixin classes are present #12440

Closed
@zzzeek

Description

@zzzeek

as always, I apologize if this falls into one of the categories of "that's expected, because what if you did this?" and/or "this is a known mypy limitation / issue number", this one does not have a great workaround since I'd have to "type:ignore" the class def itself, or have to add redundant attributes everywhere, and I will likely just re-organize the code to not have this particular issue.

mypy --version
mypy 0.950+dev.0fecec8c72605ec7bae343b28d545c477c726e81 (compiled: no)

Here's the demo - ImplementsWOMixin and ImplementsWMixin are basically the same class except one of them has an extra entry in its __bases__. mypy then decides that a local attribute that returns str instead of Optional[str] is no longer compatible with the base class:

from typing import Optional


class Base:
    description: Optional[str]


class MixinWDesc:
    description: str


class SubClass(MixinWDesc, Base):
    description: str


class SomeOtherMixin:
    pass

# succeeds
class ImplementsWOMixin(SubClass):
    pass


# fails
class ImplementsWMixin(SomeOtherMixin, SubClass):
    pass

output - it complains about MixinWDesc, but the line number is that of the ImplementsWMixin class itself, meaning every class that subclasses MixinWDesc and uses other mixins would need workarounds or type:ignores.

$ mypy test3.py 
test3.py:26: error: Definition of "description" in base class "MixinWDesc" is incompatible with definition in base class "Base"  [misc]
Found 1 error in 1 file (checked 1 source file)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inheritanceInheritance and incompatible overrides

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions