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.dev20250125001019

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.dev20250125001019
File
typedunits-0.0.1.dev20250125001019-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details
typedunits-0.0.1.dev20250125001019-cp313-cp313-win32.whl CPython 3.13 CPython 3.13 Windows x86-32 Details
typedunits-0.0.1.dev20250125001019-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.dev20250125001019-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125001019-cp313-cp313-macosx_10_13_x86_64.whl CPython 3.13 CPython 3.13 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250125001019-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
typedunits-0.0.1.dev20250125001019-cp312-cp312-win32.whl CPython 3.12 CPython 3.12 Windows x86-32 Details
typedunits-0.0.1.dev20250125001019-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.dev20250125001019-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125001019-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
typedunits-0.0.1.dev20250125001019-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
typedunits-0.0.1.dev20250125001019-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
typedunits-0.0.1.dev20250125001019-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.dev20250125001019-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125001019-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details
typedunits-0.0.1.dev20250125001019-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
typedunits-0.0.1.dev20250125001019-cp310-cp310-win32.whl CPython 3.10 CPython 3.10 Windows x86-32 Details
typedunits-0.0.1.dev20250125001019-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.dev20250125001019-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
typedunits-0.0.1.dev20250125001019-cp310-cp310-macosx_10_9_x86_64.whl CPython 3.10 CPython 3.10 macOS 10.9+ x86-64 Details

Total release size: 21.7 MB

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

