Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

attr.Factory provided with the attribute beeing initialized

Abierto
#692 6 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
tooling

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
5.8k
Forks
480
Merge medio
2 h 15 min
PR fusionados (30 d)
2

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python-attrs/attrs

Todos los issues de python-attrs/attrs

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.