serious
A dataclass model toolkit: serialization, validation, and more.
Features
- Model definitions in pure Python.
- Validation showing up in code coverage.
- Type annotations for all public-facing APIs.
- (Optionally) ensures immutability.
- Easily extensible.
- Made for people.
- Documented rigorously.
Basics
Installation
Available from PyPI:
pip install serious
Quick Example
Central part of Serious API are different Models.
Given a regular dataclass:
from dataclasses import dataclass
@dataclass
class Person:
name: str
Let’s create a JsonModel:
from serious.json import JsonModel
model = JsonModel(Person)
And use its dump/load methods:
person = Person('Albert Einstein')
model.dump(person) # {"name": "Albert Einstein"}
Validation
To add validation to the example above all we need is to add __validate__ method to person:
from dataclasses import dataclass
from typing import Optional
from serious import ValidationError, Email
@dataclass
class Person:
name: str
email: Optional[Email]
phone: Optional[str]
def __validate__(self):
if len(self.name) == 0:
raise ValidationError('Every person needs a name')
if self.phone is None and self.email is None:
raise ValidationError('At least some contact should be present')
Supported formats:
- JSON
- Python Dictionaries
- YAML
- Form data
Supported field types
- Other dataclasses
- Primitives:
str,int,float,bool - Dictionaries: only with string keys:
Dict[str, Any] - Lists, sets, deques: python collections of any serializable type
- Tuples both with and without ellipsis:
- tuples as set of independent elements (e.g.
Tuple[str, int, date]) - with ellipses, acting as a frozen list (
Tuple[str, ...])
- tuples as set of independent elements (e.g.
- Enumerations by value:
- of primitives (e.g.
OperatingSystem(Enum)) - typed enums (
Color(str, Enum)andFilePermission(IntFlag))
- of primitives (e.g.
- Decimal: encoded to JSON as string
- Datetime, date and time: encoded to the ISO 8601 formatted string
- UUID
serious.types.Timestamp: a UTC timestamp since UNIX epoch as float ms valueserious.types.Email: a string Tiny Type that supports validation and contains additional properties- custom immutable alternatives to native python types in
serious.types:FrozenList,FrozenDict
A bigger example
from dataclasses import dataclass
from serious import JsonModel, ValidationError
from typing import List
from enum import Enum
class Specialty(Enum):
Worker = 1
Fool = 2
@dataclass(frozen=True)
class Minion:
name: str
type: Specialty
@dataclass(frozen=True)
class Boss:
name: str
minions: List[Minion]
def __validate__(self):
if len(self.minions) < 2:
raise ValidationError('What kind of boss are you?')
boss = Boss("me", [Minion('evil minion', Specialty.Fool), Minion('very evil minion', Specialty.Worker)])
boss_json = """{
"name": "me",
"minions": [
{
"name": "evil minion",
"type": "Fool"
},
{
"name": "very evil minion",
"type": "Worker"
}
]
}"""
model = JsonModel(Boss, indent=4)
assert model.dump(boss) == boss_json
assert model.load(boss_json) == boss
Acknowledgements
Initially, a fork of @lidatong/dataclasses-json.
Metadata
Release files for serious 1.6.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 | Provenance | |
|---|---|---|---|---|
| serious-1.6.2.tar.gz | 28.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| serious-1.6.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 61.8 kB
Release files / serious-1.6.2.tar.gz
| Download URL | serious-1.6.2.tar.gz |
|---|---|
| Size | 28.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd1bf279d2ae860f057a893c214d2e5b538991b935ca0d1791ad9cb7b9c81ce5
|
|
BLAKE2b-256 checksum How to use checksums |
c20d4521c2b0c8ed477414bd1f53f3b4fcd6d2a28c2a7a001221dd589cc9322a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.15
|
Release files / serious-1.6.2-py3-none-any.whl
| Download URL | serious-1.6.2-py3-none-any.whl |
|---|---|
| Size | 33.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5327ffe1c30a95d91d33c6d9b2d6a98840016fc149cd4569523ac601741bc774
|
|
BLAKE2b-256 checksum How to use checksums |
a8563a444766462825506d48fd71b08a6a41b3fe6d9bc17bc2e08f2de44b0307
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.15
|
Security for serious 1.6.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.