Download URL typedunits-0.0.1.dev20250125001019-cp313-cp313-win_amd64.whl
Size 721.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
d3715f1b01b9447363b4626bb46d6742400ebd39c190f2de5dcb99a2223ac7c7
BLAKE2b-256 checksum
How to use checksums
3d05a822350ab331f74474474c5385fdaca19fcf6e1e69a8e4322d7bf39b9120
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.dev20250125001019-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250125001019-cp313-cp313-win32.whl
Size 681.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
33892f56b8a6a4f3e9fbbd5a6e2319bec4ad4cc5ff9e5a1ce6f867ea5fe8e5a5
BLAKE2b-256 checksum
How to use checksums
c916f5ee80d0e2bb0862c8f7a55337c3929ed9d2f296893ea85440cd05601e19
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.dev20250125001019-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
151a15fdbaf191fa41603e2e20c82d2f2c5ebafaae17e66954d7c64f4e03025f
BLAKE2b-256 checksum
How to use checksums
931f4558065f2b5a387dc11006896f6936f2d7768d809402acd8842455dc4214
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.dev20250125001019-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp313-cp313-macosx_11_0_arm64.whl
Size 748.2 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6c3bcbc4b637570f7ef9d0e8d1848de6e016ef0aeecf828d070e87ff5800ab13
BLAKE2b-256 checksum
How to use checksums
57a0f2a4a75bde53ced05020b0c19f6867befd0fbc50006b9e09fd2421531708
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.dev20250125001019-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp313-cp313-macosx_10_13_x86_64.whl
Size 785.4 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
924ac15672e7cd211e50a7f0e5f186149e6c1a2e22d1f8fa7aeb594ab6b6ed44
BLAKE2b-256 checksum
How to use checksums
b811b7d270247775fa5021b21c2741e4d7df3c427490a4da3c9a8d6147d10ca6
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.dev20250125001019-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp312-cp312-win_amd64.whl
Size 723.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
3cb197ddf8b7ceccbacb36c14489b356adb331950cb5c4d51adc25436e822b94
BLAKE2b-256 checksum
How to use checksums
4a089bdc8c86d2d3e063d42e50664fc7a5c2123e02c0acf9922941d6bbffd1b8
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.dev20250125001019-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250125001019-cp312-cp312-win32.whl
Size 682.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
8f87d68c380c7fac623550289ffb3f74e3b947bcfde8620971ece0a103bf13c4
BLAKE2b-256 checksum
How to use checksums
4dae9ab2ec5815dfbd0ce22da4fd1b4b859b5faa28aa015080d02b6fafe8aa62
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.dev20250125001019-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
74d575ff5a6b1e6f4ebb3d0408ef48cfc90d43fe73c2892111a27e346af3b877
BLAKE2b-256 checksum
How to use checksums
d459e883ed25ad3b4e610d3577b46d2199aed7dc6dd2ec80966e642d48bc9465
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.dev20250125001019-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp312-cp312-macosx_11_0_arm64.whl
Size 756.2 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0e6f268d607531cd06225d99bd80156cef50dcbdad45a69853c6543105ec9210
BLAKE2b-256 checksum
How to use checksums
0b2105e1e33b142156fcfb1b54ec4a6bd254f327737f86b4f7a0d5a1a6951987
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.dev20250125001019-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp312-cp312-macosx_10_13_x86_64.whl
Size 793.2 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
93a856b95e1ced852113088e29ea73395c1bf54ae55a36c4d555967922b0f119
BLAKE2b-256 checksum
How to use checksums
dbb318af8870b86e40fab0aa947ac2bc5cc9976dda86cc2685a1a23c3e842915
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.dev20250125001019-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp311-cp311-win_amd64.whl
Size 750.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
acca9bad8d3a8a9a9d69485b29fd27c1f20a98911e822ed3dfa64526a66874cc
BLAKE2b-256 checksum
How to use checksums
07ae906fa6180f30d0ef5f44f0bb4887e290c82119b3f22c2685d7d6921f08ad
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.dev20250125001019-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250125001019-cp311-cp311-win32.whl
Size 715.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
2598ae44e3653dc17fb46f87b93561be897bb9bba9c50dcf9a949598d52652e4
BLAKE2b-256 checksum
How to use checksums
b51275c3459bd5a853759730aaa8a11cb6b26c91133d944de77892138ef4fa13
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.dev20250125001019-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b1a887f9cbebafdb53f1629032594496ac10d46103c0a45d8522edef4183efe4
BLAKE2b-256 checksum
How to use checksums
160e86ce68bce4d2802f3ac27768cc55bafaf29c2a8756e4c95b5e9f68ae6ae4
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.dev20250125001019-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp311-cp311-macosx_11_0_arm64.whl
Size 767.4 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6759efd64e122d14b44ae212fefb4c9811757dc32a9c902ba4946667affee34b
BLAKE2b-256 checksum
How to use checksums
042d8150aacff5844985d34e2e2caf87f28b65ad64c8cf89162e68cf8bb9161e
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.dev20250125001019-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp311-cp311-macosx_10_9_x86_64.whl
Size 826.4 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
43b8baca1594432655db2565f0e65bb7a565f9d1454bed70d1941249d68a5565
BLAKE2b-256 checksum
How to use checksums
a9d5f8e111d05ede4e17086415e828699850470c90a8a6270a8d4b01de70bda2
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.dev20250125001019-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp310-cp310-win_amd64.whl
Size 746.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
722da83b975cc6e958acdebb55a96b13b1556f776d658c5ef215d9057327326a
BLAKE2b-256 checksum
How to use checksums
4dc6ff46ae84610e24b8581515bcc3db86326dceb8b0274220f9fa7f978748cc
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.dev20250125001019-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250125001019-cp310-cp310-win32.whl
Size 684.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
e12ede91c249ec7845f560fd58a5c0d17784ec4373cd8566728275da6c454e13
BLAKE2b-256 checksum
How to use checksums
fac0270bc02588f8002826557abab505df987417dd6e0270371b5d4d92840619
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.dev20250125001019-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
ee64aed05fdb283eb593caec12dc54f5bb28ad4a19229da2dbb040ae386f9c0f
BLAKE2b-256 checksum
How to use checksums
69ad0a253432ce53299d17c948322129abf3ab0dcc4bc74596eb20648aff93b3
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.dev20250125001019-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp310-cp310-macosx_11_0_arm64.whl
Size 735.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
aa04245c56569ac6ff538daae4f390e7d1929fdb4849d48e99e54e20d52a3d4d
BLAKE2b-256 checksum
How to use checksums
5e21256dba1db50810141494cba887a571d48b17263d736134462882da11af83
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.dev20250125001019-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250125001019-cp310-cp310-macosx_10_9_x86_64.whl
Size 792.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
06167d8d09c2b7b6c70c0eda825e511e6569c5d2beaa1538e6bd88c56c0ea3de
BLAKE2b-256 checksum
How to use checksums
d2eaafda264031fc23be1322776653e5afa67ed070fa918c0b85c915059032bd
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