python-attrs / python-attrs/attrs
attr.Factory provided with the attribute beeing initialized
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
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.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100