python / python/mypy

`safe-super` change breaks Mixin support

Open
#17,192 3 comments 6 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

We want to indicate that a (super-)class must implement a specific protocol in order to inherit from a specific (sub-) class. We used to annotate self as the super class in the subclass to express this. With mypy 1.10.0 this introduces a safe-super error. Is there currently a way to express this requirement?

To Reproduce

from typing import Protocol


class HasFormValid(Protocol):
    def form_valid(self, form):
        pass


class SaveValidFormMixin:

    def form_valid(self: HasFormValid, form):
        form.save()
        super().form_valid(form)

Expected Behavior

No error: Since we never actually inherit from the HasFormValid protocol, this should be safe.

Actual Behavior

mypy 1.10.0 treats HasFormValid.form_valid as trivial and warns about its usage. In this case, it is assured that HasFormValid.form_valid cannot be actually called via super().form_valid.

mypy <1.10.0 does not raise an error here.

Your Environment

  • Mypy version used: mypy 1.10.0 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.3

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 reproducing the provided Mixin and Protocol example with mypy 1.10.0, then trace the safe-super diagnostic and its regression tests. Add coverage for this Mixin pattern and verify that it no longer reports a false positive while existing safe-super warnings remain intact.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.