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

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

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

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

Download URL typedunits-0.0.1.dev20250214023029-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
3b05a453cc28feb2442b37eef396a16d4012d0a5f74fa0a99d6ade6ca8f859fb
BLAKE2b-256 checksum
How to use checksums
32157ffc41774eed548861b4d3d11a270511d73083a117a2b893406a19ca2027
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.dev20250214023029-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
46589c97ddcb59ae0888700093987e4b20672d7e4523b51479a7829a52f3d0d7
BLAKE2b-256 checksum
How to use checksums
02eb7a2836f789f6dec7824a2bdeaf11eaf94c8af20cee13da0789a098f0e3fc
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.dev20250214023029-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
eaa0ca7ea4233ca3d50a5cde4b37a9086a8c4cae4c55f58a58e11063fb91cb2b
BLAKE2b-256 checksum
How to use checksums
146e628ff4aa0c61ac1f844a2e284975e6119eb1c849776e617517a3ddb705e4
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.dev20250214023029-cp312-cp312-win_amd64.whl

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

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

Download URL typedunits-0.0.1.dev20250214023029-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
560d064d9b159c84ba32ce5ae5dfde6035161007a6520f61810721b7270eebe6
BLAKE2b-256 checksum
How to use checksums
baf7ef6a8c15e2d05b39a4154f3dd3443a4fff1706bcb7b382a9feffc761895b
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.dev20250214023029-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
3248f5c2b06a3595e086abee0e3c3f9f5e839b934b70a4c53c75565ed52e87f9
BLAKE2b-256 checksum
How to use checksums
6dc3232ead86dbe7887355edc3a60609b28adc0f63b0b338af77ea4368ec2f62
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.dev20250214023029-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
23a4ebf89cc807afee53aebab725505cdca4524db4ed5ed1f82852943370c6e7
BLAKE2b-256 checksum
How to use checksums
37a2870f68c5200329b37c1be4e4260408501edafb71c1209b68db6faaff067a
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.dev20250214023029-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250214023029-cp311-cp311-win_amd64.whl
Size 769.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
92de1263185227c5eea3d9c97abd588762152868b295f6b6decce2c8ff727a6c
BLAKE2b-256 checksum
How to use checksums
aead05bbea94361622db20d53e0037041141fda64aa0c9ceff71fee9486584e5
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.dev20250214023029-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250214023029-cp311-cp311-win32.whl
Size 735.1 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
1619fcf905b3dffd4b2990712cbe52d90e6a394cb87be96b9d65353efa66fc3e
BLAKE2b-256 checksum
How to use checksums
9e12c5c1038f4939144975e07e75503ea0df255146a0c96510175636af1caf34
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.dev20250214023029-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
25798551a4916158455c88dd70beafc6dccf7da8fca1106895117983e82dbee4
BLAKE2b-256 checksum
How to use checksums
4bb03041fb0079bd4a0a49f46bd5d5ace87ad247aaad3bceb1aed3255bbfff2e
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.dev20250214023029-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
4fd834297de9be67e16eab2daee6ba8f96727a8235f5f27e1ec6b20ca1957ebd
BLAKE2b-256 checksum
How to use checksums
771d82fc3f8afcd407f2b2522c55e154bcc50edf9c2519066928c07e4b2ac2bf
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.dev20250214023029-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
53251f0ddd1acb1d4a326605d0ca5a275d7bccec199cf6902ef84dc9dd27838c
BLAKE2b-256 checksum
How to use checksums
435e98547cae4ecabea21ee61eaea2b097795466822f8e8c5b5cdb21760c9eb8
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.dev20250214023029-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250214023029-cp310-cp310-win_amd64.whl
Size 766.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
32b6f7a3523d452cabc4a5bc99706835a059649c3a919712c6b0a38070a74f81
BLAKE2b-256 checksum
How to use checksums
5edf35f2aa879454cbeeff54b360e6aec93e4be1c270abb37ba79f0dcc01a35f
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.dev20250214023029-cp310-cp310-win32.whl

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

Download URL typedunits-0.0.1.dev20250214023029-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
d94595b08d298349e4ac2010ca723e43eef400b70c279dce4381e369c71575a8
BLAKE2b-256 checksum
How to use checksums
f2f35ff924451174a5b5603a468f0ab6646b6d28970457d0f905e80173622593
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.dev20250214023029-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
576fa5c51b94402d7d34b486f10a0edec5479735f65b11420aaf67162c75afe2
BLAKE2b-256 checksum
How to use checksums
9bd10e0f7fa99c4a93d0cf420670003a369a956e1910370ddad20d718af7eec5
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.dev20250214023029-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250214023029-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
c97523eeacf4ae88fa77281758ddb041c8c84af05e4020f25935a4e6d1be5758
BLAKE2b-256 checksum
How to use checksums
ddf29b0a2398e788257b11b76ec77e08e8dda5497aa74638f9500a089db840b1
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