Structure/unstructure decorators for classes
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by tracing the struct_hook and unstruct_hook interfaces described in the issue, along with how field-specific hooks are currently registered. Determine whether self- or cls-aware decorators can be supported while preserving the default structure/unstructure behavior; done means the proposed class-level interface is feasible and covered by appropriate tests.
Written by the indexing model from the issue text.
Description
I am not sure if I am missing some interface that can already do this, but I want to ultimately get something along these lines:
@attrs.define
class Example:
normal_field: str
special_field: str
prefix: ClassVar[str] = "something"
@special_field.structure
def _special_field_structure(self, attrib) -> str:
return f"{self.prefix}:{self.special_field}"
@special_field.unstructure
@classmethod
def _special_field_unstructure(cls, attrib, value: str) -> str:
prefix, value = value.split(":")
assert prefix == cls.prefix
return value
The whole prefix and handling is unimportant, it's just the interface here that I want to highlight.
Basically I want to use some default structure/unstructure functions (just changing _ to -), but allow some special fields to have a more complicated handling if needed by decorating some functions to handle things in a different way. The part where I extend the field decorators provided by attrs is related to https://github.com/python-attrs/attrs/issues/1543, let's just assume that that just works. The obstacle here is that the struct_hook/unstruct_hook do not accept self/cls interface as the first argument is always the raw value to be translated.
Any ideas if this could be enabled?
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
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.
More from python-attrs/cattrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-attrs/cattrs#761 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
python-attrs/cattrs#779 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
python-attrs/cattrs#743 · 2 comments ·
All issues in python-attrs/cattrs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100