Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

attr.Factory provided with the attribute beeing initialized

Đang mở
#692 6 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
5.8k
Fork
480
Merge trung bình
2 giờ 15 phút
Pull request đã merge (30 ngày)
2

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-attrs/attrs

Tất cả issue của python-attrs/attrs

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.