You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
asdict gives you {"bootstrap_servers": "some.server"} and not, as expected {"bootstrapServers": "some.server"}
Am I missing something obvious here to achieve this behaviour? It's a bit "just have to know" and not as expected usage if you need the struct as a dict instance for whatever reason (such internal library passing config etc). I mean, dumping it to bytes and then json.load kinda works but its a bit of a hack 🤷♂
If the default values are known on the decoding end (making serializing them redundant), it may be beneficial and desired to omit default values from the encoded message. This can be done by configuring omit_defaults=True as part of the Struct definition
This means that omit_defaults only works when you're encoding/decoding, and is not affected by asdict.
From the API, asdict doesn't seem to discard any field when omit_defaults is set. I think it's the intended behavior since it's a way to minimize the data to be transmitted in case a configuration parameter is shared between encoder and decoder.
If the default values are known on the decoding end (making serializing them redundant), it may be beneficial and desired to omit default values from the encoded message. This can be done by configuring omit_defaults=True as part of the Struct definition
This means that omit_defaults only works when you're encoding/decoding, and is not affected by asdict.
import msgspec
from msgspec import field, Struct
class Example(Struct, omit_defaults=True):
bootstrap_servers: str = field(name="bootstrap.servers")
retries: int = field(name="retries", default=5)
From the API, asdict doesn't seem to discard any field when omit_defaults is set. I think it's the intended behavior since it's a way to minimize the data to be transmitted in case a configuration parameter is shared between encoder and decoder.
Yup, it makes sense, asdict is a dict representation of the Python msgspec.Struct - not a dict representation of the message it represents (i.e what is to be encoded). Would be a neat feature/option though :)
From what I can see, msgspec seem to support typing.NotRequired, which is usually used in typing.TypedDict to provide optional fields for a dictionary. I'm currently trying to run your example as follows:
But I get the feeling the intended usage of NotRequired is only to provide support for serializing a field which is a typing.TypedDict with required/not required fields.
From what I can see, msgspec seem to support typing.NotRequired, which is usually used in typing.TypedDict to provide optional fields for a dictionary. I'm currently trying to run your example as follows:
from future import annotations
import msgspec
from typing import NotRequired
from msgspec import field, Struct
But I get the feeling the intended usage of NotRequired is only to provide support for serializing a field which is a typing.TypedDict with required/not required fields.
If you run this, it will be more clear what I mean:
Yeah, I get the picture. I'm guessing a possible feature would be to replicate the behavior of omit_defaults and pass it as an optional parameter, but I don't really see a use case for this to be honest.
Yeah, I get the picture. I'm guessing a possible feature would be to replicate the behavior of omit_defaults and pass it as an optional parameter, but I don't really see a use case for this to be honest.
Nvm, I think I found it; msgspec.to_builtins does exactly what I needed!
Activity
jacopoabramo commentedon Feb 18, 2025
From the documentation:
This means that
omit_defaultsonly works when you're encoding/decoding, and is not affected byasdict.From the API,
asdictdoesn't seem to discard any field whenomit_defaultsis set. I think it's the intended behavior since it's a way to minimize the data to be transmitted in case a configuration parameter is shared between encoder and decoder.idatsy commentedon Feb 18, 2025
Yup, it makes sense,
asdictis adictrepresentation of the Pythonmsgspec.Struct- not adictrepresentation of the message it represents (i.e what is to be encoded). Would be a neat feature/option though :)jacopoabramo commentedon Feb 18, 2025
From what I can see,
msgspecseem to supporttyping.NotRequired, which is usually used intyping.TypedDictto provide optional fields for a dictionary. I'm currently trying to run your example as follows:But I'm getting:
The only thing I can see for support of
NotRequiredis this entry in the changelog of version 0.15.1:But I get the feeling the intended usage of
NotRequiredis only to provide support for serializing a field which is atyping.TypedDictwith required/not required fields.idatsy commentedon Feb 18, 2025
If you run this, it will be more clear what I mean:
jacopoabramo commentedon Feb 18, 2025
Yeah, I get the picture. I'm guessing a possible feature would be to replicate the behavior of
omit_defaultsand pass it as an optional parameter, but I don't really see a use case for this to be honest.idatsy commentedon Feb 18, 2025
Nvm, I think I found it;
msgspec.to_builtinsdoes exactly what I needed!