Comments (5)
However, this has the downside that when I am programmatically constructing an instance of this struct, it will no longer fail fast if I've forgotten one of the arguments. I want to be sure that as new fields are added to this struct, I'm not forgetting to add them in my code in other places.
That's an interesting use case. What about using a custom classmethod
as a constructor in the places where you always want to explicitly set each field? Something like:
from __future__ import annotations
import msgspec
class Demo(msgspec.Struct):
field_one: int | None = None
field_two: int | None = None
field_three: int | None = None
@classmethod
def new(cls, *, field_one: int, field_two: int, field_three: int) -> Demo:
return cls(field_one, field_two, field_three)
# elsewhere in your code...
demo = Demo.new(field_one=1, field_two=2, field_three=3)
print(demo)
#> Demo(field_one=1, field_two=2, field_three=3)
# any locations where you forgot to add `field_three` would then error
Demo.new(field_one=1, field_two=2)
When adding a new field to the struct you'd need to remember to also add it to the classmethod, but the close proximity of the two should help you remember. Heck you could even enforce these align with a check at import time via a __init_subclass__
hook if you wanted to (I'm happy to provide an example if this interests you). IMO this is a nice low-tech solution to a code hygiene problem.
Add an
omit_none
option
This also might make sense, but would obviously take more work on my end.
As a meta conversation, I'm now wondering if options like this or omit_defaults
should be set per-call to encode
(or on the Encoder
once) rather than on the type. The logic being that sometimes you might want to encode the full model and sometimes you might want a more compact representation - but these attributes are more specific to the call site than to the type being represented?
from msgspec.
@jcrist actually, I already have the class method you speak of, and by "other places in my code" I was referring to this one class method 😆
The __init_subclass__
hook you mention would be interesting indeed, would love to see that example! Provided it adds minimal overhead (I'm instantiating millions of these quite often) I think that could work. All I need is a basic fail-fast sanity check to make sure I'm populating all fields (previously accomplished very smoothly simply by not having defaults set).
I agree with your meta comment... it seems like that would provide more flexibility. Although, for my own use-cases (currently) I personally do not need multiple flavors of serialization. I could see how it would be annoying though if I at some point in the future needed to serialize two different ways.
One possible downside I could see is that to generate the "schema" correctly you'd need to also supply the encoder you use to serialize... as the schema might also depend on encoder arguments. But on the other hand, maybe that is not a downside at all. I think something similar happened in pydantic recently (as far as needing additional arguments to generate schema properly), because FastAPI now generates potentially two different schemas... one "deserialization" schema, and one "serialization" schema... as what is required vs. not changes depending on whether reading or writing.
from msgspec.
As a meta conversation, I'm now wondering if options like this or omit_defaults should be set per-call to encode (or on the Encoder once) rather than on the type
as in #549 (my reply here) - I'd say ideal is for the class-based parameter to define a default, but also provide a way to override it in the encoder. But if this is too much work, encoder seems like the best option as it has no downsides but gives finer control.
from msgspec.
Related Issues (20)
- Inherit __init_subclass__ args to subclass HOT 1
- ormsgpack benchmark comparison HOT 1
- encode sort_keys argument HOT 16
- Allow more than one str-like type in unions HOT 5
- decode's strict=False does not cast floats to ints HOT 3
- Typed encoding HOT 1
- Validation on serialization HOT 5
- Make msgspec more aware of large data and other serialization protocols
- Recommendation for Efficiently Decoding BSON from PyMongo with msgspec
- leading underscores and `"camel"` rename strategy HOT 2
- Constraint check for typing.Optional field HOT 1
- Automatically support `functools.cached_property` without requiring setting `dict=True` HOT 2
- UnboundLocalError for `new_scope` HOT 3
- Sign release tags when making future releases? HOT 4
- How can I do data normalization on a frozen instance in `__post_init__`? HOT 1
- Cannot convert with `from_attributes` when using a rename convention HOT 3
- Cannot set `gc=False` on Generic structs? HOT 5
- Support types.MappingProxyType HOT 3
- Consider making `DecodeError` and `ValidationError` inherit from `ValueError` HOT 1
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 msgspec.