Skip to content

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

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

importcopyfromtypingimportSelfclassCloneable:
defclone(self) ->Self:
returncopy.copy(self)
classImmutable:
defclone(self) ->Self:
returnselfclassCollection(Cloneable):
passclassTuple(Immutable, Collection):
pass

Expected Behavior
No errors

Actual Behavior

error: Definitionof"clone"inbaseclass"Immutable"isincompatiblewithdefinitioninbaseclass"Cloneable" [misc]

Same code using old approach woks as expected:

importcopyfromtypingimportTypeVar_Self=TypeVar("_Self")
classCloneable:
defclone(self: _Self) ->_Self:
returncopy.copy(self)
classImmutable:
defclone(self: _Self) ->_Self:
returnselfclassCollection(Cloneable):
passclassTuple(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