Type dispatch and validation for run-time Python
Project description
Runtype is a collection of run-time type utilities for Python.
It is:
:runner: Fast! Uses an internal typesystem for maximum performance.
:brain: Smart! Supports typing, constraints, auto-casting, and much more.
:gear: Configurative! Write your own type system, and use it with dataclass and dispatch.
Modules
-
:star: validation - Provides a smarter alternative to
isinstanceandissubclass, with support for thetypingmodule, and type constraints. -
:star: dataclass - Adds run-time type validation to the built-in dataclass.
- Improves dataclass ergonomics.
- Supports automatic value casting, Pydantic-style. (Optional, off by default)
- Supports types with constraints. (e.g.
String(max_length=10)) - Supports optional sampling for faster validation of big lists and dicts.
- 40% faster than Pydantic (read here)
-
:star: dispatch - Provides fast multiple-dispatch for functions and methods, via a decorator.
- Inspired by Julia.
-
:star: type utilities - Provides a set of classes to implement your own type-system.
- Used by runtype itself, to emulate the Python type-system.
Docs
Read the docs here: https://runtype.readthedocs.io/
Install
pip install runtype
No dependencies.
Requires Python 3.6 or up.
Examples
Validation (Isa & Subclass)
from typing import Dict, Mapping
from runtype import isa, issubclass
print( isa({'a': 1}, Dict[str, int]) )
#> True
print( isa({'a': 'b'}, Dict[str, int]) )
#> False
print( issubclass(Dict[str, int], Mapping[str, int]) )
#> True
print( issubclass(Dict[str, int], Mapping[int, str]) )
#> False
Dataclasses
from typing import List
from datetime import datetime
from runtype import dataclass
@dataclass(check_types='cast') # Cast values to the target type, when applicable
class Person:
name: str
birthday: datetime = None # Optional
interests: List[str] = [] # The list is copied for each instance
print( Person("Beetlejuice") )
#> Person(name='Beetlejuice', birthday=None, interests=[])
print( Person("Albert", "1955-04-18T00:00", ['physics']) )
#> Person(name='Albert', birthday=datetime.datetime(1955, 4, 18, 0, 0), interests=['physics'])
print( Person("Bad", interests=['a', 1]) )
# Traceback (most recent call last):
# ...
# TypeError: [Person] Attribute 'interests' expected value of type list[str]. Instead got ['a', 1]
# Failed on item: 1, expected type str
Multiple Dispatch
from runtype import Dispatch
dp = Dispatch()
@dp
def append(a: list, b):
return a + [b]
@dp
def append(a: tuple, b):
return a + (b,)
@dp
def append(a: str, b: str):
return a + b
print( append([1, 2, 3], 4) )
#> [1, 2, 3, 4]
print( append((1, 2, 3), 4) )
#> (1, 2, 3, 4)
print( append('foo', 'bar') )
#> foobar
print( append('foo', 4) )
# Traceback (most recent call last):
# ...
# runtype.dispatch.DispatchError: Function 'append' not found for signature (<class 'str'>, <class 'int'>)
License
Runtype uses the MIT license.
Donate
If you like Runtype and want to show your appreciation, you can do so at my patreon page, or ko-fi page.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file runtype-0.2.6.tar.gz.
File metadata
- Download URL: runtype-0.2.6.tar.gz
- Upload date:
- Size: 19.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31818c1991c8b5e01ec2e54a53ad44af104dcf1bb4e82efd1aa7eba1047dc6dd
|
|
| MD5 |
d4fe8c7c5de0a9585de71b8659f565c7
|
|
| BLAKE2b-256 |
6b73eff59ce2d42cbf75be80bcbb423f97f787a97d558ddd60a085e6e60ec97c
|
File details
Details for the file runtype-0.2.6-py3-none-any.whl.
File metadata
- Download URL: runtype-0.2.6-py3-none-any.whl
- Upload date:
- Size: 20.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1739136f46551240a9f68807d167b5acbe4c18512de08ebdcc2fa0648d97c834
|
|
| MD5 |
05fa68817c96be10350b24cb07bdb30e
|
|
| BLAKE2b-256 |
1870f5c7c959b9a6aaa3027e4e7fe80a22ad73dbb899ccb606b8c08a5af9ed5d
|