python / python/mypy

Ordered dataclass that inherits from abstract set produces (incorrect) errors

Open
#10,823 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi, I am running mypy 0.910 and have the following code:

from collections.abc import Set
from dataclasses import dataclass

@dataclass(order=True)
class MySet(Set):
    pass

And here are the errors it produces:

test.py: error: You may not have a custom __lt__ method when order=True
test.py: error: You may not have a custom __gt__ method when order=True
test.py: error: You may not have a custom __le__ method when order=True
test.py: error: You may not have a custom __ge__ method when order=True
test.py:5: error: Only concrete class can be given where "Type[MySet]" is expected
test.py:6: error: Argument 1 of "__lt__" is incompatible with supertype "AbstractSet"; supertype defines the argument type as "AbstractSet[Any]"
test.py:6: note: This violates the Liskov substitution principle
test.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
test.py:6: error: Argument 1 of "__gt__" is incompatible with supertype "AbstractSet"; supertype defines the argument type as "AbstractSet[Any]"
test.py:6: error: Argument 1 of "__le__" is incompatible with supertype "AbstractSet"; supertype defines the argument type as "AbstractSet[Any]"
test.py:6: error: Argument 1 of "__ge__" is incompatible with supertype "AbstractSet"; supertype defines the argument type as "AbstractSet[Any]"
Found 9 errors in 1 file (checked 1 source file)

Setting order=False gets rid of all errors except this one:

test.py:5: error: Only concrete class can be given where "Type[MySet]" is expected

It also seems to be related to Set and MutableSet from collections.abc. I don't get this behavior with Mapping for example.

Any ideas what is happening here?

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 minimal dataclass example against mypy 0.910, then compare its diagnostics with the order=False and Mapping cases described in the issue. Trace how abstract Set and MutableSet methods interact with dataclass ordering. Done means the reproducer no longer produces the incorrect errors while genuine abstract-class and override errors remain.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.