Skip to main content

Phantom types for Python

Project description

phantom-types

CI Build Status Documentation Build Status Test coverage report
PyPI Package Python versions

Phantom types for Python will help you make illegal states unrepresentable and avoid shotgun parsing by enabling you to practice "Parse, don't validate".

Checkout the complete documentation on Read the Docs →

Installation

$  python3 -m pip install phantom-types

Extras

There are a few extras available that can be used to either enable a feature or install a compatible version of a third-party library.

Extra name Feature
[dateutil] Installs python-dateutil. Required for parsing strings with TZAware and TZNaive.
[phonenumbers] Installs phonenumbers. Required to use phantom.ext.phonenumbers.
[pydantic] Installs pydantic.
[hypothesis] Installs hypothesis.
[all] Installs all of the above.
$  python3 -m pip install phantom-types[all]

Examples

By introducing a phantom type we can define a pre-condition for a function argument.

from phantom import Phantom
from phantom.predicates.collection import contained


class Name(str, Phantom, predicate=contained({"Jane", "Joe"})):
    ...


def greet(name: Name):
    print(f"Hello {name}!")

Now this will be a valid call.

greet(Name.parse("Jane"))

... and so will this.

joe = "Joe"
assert isinstance(joe, Name)
greet(joe)

But this will yield a static type checking error.

greet("bird")

To be clear, the reason the first example passes is not because the type checker somehow magically knows about our predicate, but because we provided the type checker with proof through the assert. All the type checker cares about is that runtime cannot continue executing past the assertion, unless the variable is a Name. If we move the calls around like in the example below, the type checker would give an error for the greet() call.

joe = "Joe"
greet(joe)
assert isinstance(joe, Name)

Runtime type checking

By combining phantom types with a runtime type-checker like beartype or typeguard, we can achieve the same level of security as you'd gain from using contracts.

import datetime
from beartype import beartype
from phantom.datetime import TZAware


@beartype
def soon(dt: TZAware) -> TZAware:
    return dt + datetime.timedelta(seconds=10)

The soon function will now validate that both its argument and return value is timezone aware, e.g. pre- and post conditions.

Pydantic support

Phantom types are ready to use with pydantic and have integrated support out-of-the-box. Subclasses of Phantom work with both pydantic's validation and its schema generation.

class Name(str, Phantom, predicate=contained({"Jane", "Joe"})):
    @classmethod
    def __schema__(cls) -> Schema:
        return super().__schema__() | {
            "description": "Either Jane or Joe",
            "format": "custom-name",
        }


class Person(BaseModel):
    name: Name
    created: TZAware


print(json.dumps(Person.schema(), indent=2))

The code above outputs the following JSONSchema.

{
  "title": "Person",
  "type": "object",
  "properties": {
    "name": {
      "title": "Name",
      "description": "Either Jane or Joe",
      "format": "custom-name",
      "type": "string"
    },
    "created": {
      "title": "TZAware",
      "description": "A date-time with timezone data.",
      "type": "string",
      "format": "date-time"
    }
  },
  "required": ["name", "created"]
}

Development

Install development requirements, preferably in a virtualenv:

$ python3 -m pip install .[all,test]

Run tests:

$ pytest
# or
$ make test

Linting and static type checking is setup with pre-commit, after installing it you can setup hooks with the following command, so that checks run before you push changes.

# configure hooks to run when pushing
$ pre-commit install -t pre-push
# or when committing
$ pre-commit install -t pre-commit
# run all checks
$ pre-commit run --all-files
# or just a single hook
$ pre-commit run mypy --all-files

In addition to static type checking, the project is setup with pytest-mypy-plugins to test that exposed mypy types work as expected, these checks will run together with the rest of the test suite, but you can single them out with the following command.

$ make test-typing

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

Uploaded Source

Built Distribution

phantom_types-2.1.1-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: phantom-types-2.1.1.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for phantom-types-2.1.1.tar.gz
Algorithm Hash digest
SHA256 5bb875d8a387d5d73ca3921f7844ea1d83d3a5edf3b5e03603bc4cb0563d7ee7
MD5 a3a1d19abdff8955a8a765e88ff7d6fc
BLAKE2b-256 545a4ee7ef9d321e50e95c51ab2f7ba1ee52bef8b87a8327b26ce3fdfa7d36e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for phantom_types-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 13f2e7a5479d929aef091a20176011aee00b2de5d3fc6f9742c32b79d597ed9d
MD5 efa47e7c6a12d37df69369f1cc8af25c
BLAKE2b-256 3c79a08c4b8b8f32c188c62d79e6bbad85467d452c84f7deca3448391a709dff

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