attr.Factory provided with the attribute beeing initialized
还没有人认领这个 Issue。
评估
调研方向
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 分钟
- 30 天内合并 PR
- 2
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-attrs/attrs 的其他 Issue
-
难度 5/5 一周以上 新手友好度 20/100
python-attrs/attrs#1620 ·
-
难度 3/5 1-2 天 新手友好度 55/100
python-attrs/attrs#1596 · 2 条评论 · 1 个 reaction ·
-
难度 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 ·