Skip to main content

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.

Release files for sensible-types 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sensible-types 0.2.0
File Size Uploaded
sensible_types-0.2.0.tar.gz 17.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sensible-types 0.2.0
File Interpreter ABI Platform
sensible_types-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.5 kB

Release files / sensible_types-0.2.0.tar.gz

Download URL sensible_types-0.2.0.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e8a6cb308e6f820f94502a8bb83f4d3d80a1cead677f1690f47c0868675a0f50
BLAKE2b-256 checksum
How to use checksums
8feb1ecbc9e7e66aec21da3955fb99d3621ef73991e023335758107be1d89ab7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / sensible_types-0.2.0-py3-none-any.whl

Download URL sensible_types-0.2.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61338ec510d73dfb0c27309824bc6d489c39ea84b8267a471072de06c55dcb6a
BLAKE2b-256 checksum
How to use checksums
6c33a8e70fdb20dde9e58ba20f60f7538c3b73cd2cfe3dce0b7092815ce57b7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page