False positive `safe-super` errors for properties defined in protocol classes using inheritance
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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