Skip to content

Inheritable kw_only for Struct (e.g., kw_only_default=True) #841

Closed

Description

@nightsailer

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!

Activity

  1. changed the title [-]Inheritable `kw_only` for `Struct` (e.g., `kw_always=True`)[/-] [+]Inheritable `kw_only` for `Struct` (e.g., `kw_only_default=True`)[/+] on Apr 28, 2025
  2. kent-foodini commented on Jul 3, 2025

    @kent-foodini

    I'd love that option as well. Great library - thanks for the great project.

  3. ofek commented on Oct 25, 2025

    @ofek
    Member

    Resolved by #890

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions