python / python/mypy

incompatible override false positive on method that uses descriptor with contravariant generic

Open
#17,736 0 comments 1 reaction 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

# mypy: enable-incomplete-feature=NewGenericSyntax

from typing import Callable, override


class Foo[T]:
    def __init__(self, value: Callable[[T], None]) -> None:
        pass

    def __get__(self, instance: T, owner: type[T]) -> int:
        return 1


class Bar:
    @Foo
    def foo(self) -> None:
        pass


class Baz(Bar):
    @Foo
    @override
    def foo(self) -> None: # error (override)
        pass
main.py:23: error: Signature of "foo" incompatible with supertype "Bar"  [override]
main.py:23: note:      Superclass:
main.py:23: note:          Foo[Bar]
main.py:23: note:      Subclass:
main.py:23: note:          Foo[Baz]

playground

although this error is usually correct because the generic is contravariant, in this case i believe it's safe because the descriptor prevents the Foo class from being accessible on instances of Bar and Baz:

Bar().foo # int
Baz().foo # int

fyi pyright does not report an error in this case.

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 supplied minimal Python reproducer in mypy and inspect the override diagnostic for descriptor-decorated methods with contravariant generic parameters. Compare the reported Foo[Bar]/Foo[Baz] types with the shown runtime behavior; done means this safe case no longer emits [override] while genuinely incompatible overrides remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
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.