File: structs.rst

package info (click to toggle)
python-msgspec 0.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,356 kB
  • sloc: javascript: 23,944; ansic: 20,540; python: 20,465; makefile: 29; sh: 19
file content (1015 lines) | stat: -rw-r--r-- 34,501 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
Structs
=======

Structs are the preferred way of defining structured data types in ``msgspec``.
They're written in C and are quite speedy and lightweight (:doc:`measurably
faster <benchmarks>` to create/compare/encode/decode than similar options like
dataclasses_, attrs_, or pydantic_). They're great for representing structured
data both for serialization and for use in an application.

Structs are defined by subclassing from `msgspec.Struct` and annotating the
types of individual fields. Default values can also be provided for any
optional arguments. Here we define a struct representing a user, with one
required field and two optional fields.

.. code-block:: python

    >>> import msgspec

    >>> from typing import Set, Optional

    >>> class User(msgspec.Struct):
    ...     """A struct describing a user"""
    ...     name : str
    ...     email : Optional[str] = None
    ...     groups : Set[str] = set()

- ``name`` is a *required* field expecting a `str`

- ``email`` is an *optional* field expecting a `str` or `None`, defaulting to
  `None` if no value is provided.

- ``groups`` is an *optional* field expecting a `set` of `str`. If no value is
  provided, it defaults to the empty set.

Struct types automatically generate a few methods based on the provided type
annotations:

- ``__init__``
- ``__repr__``
- ``__copy__``
- ``__replace__``
- ``__eq__`` & ``__ne__``
- ``__match_args__`` (for Python 3.10+'s `pattern matching`_)
- ``__rich_repr__`` (for pretty printing support with rich_)

.. code-block:: python

    >>> alice = User("alice", groups={"admin", "engineering"})

    >>> alice
    User(name='alice', email=None, groups={'admin', 'engineering'})

    >>> bob = User("bob", email="bob@company.com")

    >>> bob
    User(name='bob', email='bob@company.com', groups=set())

    >>> alice.name
    "alice"

    >>> bob.groups
    set()

    >>> alice == bob
    False

    >>> alice == User("alice", groups={"admin", "engineering"})
    True

Note that it is forbidden to override ``__init__``/``__new__`` in a struct
definition, but other methods can be overridden or added as needed. If you need
to customize the generated ``__init__``, see :ref:`struct-post-init`.

The struct fields are available via the ``__struct_fields__`` attribute (a
tuple of the fields in argument order ) if you need them. Here we add a method
for converting a struct to a dict.

.. code-block:: python

    >>> class Point(msgspec.Struct):
    ...     """A point in 2D space"""
    ...     x : float
    ...     y : float
    ...
    ...     def to_dict(self):
    ...         return {f: getattr(self, f) for f in self.__struct_fields__}
    ...

    >>> p = Point(1.0, 2.0)

    >>> p.to_dict()
    {"x": 1.0, "y": 2.0}


Default Values
--------------

Struct fields may be given default values, which are used if no value is
provided to ``__init__``, or when decoding a message. Default values are
configured as part of a Struct definition by assigning them after a field's
type annotation.

.. code-block:: python

    >>> from msgspec import Struct, field

    >>> import uuid

    >>> class Example(Struct):
    ...     a: int = 1
    ...     b: uuid.UUID = field(default_factory=uuid.uuid4)
    ...     c: list[int] = []

    >>> Example()
    Example(a=1, b=UUID('f63219d5-e9ca-4ae8-afd0-cba30e84222d'), c=[])

    >>> Example(a=2)
    Example(a=2, b=UUID('319a6c0f-2841-4439-8bc8-2c1daf7d77a2'), c=[])

    >>> Example().c is Example().c  # new list instance used each time
    False

Default values may be one of 3 kinds:

- A "static" default value. Here the same default value is used for all
  instances. These are specified by assigning the default value itself as part
  of the field definition (as in ``a`` above). Most default values will be of
  this variety.

- A "dynamic" default value. Here a new default value is used for every
  instance. These are specified by passing a 0-argument callable to the
  ``default_factory`` argument of `msgspec.field` (as in ``b`` above). This
  function will be called as needed to create a new default value per instance.
  These are mainly useful for occasions where you need dynamic defaults, or
  when a default value is a mutable object that you don't want to share between
  all instances of the struct (a `common gotcha
  <https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments>`_
  in Python). Note that since the ``default_factory`` callables take no
  arguments, you might need to make use of a lambda_ or `functools.partial` to
  forward any additional parameters needed to the default factory.

- Builtin *empty* mutable collections (``[]``, ``{}``, ``set()``, and
  ``bytearray()``) may be used as default values (as in ``c`` above). Since
  defaults of these types are so common, these are "syntactic sugar" for
  specifying the corresponding ``default_factory`` (to avoid accidental sharing
  of mutable values). A default of ``[]`` is identical to a default of
  ``field(default_factory=list)``, with a new list instance used each time.
  Specifying a non-empty mutable collection (e.g. ``[1, 2, 3]``) as a default
  value will cause the struct definition to error (you should manually define a
  ``default_factory`` in this case).

.. _struct-post-init:

Post-Init Processing
--------------------

If a struct type defines a ``__post_init__(self)`` method, this will be called
at the end of the generated ``__init__`` method. It has the same semantics as the
``dataclasses`` method `of the same name
<https://docs.python.org/3/library/dataclasses.html#post-init-processing>`__.
This method may be useful for adding additional logic to the init (such as
custom validation).

In addition to in ``__init__``, the ``__post_init__`` hook is also called when:

- Decoding into a struct type (e.g. ``msgspec.json.decode(..., type=MyStruct)``)
- Converting into a struct type (e.g. ``msgspec.convert(..., type=MyStruct)``)

In these cases any `TypeError` or `ValueError` exceptions raised by this method
will be considered "user facing" and converted into a `msgspec.ValidationError`
with additional context. All other exceptions will be raised directly.

.. code-block:: python

    >>> import msgspec

    >>> class Interval(msgspec.Struct):
    ...     low: float
    ...     high: float
    ...
    ...     def __post_init__(self):
    ...         if self.low > self.high:
    ...             raise ValueError("`low` may not be greater than `high`")

    >>> Interval(1, 2)  # valid interval
    Interval(low=1, high=2)

    >>> Interval(2, 1)  # invalid interval
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 6, in __post_init__
    ValueError: `low` may not be greater than `high`

    >>> msgspec.json.decode(b'{"low": 2, "high": 1}', type=Interval)  # invalid interval from JSON
    Traceback (most recent call last):
      File "<stdin>", line 6, in __post_init__
    ValueError: `low` may not be greater than `high`

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    msgspec.ValidationError: `low` may not be greater than `high`

.. _struct-field-ordering:

Field Ordering
--------------

When defining a new struct type, fields are stored in the order they're defined
(keyword-only fields excluded, more on this later). This is nice for
readability since the generated ``__init__`` matches the field order.

.. code-block:: python

    class Example(msgspec.Struct):
        a: str
        b: int = 0

The generated ``__init__()`` for ``User`` looks like:

.. code-block:: python

    def __init__(self, a: str, b: int = 0):

One consequence of this is that you can't put fields without defaults after
fields with defaults, since the Python VM doesn't allow keyword arguments
before positional arguments. The following struct definition will error:

.. code-block:: python

   >>> class Invalid(msgspec.Struct):
   ...     a: str = ""
   ...     b: int  # oop, no default!
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: Required field 'b' cannot follow optional fields. Either reorder
   the struct fields, or set `kw_only=True` in the struct definition.

Thankfully the error message includes some solutions:

- Reorder the struct fields, putting all required fields before all optional
  fields.

- Set ``kw_only=True`` in the struct definition. This option makes all fields
  defined on the struct `keyword-only parameters`_.

Keyword-only parameters have no such restriction; required and optional
parameters can be mixed in any order.

.. code-block:: python

   >>> class Example(msgspec.Struct, kw_only=True):
   ...     a: str = ""
   ...     b: int  # this is fine with kw_only=True

   >>> Example(a="example", b=123)
   Example(a='example', b=123)

Note that the ``kw_only`` setting only affects fields defined on that class,
*not* those defined on base or subclasses. This means you can define
keyword-only parameters on a base class then add positional parameters in a
subclass. All keyword-only parameters are reordered to go after all positional
fields.

.. code-block:: python

   >>> class Base(msgspec.Struct, kw_only=True):
   ...     a: str = ""
   ...     b: int

   >>> class Subclass(Base):
   ...     c: float
   ...     d: bytes = b""

The generated ``__init__()`` for ``Subclass`` looks like:

.. code-block:: python

    def __init__(self, c: float, d: bytes = b"", * a: str, b: int = 0):

The field ordering rules for ``Struct`` types are identical to those for
`dataclasses`, see the `dataclasses docs <dataclasses>`_ for more information.

Class Variables
---------------

Like `dataclasses`, `msgspec.Struct` types will exclude any attribute
annotations wrapped in `typing.ClassVar` from their fields.

.. code-block:: python

   >>> import msgspec

   >>> from typing import ClassVar

   >>> class Example(msgspec.Struct):
   ...     x: int
   ...     a_class_variable: ClassVar[int] = 2

   >>> Example.a_class_variable
   2

   >>> Example(1)  # only `x` is counted as a field
   Example(x=1)

Note that if using `PEP 563`_ "postponed evaluation of annotations" (e.g.
``from __future__ import annotations``) only the following spellings will work:

- ``ClassVar`` or ``ClassVar[<type>]``
- ``typing.ClassVar`` or ``typing.ClassVar[<type>]``

Importing ``ClassVar`` or ``typing`` under an aliased name (e.g. ``import
typing as typ`` or ``from typing import ClassVar as CV``) will not be properly
detected.

Type Validation
---------------

Unlike some other libraries (e.g. pydantic_), the type annotations on a
`msgspec.Struct` class are not checked at runtime during normal use. Types are
only checked when *decoding* a serialized message when using a `typed decoder
<typed-deserialization>`.

.. code-block:: python

    >>> import msgspec

    >>> class Point(msgspec.Struct):
    ...     x: float
    ...     y: float

    >>> # Improper types in *your* code aren't checked at runtime
    ... Point(x=1, y="oops")
    Point(x=1, y='oops')

    >>> # Improper types when decoding *are* checked at runtime
    ... msgspec.json.decode(b'{"x": 1.0, "y": "oops"}', type=Point)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    msgspec.ValidationError: Expected `float`, got `str` - at `$.y`

This is intentional. Static type checkers like mypy_/pyright_ work well with
``msgspec``, and can be used to catch bugs without ever running your code. When
possible, static tools or unit tests should be preferred over adding expensive
runtime checks which slow down every ``__init__`` call.

The input(s) to your programs however cannot be checked statically, as they
aren't known until runtime. As such, ``msgspec`` does perform type validation
when decoding messages (provided an expected decode type is provided). This
validation is fast enough that it is *negligible in cost* - there is no added
performance benefit when not using it. In fact, in most cases it's faster to
decode a message into a type validated `msgspec.Struct` than into an untyped
`dict`.


Pattern Matching
----------------

If using Python 3.10+, `msgspec.Struct` types can be used in `pattern matching`_
blocks. Replicating an example from `PEP 636`_:

.. code-block:: python

    # NOTE: this example requires Python 3.10+
    >>> import msgspec

    >>> class Point(msgspec.Struct):
    ...     x: float
    ...     y: float

    >>> def where_is(point):
    ...     match point:
    ...         case Point(0, 0):
    ...             print("Origin")
    ...         case Point(0, y):
    ...             print(f"Y={y}")
    ...         case Point(x, 0):
    ...             print(f"X={x}")
    ...         case Point():
    ...             print("Somewhere else")
    ...         case _:
    ...             print("Not a point")

    >>> where_is(Point(0, 6))
    "Y=6"


Equality and Order
------------------

By default struct types define an ``__eq__`` method based on the type
definition. This enables support for equality comparisons. Additionally, you
may configure ``order=True`` to make a struct type *orderable* through
generation of ``__lt__``, ``__le__``, ``__gt__``, and ``__ge__`` methods. These
methods compare and order instances of a struct type the same as if they were
tuples of their field values (in definition order).

.. code-block:: python

    >>> class Point(msgspec.Struct, order=True):
    ...     x: float
    ...     y: float

    >>> Point(1, 2) == Point(1, 2)
    True

    >>> Point(1, 2) < Point(3, 4)
    True


In *rare* instances you may opt to disable generation of the ``__eq__`` method
by configuring ``eq=False``.  Equality checks will then fall back to *identity
comparisons*, where the only value a struct instance of that type will compare
equal to is itself.

.. code-block:: python

    >>> class Point(msgspec.Struct, eq=False):
    ...     x: float
    ...     y: float


    >>> p = Point(1, 2)

    >>> p == Point(1, 2)
    False

    >>> p == p  # identity comparison only
    True


Frozen Instances
----------------

A struct type can optionally be marked as "frozen" by specifying
``frozen=True``. This disables modifying attributes after initialization, and
adds a ``__hash__`` method to the class definition. Note that for the
``__hash__`` to work, all fields on the struct must also be hashable.

.. code-block:: python

    >>> class Point(msgspec.Struct, frozen=True):
    ...     """This struct is immutable & hashable"""
    ...     x: float
    ...     y: float
    ...

    >>> p = Point(1.0, 2.0)

    >>> {p: 1}  # frozen structs are hashable, and can be keys in dicts
    {Point(1.0, 2.0): 1}

    >>> p.x = 2.0  # frozen structs cannot be modified after creation
    Traceback (most recent call last):
        ...
    AttributeError: immutable type: 'Point'


.. _struct-tagged-unions:

Tagged Unions
-------------

By default a serialized struct only contains information on the *values*
present in the struct instance - no information is serialized noting which
struct type corresponds to the message. Instead, the user is expected to
know the type the message corresponds to, and pass that information
appropriately to the decoder.

.. code-block:: python

    >>> import msgspec

    >>> class Get(msgspec.Struct):
    ...     key: str

    >>> msg = msgspec.json.encode(Get("my key"))

    >>> msg  # No type information present in the message
    b'{"key":"my key"}'

    >>> msgspec.json.decode(msg, type=Get)
    Get(key='my key')

In most cases this works well - schemas are often simple and each value may
only correspond to at most one Struct type. However, sometimes you may have a
message (or a field in a message) that may contain one of a number of different
structured types. In this case we need some way to determine the type of the
message from the message itself!

``msgspec`` handles this through the use of `Tagged Unions`_. A new field (the
"tag field") is added to the serialized representation of all struct types in
the union. Each struct type associates a different value (the "tag") with this
field. When the decoder encounters a tagged union it decodes the tag first and
uses it to determine the type to use when decoding the rest of the object. This
process is efficient and makes determining the type of a serialized message
unambiguous.

The quickest way to enable tagged unions is to set ``tag=True`` when defining
every struct type in the union. In this case ``tag_field`` defaults to
``"type"``, and ``tag`` defaults to the struct class name (e.g. ``"Get"``).

.. code-block:: python

    >>> import msgspec

    >>> from typing import Union

    >>> # Pass in ``tag=True`` to tag the structs using the default configuration
    ... class Get(msgspec.Struct, tag=True):
    ...     key: str

    >>> class Put(msgspec.Struct, tag=True):
    ...     key: str
    ...     val: str

    >>> msg = msgspec.json.encode(Get("my key"))

    >>> msg  # "type" is the tag field, "Get" is the tag
    b'{"type":"Get","key":"my key"}'

    >>> # Create a decoder for decoding either Get or Put
    ... dec = msgspec.json.Decoder(Union[Get, Put])

    >>> # The tag value is used to determine the message type
    ... dec.decode(b'{"type": "Put", "key": "my key", "val": "my val"}')
    Put(key='my key', val='my val')

    >>> dec.decode(b'{"type": "Get", "key": "my key"}')
    Get(key='my key')

    >>> # A tagged union can also contain non-struct types.
    ... msgspec.json.decode(
    ...     b'123',
    ...     type=Union[Get, Put, int]
    ... )
    123

If you want to change this behavior to use a different tag field and/or value,
you can further configure things through the ``tag_field`` and ``tag`` kwargs.
A struct's tagging configuration is determined as follows.

- If ``tag`` and ``tag_field`` are ``None`` (the default), or ``tag=False``,
  then the struct is considered "untagged". The struct is serialized with only
  its standard fields, and cannot participate in ``Union`` types with other
  structs.

- If either ``tag`` or ``tag_field`` are non-None, then the struct is
  considered "tagged". The struct is serialized with an additional field (the
  ``tag_field``) mapping to its corresponding ``tag`` value. It can participate
  in ``Union`` types with other structs, provided they all share the same
  ``tag_field`` and have unique ``tag`` values.

- If a struct is tagged, ``tag_field`` defaults to ``"type"`` if not provided
  or inherited. This can be overridden by passing a tag field explicitly (e.g.
  ``tag_field="kind"``). Note that ``tag_field`` must not conflict with any
  other field names in the struct, and must be the same for all struct types in
  a union.

- If a struct is tagged, ``tag`` defaults to the class name (e.g. ``"Get"``) if
  not provided or inherited. This can be overridden by passing a string (or
  less commonly an integer) value explicitly (e.g. ``tag="get"``).  ``tag`` can
  also be passed a callable that takes the class qualname and returns a valid tag
  value (e.g. ``tag=str.lower``). Note that tag values must be unique for all
  struct types in a union, and ``str`` and ``int`` tag types cannot both be
  used within the same union.

If you like subclassing, both ``tag_field`` and ``tag`` are inheritable by
subclasses, allowing configuration to be set once on a base class and reused
for all struct types you wish to tag.

.. code-block:: python

    >>> import msgspec

    >>> from typing import Union

    >>> # Create a base class for tagged structs, where:
    ... # - the tag field is "op"
    ... # - the tag is the class name lowercased
    ... class TaggedBase(msgspec.Struct, tag_field="op", tag=str.lower):
    ...     pass

    >>> # Use the base class to pass on the configuration
    ... class Get(TaggedBase):
    ...     key: str

    >>> class Put(TaggedBase):
    ...     key: str
    ...     val: str

    >>> msg = msgspec.json.encode(Get("my key"))

    >>> msg  # "op" is the tag field, "get" is the tag
    b'{"op":"get","key":"my key"}'

    >>> # Create a decoder for decoding either Get or Put
    ... dec = msgspec.json.Decoder(Union[Get, Put])

    >>> # The tag value is used to determine the message type
    ... dec.decode(b'{"op": "put", "key": "my key", "val": "my val"}')
    Put(key='my key', val='my val')

    >>> dec.decode(b'{"op": "get", "key": "my key"}')
    Get(key='my key')


.. _omit_defaults:

Omitting Default Values
-----------------------

By default, ``msgspec`` encodes all fields in a Struct type, including optional
fields (those configured with a default value).

.. code-block:: python

    >>> import msgspec

    >>> class User(msgspec.Struct):
    ...     name : str
    ...     email : Optional[str] = None
    ...     groups : Set[str] = set()

    >>> alice = User("alice")

    >>> alice  # email & groups are using the default values
    User(name='alice', email=None, groups=set())

    >>> msgspec.json.encode(alice)  # default values are present in encoded message
    b'{"name":"alice","email":null,"groups":[]}'

If the default values are known on the decoding end (making serializing them
redundant), it may be beneficial and desired to omit default values from the
encoded message. This can be done by configuring ``omit_defaults=True`` as part
of the Struct definition:

.. code-block:: python

    >>> import msgspec

    >>> class User(msgspec.Struct, omit_defaults=True):
    ...     name : str
    ...     email : Optional[str] = None
    ...     groups : Set[str] = set()

    >>> alice = User("alice")

    >>> msgspec.json.encode(alice)  # default values are omitted
    b'{"name":"alice"}'

    >>> bob = User("bob", email="bob@company.com")

    >>> msgspec.json.encode(bob)
    b'{"name":"bob","email":"bob@company.com"}'

Omitting defaults reduces the size of the encoded message, and often also
improves encoding and decoding performance (since there's less work to do).

Note that detection of default values is optimized for performance; in certain
situations a default value may still be encoded. For the curious, the current
detection logic is as follows:

.. code-block:: python

    >>> def matches_default(value: Any, default: Any) -> bool:
    ...     """Whether a value matches the default for a field"""
    ...     if value is default:
    ...         return True
    ...     if type(value) != type(default):
    ...         return False
    ...     if type(value) in (list, set, dict) and (len(value) == len(default) == 0):
    ...         return True
    ...     return False


.. _forbid-unknown-fields:

Forbidding Unknown Fields
-------------------------

By default ``msgspec`` will skip unknown fields encountered when decoding into
``Struct`` types. This is normally desired, as it allows for
:doc:`schema-evolution` and more flexible decoding.

One downside is that typos may go unnoticed when decoding ``Struct`` types with
optional fields. For example:

.. code-block:: python

    >>> class Example(msgspec.Struct):
    ...     field_one: int
    ...     field_two: bool = False

    >>> msgspec.json.decode(
    ...     b'{"field_one": 1, "field_twoo": true}',  # oops, a typo
    ...     type=Example
    ... )
    Example(field_one=1, field_two=False)

In this example, the misspelled ``"field_twoo"`` is ignored since no field with
that name exists. Since ``field_two`` has a default value, the default is used
and no error is raised for a missing field.

To prevent typos like this, you can configure ``forbid_unknown_fields=True`` as
part of the struct definition. If this option is enabled, any unknown fields
encountered will result in an error.

.. code-block:: python

    >>> class Example(msgspec.Struct, forbid_unknown_fields=True):
    ...     field_one: int
    ...     field_two: bool = False

    >>> msgspec.json.decode(
    ...     b'{"field_one": 1, "field_twoo": true}',  # oops, a typo
    ...     type=Example
    ... )
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    msgspec.ValidationError: Object contains unknown field `field_twoo`


Renaming Fields
---------------

Sometimes you want the field name used in the encoded message to differ from
the name used by your Python code. Perhaps you want a ``camelCase`` naming
convention in your JSON messages, but to use ``snake_case`` field names in
Python.

``msgspec`` supports two places for configuring a field's name used for
encoding/decoding:

**On the field definition**

If you're only renaming a few fields, you might find configuring the new names
as part of the field definition to be the simplest option. To do this you can
use the ``name`` argument in `msgspec.field`. Any fields declared with this
option will use the new name for encoding/decoding.

.. code-block:: python

    >>> import msgspec

    >>> class Example(msgspec.Struct):
    ...     x: int
    ...     y: int
    ...     z: int = msgspec.field(name="field_z")  # renamed to "field_z"

    >>> # Python code uses the original field names
    ... ex = Example(x=1, y=2, z=3)

    >>> # Encoded messages use the renamed field names
    ... msgspec.json.encode(ex)
    b'{"x":1,"y":2,"field_z":3}'

    >>> # Decoding also uses the renamed field names
    ... msgspec.json.decode(b'{"x": 1, "y": 2, "field_z": 3}', type=Example)
    Example(x=1, y=2, z=3)

**On the struct definition**

If you're renaming lots of fields (especially if you're renaming them with a
naming convention like ``camelCase``), you may wish to make use of the
``rename`` configuration option in the `Struct` definition instead. This can
take a few different values:

- ``None``: the default, no field renaming (``example_field``)
- ``"lower"``: lowercase all fields (``example_field``)
- ``"upper"``: uppercase all fields (``EXAMPLE_FIELD``)
- ``"camel"``: camelCase all fields (``exampleField``)
- ``"pascal"``: PascalCase all fields (``ExampleField``)
- A mapping from field names to the renamed names. Field names missing from the
  mapping will not be renamed.
- A callable (signature ``rename(name: str) -> Optional[str]``) to use to
  rename all field names. Note that ``None`` for a return value indicates the
  original field name should be used.

The renamed field names are used for encoding and decoding only, any python
code will still refer to them using their original names.

.. code-block:: python

    >>> import msgspec

    >>> class Example(msgspec.Struct, rename="camel"):
    ...     """A struct with fields renamed using camelCase"""
    ...     field_one: int
    ...     field_two: str

    >>> # Python code uses the original field names
    ... ex = Example(1, field_two="two")

    >>> # Encoded messages use the renamed field names
    ... msgspec.json.encode(ex)
    b'{"fieldOne":1,"fieldTwo":"two"}'

    >>> # Decoding uses the renamed field names
    ... msgspec.json.decode(b'{"fieldOne": 3, "fieldTwo": "four"}', type=Example)
    Example(field_one=3, field_two='four')

    >>> # Decoding errors also use the renamed field names
    ... msgspec.json.decode(b'{"fieldOne": 5}', type=Example)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    msgspec.ValidationError: Object missing required field `fieldTwo`

If renaming to camelCase, you may run into issues if your field names contain
acronyms (e.g. ``FQDN`` in ``setHostnameAsFQDN``). Some JSON style guides
prefer to fully-uppercase these components (``FQDN``), but ``msgspec`` has no
way to know if a component is an acroynm or not (and so will result in
``Fqdn``). As such, we recommend using an explicit dict mapping for renaming if
generating `Struct` types to match an existing API.

.. code-block:: python

    # https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#podspec-v1-core
    # An explicit mapping from python name -> JSON field name
    v1podspec_names = {
        ...
        "service_account_name": "serviceAccountName",
        "set_hostname_as_fqdn": "setHostnameAsFQDN",
        ...
    }

    # Pass the mapping to `rename` to explicitly rename all fields
    class V1PodSpec(msgspec.Struct, rename=v1podspec_names):
        ...
        service_account_name: str = ""
        set_hostname_as_fqdn: bool = False
        ...


Note that if both the ``rename`` configuration option and the ``name`` arg to
`msgspec.field` are used, names set explicitly via `msgspec.field` take
precedence.

.. code-block:: python

    >>> import msgspec

    >>> class Example(msgspec.Struct, rename="camel"):
    ...     field_x: int
    ...     field_y: int = msgspec.field(name="y")  # set explicitly

    >>> msgspec.json.encode(Example(1, 2))
    b'{"fieldX":1,"y":2}'


Encoding/Decoding as Arrays
---------------------------

By default Struct objects encode the same dicts, with both the keys and values
present in the message.

.. code-block:: python

    >>> import msgspec

    >>> class Point(msgspec.Struct):
    ...     x: int
    ...     y: int

    >>> msgspec.json.encode(Point(1, 2))
    b'{"x":1,"y":2}'

If you need higher performance (at the cost of more inscrutable message
encoding), you can set ``array_like=True`` on a struct definition. Structs with
this option enabled are encoded/decoded as array-like types, removing the field
names from the encoded message. This can provide on average another ~2x speedup
for decoding (and ~1.5x speedup for encoding).

.. code-block:: python

    >>> class Point2(msgspec.Struct, array_like=True):
    ...     x: int
    ...     y: int

    >>> msgspec.json.encode(Point2(1, 2))
    b'[1,2]'

    >>> msgspec.json.decode(b'[3,4]', type=Point2)
    Point2(x=3, y=4)

Note that :ref:`struct-tagged-unions` also work with structs with
``array_like=True``. In this case the tag is encoded as the first item in the
array, and is used to determine which type in the union to use when decoding.

.. code-block:: python

    >>> import msgspec

    >>> from typing import Union

    >>> class Get(msgspec.Struct, tag=True, array_like=True):
    ...     key: str

    >>> class Put(msgspec.Struct, tag=True, array_like=True):
    ...     key: str
    ...     val: str

    >>> msgspec.json.encode(Get("my key"))
    b'["Get","my key"]'

    >>> msgspec.json.decode(
    ...     b'["Put", "my key", "my val"]',
    ...     type=Union[Get, Put]
    ... )
    Put(key='my key', val='my val')


