Phantom types for Python
Project description
phantom-types
Phantom types for Python.
Installation
python3 -m pip install phantom-types
Abstract
- Make illegal states unrepresentable.
- Parse, don't validate
- Ghosts of Departed Proofs
- Abuse
__instancecheck__
and type-guards. - Values are checked at runtime but no extra instances/subclasses are instantiated.
Usage
Builtin types
phantom.datetime
TZAware
TZNaive
phantom.numeric
Natural
NegativeInt
Portion
phantom.re
Match
phantom.sized
NonEmpty
Empty
Creating phantom types
To create new phantom types, subclass phantom.base.Phantom
and define an
__instancecheck__
method:
from typing import Any
from typing import TYPE_CHECKING
from phantom.base import Phantom
class Greeting(str, Phantom):
@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 phantom-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
phantom-types-0.1.0.tar.gz
(6.7 kB
view details)
Built Distribution
File details
Details for the file phantom-types-0.1.0.tar.gz
.
File metadata
- Download URL: phantom-types-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e7de31f50ef55fd6bdb8cab71b0673ba545810c4a98b607251cc47694d823b5 |
|
MD5 | 88ef9255b134e1b5057a1366024c3cf0 |
|
BLAKE2b-256 | 187f4598af72395c0ce5e08ba6a2d3f4326d9c91613f63774f271dd6659dcc85 |
File details
Details for the file phantom_types-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: phantom_types-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7c164967f9384abed816abc09897703cc01aa74d92599bd840c9136b9e8ae47 |
|
MD5 | fdc498da1579f0245ff43e51a4af4c84 |
|
BLAKE2b-256 | a541d83252a29f0bace9835d8e5fc2b5d7067b145c1888b9033e8d61c65d3e42 |