python / python/mypy

type: ignore for “incompatible with supertype” error in subclass should suffice for subsubclasses too

Open
#10,197 1 comment 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-inheritance topic-type-ignore
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

class A:
    def foo(self, s: str) -> str: ...

class B(A):
    def foo(self, n: float) -> float: ... # type: ignore

class C(B):
    def foo(self, n: float) -> float: ...

Mypy playground link

Mypy is correct to report an error for B.foo that necessitated a type: ignore comment. However, having added that type: ignore for B.foo, mypy should not also report an error for C.foo. Although C.foo is inconsistent with A.foo, it’s consistent with B.foo, and I have already asked mypy to ignore the inconsistency between B.foo and A.foo.

To put this another way, I recognize that mypy would fail to catch the runtime error in a: A = C(); a.foo("str"), but this would still be a runtime error if C.foo did not override B.foo, so the blame should be assigned solely to B.foo and not to C.foo.

This comes up in the typeshed stubs for markdown.inlinepatterns:

class Pattern:
    def handleMatch(self, m: Match) -> Optional[Union[str, Element]]: ...

class InlineProcessor(Pattern):
    def handleMatch(self, m: Match, data) -> Union[Tuple[Element, int, int], Tuple[None, None, None]]: ...  # type: ignore

These stubs correctly reflect the upstream API which uses handleMatch with a different signature in subclasses of InlineProcessor than in other subclasses of Pattern. That wasn’t a great API design decision, but now that it’s made and the type: ignore was added to InlineProcessor.handleMatch, ideally I shouldn’t have to repeat the same type: ignore in order to write a custom subclass of InlineProcessor whose handleMatch satisfies the signature of InlineProcessor.handleMatch.

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

Reproduce the subclass-checking behavior in the linked mypy playground, then compare it with the typeshed stubs at stubs/Markdown/markdown/inlinepatterns.pyi and the upstream markdown/treeprocessors.py implementation. Done means a type: ignore on InlineProcessor.handleMatch also prevents an unnecessary incompatible-with-supertype error for a compatible subclass override, while the original B.foo diagnostic remains suppressed.

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.