msgspec
msgspec copied to clipboard
Inheritable `kw_only` for `Struct` (e.g., `kw_only_default=True`)
Description
Description
Currently, kw_only=True on msgspec.Struct does not inherit to subclasses.
I'm building a pydantic-like wrapper over msgspec, and I want users to define models without repeating kw_only=True:
class BaseModel(Struct, kw_only=True):
...
class User(BaseModel):
age: int = Field(...)
name: str
Today, users must write:
class User(BaseModel, kw_only=True):
age: int = Field(...)
name: str
which hurts usability.
⸻
Proposal
Add an option like kw_always=True that enables kw_only automatically for subclasses, without needing to re-specify it.
⸻
Why it’s needed • Easier migration for pydantic users • Cleaner API • Avoids complex workarounds (like dynamically generating inner structs)
⸻
Current workaround
I tried subclassing StructMeta but failed. My only working solution is generating a dynamic InnerStruct at runtime, which complicates type handling, nested structures, and serialization.
⸻
Impact
Supporting kw_only_default =True would significantly improve framework and library development on top of msgspec.
Thanks for considering!