Subclass disambiguation for nested structures.
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with c.unstructure after configuring include_subclasses for ToyBox and Toy with configure_tagged_union, and reproduce the nested Toy list case. Trace how the tagged-union hook is composed with make_dict_unstructure_fn(Toy, c, _cattrs_omit_if_default=True); done means nested subtype data includes _type while the existing top-level tag and requested hook behavior remain intact.
Written by the indexing model from the issue text.
Description
- cattrs version: 23.2.3
- Python version: 3.10.12
- Operating System: Ubuntu-22.04
I'm using attrs 23.2.0, if that matters.
Description
Hi! I am having trouble adding the special _type key to the unstructured data to inform the structurer how to deal with subtypes. It seems to work for the top level structure, but not a second level structure that also has subtypes.
Any help would be appreciated :-)
What I Did
This is some example code:
from enum import Enum
from attrs import define, field
import cattr
from cattrs.strategies import configure_tagged_union, include_subclasses
from typing import ClassVar, List
class Material(Enum):
WOOD = "wood"
PLASTIC = "plastic"
@define
class Toy:
material: ClassVar[Material]
name: str
@define
class Lego(Toy):
material = Material.PLASTIC
@define
class Train(Toy):
material = Material.WOOD
@define
class ToyBox:
size: ClassVar[int]
material: Material
contents: List[Toy] = field(factory=list)
def add_toy(self, toy):
if len(self.contents) >= self.size:
raise ValueError("ToyBox is full")
self.contents.append(toy)
@define
class SmallToyBox(ToyBox):
size = 5
@define
class LargeToyBox(ToyBox):
size = 10
c = cattr.Converter()
include_subclasses(ToyBox, c, union_strategy=configure_tagged_union)
include_subclasses(Toy, c, union_strategy=configure_tagged_union)
box = SmallToyBox(material=Material.WOOD)
box.add_toy(Lego("space"))
box.add_toy(Lego("house"))
box.add_toy(Train("stream"))
box.add_toy(Train("electric"))
unstructured = c.unstructure(box)
import pprint
pprint.pprint(unstructured)
And I get the following:
{'_type': 'SmallToyBox',
'contents': [{'name': 'space'},
{'name': 'house'},
{'name': 'stream'},
{'name': 'electric'}],
'material': 'wood'}
which, as you can see, is not including the _type argument in the nested data structure Toy that needs to deal with subtypes.
A possibly related issue is that I want to be able to string together multiple hooks together. Specifically I want to also include this:
hook = make_dict_unstructure_fn(Toy, c, _cattrs_omit_if_default=True) in addition to supporting subclasses.
How can I do that?
Thanks!
- 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 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
All issues in python-attrs/cattrs
Similar issues
-
triage/confirmed
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
Difficulty 1/5 Under an hour Newbie friendliness 92/100
NousResearch/hermes-agent#118866 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
apache/cloudstack#14222 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100