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.

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 a tool, 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.1.0.tar.gz (12.2 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.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sensible_types-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.28

File hashes

Hashes for sensible_types-0.1.0.tar.gz
Algorithm Hash digest
SHA256 96ee0f0c4cf97ecfbd26e827f73c607df616b6ff0fbabd76669ed5b5837371a1
MD5 066f17c7002d82699b025a6a4975c7af
BLAKE2b-256 4af7151c50abc5cfab158a216721d80728dab5349d2bb0571eeffad5047d7ac8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sensible_types-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78fbd7295c3bfaa10d2f7de631f21bf0d7cf5574c046153c0daa828dc57aeddf
MD5 0e14ca568a258d208336a0f36c2ada39
BLAKE2b-256 f33a4968399034fb9732321ab9e8d0bcf8fbaf17028ad061a10b7467997e531c

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