attr.Factory provided with the attribute beeing initialized
まだ誰も着手していません。
評価
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 5.8k
- フォーク
- 480
- 平均マージ
- 2時間 15分
- マージ済み PR(30日)
- 2
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python-attrs/attrs のほかの issue
-
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
python-attrs/attrs#1620 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
python-attrs/attrs#1596 · コメント 2 件 · リアクション 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
python-attrs/attrs#1549 · コメント 3 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 38/100
python-attrs/attrs#1543 · コメント 2 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
python-attrs/attrs#1532 ·
python-attrs/attrs の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
stephrobert/dsoxlab#238 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
sublimehq/package_control#1780 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
nwg-piotr/nwg-displays#145 ·