Skip to main content

No project description provided

Project description

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

Project details


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.1.0.tar.gz (104.9 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.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: msgspec_extras-0.1.0.tar.gz
  • Upload date:
  • Size: 104.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.1.0.tar.gz
Algorithm Hash digest
SHA256 47a29164da3ef925a31b1296fa07a9bd498dda4b3168fc01cf7721990f59c7cb
MD5 cc12651ca583e6fa1de7d05d59ba8672
BLAKE2b-256 352413ed6ca2dd625bbaef469b7c18ee32029c187f9afe8cf472f5e305877ad4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: msgspec_extras-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0909c71953babd24334e2fec8191f784f149e2782d2ffbfd8a08b87cccd6b6d1
MD5 bc41ca40a2fa0a7d6a46fed349a859af
BLAKE2b-256 858cc9542a5e886f348cc8d7b254b643b54755197774837ab83075a3c58572a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page