A small collection of types that extend msgspec's built-in type support.
Project description
msgspec-extras
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 todayPastDatetime- must be earlier than nowFutureDate- must be later than todayFutureDatetime- 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file msgspec_extras-0.2.0.tar.gz.
File metadata
- Download URL: msgspec_extras-0.2.0.tar.gz
- Upload date:
- Size: 69.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
977210ba14a2b1c81815a2e3ddb7501bf323bdc53b994a5f98123e06038e732e
|
|
| MD5 |
865d60d662b877006dcafd4863ff5b6c
|
|
| BLAKE2b-256 |
2b7c3613f7de4d520b6c49a2da47edf21ac800f8cb3c91874e88dbd16fcf1113
|
File details
Details for the file msgspec_extras-0.2.0-py3-none-any.whl.
File metadata
- Download URL: msgspec_extras-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e63b224ae5d27a8ed0a94bdd6745dfa459a3d87b76bd9a7cdec7531fd758781
|
|
| MD5 |
913c70aef551b7650ebe3135d6425c41
|
|
| BLAKE2b-256 |
a928681bee5a14ffdb4093b44f91b58eb640a667cdacbdc6fc6642c564be243f
|