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
Release files for msgspec-extras 0.2.2
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.2.2.tar.gz | 80.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| msgspec_extras-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 85.7 kB
Release files / msgspec_extras-0.2.2.tar.gz
| Download URL | msgspec_extras-0.2.2.tar.gz |
|---|---|
| Size | 80.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
748d0ae647fc730a92b53b3a862e71ddc87587addacdcc104f1daead7537a350
|
|
BLAKE2b-256 checksum How to use checksums |
cde53d45bf4d00071bc231557d432f3710ad441df546ac2321486afd2ebcd4d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","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.2.2-py3-none-any.whl
| Download URL | msgspec_extras-0.2.2-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e22927b614fc0d5d2be815d000e2ac69fb7944b4ed0e3c5ed20b273f41297355
|
|
BLAKE2b-256 checksum How to use checksums |
45c4689e73619c016b9fd0df93c877771d74f18230623a344ae1089f0ed7f438
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","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}
|