dependent-types
Dependent types for Python.
Installation
python3 -m pip install dependent-types
Abstract
- Make illegal states unrepresentable.
- Parse, don't validate
- Abuse
__instancecheck__and type-guards. - Values are checked at runtime but no extra instances/subclasses are instantiated.
Usage
Builtin types
dept.datetime
TZAwareTZNaive
dept.numeric
NaturalNegativeIntPortion
dept.re
Match
dept.sized
NonEmptyEmpty
Creating dependent types
To create new dependent types, subclass dept.base.Dependent and define an
__instancecheck__ method:
from typing import Any
from typing import TYPE_CHECKING
from dept.base import Dependent
class Greeting(str, Dependent):
@classmethod
def __instancecheck__(cls, instance: Any) -> bool:
return (
isinstance(instance, str)
and instance.startswith(("Hello", "Hi"))
)
hello = "Hello there"
# We can narrow types using mypy's type guards
assert isinstance(hello, Greeting)
# or explicitly when we need to
hi = Greeting.from_instance("Hi there")
# The runtime types are unchanged and will still be str for our greetings
assert type(hello) is str
assert type(hi) is str
# But their static types will be Greeting, retaining the information that our
# strings are not just any strs
if TYPE_CHECKING:
reveal_type(hello)
reveal_type(hi)
# As this string doesn't fulfill our __instancecheck__, it will not be an
# instance of Greeting.
assert not isinstance("Goodbye", Greeting)
Checkout out the dacite example for how to create dataclasses with rich dependently typed fields without duplicating type definitions or losing parsed information.
Metadata
Release files for dependent-types 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dependent-types-0.0.1.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dependent_types-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.0 kB
Release files / dependent-types-0.0.1.tar.gz
| Download URL | dependent-types-0.0.1.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9809734e55e8728c82bc22aaeac81120e60f989fe3ccd0e3f55fdd5890f85724
|
|
BLAKE2b-256 checksum How to use checksums |
56362e3d6ec024e0e19f79b3d5bde84f6c2708946153b409413f197f8d9d934a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
|
Release files / dependent_types-0.0.1-py3-none-any.whl
| Download URL | dependent_types-0.0.1-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
63d65448486f8ca8fdc7b568fa69996be77549881cfc3f64f89b6cdd58e071ea
|
|
BLAKE2b-256 checksum How to use checksums |
facce3c37551815b077a20ed43c7a5fae9956e989a5c6d2a10c31f0faafb4458
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1
|