Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

TUnits - Fast Python Units

Continuous Integration

Implements unit of measurement arithmetic, where a number is associated with a product of powers of base units and values with compatible units can be added.

The library is written in Cython for speed. The library provides the ability to statically check dimensionality type (see below) and a limited protobuffer serialization support for select units (see below). Contributions to extend support for more units are welcome.

A precompiled wheel can be installed using pip install typedunits [--pre].

Example

>> import tunits
>> from tunits.units import meter, km, N, MHz

>> print(3*MHz)
Frequency(3, 'MHz')

>> print(5*meter + km)
Length(1005.0, 'm')

>> print(N/meter)
N/m

>> print((N/meter).in_base_units())
kg/s^2

>> print(2*km / tunits.Value(3, 's'))
0.666666666667 km/s

Static Type Check

TypedUnits provides the ability to statically check the dimensionality of variables and parameters. For example mypy would complain about incompatible types for the following code.

from tunits import Frequency, LengthArray
from tunits.units import meter, km, MHz

def frequency_fn(f: Frequency) -> Frequency:
    return 3*f

x = 2 * meter
x_arr = LengthArray([1, 2], km)
y = 3 * km
f = 7 * MHz

# okay
print(frequency_fn(f))
print(x + y)
print(x_arr - y)

# not okay
print(frequency_fn(x))
print(f + x)
print(x - f)
frequency_fn(x_arr)
$ mypy my_code.py
my_code.py:18: error: Argument 1 to "frequency_fn" has incompatible type "Length"; expected "Frequency"  [arg-type]
my_code.py:19: error: No overload variant of "__add__" of "Value" matches argument type "Length"  [operator]
my_code.py:19: note: Possible overload variants:
my_code.py:19: note:     def __add__(self, int | float | complex | number[Any], /) -> Frequency
my_code.py:19: note:     def __add__(self, ValueArray | list[Any] | tuple[Any] | ndarray[Any, dtype[Any]], /) -> ValueArray
my_code.py:19: note:     def __add__(self, Frequency, /) -> Frequency
my_code.py:20: error: No overload variant of "__sub__" of "Value" matches argument type "Frequency"  [operator]
my_code.py:20: note: Possible overload variants:
my_code.py:20: note:     def __sub__(self, int | float | complex | number[Any], /) -> Length
my_code.py:20: note:     def __sub__(self, list[Any] | tuple[Any] | ndarray[Any, dtype[Any]], /) -> ValueArray
my_code.py:20: note:     def __sub__(self, Length, /) -> Length
my_code.py:21: error: Argument 1 to "frequency_fn" has incompatible type "LengthArray"; expected "Frequency"  [arg-type]
Found 4 errors in 1 file (checked 1 source file)

Serialization support

TypedUnits provides protobuffer serialization support for selected units. Contributions are welcome to increase serialization coverage.

>> from tunits import Frequency
>> from tunits.units import MHz
>>
>> v = 3*MHz
>> msg = v.to_proto()
>> print(msg)
units {
  unit: HERTZ
  scale: MEGA
}
real_value: 3

>> Frequency.from_proto(msg)
Frequency(3.0, 'MHz')

Installation

  1. To install a precompiled wheel (add --pre for prelease version)

    pip install typedunits # [--pre] 
    
  2. To locally build the latest version from the main branch

    pip install git+https://github.com/quantumlib/TypedUnits
    
  3. For a local editable copy

    git clone https://github.com/quantumlib/TypedUnits
    cd TypedUnits
    pip install -e .
    

Development and Testing

  1. Clone the repository.

    git clone https://github.com/quantumlib/TypedUnits
    
    cd TypedUnits
    

    All future steps assume you are in the repository's directory.

  2. Install dev environment dependencies.

    pip install -r dev_tools/dev.env.txt
    
  3. Install TUnits

    pip install -e .
    
  4. Test.

    pytest
    

Formatting

dev_tools/format.sh  # to format
dev_tools/format.sh --check  # to verify format

Note: This is not an officially supported Google product

Metadata

Release files for typedunits 0.0.1.dev20250305213057

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

Built distributions (wheels)

Table of built distributions (wheels) for typedunits 0.0.1.dev20250305213057
File
typedunits-0.0.1.dev20250305213057-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
typedunits-0.0.1.dev20250305213057-cp313-cp313-win32.whl CPython 3.13 CPython 3.13 Windows x86-32 Details
typedunits-0.0.1.dev20250305213057-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_10_13_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250305213057-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20250305213057-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20250305213057-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250305213057-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20250305213057-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20250305213057-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20250305213057-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20250305213057-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20250305213057-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 23.3 MB

