Skip to main content

msgspec-extras

PyPI - Python PyPI - Status PyPI - Version PyPI - License

prek Ruff ty

status-badge

A small collection of types that extend msgspec's built-in type support.

Installation

pdm add msgspec-extras

Type Support

msgspec-extras supports all msgspec native types - see the full list in the msgspec documentation. It also adds the following custom types.

Datetime

  • PastDate - must be earlier than today
  • PastDatetime - must be earlier than now
  • FutureDate - must be later than today
  • FutureDatetime - must be later than now

These types require the enc_hook/dec_hook to be passed to msgspec's encode/decode calls.

from datetime import datetime, date

import msgspec
from msgspec_extras import PastDate, PastDatetime, FutureDate, FutureDatetime, enc_hook, dec_hook

past_date = msgspec.json.decode('"2020-03-21"', type=PastDate, dec_hook=dec_hook)
past_date_json = msgspec.json.encode(past_date, enc_hook=enc_hook)

msgspec.json.decode('"2020-03-21"', type=FutureDate, dec_hook=dec_hook)
# Raises `msgspec.ValidationError` as the date isn't in the future

Numeric

Type Constraint
PositiveInt > 0
PositiveFloat > 0
NonNegativeInt >= 0
NonNegativeFloat >= 0
NegativeInt < 0
NegativeFloat < 0
NonPositiveInt <= 0
NonPositiveFloat <= 0

String

  • HttpUrl - must be a valid HTTP or HTTPS URL

Usage

The numeric and string types are just Annotated aliases over msgspec's native types (e.g. Annotated[int, msgspec.Meta(gt=0)]), so they need no hooks and work as drop-in replacements anywhere you'd use int, float, or str:

import msgspec
from msgspec_extras import PositiveFloat


class Product(msgspec.Struct):
    name: str
    price: PositiveFloat


product = msgspec.json.decode(b'{"name": "Widget", "price": 5.1}', type=Product)
msgspec.json.encode(product)

Socials

Social - Matrix

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msgspec_extras-0.2.1.tar.gz (69.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

msgspec_extras-0.2.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file msgspec_extras-0.2.1.tar.gz.

File metadata

  • Download URL: msgspec_extras-0.2.1.tar.gz
  • Upload date:
  • Size: 69.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for msgspec_extras-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a889ef427ffafb8778bf4c58b450e480f89fa44616a0b50c4a8a2c797749a0ad
MD5 3f788065962fa5ddd2bcfe15229f01a4
BLAKE2b-256 f4fa5d39fbcc9ed895ec2ba3e4126e22b3fcfa289a11b8b27d88718b5fd86316

See more details on using hashes here.

File details

Details for the file msgspec_extras-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: msgspec_extras-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for msgspec_extras-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0473c44608f0e4c03c8124aa36a36076e1f68a130dcdbc4683daecffa3d146aa
MD5 5de15f8448c81d0268ddcc16127e446c
BLAKE2b-256 deb54839230ee88bf9c0e77f7f506495690c17dfed699d178e9dab3f87017032

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page