Comments (3)
Another thing I tried was:
_cattr_converter.register_unstructure_hook(
Toy,
lambda o: {"_type": type(o).__name__, **_cattr_converter.unstructure(o)},
)
but that caused an infinite loop.
from cattrs.
Hi,
sorry for the delayed response, I was on vacation.
These strategies are somewhat stateful since a lot of them do some of the work at configuration time, rather than structure/unstructure time. This means the order in which they are applied matters.
I tried switching the order of the hooks here:
c = cattr.Converter()
include_subclasses(Toy, c, union_strategy=configure_tagged_union)
include_subclasses(ToyBox, c, union_strategy=configure_tagged_union)
Doing this, the output now is:
{'_type': 'SmallToyBox',
'contents': [{'_type': 'Lego', 'name': 'space'},
{'_type': 'Lego', 'name': 'house'},
{'_type': 'Train', 'name': 'stream'},
{'_type': 'Train', 'name': 'electric'}],
'material': 'wood'}
That that look like what you were going for?
from cattrs.
Yes! Thank you!
I hope you had a good holiday :-)
Can you explain why/how it works by registering Toy
prior to ToyBox
? It seems a little magical and I'd love to know how that works in case I encounter a simlar issue in the future.
Also, how might I include the hook = make_dict_unstructure_fn(Toy, c, _cattrs_omit_if_default=True)
logic in addition to the subclass logic? I can put together some example code of what I'm looking for if you want. Let me know if you'd prefer that to be a separate issue too.
Thanks again, Tin!
from cattrs.
Related Issues (20)
- Dataclass structuring fails with misleading error message.
- Default field conversion conflicts with user-defined field converter function HOT 3
- [macOS] Error when trying to run tests: `ImportError: cannot import name 'CodecOptions' from 'bson'` HOT 2
- How to hook into structuring of a simple dict? HOT 6
- Incorrect dispatch in Python 3.12 HOT 4
- Why does structuring of datetime in attrs class need a structure hook? HOT 1
- `prefer_attrib_converters` doesn't work when the field is aliased HOT 2
- Register structure hook only for optional types HOT 2
- TYPE_CHECKING and init=False HOT 5
- Problem with tagged union example HOT 3
- Unstructuring generics HOT 5
- Inheriting overrides HOT 3
- 23.2.3: test suite fails with pytest 8.2.1 HOT 5
- Register multiple hooks for a class HOT 3
- Nested class structure HOT 4
- Derived class disambiguating fails, but only sometimes. HOT 4
- Fields with init=False Don't Get Serialized HOT 3
- Calling `include_subclasses()` prevents later structure hooks from working HOT 1
- Python 3.13.0b2: 4 tests failuires HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cattrs.