Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Typing error when defining an enum of an attrs class

Aperta
#1,287 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python

Direzione di ricerca

Start by running the supplied Python reproduction with attrs.define, attrs.frozen, dataclasses.dataclass, Enum, and mypy to confirm the differing typing and runtime behavior. Trace the attrs-generated methods involved in enum multiple inheritance, then add focused regression coverage showing the intended consistency between mypy results and runtime behavior.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug Typing

When trying to define an enum of an @attrs.define class, Mypy gives typing errors, but it works in runtime.

When trying to define an enum of an @attrs.frozen class, Mypy doesn't give an error, but it errors at runtime.

When trying to define an enum of a @dataclasses.dataclass class, Mypy doesn't give an error, and it works at runtime.

I started noticing this today after I removed support for Python 3.9 from my project, and ran poetry update.

from dataclasses import dataclass
from enum import Enum

import attrs
from pytest import raises


@attrs.define
class AttrsDefine:
    a: int
    b: float


# error: Definition of "__hash__" in base class "AttrsDefine" is incompatible with definition in base class "Enum"  [misc]
class AttrsDefineEnum(AttrsDefine, Enum):
    X = (1, 2.0)
    Y = (3, 4.0)


@attrs.frozen
class AttrsFrozen:
    a: int
    b: float


@dataclass
class Dataclass:
    a: int
    b: float


class DataclassEnum(Dataclass, Enum):
    X = (1, 2.0)
    Y = (3, 4.0)


def test_enums() -> None:
    with raises(attrs.exceptions.FrozenInstanceError) as exc_info:

        # attr.exceptions.FrozenInstanceError
        class AttrsFrozenEnum(AttrsFrozen, Enum):
            X = (1, 2.0)
            Y = (3, 4.0)

        print(repr(AttrsFrozenEnum.X))

    print(exc_info.exconly())
    print(repr(AttrsDefineEnum.X))
    print(repr(DataclassEnum.X))
❯ mypy attrenum.py
attrenum.py:15: error: Definition of "__hash__" in base class "AttrsDefine" is incompatible with definition in base class "Enum"  [misc]
Found 1 error in 1 file (checked 1 source file)
❯ pytest -s attrenum.py
=================================================================================== test session starts ===================================================================================
platform linux -- Python 3.11.6, pytest-7.4.0, pluggy-1.2.0
rootdir: /home/micael/projects/playground
collected 1 item                                                                                                                                                                          

attrenum.py attr.exceptions.FrozenInstanceError
AttrsDefineEnum(a=1, b=2.0)
<DataclassEnum.X: Dataclass(a=1, b=2.0)>
.

==================================================================================== 1 passed in 0.01s ====================================================================================
❯ python --version
Python 3.11.4
❯ mypy --version
mypy 1.10.0 (compiled: yes)
❯ pip freeze
attrs==23.2.0
iniconfig==2.0.0
mypy==1.10.0
mypy-extensions==1.0.0
packaging==24.0
pluggy==1.5.0
pytest==8.2.1
typing_extensions==4.11.0
Lingua principale
Python
Stelle
5.8k
Fork
480
Merge medio
2h 15m
PR unite (30g)
2

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-attrs/attrs

Tutte le issue di python-attrs/attrs

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.