Declare & parse data types & schemas
Project description
uType
utype is a data type & schema declaration & parsing library based on Python type annotations, enforce type and constraints at runtime
- Version:
0.2.0
[test] - Documentation: https://utype.io
- Source Code: https://github.com/utilmeta/utype
Core Features
- Enforce types, data classes, function params and result parsing at runtime based on Python type annotation
- Support a variety of constraints, logical operator and flexible parsing options
- Highly extensible, all type transformer can be register, extend and override
- Support JSON-Schema documentation with OpenAPI compatibility
Installation
pip install -U utype
utype requires Python >= 3.7
Usage Example
Basic Types & Constraints
from utype import Rule, exc
class PositiveInt(int, Rule):
gt = 0
assert PositiveInt(b'3') == 3
try:
PositiveInt(-0.5)
except exc.ParseError as e:
print(e)
"""
Constraint: <gt>: 0 violated
"""
Data Class
from utype import Schema, Field, exc
from datetime import datetime
class UserSchema(Schema):
username: str = Field(regex='[0-9a-zA-Z]{3,20}')
signup_time: datetime
print(UserSchema(username='bob', signup_time='2022-10-11 10:11:12'))
#> UserSchema(username='bob', signup_time=datetime.datetime(2022, 10, 11, 10, 11, 12))
try:
UserSchema(username='@invalid', signup_time='2022-10-11 10:11:12')
except exc.ParseError as e:
print(e)
"""
Constraint: <regex>: '[0-9a-zA-Z]{3,20}' violated
"""
RoadMap & Contribution
Licence
Apache 2.0
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
utype-0.2.0.tar.gz
(46.6 kB
view hashes)
Built Distribution
utype-0.2.0-py3-none-any.whl
(52.9 kB
view hashes)