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

on_setattr doesn't apply during init

Aperta
#672 3 commenti 3 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
20/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python

Direzione di ricerca

Start by reproducing the minimal Test example and compare construction with post-instantiation assignment. Then trace the on_setattr and initialization entry points in the attrs package and review the existing tests and documentation for validators. Done means the intended behavior for validation during init is decided and covered by a documented, tested approach.

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

Descrizione

Documentation

This may be by design, but I was hopeful it would work. When an attribute is set during init, on_setattr does not seem to apply, but it does after instantiation. What's the best way to hook in so I can add a validator to all Attributes? Looking to essentially create a class that enforces all values are set to valid types, without adding the same validator to each attribute when I can have some classes with dozens of attributes.

import attr

def raise_value_error(instance, attribute, value):
    raise ValueError

@attr.define(on_setattr=raise_value_error)
class Test:
    a: int

test = Test("a string")  # fine
test.a = "another string"  # fails
Lingua principale
Python
Stelle
5.8k
Fork
490
Merge medio
3g 12h
PR unite (30g)
3

Preparare l'ambiente

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.