Skip to content

Emit json schema defaults for dataclass and attrs default_factory fields - #1222

Merged
sobolevn merged 2 commits into
msgspec:mainfrom
Rodrigo-Palma:fix-dataclass-schema-factory-default
Oct 13, 2026
Merged

sobolevn merged 2 commits into
msgspec:mainfrom
Rodrigo-Palma:fix-dataclass-schema-factory-default

Conversation

@Rodrigo-Palma

Copy link
Copy Markdown
Contributor

msgspec.json.schema sets "default" for a Struct field with a list/dict/set/bytearray default_factory (#1183), but not for the same field on a dataclass or attrs class:

@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.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sobolevn
sobolevn added this pull request to the merge queue Oct 13, 2026
Merged via the queue into msgspec:main with commit b774b08 Oct 13, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants