diff --git a/docs/source/structs.rst b/docs/source/structs.rst index 334bd994..e83ce822 100644 --- a/docs/source/structs.rst +++ b/docs/source/structs.rst @@ -161,6 +161,14 @@ at the end of the generated ``__init__`` method. It has the same semantics as th This method may be useful for adding additional logic to the init (such as custom validation). +.. important:: + + ``__post_init__`` will *not* be called when using any of + + - :func:`msgspec.structs.replace`, + - :func:`copy.replace`, + - :func:`copy.copy`, + 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)``) diff --git a/msgspec/__init__.pyi b/msgspec/__init__.pyi index 4ad5dec8..dc1878fd 100644 --- a/msgspec/__init__.pyi +++ b/msgspec/__init__.pyi @@ -17,7 +17,7 @@ from typing import ( overload, ) -from typing_extensions import dataclass_transform, Buffer +from typing_extensions import dataclass_transform, Buffer, Self from . import inspect, json, msgpack, structs, toml, yaml @@ -118,6 +118,7 @@ class Struct(metaclass=StructMeta): def __rich_repr__( self, ) -> Iterable[Union[Any, Tuple[Any], Tuple[str, Any], Tuple[str, Any, Any]]]: ... + def __replace__(self, **changes: Any) -> Self: ... def defstruct( name: str,