Skip to main content

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
  • 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


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.0.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

phantom_types-0.0.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file phantom-types-0.0.1.tar.gz.

File metadata

  • Download URL: phantom-types-0.0.1.tar.gz
  • Upload date:
  • Size: 6.5 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.1.3 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for phantom-types-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b0cbe87231d46001778c51f8d223bc5b3c0368d5361580d2d5258dcdf0812beb
MD5 601d105e02f7c6d5b6fbdebee37fb76d
BLAKE2b-256 667f249d9b0ea43f0348a7a6fddb482a10e35675b59bc8babd56dcee462630e0

See more details on using hashes here.

File details

Details for the file phantom_types-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: phantom_types-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.6 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.1.3 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for phantom_types-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1eac10a75dc1f9242865c0029ee1ef9583198418b3eb6cd614978d8da366e760
MD5 93701e353e25fd030f963008c65b5d3c
BLAKE2b-256 7f0edabceca47322b06a5ef235c4f3ba0868ecb05d0eb8c992b5327983fe1bc8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page