Skip to main content

Range-Typed Integers

This package provides integer types that have a specific range of valid values.

Usage

The package provides some types (as NewType) that represent their respective Rust integer type: u8, u16, u32, u64, i8, i16, i32 and i64:

from range_typed_integers import u8, i8

# u8 is an unsigned 8-bit integer so its range is 0-255:
a: u8 = 12  # valid
a: u8 = -12  # invalid - The type checker should mark this as an error.
a: u8 = 900  # invalid - The type checker should mark this as an error.

The types are defined as NewTypes of ValueRange annotated integers. This is also how you can define your own custom ranged integer types. As an example this is literally how range_typed_integers.i8 is defined:

from typing import Annotated, NewType
from range_typed_integers import ValueRange

i8 = NewType('i8', Annotated[int, ValueRange(-128, 127)])

To cast values to typed integers, you can:

  • use the NewType’s constructor: i8(12)

  • Use the “checked constructors”: i8_checked(12). These will raise a IntegerBoundError if the value is out of range (subclass of OverflowError).

  • use the cast function: cast(i8, 12)

Runtime checking

You can use the function check_int to check if an integer fits into any integer type.:

>>> from range_typed_integers import check_int, u8
>>>
>>> class A
...     field: u8
...
>>> # You can use this directly with a type.
>>> check_int(u8, 0)
True
>>> check_int(u8, -1)
False
>>> # Or have the function look up the type annotations for an attribute on an object.
>>> check_int((A, 'field'), 0)
True
>>> check_int((A, 'field'), 256)
False
>>> # You can also use this with non-ranged integers (will always return True).
>>> check_int(int, 1234567)
True
>>> # Or even arbitrary types (will always return True and do no type validation).
>>> check_int(str, 1234)
True

For ease-of-use the types shipped with this package, also have “checked constructors” (eg. u8_checked), that will cast a value to their type and raise an IntegerBoundError if the value is out of range.

MyPy and Python Support

This is only truly supported in Python 3.9+, due to the lack of the Annotated type in earlier versions.

However 3.8 is also supported via typing_extensions. Note however, that MyPy with Python 3.8 will not accept Annotated as a type to use with NewType.

Metadata

Release files for range-typed-integers 1.0.1

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

Built distribution (wheel)

Table of built distributions (wheels) for range-typed-integers 1.0.1
File Interpreter ABI Platform
range_typed_integers-1.0.1-py3-none-any.whl Python 3 none any Details

Release files / range_typed_integers-1.0.1-py3-none-any.whl

Download URL range_typed_integers-1.0.1-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35d39a41642503c5c5117e26798713f081b1beece1b2afd8f1ba70c8d90f63c5
BLAKE2b-256 checksum
How to use checksums
2e49565350c6fab3f5a3e2c46633290117060e70e2501544cdf3bde1d1d5d0fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.12

Release history Release notifications | RSS feed

This release

1.0.1 This release

1 release file

1.0.0

1 release file

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