Repository navigation
Fix json schema defaults for set / bytearray default_factory fields - #1183
Conversation
Siyet
left a comment
There was a problem hiding this comment.
Thanks for the quick fix. The change looks right to me.
Two things before this can be merged.
The format check fails on the new line, so CI is red and the test jobs were skipped entirely. Pls run just hooks and push the reformatted version.
The change also needs a regression test. There is currently no coverage for set or bytearray factory defaults in tests/unit/test_schema.py, so a future regression would go unnoticed. A test parametrized over list, dict, set and bytearray asserting the emitted default and calling json.dumps on the resulting schema would cover it, with list and dict as controls.
Hello. Thanks for the review, addressed both changes in the two commits above. |
Siyet
left a comment
There was a problem hiding this comment.
Thx for the fix. Both points are addressed.
Worth noting the fix covers more than the test shows: a bare tags: set[str] = set() becomes factory internally, so it produced the same unusable value before this change.
Should I update the test to cover this edge-case as well before the PR can get merged? |
|
No need, the existing test already covers it. A bare Good to go from my side |
Oh, I originally misunderstood your previous comment. I see now, thanks. |
0.22.0 has not been published yet, and the PyPI trusted publisher needed for the upload is now configured (msgspec#1134). Entries for changes merged after 2026-08-11 are under Unreleased, and the GitHub release form is pre-filled from the first `Version` section only (`scripts/release_github.py`), so those entries would be left out. This moves them into the 0.22.0 section, groups the section by kind (features and platforms, fixes, type stubs, docs), merges the two "Many type stub improvements and fixes" entries into one, adds an entry for msgspec#1183 and sets the release date. No other entry text was changed. Co-authored-by: Tseluiko Aleksandr <[email protected]>
…lds (msgspec#1222) `msgspec.json.schema` sets `"default"` for a Struct field with a `list`/`dict`/`set`/`bytearray` `default_factory` (msgspec#1183), but not for the same field on a dataclass or attrs class: ```python @dataclasses.dataclass class D: x: list[int] = dataclasses.field(default_factory=list) msgspec.json.schema(D)["$defs"]["D"]["properties"]["x"] # {'type': 'array', 'items': {'type': 'integer'}} ``` This applies the Struct rule to the dataclass/attrs/TypedDict branch. `test_dataclass_or_attrs` now expects `"default": {}` for its `dict` factory field. Co-authored-by: sobolevn <[email protected]>
Fixes #1182
Now values are json-serializable:
[]/""instead of rawset()/bytearray().