Inheriting overrides
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start with cattrs.Converter hook registration and make_dict_unstructure_fn, using the FruitBasket and MixedBasket example to trace how class-specific overrides are applied. Define whether inherited overrides should be discovered automatically while preserving subclass overrides, then verify the intended result is {'bananas': 5, 'peaches': 3}.
Written by the indexing model from the issue text.
Description
- cattrs version: 23.2.3
- Python version: 3.9
- Operating System: macOS
Description
I am wondering if cattrs provides a clever built-in mechanism reuse/inherit overrides from another class. It sounds like an easy problem at first, but I really can't figure out a smart way to achieve it.
What I Did
Consider the following code example and let's assume I have an entire class hierarchy below FruitBasket. In this example, I want that apples are always unstructured as bananas no matter where we are in the class hierarchy. In addition, the subclasses themselves might want to add their own custom overrides:
import cattrs
from attrs import define
from cattrs.gen import make_dict_unstructure_fn
@define
class FruitBasket:
apples: int
@define
class MixedBasket(FruitBasket):
oranges: int
converter = cattrs.Converter()
converter.register_unstructure_hook(
FruitBasket,
make_dict_unstructure_fn(
FruitBasket, converter, apples=cattrs.override(rename="bananas")
),
)
converter.register_unstructure_hook(
MixedBasket,
make_dict_unstructure_fn(
MixedBasket, converter, oranges=cattrs.override(rename="peaches")
),
)
print(converter.unstructure(MixedBasket(apples=5, oranges=3)))
So the goal is that the latter gives a {'bananas': 5, 'peaches': 3}, which I can of course achieve by adding the override again to the second hook. However, my question is if there is an automatic way to do so, which doesn't require me to keep track of the overrides while going to down the hierarchy and register them again and again.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 165
- Avg merge
- 3d 7m
- Merged PRs (30d)
- 6
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing 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 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
python-attrs/cattrs#743 · 2 comments ·
Maintainers usually reply within 1 day
All issues in python-attrs/cattrs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 91/100
TencentCloud/Octop#1577 · 1 comment ·
Maintainers usually reply within 1 day
-
bug frontend
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
PedestrianDynamics/pyFDS-Evac#552 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
resend/resend-skills#144 ·
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 68/100