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
Metadata
Release files for msgspec-extras 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| msgspec_extras-0.3.0.tar.gz | 76.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| msgspec_extras-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 82.2 kB
Release files / msgspec_extras-0.3.0.tar.gz
| Download URL | msgspec_extras-0.3.0.tar.gz |
|---|---|
| Size | 76.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
66da6ca628c0673647e14619e269e3ba1e358efb98040bc53eb265254abf9923
|
|
BLAKE2b-256 checksum How to use checksums |
205524a2c0b97e1180f127845df016905a7c5d0b9eb292326d91c96156dbf301
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","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}
|
Release files / msgspec_extras-0.3.0-py3-none-any.whl
| Download URL | msgspec_extras-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4f45055df99e6f78073f3c48aa23de97b2eec424c1bbc975797a10f357087b3
|
|
BLAKE2b-256 checksum How to use checksums |
4fe1f7d9aa4890fb4934e0aa246ad5973b8a85ce69da64233fa1721f62dbadb9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","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}
|