Conversation
This adds support for keyword-only parameters in Structs, via a
`kw_only` configuration option. This option has the same semantics and
behavior as the `kw_only` parameter for dataclasses. If true, all
parameters specified on the respective class (*not* base classes) are
keyword-only.
This commit also changes the field-ordering algorithm used by msgspec to
match the behavior of dataclasses. This is a *breaking change*, but
helps maintain compatibility with the larger Python ecosystem (it also
makes `pyright` type check fields with the proper order).
The main way this breaking change may affect users is by causing struct
definitions with required parameters defined after optional ones to fail
at import time.
For example, the following definition is now invalid and will error at
import time.
```python
class MyStruct(Struct):
x: int = 1 # an optional parameter
y: int # a required parameter
```
To resolve this issue, you can either:
- reorder the fields so `x` is after `y`
- add a default value to `y`
- Specify `kw_only=True` in the struct definition. Keyword-only fields
may mix required and optional fields in any order, but impose
limitations on the caller.
It is unfortunate that a breaking change was needed here, but the
compatibility with `dataclasses` (and `pyright`) feels worth it. Better
break things now than later.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds support for keyword-only parameters in Structs, via a
kw_onlyconfiguration option. This option has the same semantics and behavior as thekw_onlyparameter for dataclasses. If true, all parameters specified on the respective class (not base classes) are keyword-only.This commit also changes the field-ordering algorithm used by msgspec to match the behavior of dataclasses. This is a Breaking Change, but helps maintain compatibility with the larger Python ecosystem (it also makes
pyrighttype check fields with the proper order in the presence of subclasses).The main way this breaking change may affect users is by causing struct definitions with required parameters defined after optional ones to fail at import time.
For example, the following definition is now invalid and will error at import time.
To be clear, dataclasses & attrs have this same restriction - only msgspec lacked this restriction before, but now has it for compatibility with dev tools like pyright
To resolve this issue, you can either:
xis afteryykw_only=Truein the struct definition. Keyword-only fields may mix required and optional fields in any order, but impose limitations on the caller.It is unfortunate that a breaking change was needed here, but the compatibility with
dataclasses(andpyright) feels worth it. Better break things now than later.