A collection of msgspec extensions: custom types, cast helpers, decode hooks, encode hooks and deprecation system.
import msgspex
from msgspex.custom_types import Email, datetime
value = msgspex.decoder.decode('"user@example.com"', type=Email)
dt = msgspex.decoder.decode('"2024-01-02T03:04:05Z"', type=datetime)
payload = msgspex.encoder.encode(dt)pip install msgspex
uv add msgspex
poetry add msgspexAfter import msgspex, all hooks and types are registered automatically.
Option[T]— optional value type based onkungfu(Some | Nothing | msgspec.UnsetType).
There is also decode-hook integration for kungfu.Sum (not a custom type, but supported by the decoder).
date— re-export ofdatetime.date.datetime— meta-type that coversStringTimestampDatetime,IntTimestampDatetime,FloatTimestampDatetime,ISODatetime(alias:isodatetime), anddatetime.datetime.timedelta— subclass ofdatetime.timedeltawith cast support.StrEnum,IntEnum,FloatEnum,BaseEnumMeta—enumextensions for stable handling of unknown values.Literal— runtime type conceptually compatible withtyping.Literal.dataclasses.InitVar— Passing vars to a__post_init__method.
Email—format: emailIDNEmail—format: idn-emailURI—format: uriURIReference—format: uri-referenceIRI—format: iriIRIReference—format: iri-referenceHostname—format: hostnameIDNHostname—format: idn-hostnameIPv4—format: ipv4IPv6—format: ipv6JsonPointer—format: json-pointerRelativeJsonPointer—format: relative-json-pointerRegex—format: regexInt32,Int64— range-limited integer typesFloat32,Float64— finite, range-limited floating-point types
UUID, Decimal, date and time already supported by msgspec.
msgspex is MIT licensed