Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subclass using @dataclass(order=True) have unavoidable "not using @override" error for __lt__ etc, with --enable-error-code=explicit-override #17224

Open
huonw opened this issue May 8, 2024 · 1 comment
Labels
bug mypy got something wrong good-second-issue topic-dataclasses topic-inheritance Inheritance and incompatible overrides

Comments

@huonw
Copy link

huonw commented May 8, 2024

Bug Report

If one has one dataclass B inherit from another A and both are annotated with order=True, enabling the explicit-override error code gives errors attached to the subclass B about missing @overrides on __lt__ (etc.) errors. They seem to be unavoidable: there's no method to attach @override to.

To Reproduce

Run this with mypy --enable-error-code=explicit-override:

from dataclasses import dataclass

@dataclass(order=True)
class A:
    pass

@dataclass(order=True)
class B(A):
    pass

All-in-one reproducer:

# Setup:
cd $(mktemp -d)

python3 -m venv .venv
.venv/bin/python -m pip install mypy==1.10.0 mypy-extensions==1.0.0 typing_extensions==4.11.0

cat > test_mypy.py <<EOF
from dataclasses import dataclass

@dataclass(order=True)
class A:
    pass

@dataclass(order=True)
class B(A):
    pass
EOF

# BUG:
.venv/bin/python -m mypy --enable-error-code=explicit-override ./test_mypy.py

Expected Behavior

Actual Behavior

Output:

test_mypy.py:10: error: Method "__lt__" is not using @override but is overriding a method in class "test_mypy.A"  [explicit-override]
test_mypy.py:10: error: Method "__gt__" is not using @override but is overriding a method in class "test_mypy.A"  [explicit-override]
test_mypy.py:10: error: Method "__le__" is not using @override but is overriding a method in class "test_mypy.A"  [explicit-override]
test_mypy.py:10: error: Method "__ge__" is not using @override but is overriding a method in class "test_mypy.A"  [explicit-override]
Found 4 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: --enable-error-code=explicit-overide
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12
@huonw huonw added the bug mypy got something wrong label May 8, 2024
@huonw huonw changed the title Super/subclasses using @dataclass(order=True) have unavoidable "not using @override" error for __lt__ etc, with --enable-error-code=explicit-override Subclass using @dataclass(order=True) have unavoidable "not using @override" error for __lt__ etc, with --enable-error-code=explicit-override May 8, 2024
@JelleZijlstra JelleZijlstra added topic-inheritance Inheritance and incompatible overrides topic-dataclasses labels May 8, 2024
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented May 20, 2024

If someone wants to attempt a PR, I think there's a plugin_generated attribute that could be used to gate this error. You'd need to look at dataclasses.py and checker.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong good-second-issue topic-dataclasses topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

No branches or pull requests

3 participants