python / python/mypy

False positive `safe-super` errors for properties defined in protocol classes using inheritance

Open
#14,757 7 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Empty properties defined in a protocol class that is subclassed by another protocol are incorrectly classified by mypy as abstract methods - this can then result in a safe-super error if the parent property is accessed via super() within a class that is type hinted with the child protocol, e.g. when type hinting self in mixin classes (as described here).

To Reproduce

from __future__ import annotations

from typing import Protocol, Optional, NoReturn


class Dataset(Protocol):
    def validate(self) -> Optional[NoReturn]:
        ...

    @property
    def has_schema(self) -> bool:
        ...


class TSDataset(Dataset, Protocol):
    @property
    def num_rows(self) -> int:
        ...


class TSDatasetMixin:
    def check_schema(self: TSDataset) -> None:
        super().validate()
        if super().has_schema:
            print('Dataset has a schema')

Expected Behavior

No errors.

Actual Behavior

mypy_safe_super_protocol.py:24: error: Call to abstract method "has_schema" of "Dataset" with trivial body via super() is unsafe  [safe-super]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.0

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 issue with the shown example, using mypy_safe_super_protocol.py as the reported reproduction file. Start by tracing how safe-super checks classify inherited protocol properties; done means the example reports no safe-super error while preserving the existing validation behavior.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.