Integer type aliases that specify valid value ranges.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file range_typed_integers-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: range_typed_integers-1.0.1-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35d39a41642503c5c5117e26798713f081b1beece1b2afd8f1ba70c8d90f63c5 |
|
MD5 | ff8901e86953d35fa680991fc7501b65 |
|
BLAKE2b-256 | 2e49565350c6fab3f5a3e2c46633290117060e70e2501544cdf3bde1d1d5d0fe |