Skip to content

Self as return type annotation in base class method breaks multi-inheritance #14640

Closed
@uriyyo

Description

@uriyyo

Bug Report

Self as return type annotation in base class method breaks multi-inheritance.

(A clear and concise description of what the bug is.)

To Reproduce

import copy

from typing import Self


class Cloneable:
    def clone(self) -> Self:
        return copy.copy(self)


class Immutable:
    def clone(self) -> Self:
        return self


class Collection(Cloneable):
    pass


class Tuple(Immutable, Collection):
    pass

Expected Behavior
No errors

Actual Behavior

error: Definition of "clone" in base class "Immutable" is incompatible with definition in base class "Cloneable"  [misc]

Same code using old approach woks as expected:

import copy

from typing import TypeVar


_Self = TypeVar("_Self")


class Cloneable:
    def clone(self: _Self) -> _Self:
        return copy.copy(self)


class Immutable:
    def clone(self: _Self) -> _Self:
        return self


class Collection(Cloneable):
    pass


class Tuple(Immutable, Collection):
    pass

pyright doesn't raise an error.

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inheritanceInheritance and incompatible overridestopic-self-typesTypes for self

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions