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

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

Total release size: 22.4 MB

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

Download URL typedunits-0.0.1.dev20250211014127-cp313-cp313-win_amd64.whl
Size 742.2 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
5b9234b0f03567b24e765e1cdd7f65d1900563a47e207092957b81f992a7b9dc
BLAKE2b-256 checksum
How to use checksums
acb84438354aab664a54646ff40480994ad1075aa58e3b50204092f19eb299c7
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.dev20250211014127-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250211014127-cp313-cp313-win32.whl
Size 699.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
e924d1c34605cf97f179cb46e8fd7acbb4219ba6aac53a726040e33764339380
BLAKE2b-256 checksum
How to use checksums
e3eea68bf1a122fcba8e19b2b43e10d697de82c57ccf5685d76642274f7a97c0
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.dev20250211014127-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-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
d760d5360b6df6c14011b69446e3146aca5e990d857f249125c0f0720dea1153
BLAKE2b-256 checksum
How to use checksums
b5d3e13fd94261306a052efb14ee81d511c3c7d978704d1c726cdfb0ee7f3d96
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.dev20250211014127-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp313-cp313-macosx_11_0_arm64.whl
Size 769.8 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4a7338431984cc528befb3a9b99f230cdaab754d284d69e3d7f599fb7b1194e9
BLAKE2b-256 checksum
How to use checksums
567acf662af9ff191f3cf7af6759369d7cdeb25149dd342685a896da36cfd412
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.dev20250211014127-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp313-cp313-macosx_10_13_x86_64.whl
Size 809.1 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
6e3662c9318c66c1d617005b0e920cd4dafed68f72e4ddfb80ffbc4d30fa4d59
BLAKE2b-256 checksum
How to use checksums
ee440c17d129a2c441a14a2dca21def31a902ac27b27b731564a9863c619386a
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.dev20250211014127-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp312-cp312-win_amd64.whl
Size 743.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
58fc544eed41039f70ea556b1be0a5f7d7e3fde1f152f643840a3b844f9ef565
BLAKE2b-256 checksum
How to use checksums
436157f1fda14f0c472b969a41e49abe99a754e7f4c78ba7ac0ce700750c7a5e
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.dev20250211014127-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250211014127-cp312-cp312-win32.whl
Size 700.6 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
55d3678cf23eadcef899e77684e7ecd8c08ea8e99ac8d2411f4684bd50a0fc26
BLAKE2b-256 checksum
How to use checksums
2b1bebef2b3b79894b63236f527875fa71a6890485e9b984f4728d723aeec7c7
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.dev20250211014127-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-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
751630383f74be8aba4dd60365dcafabfb6b1ba7b5490bf84ec47ad15f7fc346
BLAKE2b-256 checksum
How to use checksums
7fca2c7954ebcd45cb8a42bb330ba781129bbcda54d267049e8ae717cad00f4c
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.dev20250211014127-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp312-cp312-macosx_11_0_arm64.whl
Size 776.9 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a81b3a6fdd3b3fd9a89f05699214cd3795aaf53f6025bc58eb290646d9cee98a
BLAKE2b-256 checksum
How to use checksums
70549cae935263c3bdd579e7d37b400610286b78c489fd6a9e5150d9b3ea31cb
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.dev20250211014127-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp312-cp312-macosx_10_13_x86_64.whl
Size 815.2 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
35c606b561528e71454ef92987050156ba7c71a9c6f0a8f76e1bc66660776418
BLAKE2b-256 checksum
How to use checksums
89c35631708e429d928e44d2609109ecb20a02650ad77b74860154f91ffb258c
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.dev20250211014127-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp311-cp311-win_amd64.whl
Size 770.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
600ecf80e3b25d19c74ed16369c406e5bcb8a53429222a89765f311a357d6738
BLAKE2b-256 checksum
How to use checksums
83ce0e041c2b44c8a38c4b17634cc2b22be934eb3e8b1bde483199c49c0343a4
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.dev20250211014127-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250211014127-cp311-cp311-win32.whl
Size 735.5 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
eb0b6543c8ce488b59b84d64b95a70f24442895b8c8b1926a4668abcce122f4c
BLAKE2b-256 checksum
How to use checksums
700878e18bd2020ea5df3604d5fd32c08cf3518785d9bd2adbd9d62744dda61f
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.dev20250211014127-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-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
588914c27411abe464f61e679a60efe19b1c6ab60acd1d4342ba1bb36957c452
BLAKE2b-256 checksum
How to use checksums
2b5028e18af38455d260f64c86d5d299a2229dc0727e3ae91557e60e48cebb61
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.dev20250211014127-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp311-cp311-macosx_11_0_arm64.whl
Size 791.0 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
771aa74e13155f1dc84f3074a5adb7e5b0ee7b2c9219759dc028190634b3ded7
BLAKE2b-256 checksum
How to use checksums
9cb148bdf843ddf83a82614647a70693d6a7fd762c18b5548bbd190e60840ecd
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.dev20250211014127-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp311-cp311-macosx_10_9_x86_64.whl
Size 852.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ee390ac6031574298b170dd5d3c956549ff6071e68817dda6aec0158368911b5
BLAKE2b-256 checksum
How to use checksums
4751621bc7d7c7a327d7c471c1f19fd71be4fb427d8117e955bf1e9b806ac774
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.dev20250211014127-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp310-cp310-win_amd64.whl
Size 766.9 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
e5238ea84b861089fdb3bac66b31a68261ee6590dc382ecdae2deb3584f8bbd9
BLAKE2b-256 checksum
How to use checksums
9bd745c276fea67d2499ede6d540fda95800200141d26c55c3fffa42dc175f2a
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.dev20250211014127-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250211014127-cp310-cp310-win32.whl
Size 704.0 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
35ed3eec425fd1c4ebca613498aa1ab21ce382fc008db71eba5cdae6457ed085
BLAKE2b-256 checksum
How to use checksums
82dec6c6faa71d2b2748085477c2633f447622d76c8d320212f668bd2857b258
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.dev20250211014127-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-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
5b8153dd9ba88b7672618db0c565b92ee5fa95d9e593f715898dad9270a0d50d
BLAKE2b-256 checksum
How to use checksums
28e9c0a1d448a97733d06f88d7a462ca2e77fd237bdc56026b1f1105fc48010c
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.dev20250211014127-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp310-cp310-macosx_11_0_arm64.whl
Size 757.7 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c0792c88eaf89485d92bab7698735257bd1c63d294c564c0f66497d2e582b0be
BLAKE2b-256 checksum
How to use checksums
2caed054577331cf13025e8e12fad1bdd1f0a508a1ea4bc38bed67522efec8d6
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.dev20250211014127-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250211014127-cp310-cp310-macosx_10_9_x86_64.whl
Size 817.0 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
4402ac2e5fd7491b20e28f53e57aa6a74d7c82c10e4dc161758eef08078483f8
BLAKE2b-256 checksum
How to use checksums
70f243cb2adeb526a9a3c1f83cfe922c3213781a6221fc74d9244ca349429543
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