jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.3k stars 67 forks source link

Make `kw_only` and `rename` part of `StructConfig` #574

Closed wikiped closed 11 months ago

wikiped commented 11 months ago

Description

Thank you for this library.

It is somewhat unexpected (without thorough analysis of the docs) to see that the following:

import msgspec

class Base(msgspec.Struct, kw_only=True):
    ...

class Parent(Base):
    a: str | None = None
    b: str

Throws an error:

    class Parent(Base):
E   TypeError: Required field 'b' cannot follow optional fields. Either reorder the struct fields, or set `kw_only=True` in the struct definition.

Not that it is major issue, but given that kw_only and rename are part of class constructor (as well as all StructConfig fields) the expectation is/was that they will also be carried along (inherited by subclasses).

And apparently rename is getting inherited by the subclasses of Base (unlike the kw_only field), despite the fact that it is not part of StructConfig.

So would it make sense to make both fields part of StructConfig and therefore be explicit about their inheritance behaviour like other StructConfig fields?

wikiped commented 11 months ago

Apparently the docs are saying that kw_only is intentionally not-inheritable, so RTFM is the way to go...