Closed scottee closed 9 months ago
Howdy!
You're on the right track but you're applying the strategy to the wrong class. :)
If you change it to this instead:
include_subclasses(Base, converter)
the output changes to:
TestConfig(c1=Child1(a='foo', b='blah', c=[]), c2=Child1(a='yada', b='wonka', c=['c1']), c3=Child2(d='pkg.module.func'))
What you're actually doing by applying the strategy is making the rules for Base
be equivalent to the rules for Union[Child2, Child1, Base]
. The fact that TestConfig
is using Base
is coincidental. For example: this also works now:
converter.structure([{"a": 1, "b": 1}], list[Base])
# [Child1(a='1', b='1', c=[])]
Hope that helps!
Description
I'm trying to have attrs of a class be typed to a base class, and have the config structure them into child classes. However, the attrs always come out as the base class. This seems straightforward. Am I doing something wrong?
What I Did
Below is the python I used:
If you run this cattrs parses to the base class: