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

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

Total release size: 23.4 MB

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

Download URL typedunits-0.0.1.dev20250305230914-cp313-cp313-win_amd64.whl
Size 777.4 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
9da1c029afe85252a4d3108be6f5a5b6f60e43b90057d27a635ac79a3736e7e2
BLAKE2b-256 checksum
How to use checksums
ece0a5cdab101c920b7b90941af19796b164df46285e902d7ae91d5124bd8776
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.dev20250305230914-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250305230914-cp313-cp313-win32.whl
Size 734.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
de7b11f02e91512868602735c162e7bab9b1fbc55ff83869c85b3798cc650e6e
BLAKE2b-256 checksum
How to use checksums
435adb74eca10b2e2b5e334c09f5ff9be8eafaf437fb61f3ccde565c3a8eeeaf
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.dev20250305230914-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305230914-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
78bb6e647339431cef6ad59ce3da1b15795b5b2be36cd27c53c9c9975ad64e7a
BLAKE2b-256 checksum
How to use checksums
0ad9619c7ea27e9073a6ad6777b61f68d46036af3053d1d9d4c9010b91ad59b4
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.dev20250305230914-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305230914-cp313-cp313-macosx_11_0_arm64.whl
Size 804.5 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
63773f89628a27f3081433f8d401bb6f416561b1589343b404214700bd773fe2
BLAKE2b-256 checksum
How to use checksums
1b9d3cb24d62e8734168b622f78ccbd42bcf0e668dff85db16e10efbb84c947e
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.dev20250305230914-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250305230914-cp312-cp312-win_amd64.whl
Size 778.5 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
5a15109a5137441ea0c9b4f8d233b4f1dffe946f55d661aa8015f24b2e48bbc5
BLAKE2b-256 checksum
How to use checksums
2db40cbe272538d5b06f5006743adc746b46165219bcf73664456ce313b0817c
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.dev20250305230914-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250305230914-cp312-cp312-win32.whl
Size 735.8 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
a24df92c25f99a20848dc93fb5b199a4f141ea5ddce645bde4b8e84bfacfa0ec
BLAKE2b-256 checksum
How to use checksums
08e61ee4b0c7527936f4d82f71f9b927c88e0a6f5b18636b7afdca6145483100
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.dev20250305230914-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305230914-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
8d840f6327acc7cc36b16423d65df3c7fe87aea39829156243036f89c8d87f93
BLAKE2b-256 checksum
How to use checksums
51a2e13513e8ca824872523d7ed0ab9b5a20039f6a39f35aa8f00e104d16ad57
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.dev20250305230914-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305230914-cp312-cp312-macosx_11_0_arm64.whl
Size 811.9 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
cc71862537cf409ca9d157480fc9d1cd91000ccbd368f0b9a65cf448995a7dfb
BLAKE2b-256 checksum
How to use checksums
e9194924c4b0deff1f20d69afef53f476bd64a08cbf29eca604cc30c13dd23b0
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.dev20250305230914-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250305230914-cp311-cp311-win_amd64.whl
Size 806.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
99fe54925bdab9c536f379e93ce3e69e62dce4da2dcfc06532c6a3d71c4f3cdd
BLAKE2b-256 checksum
How to use checksums
1e914230ad679eaa24b057fcc1d141357d4a504b7a12e4c4baa276d203b3396c
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.dev20250305230914-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250305230914-cp311-cp311-win32.whl
Size 770.7 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
31bbc3fca40eb210071ebc215b79522eee3e3b4d34b664517b2c66144d1262fe
BLAKE2b-256 checksum
How to use checksums
04645709cee6dd9812b04760f0f2827c81a4164a75edc058ce99717168bb5b22
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.dev20250305230914-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305230914-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
27c24d8f316936d008264cebd3c2e0b97c4291ff9588c760c0ad0a50674f3cf0
BLAKE2b-256 checksum
How to use checksums
367a3b4d45de8024edb45c8c7b946fcc50994cce78c455daa7221888b368da2f
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.dev20250305230914-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305230914-cp311-cp311-macosx_11_0_arm64.whl
Size 827.4 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2c260cc40c9b910479e73a872daad1344e5b59703b5ca257f193867197271db6
BLAKE2b-256 checksum
How to use checksums
8a04fce43f99e3fa277f7fe7a2d11cdeeab0590dcc284c60b8284906e8a8d23d
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.dev20250305230914-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305230914-cp311-cp311-macosx_10_9_x86_64.whl
Size 889.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7949d3606c6dae0295c8691fe952101ee51aefbde6c8dcbf26ee097bb8c378be
BLAKE2b-256 checksum
How to use checksums
81dd9318597394a9614a0601bbc0ffa4c674a9493a12710de662218016955130
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.dev20250305230914-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305230914-cp310-cp310-win_amd64.whl
Size 803.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
a013230287b86381bd5b8777e3a857f97c25a2e4fb2f20413e2b276470cac3a3
BLAKE2b-256 checksum
How to use checksums
41ea9a41497dbcc58644ddf68e60e144bb62b54a050eae44c3d0b3d82a30155c
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.dev20250305230914-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250305230914-cp310-cp310-win32.whl
Size 738.5 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
690d3573b652c895c80e4e45f7ab5b9d07dcb284f0b20de95d3e3ebf84f8439d
BLAKE2b-256 checksum
How to use checksums
46c40a2ad17a59964d65e3210b8c1b48984479b886c489ad8d60164aa7d20e53
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.dev20250305230914-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305230914-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
5bdb7a4af9bbb6456eccb89acb7b70d457ad3084ec6ad7c09482d8e8327aa674
BLAKE2b-256 checksum
How to use checksums
e053cf39a6d53b7c40b7c24a9cb13a615775d7dde72c81d79bf2f4395da9f015
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.dev20250305230914-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305230914-cp310-cp310-macosx_11_0_arm64.whl
Size 794.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c64aa8bc8f3eadefa4d5199e001c814f3e277c2f07506d0a545bab727ea72305
BLAKE2b-256 checksum
How to use checksums
e0f71cb30e3c28248f3ab3d71713e1ade4e037ac359ce93a70baab3b8bcc3cf7
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.dev20250305230914-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305230914-cp310-cp310-macosx_10_9_x86_64.whl
Size 853.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
97e84b77dea5e8626dc2fb549ee037c6bf286fa08a9c0fd72d4e95324bb4ea49
BLAKE2b-256 checksum
How to use checksums
4c41e8a996c5c3717b3c622432b744fa6116cfa35fc7e739a0115bb49450dcbc
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