Comments (7)
I don't disagree - big fan of immutable data structures, and the explicit > implicit is true for sure. However - that's the json i got! msgspec
is so much faster than pydantic that i gotta ask, at least.
from msgspec.
One thing you could do now is to have a "private" field with optional list and a property that isn't optional.
from msgspec.
That's actually the direction i went, but the setters and append
was all a little bit beyond the level of hackery I was willing to commit. If it was a read-only field, the private optional field and non-optional @property
would work great. I was going to try to write some sort of helper class that would act like a list and support append, but eh. A bridge too far. Thanks for taking the time to reply though, I appreciate it!
from msgspec.
no prob. I am a big "immutables" fan, so wasn't thinking about the need for mutable fields.
another idea for you then - you can have phantom types, just for deserialization, which will have optional fields and post_init logic. Then you'll just convert from them to the final type to be used in the code. This can be easily automated, either via code generation (explicit approach), or you could even play around with generating the phantom types from the "true" types on the fly
on the topic, I think all the implicit logic, like treating nulls as missing value, is more headache then gain in the long run. pydantic was (or maybe still is) is so confusing with None/Optional handling. As PEP20 says - Explicit is better than implicit.
from msgspec.
One thing you could do now is to have a "private" field with optional list and a property that isn't optional.
Something like this right?
from msgspec import Struct
class Example(Struct, frozen=True):
raw: str | None = ""
@property
def final(self) -> int:
return self.raw or ""
Yeah I guess users of this class would find it surprising to set raw
, but use final
from msgspec.
another idea for you then - you can have phantom types, just for deserialization, which will have optional fields and post_init logic. Then you'll just convert from them to the final type to be used in the code.
@mishamsk do you mean like this? I might have misunderstood
from msgspec import Struct
class Example(Struct, frozen=True):
x: str
class DecodableExample(Struct, frozen=True):
x: str | None = ""
def finalize(self) -> Example:
return Example(x=self.x or "")
I think this is OK, but the ergonomics aren't great when this type is nested in a deep structure. I think you would end up copying every class above it, like this:
class Parent(Struct, frozen=True):
ex: tuple[Example, ...]
class DecodableParent(Struct, frozen=True):
ex: tuple[DecodableExample, ...]
def finalize(self) -> Parent:
return Parent(ex=tuple(e.finalize() for e in self.ex))
import msgspec
msgspec.json.decode(b'{"ex":[{"x":null}]}', type=DecodableParent).finalize()
# Parent(ex=(Example(x=''),))
I often want to coerce a None
to the default value in decoding - I prefer not to use union types with None as I find it leads to a proliferation of None checking code.
from msgspec.
@jack-mcivor yes, that's what I roughly meant. Agree that it is not ideal, and for deeply nested models almost beats the purpose of msgspec...
from msgspec.
Related Issues (20)
- Segmentation fault when using msgspec.Meta HOT 2
- msgspec.Struct as dict keys and Dict[TypeVar, TypeVar] support
- Support pattern for str collection types HOT 1
- Omitting defaults does not work with array-like structs HOT 1
- Collect all ValidationErrors instead of failing fast HOT 2
- Unable to handle a self-referencing union of primitive types HOT 2
- JSON and msgpack encoding fails to skip UNSET values in array_like Struct HOT 1
- Creating a msgspec.Struct abstract class
- nested tagged unions enumeration HOT 3
- Encoding exceptions - segfault HOT 1
- Ignore Pydantic dataclasses
- "Buffer overflow detected" SIGABRT in json_encode_long when compiled with -D_FORTIFY_SOURCE=2 -O3 on x86_64 with gcc 13.2.0 HOT 1
- Creating a Struct From an Unknown JSON Schema HOT 3
- Built-in support for numpy data-types
- Best way to encode large JSON plain text into msgpack struct and filter entries HOT 4
- msgpack.Encoder flag to skip any coercion of data types not native to the encoding HOT 1
- Allow to customize tag field position in array_like struct encoding
- Inspection errors on valid union syntax for Python < 3.10 HOT 5
- How to use dict in msgspec.Struct HOT 1
- Do you mind me package this into nixpkgs?
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.