Numerary

0.4.4 · active · verified Thu Apr 16

Numerary is a Python library that provides a set of type-hinting protocols (e.g., `Integer`, `Float`, `Real`, `Complex`, `Number`) designed for more precise and robust type-checking of numeric types in Python. It is currently at version 0.4.4 and is under active development with frequent, smaller updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `numerary` protocols for runtime type checking with `isinstance` and static type hints. The protocols allow distinguishing between different categories of numbers.

from numerary import Integer, Float, Real, Number

def process_number(n: Number) -> str:
    if isinstance(n, Integer):
        return f"It's an integer: {n} (type: {type(n).__name__})"
    elif isinstance(n, Float):
        return f"It's a float: {n} (type: {type(n).__name__})"
    elif isinstance(n, Real):
        return f"It's a real number: {n} (type: {type(n).__name__})"
    else:
        return f"It's a generic number: {n} (type: {type(n).__name__})"

print(process_number(5))
print(process_number(5.0))
print(process_number(3 + 4j))
print(process_number(10.5))

view raw JSON →