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

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

Total release size: 22.7 MB

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

Download URL typedunits-0.0.1.dev20250903181955-cp313-cp313-win_amd64.whl
Size 738.0 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
a64f2b2a10b222861bc31b8d429af6811c643dc51686cafdb759278ab831f97f
BLAKE2b-256 checksum
How to use checksums
db76dbaf5542bce3922d2d3af2954446ad1ba52ea4a39246528a8aa2edc5cc25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp313-cp313-win32.whl
Size 683.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
9cdf913aa91b4730a1c2ad5aa6a7c008069e01fae19403a76721bad0be6cf12b
BLAKE2b-256 checksum
How to use checksums
c2536c1576cd27ab05f7d32b67d1900199b08f452a9ccde547917bc1502176ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-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
eda6c935ee318df1494ec4cd7ac66938a42c17324c929c7315fa74bba9d2629d
BLAKE2b-256 checksum
How to use checksums
8d809089862cc5ae0f3a4cdf005f38f770a6545bc4268ad066693c836ce64838
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp313-cp313-macosx_11_0_arm64.whl
Size 755.5 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
db64f54d229861670f69441259f70ff8a43de028170637dcba912d9185a575ce
BLAKE2b-256 checksum
How to use checksums
998e3a5dccd57984b410ae915cacf2d0ce4f2bea27aac681cda6e9edc42e9272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp313-cp313-macosx_10_13_x86_64.whl
Size 798.2 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
de90e77ed2311fba9eff87990555304b8a2038b4b3c19f7091a11d8e6c091984
BLAKE2b-256 checksum
How to use checksums
55d86dd3b60b0fe233671e70eb652063c194131546a5e0db73c63c81441de1b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp312-cp312-win_amd64.whl
Size 734.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
47b5d8c26d6efede9aa401406119e19f2249edb838f31741bc7a143aafa41384
BLAKE2b-256 checksum
How to use checksums
6a2e7d2cd9769b845053a0988ebe35116bb1df82a963154511acdf31ac709b75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp312-cp312-win32.whl
Size 684.5 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
bd74e360186d4a1df0568d8e72556a33e051baad5b6258f88d3a54dc3d3ac968
BLAKE2b-256 checksum
How to use checksums
002185eb5bfac32d543979259fd8ea11c8cc64347b71e53ec9f681e491321bd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-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
dcbf6f6377668ae9c696a491d667905f5c46480064b2de190a16154d63504e8c
BLAKE2b-256 checksum
How to use checksums
ba748bc1a937bcec686d3a8ed8c47baff989c57a16ff4e7ffb02af0c46420787
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp312-cp312-macosx_11_0_arm64.whl
Size 755.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1328124c725588d9030729ccaaca701413c362feddd6af9aa1155e319c6601d4
BLAKE2b-256 checksum
How to use checksums
c16b3c76a92444f7be62a51a083ac7fdecfcafbd1906b1526b1a1373b6c1d7f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp312-cp312-macosx_10_13_x86_64.whl
Size 803.9 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
32f8817d5c2b25b90ba6671039714412f1a2d0dd379913dfa19210018e206235
BLAKE2b-256 checksum
How to use checksums
9151e0c3017d2db94de5f6101afd31b1cbe159116e7bc3cec09b1b7ffdddd49d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp311-cp311-win_amd64.whl
Size 814.7 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
16b3086af0aaa980d01a3ca7018ee10665a9b941cc15e10b84ade9d36a0fec27
BLAKE2b-256 checksum
How to use checksums
f9b09a127a8e67d394028ef5a17d3fca65f929b84f6b81129e763a747bc66e86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp311-cp311-win32.whl
Size 748.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
3459f4a20fe7b1a6c97424b2d307409d8ea1316646f7b3687f8acc40518cdf67
BLAKE2b-256 checksum
How to use checksums
9f163c6082f9da50bc64e521d1f775c18c61f020736f38a3a63ab357ff283887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
9168687d695ad5ceb8da7f8de8f7d3e0f86c0e410ba8d2dce8ba34bac68bdd95
BLAKE2b-256 checksum
How to use checksums
bd73715a47d39af08c40faf3efabc03708930d21856cbc826a3af43391941e83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp311-cp311-macosx_11_0_arm64.whl
Size 782.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7ac0236b6aa1bb7a4815cc7a2bec2ed99c66bc3788eb1d7863e6f779f401847d
BLAKE2b-256 checksum
How to use checksums
a6511d1538a5ee8e1649fa10eea8ba52d001df1dbe7a96734fae93e18bbbe51f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp311-cp311-macosx_10_9_x86_64.whl
Size 848.9 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
812943a134994e4bf528ef78be723765bc3b8d7ed2ca782474eaee7af67645f5
BLAKE2b-256 checksum
How to use checksums
65c1c184621a55e9b1907f6d02dec7b4ee0f7dc5f5caf319ea8c03a1463aaf69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp310-cp310-win_amd64.whl
Size 814.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
961183523ed8f76490e5e64cd4b168a070815a32dceb2b66705cc26e3a0500d0
BLAKE2b-256 checksum
How to use checksums
de594dc3246d721cb228ca23b249b16071eda79672a4655a4cefa2b3f60af3ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp310-cp310-win32.whl
Size 719.6 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
f4ad46e4cbdad267a58bd76d5db8c693a0d8dddac4f4135c1a45a6280fd4547e
BLAKE2b-256 checksum
How to use checksums
f53c6cd8a6e01b9a37b5fee50273f5bd89f89dfaef3a55379ebb204a5a48efb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-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
949ed732c9f805468f3496cdc18d697bb50601263a6468e6c38f40666f50406e
BLAKE2b-256 checksum
How to use checksums
f7d33982ed54b10dc24896b057a18c0d78b6e2b15a6ee488f428934c0b73104a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp310-cp310-macosx_11_0_arm64.whl
Size 745.6 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e3e61a47a230cfe251ef7420284804634df8d8f45af7ce7457a213824babea1a
BLAKE2b-256 checksum
How to use checksums
9470b67d83a7708aad954740b1f465379306cf695f642e84d26447b5c1de8ab0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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

Download URL typedunits-0.0.1.dev20250903181955-cp310-cp310-macosx_10_9_x86_64.whl
Size 811.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
e4a2db205c92e30a0c153b98d0b52bedea7e6aaa9ffe6e2577af242a0481d480
BLAKE2b-256 checksum
How to use checksums
40eb8d5e2428f7cdd53a0658712f3ce630bbb726529c9f1555bb79630d9b47ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.23

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