Python classes with types validation at runtime.
Project description
typedclasses
Python classes with types validation at runtime. (Experimental & Under Development)
Installation
You can install this library using Python's favorite, pip
package manager.
pip install -U typedclasses
How it works
Using typedclasses, you can create classes in dataclasses
-like manner i.e using type annotations and library will enforce types for
that class at runtime. Here's an example:
import typing
from typedclasses import TypedClass
class User(TypedClass):
id: int
name: str
email: typing.Optional[str] = None
Parameters will be validated when initialising above class. Since email
has a default value set, It is optional to pass
it as a parameter while instansiating:
>>> User(id=1, name="foobar") # runs fine
>>> User(id="1", name="foobar")
TypeError: Parameter 'id' must be an instance of <class 'int'>, <class 'str'> is unsupported.
This library also provides validation for various generic types from typing
module:
class Foo(TypedClass):
x: typing.Union[str, int]
Foo(x="a") # ok
Foo(x=1) # ok
Foo(x=True) # invalid
List of all types supported from typing
module can be found in the documentation.
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 Distributions
Built Distribution
Hashes for typedclasses-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00490ee7f36d75d79ac452401a24d26dcac8092915e2ffa070b4c2b6effeed8d |
|
MD5 | 9a99e3bf3437f50277667e60efb0b90d |
|
BLAKE2b-256 | f37fff4a2e8ecf4356c66de0ccc2cc533ffced138fc3ea3dc0ad3fc646dda092 |