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

Extending attrs-generated `__eq__`

Aperta
#1,452 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
42/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python

Direzione di ricerca

Start at attrs' generated eq entry point and compare it with the attrs_init extension path shown in the issue. Use the provided X and magic_object example to define the expected behavior, including preserving generated field comparisons while allowing custom equality logic.

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

Descrizione

Hi, I fear that there is probably a super obvious answer to this question, but I somehow cannot wrap my head around it: is there any way how I can "extend" the attrs-generated __eq__ method, similar to how it's possible for __init__ by calling __attrs_init__? 🤔

Here just a dummy example to illustrate the situation. Right now, the only way I can get it work is by writing the boilerplate myself.

from attrs import define

magic_object = object()
"""Is equal to all objects."""


@define
class X:
    here: int
    are: float
    many: str
    fields: bool

    def __eq__(self, other):
        if other is magic_object:
            return True
        
        # Ideally, I want to call something like:
        # return __attrs_eq__(other)

        # Instead, I have this boilerplate code!
        return (
            super().__eq__(other)
            and isinstance(other, X)
            and self.here == other.here
            and self.are == other.are
            and self.many == other.many
            and self.fields == other.fields
        )


assert X(0, 0.0, "a", True) == X(0, 0.0, "a", True)
assert X(0, 0.0, "a", True) != X(0, 0.0, "a", False)
assert X(0, 0.0, "a", True) == magic_object
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.