python / python/mypy

Inconsistent errors with superclass Liskov violations

Open
#7,336 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs discussion priority-1-normal topic-inheritance
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

  • Are you reporting a bug, or opening a feature request?

Bug report

class ParentA:
    def get_description(self, s: str): ...


class ParentB:
    def get_description(self, i: int): ...


# This is a Liskov violation and needs to be ignored.
class ParentViolation(ParentA, ParentB):  # type: ignore
    ...


class ShouldBeIrrelevant:
    ...


# Inheriting from a violating parent, showing no error
class NoViolation(ParentViolation):
    ...


# Inheriting from a violating parent, but an extra class makes a violation appear
class SpuriousViolation(ParentViolation, ShouldBeIrrelevant):
    ...
  • What is the actual behavior/output?

The NoViolation class line has no errors.
The SpuriousViolation class line has an error of:
error: Definition of "get_description" in base class "ParentA" is incompatible with definition in base class "ParentB"

  • What is the behavior/output you expect?

Either they should both error, or neither should. Personally, I would prefer it if neither errored, since the Liskov violation has already been explicitly ignored by the ParentViolation class. However, I also don't have enough context to know whether this is actually the correct solution.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy 0.720, and verified with mypy 0.730+dev.17a93eac7d1d78e31fc9b347682d86d8d53fe02b

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied Python snippet with mypy 0.730+dev and compare diagnostics on the NoViolation and SpuriousViolation class lines. Trace how the checker handles the ignored ParentViolation and inherited superclass definitions; the issue is done when both cases receive consistent treatment, with a regression test covering the two examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.