Runtime Definition
------------------

In some cases it can be useful to dynamically generate `msgspec.Struct` classes
at runtime. This can be handled through the use of `msgspec.defstruct`, which
has a signature similar to `dataclasses.make_dataclass`. See
`msgspec.defstruct` for more information.

.. code-block:: python

    >>> import msgspec

    >>> Point = msgspec.defstruct("Point", [("x", float), ("y", float)])

    >>> p = Point(1.0, 2.0)

    >>> p
    Point(x=1.0, y=2.0)


.. _struct-gc:

Disabling Garbage Collection (Advanced)
---------------------------------------

.. warning::

    This is an advanced optimization, and only recommended for users who fully
    understand the implications of disabling the GC.

Python uses `reference counting`_ to detect when memory can be freed, with a
periodic `cyclic garbage collector`_ pass to detect and free cyclic references.
Garbage collection (GC) is triggered by the number of uncollected GC-enabled
(objects that contain other objects) objects passing a certain threshold. This
design means that garbage collection passes often run during code that creates
a lot of objects (for example, deserializing a large message).

By default, `msgspec.Struct` types will only be tracked if they contain a
reference to a tracked object themselves. This means that structs referencing
only scalar values (ints, strings, bools, ...) won't contribute to GC load, but
structs referencing containers (lists, dicts, structs, ...) will.

.. code-block:: python

    >>> import msgspec

    >>> from typing import Any

    >>> import gc

    >>> class Example(msgspec.Struct):
    ...     x: Any
    ...     y: Any

    >>> ex1 = Example(1, "two")

    >>> # ex1 is untracked, since it only references untracked objects
    ... gc.is_tracked(ex1)
    False

    >>> ex2 = Example([1, 2, 3], (4, 5, 6))

    >>> # ex2 is tracked, since it references tracked objects
    ... gc.is_tracked(ex2)
    True

If you *are certain* that your struct types can *never* participate in a
reference cycle, you *may* find a :ref:`performance boost
<struct-gc-benchmark>` from setting ``gc=False`` on a struct definition. This
boost is tricky to measure in isolation, since it should only result in the
garbage collector not running as frequently - an integration benchmark is
recommended to determine if this is worthwhile for your workload. A workload is
likely to benefit from this optimization in the following situations:

- You're allocating a lot of struct objects at once (for example, decoding a
  large object). Setting ``gc=False`` on these types will reduce the
  likelihood of a GC pass occurring while decoding, improving application
  latency.
- You have a large number of long-lived struct objects. Setting ``gc=False``
  on these types will reduce the load on the GC during collection cycles of
  later generations.

Struct types with ``gc=False`` will never be tracked, even if they reference
container types. It is your responsibility to ensure cycles with these objects
don't occur, as a cycle containing only ``gc=False`` structs will *never* be
collected (leading to a memory leak).

.. _type annotations: https://docs.python.org/3/library/typing.html
.. _pattern matching: https://docs.python.org/3/reference/compound_stmts.html#the-match-statement
.. _PEP 636: https://peps.python.org/pep-0636/
.. _PEP 563: https://peps.python.org/pep-0563/
.. _dataclasses: https://docs.python.org/3/library/dataclasses.html
.. _attrs: https://www.attrs.org/en/stable/index.html
.. _pydantic: https://pydantic-docs.helpmanual.io/
.. _mypy: https://mypy.readthedocs.io/en/stable/
.. _pyright: https://github.com/microsoft/pyright
.. _reference counting: https://en.wikipedia.org/wiki/Reference_counting
.. _cyclic garbage collector: https://devguide.python.org/garbage_collector/
.. _tagged unions: https://en.wikipedia.org/wiki/Tagged_union
.. _rich: https://rich.readthedocs.io/en/stable/pretty.html
.. _keyword-only parameters: https://docs.python.org/3/glossary.html#term-parameter
.. _lambda: https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions