Skip to content

omit_defaults supports specifying tuple/frozenset default values via default_factory#653

Merged
jcrist merged 1 commit intomainfrom
omit-defaults-immutable-factories
Mar 10, 2024
Merged

omit_defaults supports specifying tuple/frozenset default values via default_factory#653
jcrist merged 1 commit intomainfrom
omit-defaults-immutable-factories

Conversation

@jcrist
Copy link
Copy Markdown
Owner

@jcrist jcrist commented Mar 10, 2024

Previously omit_defaults would only detect tuple/frozenset fields containing default values if the default was specified by value.

class Ex(msgspec.Struct, omit_defaults=True):
    x: tuple = ()                                    # omit_defaults would work properly with this spelling
    y: tuple = msgspec.field(default=())             # or with this spelling
    z: tuple = msgspec.field(default_factory=tuple)  # but not with this spelling

We now also support if these types are specified via default_factory (all 3 cases above now properly work with omit_defaults=True).

Note that since tuple and frozenset values are immutable there's no real reason to prefer default_factory for defaults of these types.

Fixes #652.

Previously `omit_defaults` would only detect `tuple`/`frozenset` fields
containing default values if the default was specified by value. We now
also support if these types are specified via `default_factory`.

Note that since `tuple` and `frozenset` values are immutable there's no
real reason to prefer `default_factory` for defaults of these types.
@jcrist jcrist merged commit 13a06dd into main Mar 10, 2024
@jcrist jcrist deleted the omit-defaults-immutable-factories branch March 10, 2024 07:53
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.

omit_defaults does not omit tuples and frozensets

1 participant