Dependent types for Python
Project description
dependent-types
Dependent types for Python.
Installation
python3 -m pip install dependent-types
Abstract
- Make illegal states unrepresentable.
- Abuse
__instancecheck__
and type-guards.
Usage
Builtin types
dept.datetime
TZAware
TZNaive
dept.numeric
Natural
NegativeInt
Portion
dept.sized
NonEmpty
Empty
Creating dependent types
To create new dependent types, subclass dept.base.Dependent
and define a
__instancecheck__
method:
from typing import Any
from dept.base import Dependent
class StartsWithHello(str, Dependent[str]):
def __instancecheck__(self, instance: Any) -> bool:
return isinstance(instance, str) and instance.startswith("Hello")
isinstance("Hello there", StartsWithHello) # True
isinstance("Hi there", StartsWithHello) # False
Checkout out the dacite example for how to create dataclasses with rich dependently typed fields without duplicating type definitions or losing parsed information.
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
dependent-types-0.0.0.tar.gz
(5.7 kB
view hashes)
Built Distribution
Close
Hashes for dependent_types-0.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b290d342138299aebd57c04daade5b685c38124c5bd18136a424aa0bc24b47fe |
|
MD5 | 0f3773e86fc7a53dd25c0b25235a1709 |
|
BLAKE2b-256 | e4ba977d4fa08378ca18cd5430d7dfa8b7141042e1128ff93e926e2c83ab97b0 |