Skip to content

to_builtins function converts a Struct object without default values #748

Closed as not planned

Description

@luwqz1
import msgspec


class User(msgspec.Struct, omit_defaults=True):
    id: int
    name: str
    city: str = msgspec.field(default="Aurora")


user = User(id=1, name="John")
print(user)  # User(id=1, name='John', city='Aurora')
print(msgspec.to_builtins(user))  # {'id': 1, 'name': 'John'}

user2 = msgspec.json.decode(b'{"id":2,"name":"Alex"}', type=User)
print(user2)  # User(id=2, name='Alex', city='Aurora')
print(msgspec.to_builtins(user2))  # {'id': 2, 'name': 'Alex'}

Activity

  1. luwqz1 commented on Oct 20, 2024

    @luwqz1
    ContributorAuthor

    Probably it was intended because it makes some sense. I will close this issue, but if the maintainer decides that it is a bug, I will reopen this issue for discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions