python / python/mypy

Errors on @overload in supertype signatures

Open
#20,418 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Given ABC A which have two implementations B and C. Method C.view() may return either B or C, depending on argument type. When using @overload to describe this, mypy complains with "Signature of "view" incompatible with supertype "A"". However, the type annotations provided by the overload works as expected.

To Reproduce

from __future__ import annotations
from abc import ABC, abstractmethod
from typing import overload, reveal_type

class A(ABC):
    @abstractmethod
    def view(self, x: int | None = None) -> A: ...

class B(A):
    def view(self, x: int | None = None) -> B:
        return B()

class C(A):
    @overload  # mypy complains: "Signature of "view" incompatible with supertype "A""
    def view(self, x: int) -> B: ...
    @overload
    def view(self, x: None = None) -> C: ...
    def view(self, x: int | None = None) -> B | C:
        if x is None:
            return C()
        else:
            return B()

# This works as expected when @overload is used
reveal_type(C().view(None))  # Revealed type is C
reveal_type(C().view(5))     # Revealed type is B

https://gist.github.com/mypy-play/00b068b5b6eeebbea6f9486dba468708

Actual Behavior

main.py:14: error: Signature of "view" incompatible with supertype "A"  [override]
main.py:14: note:      Superclass:
main.py:14: note:          def view(self, x: int | None = ...) -> A
main.py:14: note:      Subclass:
main.py:14: note:          @overload
main.py:14: note:          def view(self, x: int) -> B
main.py:14: note:          @overload
main.py:14: note:          def view(self, x: None = ...) -> C

Your Environment

  • Mypy version used: 1.19.0
  • Mypy command-line flags: Default args in gist
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: Python 3.12

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

Start by running the provided Python reproducer with mypy 1.19.0 and inspect the overload and supertype-signature compatibility check. Done means the example no longer reports an incompatible override while reveal_type still reports C for None and B for 5.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.