Release files / typedunits-0.0.1.dev20250305213057-cp313-cp313-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp313-cp313-win_amd64.whl
Size 776.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
9038505c310bb4bbd1c0f24689064c865bfa068a80f7c8ca1bba7b486d8d3e91
BLAKE2b-256 checksum
How to use checksums
6d5422cbaf63f77de6bf42cb1cbf6eeb507ff406b307f6a7bb6a499669a84147
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250305213057-cp313-cp313-win32.whl
Size 734.2 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
633d877a8731c0760c4f4b9428f0b5a88d70ee0b93bddf78637654793345509d
BLAKE2b-256 checksum
How to use checksums
5efed390cc45d634625c1c72b393d69160f3edcad0e220a98b51a3c305c0fc45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
34fc4f6537d6f213784bb305021b2fd1a5d6c9e1649814fb5b99f2cfdca20fb1
BLAKE2b-256 checksum
How to use checksums
d85e3d85e73671584007f5b4103dcde17095825772b27e76f525966e56600c91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_11_0_arm64.whl
Size 803.7 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1f08b6f603d7429b86e8f36c1fffdd12e0ff5adbd802f38eff59ac75c86d5d21
BLAKE2b-256 checksum
How to use checksums
2b9c8b5ac441e50588479c91d5705e5893bbe909ccd2da0823274a6038f3592f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp313-cp313-macosx_10_13_x86_64.whl
Size 843.7 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
6cd3945611988dac54940d3bc8d4d1c370668dda46af8c3e4fe72fef2ab1251d
BLAKE2b-256 checksum
How to use checksums
ad5e9fd430dd653702db5da5c1a893617a593f45c7154f885843232531b99926
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp312-cp312-win_amd64.whl
Size 777.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
8bc5b9c3420e437410604d1283b524a18c0af395f3ca1aa95e9171bb7156111e
BLAKE2b-256 checksum
How to use checksums
4981baec77903815a0931b09892999a63d446531c17256a112e39b9cd00fdc80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250305213057-cp312-cp312-win32.whl
Size 735.3 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
e2283708ed241acca9a24247c4262ce1f49f0189405c8fdef874c687e2631d02
BLAKE2b-256 checksum
How to use checksums
73373c80807635faeadcc86d892cd16cced654604202f8d5798648fbea88bc22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b785bf2b495c7157bf39d0d6c19dc65631d44c4450bc3e673d162739b076e24c
BLAKE2b-256 checksum
How to use checksums
e5a38aa7efd375a2dc445abb19e0f35f19ed55a3fe9430d3c69888009a6ef818
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_11_0_arm64.whl
Size 811.2 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d6559d50246efe92405567e774c7c12cded274e156d06a93bc6ec0507abde231
BLAKE2b-256 checksum
How to use checksums
fa47a6a08654421cc45f7182045ab2cb5bbf03e0932704d3fb8f623022097882
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp312-cp312-macosx_10_13_x86_64.whl
Size 852.2 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
5e1dc8ce0bc43e31e6e6cba5d04fd5bcacb86fc81120eef110b2ebd5877b8dfd
BLAKE2b-256 checksum
How to use checksums
35bea053438cf6a9e275c219e0211bb1b614f327850540d34646a06361820d67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp311-cp311-win_amd64.whl
Size 805.8 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
87ed06b0216bfebe8755768459ffb1c1866ccc7105d661d74409f383d9acac9c
BLAKE2b-256 checksum
How to use checksums
af614f3cd1871544277a95372b10cb504bca99b7558f3183271c5dc11793fa10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250305213057-cp311-cp311-win32.whl
Size 770.0 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
7400ccedb658671847cd6b5ce0845e5804dfe37e9ed54a7fa62bb0701a7b1ab1
BLAKE2b-256 checksum
How to use checksums
7d3eb05020748deb141e0b68897bc01054e7e0031e0b6504dc82ffbdf3167442
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
64fe258405d2aec078ab46cd04a65971174c909a0021b107e1c4bdabc67e8ebc
BLAKE2b-256 checksum
How to use checksums
5acca5b57261915b55910e21db3c615fa13a87575ba1fcc9f5edc4639de82109
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_11_0_arm64.whl
Size 826.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7963b59003a800ce1337d10ed07f3cfa525feb7e894e60fe7542853c64b6125d
BLAKE2b-256 checksum
How to use checksums
deb10071c71eaa588ffeed675d157d11cf3a660ea1a8b6831f1d93f3967c5569
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp311-cp311-macosx_10_9_x86_64.whl
Size 888.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
68dede02766bb20197a87369a1e32cf69a2553cf96e0b14cbb220f794f345dfb
BLAKE2b-256 checksum
How to use checksums
6adeeaeef55f224eb729ff67d31f904368f8b6efc592f151d178a26b62478718
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp310-cp310-win_amd64.whl
Size 802.3 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
ebf8a83669b386930e930ce0b4bfb79f7f8527de699d9991206004240b51def4
BLAKE2b-256 checksum
How to use checksums
0587f78fcb19201a1f7233d9a6d4afa7adb03471d30cf6f5db33d1417d13de6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250305213057-cp310-cp310-win32.whl
Size 737.9 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
b63d2d34944fa21610121ff402851e4a6759a3335a95cf8b3ee94a3601b14e95
BLAKE2b-256 checksum
How to use checksums
276fd87f538b29d58a555689e86771417a8332821283ab70c7f0c958fe579be8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e8585f284a4d48059e7732e031e1bf47bdbeab32616391945803abf334deb865
BLAKE2b-256 checksum
How to use checksums
5f440688f03ab12eeecd7cd8cf61251719bdec64fbec38acd84987cd89b938c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_11_0_arm64.whl
Size 793.5 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ad5b258360f04339e0064a8ee541929e43ea16e94debe07fd8696bf022825518
BLAKE2b-256 checksum
How to use checksums
8b4838bfae4d023ba2a23dc755ccab8d94a66f0e98a8213b724508b89c52d515
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305213057-cp310-cp310-macosx_10_9_x86_64.whl
Size 853.0 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
97fcc880019a3fe20a045a1cda58a4ecdd23eb76e50cb075762d628e3d635455
BLAKE2b-256 checksum
How to use checksums
710d8066badefaf12bb0e98c858e2ab935b0c03160f27e415856935fab2c4373
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release history Release notifications | RSS feed

0.0.2

30 release files

0.0.1

24 release files

This release
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