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

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

Total release size: 22.3 MB

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

Download URL typedunits-0.0.1.dev20250212005636-cp313-cp313-win_amd64.whl
Size 741.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
e6507767e8d60a00a7c943cfdc8c629be0c185e981edcec4746ac5aea5a73ed1
BLAKE2b-256 checksum
How to use checksums
429c5ccb998eb050fea45a79d2e7e01b851ac7ce8de938852061bf606f0d770d
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.dev20250212005636-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250212005636-cp313-cp313-win32.whl
Size 699.1 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
e9357aa8488a28e2cbe53f68ac6aacc989ec6927cb1eb75d7e760b9f8bf5f620
BLAKE2b-256 checksum
How to use checksums
05f019692e5e80829ed2265800a07a942cf8ab4ca31a282feda2e82409579888
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.dev20250212005636-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
7244f69fa6650dcc5d66f99490b823c4584d5b7be91a5e86fcad2081b11981ed
BLAKE2b-256 checksum
How to use checksums
d3320bfc6dc48479de7c887a8c8becb1f6af80a75b5769803851c83155d493e8
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.dev20250212005636-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp313-cp313-macosx_11_0_arm64.whl
Size 769.1 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6c857c6695854fcd05e60462a74676c65a1aa6b3a39cb6ecdc03e0963ef1a59c
BLAKE2b-256 checksum
How to use checksums
7540dfef78a599893d725859b94c86410ed93f4fe2e71199445e105cb7262b3e
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.dev20250212005636-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp313-cp313-macosx_10_13_x86_64.whl
Size 808.3 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
9df74213483d82e89797efb4b7b61eded23b4204bedcb6c7ac426154e3f32b36
BLAKE2b-256 checksum
How to use checksums
a8d5ea961a7b6cd55aa2711573d454389f5734a389c8cdb17d245ad4e02c0ddb
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.dev20250212005636-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp312-cp312-win_amd64.whl
Size 742.6 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
c973044621841240f25a645c157d7a327c34b336e2155cbf8935eec3b4536814
BLAKE2b-256 checksum
How to use checksums
b65088e17a36ca7d2969e43512d0d96b658467a0599ffec1cda3e1a3871a1999
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.dev20250212005636-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250212005636-cp312-cp312-win32.whl
Size 700.1 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
6dc74f9401218cf966f9f5e8b431658331cc33e47244ef640fca1528f5253eb2
BLAKE2b-256 checksum
How to use checksums
7e1f2a77aa1896592b12253eb0271c9a49641f21c096722fcfcd37a1f9cf18b9
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.dev20250212005636-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
f0cd3b2361612a000bd20a8d8a6a0524b9aba8585b01e5b61013f44fe2d15b56
BLAKE2b-256 checksum
How to use checksums
4e2089d5a8ac2e82807298f41571fe05392d62a6b7954f8f8854f7fde9badc52
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.dev20250212005636-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp312-cp312-macosx_11_0_arm64.whl
Size 776.3 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
19630548131dc1ceaae6c8aa992b5889a753ea03062c10bc4e155f5200da4f56
BLAKE2b-256 checksum
How to use checksums
3f58c91f70f5e9d7cfbe492e49062902f352ebc0f1af949785acc052d94f2d32
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.dev20250212005636-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp312-cp312-macosx_10_13_x86_64.whl
Size 814.4 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
5073feb6297de8550a9b05520a98e987de77a09a310e12e4f3071a5e9dd87956
BLAKE2b-256 checksum
How to use checksums
9e7368ad88da2574c4c71bc32a215de9329eb1e3729815769b302f58437068b7
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.dev20250212005636-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp311-cp311-win_amd64.whl
Size 769.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
121f17197b8c93d884651956e3ebb0fd159016affeca4e273383602cc9de3ecb
BLAKE2b-256 checksum
How to use checksums
474faf6a597b94b63e5464ea642a55de5ea30103bbec71c162f3926f95c554e2
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.dev20250212005636-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250212005636-cp311-cp311-win32.whl
Size 735.1 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
aee63b96cc6aa0a655d5c89632d560e6af2819dce806f29a2498e44358105453
BLAKE2b-256 checksum
How to use checksums
3524a03ffcad09fad8ecb6cca166888c159274215f63eeb9717e171091662264
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.dev20250212005636-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250212005636-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
6abca848ed7813d20d379cb48d2d96536ef995d8af460d68c995e23fac8fa66a
BLAKE2b-256 checksum
How to use checksums
fb28854c71f8a8f4b55e1e05ec49c35bf6605cf3c986a4bececdfa2a9eaef6cc
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.dev20250212005636-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp311-cp311-macosx_11_0_arm64.whl
Size 790.3 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
22a4c02a7513e1b27734dbfd6b9e2339981c323615a3e8514394e20044fa68fa
BLAKE2b-256 checksum
How to use checksums
3b693e28b13cf6538763d0cba403579fdf89b715c3f285bf1764ff8d86d77d36
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.dev20250212005636-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp311-cp311-macosx_10_9_x86_64.whl
Size 851.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
cdc343de97ce3095b20ac5315e8a5e691af9e5b1640b7687b02b3bfb47fa5e8d
BLAKE2b-256 checksum
How to use checksums
7484b4d75435b1d4ad7c00ddde63d8236ffc3627acede59d6b665cf542724297
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.dev20250212005636-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp310-cp310-win_amd64.whl
Size 766.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
0b093d7ad20e9d64fed1953800cb5068072b65205001309ba820180bd4075dcd
BLAKE2b-256 checksum
How to use checksums
da0e34610e32b12a99d1e542d2c6b339d3303a82cb2d5dace9f677d4e1b1a3f8
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.dev20250212005636-cp310-cp310-win32.whl

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

Download URL typedunits-0.0.1.dev20250212005636-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
2a83f9fcf02d8e49a2bae4bea1565dcb8fefb40a1b4dc86f9d982f6fbeda5952
BLAKE2b-256 checksum
How to use checksums
90b21b98587ea4e30dbc53cfd633c8c2c1af03bf79eb35f0e5e14cbda9b58c97
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.dev20250212005636-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp310-cp310-macosx_11_0_arm64.whl
Size 756.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bc94c2b38f67f963d26622c610b1faf2c459f4751c3816df037ea3d95bfea053
BLAKE2b-256 checksum
How to use checksums
792ab6d90c82bb45791aa44525c21c7f0f59e754901b21f1690e3694b7556fae
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.dev20250212005636-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250212005636-cp310-cp310-macosx_10_9_x86_64.whl
Size 815.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
dada9f52af9f65634d0c5df96523a98ab909917b0b4983cd65b1f110bba8744b
BLAKE2b-256 checksum
How to use checksums
992bf2e49eb68e29fb937ae6db06b46c88dc9e289f775105d86bc54ee8c5f5e8
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