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

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

Download URL typedunits-0.0.1.dev20250227014329-cp313-cp313-win_amd64.whl
Size 751.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
a9e8ff4bf40443c4e927f2533f0b4cf04c2096eee1c2451fe0d15d1129bbf794
BLAKE2b-256 checksum
How to use checksums
3e6cea83f390310daa5f40b851af6d58161ef88d76ce8437a8522c1d95b64dba
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.dev20250227014329-cp313-cp313-win32.whl

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

Download URL typedunits-0.0.1.dev20250227014329-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
479220fb8686f6e04e353130911866966f12553fcf0f26c8844e5b41f07c0ab2
BLAKE2b-256 checksum
How to use checksums
5f08b7771075d62340cfc74631e1ece44ac73335b4389d9719e473f1d9baf888
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.dev20250227014329-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp313-cp313-macosx_11_0_arm64.whl
Size 779.7 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
92ad8f765ebb23cee90540cf9e01ad770d4be9d078e123204c19c2f527df67d0
BLAKE2b-256 checksum
How to use checksums
6e92b0ca444a505fe413e0a1ad79ee0ba9543b9e86f5ff1bd00695dbeb17f199
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.dev20250227014329-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp313-cp313-macosx_10_13_x86_64.whl
Size 819.8 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
25910452f65400ae315c1ba123cb21b14f2ad067b6ab77c5aff85c4fb5d79ca8
BLAKE2b-256 checksum
How to use checksums
0497a8d270c4c41a98ed8a2ab84ef7a9b823d3d32e9eb89e559ecf9b309ab083
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.dev20250227014329-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp312-cp312-win_amd64.whl
Size 753.1 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
2e4fa347cdaa0a92a63facb09a23c61706d9ac49457114a735e337510258b429
BLAKE2b-256 checksum
How to use checksums
51ecee647ef8403a947ea6b9e1f10bb948f422fafc7e5a24ffcbfac741672ac6
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.dev20250227014329-cp312-cp312-win32.whl

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

Download URL typedunits-0.0.1.dev20250227014329-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
c8a91223ea318acfc31547b339caab4d7f52a3d7791450a9091472adb92381d7
BLAKE2b-256 checksum
How to use checksums
147dd6cb700d37719197a05187f9f699ce947916b1bafcfe059dc119e5e64f2d
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.dev20250227014329-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp312-cp312-macosx_11_0_arm64.whl
Size 787.3 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a0e5772b655d9fb53e0509f50705b432e842c9a189e77298a13924ce586dd4b5
BLAKE2b-256 checksum
How to use checksums
4f95eb576232d56ee86dec2424fe0dc974bf969f65b1e7925352d81ac1a712ae
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.dev20250227014329-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250227014329-cp311-cp311-win_amd64.whl
Size 780.5 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
f22fa14994875028cf29ee83c5b0a88427b8061ce31f6437a97e35e1fb62990e
BLAKE2b-256 checksum
How to use checksums
c9976dfb06c9b62d0cf58563976d4d61067e4396259f8257e1c57c2a719cae71
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.dev20250227014329-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250227014329-cp311-cp311-win32.whl
Size 744.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
22b3eb4af2dd50aceea5b5bb143bbd7bb4df0e8bfe9ff70a10a3b6c14304f034
BLAKE2b-256 checksum
How to use checksums
50d9421dbc362fdd1e7349d104f068e7ede261007d09d995b4f97988a6102469
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.dev20250227014329-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250227014329-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
68916877c01a3ff89a8948fda2856006d69bdb1ab4d82ce09701352ab46ffa3f
BLAKE2b-256 checksum
How to use checksums
bf98fa8d3c8f5d0d87b66241f86ecad79b56bff408c247977fb609924d45d290
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.dev20250227014329-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp311-cp311-macosx_11_0_arm64.whl
Size 804.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c8c1573cc14bd5343665855757e5bd1d1b5e58dbc307dd76beb2f4c539b30d41
BLAKE2b-256 checksum
How to use checksums
a306f3590c39a86b588ee7dfebc04c9d205c415c62d0ff5611b87ce82de360f5
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.dev20250227014329-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp311-cp311-macosx_10_9_x86_64.whl
Size 863.6 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
3b94867c2dbedc2b162286aac7260b9e2c72169c6189eb1c1c1733c1cc35daab
BLAKE2b-256 checksum
How to use checksums
3790c28a11b8cee0b3479b6701077718109637fdcc91f811e33681a0a4ef0038
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.dev20250227014329-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp310-cp310-win_amd64.whl
Size 776.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
716842b0be1c7620230e6c16c2f0fc22e5b57330b481b0050ec4fd733a33e7c8
BLAKE2b-256 checksum
How to use checksums
725b5d7ca24f39c2a8f37216d692068b5309acc9d8c9ac0ed09c6a64849df8df
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.dev20250227014329-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250227014329-cp310-cp310-win32.whl
Size 714.0 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
175ee32133c2709ea9951d10df3fc04d07e3062bd2dcefb5e3ad6ef2ca357bfd
BLAKE2b-256 checksum
How to use checksums
741fca73ef6ed52147688dee650666fced7f8ff08354280a126870b1003fa09a
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.dev20250227014329-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250227014329-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
70a47f46e65f30cbd0be1adf9bc80b3500146ad7018f0724f812bf91a7307457
BLAKE2b-256 checksum
How to use checksums
8f938e6193df89b3961388f88054fa0c8269bc198eaa97825eae323afb51a81f
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.dev20250227014329-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp310-cp310-macosx_11_0_arm64.whl
Size 772.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1689030a4a9e2a5fd171bff8a4941dc3074179610155a486231fd3a48558b56d
BLAKE2b-256 checksum
How to use checksums
650ff7b7c24aa3c60e5716c90fcef1920b6f4f3cb6cb80053ce0d0950e435b2d
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.dev20250227014329-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250227014329-cp310-cp310-macosx_10_9_x86_64.whl
Size 829.5 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
b0c7c0cc1ace6ffa228e7e3585a2edaeda41c78eef410536fe6296ee3ae90486
BLAKE2b-256 checksum
How to use checksums
2933657fd93302b65d6af4f9a1cacc0a762b01727fe26260998a48cbcf6b5e3f
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