Skip to main content

A collection of useful types for Python, to drive correctness in your programs.

Project description

Sensible Types

A collection of useful types for Python, to drive correctness in your programs.

Installation

pip3 install sensible-types  # with pip
poetry add sensible-types    # with poetry
uv add sensible-types        # with uv

View on PyPI.

Why?

Parse, don't validate.

How many times have you written code of this nature?

def foo(a: int):
    if a < 0:
        raise ValueError(f"The input `a` should be positive. Actual value: {a}")

    # actual logic
    ...

The requirement for input validation indicates an error in your type system.

It should not be possible to pass a negative int to a function that can only work with a positive int — that is an invalid state that your program should never find itself in.

Unfortunately, Python doesn't make this easy. We should ideally be able to do something like this:

def foo(a: uint):
    # straight into the logic, as the input comes pre-validated
    ...

But of course, Python does not have an unsigned integer type.

Type-driven design leads to more correct, reliable software, where tests only have to focus on verifying that your business logic works rather than verifying that your inputs are valid. To borrow a phrase from Rust 🦀, it makes invalid states unrepresentable.

This package aims to provide new primitive types with additional constraints that are commonly required.

For now, it only contains PositiveInt, but it may be extended in the future as new use cases arise.

But Python doesn't care about types at runtime

You're right.

But that doesn't mean that you, the developer, should not care about the correctness and reliability of your program.

You should be using strict type checking tools like Pyright to analyse your code. Without such tools, all the type-driven design in the world won't save you from Python.

There still won't be anything stopping your program from running if you pass in an incorrect type, but at least you'll see errors in you IDE if you've written incorrect code.

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

sensible_types-0.2.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sensible_types-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file sensible_types-0.2.0.tar.gz.

File metadata

  • Download URL: sensible_types-0.2.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for sensible_types-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e8a6cb308e6f820f94502a8bb83f4d3d80a1cead677f1690f47c0868675a0f50
MD5 250fefd3b7fef780ed8540d7e533a047
BLAKE2b-256 8feb1ecbc9e7e66aec21da3955fb99d3621ef73991e023335758107be1d89ab7

See more details on using hashes here.

File details

Details for the file sensible_types-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sensible_types-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for sensible_types-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 61338ec510d73dfb0c27309824bc6d489c39ea84b8267a471072de06c55dcb6a
MD5 f9bfd78d6f370a334b369229233aed86
BLAKE2b-256 6c33a8e70fdb20dde9e58ba20f60f7538c3b73cd2cfe3dce0b7092815ce57b7e

See more details on using hashes here.

Supported by

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