Vue d'ensemble

msgspec 0.9, publié le 13 septembre 2022, ajoute les contraintes de validation, la génération de JSON Schema et le support Rich pour l'affichage.

Fonctionnalités principales

Contraintes et JSON Schema

Les champs des Structs supportent des contraintes de validation (ge, le, pattern) et peuvent générer un schéma JSON automatiquement.

python
from typing import Annotated
import msgspec
from msgspec import Meta

class Product(msgspec.Struct):
    name: Annotated[str, Meta(min_length=1)]
    price: Annotated[float, Meta(ge=0)]
    quantity: Annotated[int, Meta(ge=0, le=9999)]

schema = msgspec.json.schema(Product)
print(schema)  # JSON Schema dict

Support Rich

Les Structs s'affichent avec Rich pour un rendu coloré et structuré dans le terminal.

python
import msgspec
from rich import print

class Config(msgspec.Struct):
    host: str = 'localhost'
    port: int = 8000
    debug: bool = False

print(Config())  # affichage Rich coloré

Sources