Skip to main content

Declare & parse data types & schemas

Project description

uType

Code style: black

utype is a data type & schema declaration & parsing library based on Python type annotations, enforce type and constraints at runtime

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


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)

Uploaded Source

Built Distribution

utype-0.2.0-py3-none-any.whl (52.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page