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

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

Total release size: 23.6 MB

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

Download URL typedunits-0.0.1.dev20250422140511-cp313-cp313-win_amd64.whl
Size 783.4 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
29801c78b4b4c8d80a459905f3c283a4d3b49f1079abf875f30f9d6eb8dd6078
BLAKE2b-256 checksum
How to use checksums
bfd3cc716c83e065ded46c7beebb6858d8de9dea89539333f0df21b71fd70109
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp313-cp313-win32.whl
Size 741.4 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
03811af157b9cd062c54c840183aa72298d18535fa762d39edde4a9d446df34a
BLAKE2b-256 checksum
How to use checksums
35ae616968ece2723542d96d10e311ede7148bec6c19c8b3c2f3d9cc295da161
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b92782315e52e900854d984a693130dae4e1a44083fb9ad689453fb22bc3ae4d
BLAKE2b-256 checksum
How to use checksums
4ea3c8959ed4b8124fa56431f8c3e523d22c4adbc21886585fc910b11a767289
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp313-cp313-macosx_11_0_arm64.whl
Size 814.1 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b20fb4293f40b6ed541f5dc66d8be2ad14d3fd8363e6a0f436ec324a55f2f9fe
BLAKE2b-256 checksum
How to use checksums
202715c1ab57cbe9e49a6620a1a9dbd062d59a9bb448afe332f4163bc6ad9fcb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp313-cp313-macosx_10_13_x86_64.whl
Size 854.7 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
e5b8b6775012ce7628ad523a9043c51c4ab4ed8c91fe8f2e8d6ed5bb52fb6a9d
BLAKE2b-256 checksum
How to use checksums
a7afc09de5f0f3ba31f8ce3b65f3e065913781c80d6acf45cade79137611d8b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp312-cp312-win_amd64.whl
Size 784.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
e6170153064297500002d4c44d52fbed7d16c7b27c8f43f86573f8f1472c5a13
BLAKE2b-256 checksum
How to use checksums
abb379e82d30bc01ec5bcb1976b86ea5552a7d3294936aaf441bb8ce352ff7ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp312-cp312-win32.whl
Size 742.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
9e26a72a8b3a5ba4bc051f4e29a7891522d455287d136a06fe34a85198fcf176
BLAKE2b-256 checksum
How to use checksums
4db3cca75d9a421c4c4ec78954eadf009dc9a40ae027aa8afdf6ad91bf2234b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
48eff2f6a738e46f927a496b5443d720ca55a8896e5d13ca1e20063c10af9c14
BLAKE2b-256 checksum
How to use checksums
725ccb4793adcc9a2df23198953e3ecf8f71d18c5947e7e466b663f7e4d0ada7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp312-cp312-macosx_11_0_arm64.whl
Size 821.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1b9edd07f75e2ebd42adfa4ad1de7a83f6d935cfebf8cce3a7d55a2dede27be6
BLAKE2b-256 checksum
How to use checksums
271100d79b442568d1266e272b30ab36a3214e11cff3c548070f620fb50fb992
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp312-cp312-macosx_10_13_x86_64.whl
Size 862.4 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
316dcd72a9025655806d0dcd769574555016ac5579a0798c3fc7575a9e0602ac
BLAKE2b-256 checksum
How to use checksums
00b312d3f685fb9ffe179f73481f49d05d9c8946d7ae9fd3b588d1579f69aae5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp311-cp311-win_amd64.whl
Size 813.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
f5e8f30990da6d0c448493c8753cb9c50c19192b8b2789aef984472b966407cd
BLAKE2b-256 checksum
How to use checksums
931e7bcfb9bf3a86ea9442bfc728d1fa221da4aa1fd5c538ef1f1820cd91a73a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp311-cp311-win32.whl
Size 778.1 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
ee7159bfbf3847b1ab2abdad3b83b7ff2d55b8e4d9482d3c72680448e2de5eb8
BLAKE2b-256 checksum
How to use checksums
b2df2c153922866b6d5dd59f451d726d3d235023cd42eee2e28f8a7563747885
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-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
55133b1268398a36798676b7136ceb67e955a7a4e1872f3368ec13bba50bf147
BLAKE2b-256 checksum
How to use checksums
315eaa136a904c81e31bdd2af481eb740a14c4483fa4fbef01c89dcc62da6517
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp311-cp311-macosx_11_0_arm64.whl
Size 837.0 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bee155c8240882ce61d01da02f96693b5663f675d08b005fe63599d6b9610e9c
BLAKE2b-256 checksum
How to use checksums
887d8d670c67ace2da4d75c727433f3b98aafa1ac6f2c765ec91b2233327d50e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp311-cp311-macosx_10_9_x86_64.whl
Size 898.6 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
716fa947f8f03a68a19fbc418aef3d5ea66ac59fc074a0d85eeecd77f1e545d5
BLAKE2b-256 checksum
How to use checksums
7867e3402191b7fb8408470a45e143a3758eed312d2b8e6b6983c91cc336a76d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp310-cp310-win_amd64.whl
Size 809.9 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
1521698a0bcdbfdebddae2d10516e8fcbddedfa4ab42e1d7085d3e8e765aef47
BLAKE2b-256 checksum
How to use checksums
7e31dc93cf7831d4d81e3c7bed845b9a3e0f7313472d1a8ac1773ce3219ac2c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp310-cp310-win32.whl
Size 745.1 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
3730c78e35bf5d23a5abec1cb8d70ce75f36781db150ad649b65450274bf2f1e
BLAKE2b-256 checksum
How to use checksums
f32df74d7abe5e5792ea1c93fcd8eae74ec462e09d03f95b418ed15497cf7455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-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
4ce637557eb40e06a290612fe70ab11fc026bc0b9538ed505f2c6537170f07bf
BLAKE2b-256 checksum
How to use checksums
885d9ace07667a6f54811247ed77eae9d60fd2baa4e41e4c60a259ae0c635af1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp310-cp310-macosx_11_0_arm64.whl
Size 802.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4dd6592ff24b11f020300b1b983ae25addc7a13ed400549e4e67733d53911efd
BLAKE2b-256 checksum
How to use checksums
246964cf06b58ec65087d55913a0016729ca4d3ffdda158ce521fca8f90c07ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250422140511-cp310-cp310-macosx_10_9_x86_64.whl
Size 861.8 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
1ede7254e45d64512802dd3b980f50973dc2b84e9d659d9c2161f7d38cee9c7b
BLAKE2b-256 checksum
How to use checksums
1ce81ac2cfcf2e426e972b911571c29ac0e39011487a9cd00fa5e34724dc3834
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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