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

attr.Factory provided with the attribute beeing initialized

Aperta
#692 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
tooling

Direzione di ricerca

Start by reading the attr.Factory, converters.default_if_none, Attribute, and metadata documentation linked or named in the issue. Determine whether a factory can receive attribute context without mutating frozen Attribute instances. Done should be a supported way to derive an environment key from an attribute name or metadata, with the requested behavior documented and tested.

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

Descrizione

Feature

Hi there,

I'm looking for a way to read default value for an attribute from os.environ

The natural way i see for this is to use a attr.Factory in combination with attr.converters.default_if_none. This factory would call os.environ with some env-key built with attribute's name or metadata.

But i dont see how this factory could be provided with the attribute's name or meta-data.

The other way i tried to was to build a custom decorator

def env_binding(prefix):
    def modify_ib_default(attribute):
        env_key = (prefix + '_' + attribute.name).upper()
        attribute.default = os.environ.get(env_key, attribute.default)
        return attribute

    def wrap(cls):
        new_cls = attr.s(cls)
        new_cls.__attrs_attrs__ = tuple(map(modify_ib_default, new_cls.__attrs_attrs__))
        return new_cls
    return wrap

but since Attribute instances are read-only (setattr --> raise FrozenInstanceError), this method is not valid...

Note: I know the nice python attr-based module config-environ but I need a different behavior.

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.