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

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

Total release size: 23.3 MB

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

Download URL typedunits-0.0.1.dev20250415001833-cp313-cp313-win_amd64.whl
Size 775.3 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
b320be2a24aefd29979c60e88ec262fd894b6cd073885cac7aae093f7a347a0c
BLAKE2b-256 checksum
How to use checksums
5ade28af40647f4ed4eef9fcb19d318611083f458e111d857201da0251a485f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp313-cp313-win32.whl
Size 733.0 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
f3128c727353034b42599045fb241eb33fa492d555f220635a38c8b9d54100ca
BLAKE2b-256 checksum
How to use checksums
4ee669c3f6889b97fee366d45d57d2ee30e99e21e9b5f82510d9f267f5744ace
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-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
2e83cc264bd9844d72abb476de448073144ba268112d7ebb753beb793506eec2
BLAKE2b-256 checksum
How to use checksums
072ecb7619d6d6435f035249409c6fb6b8d38659b2bd4c26894418b286286c8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp313-cp313-macosx_11_0_arm64.whl
Size 805.8 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b7ad1250f430d8ebada9587b848b84723fe93beff6010b83ee499b4077cc9fd5
BLAKE2b-256 checksum
How to use checksums
d45e7525533ff7e6b102ea27202f094edb9109db2ca2bced8fc1333d5fbd8ce5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp313-cp313-macosx_10_13_x86_64.whl
Size 843.4 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
eabcf65acc65c0f02662b2edcf02c0ba29e23141d162a721e96e16566598d7dc
BLAKE2b-256 checksum
How to use checksums
cdee6bac77fc59bd7e37f87bea55cf062a8fb9c480a84835f5e26746a7b7757d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp312-cp312-win_amd64.whl
Size 776.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
50216dd5e9cf804e8888661a957f126f46994e21d125af5f6d39278c84abcc1b
BLAKE2b-256 checksum
How to use checksums
02351ddb00b120024b6c5c00fb627c8a6ea9863fd819ae8cbd2f99c9610c9500
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp312-cp312-win32.whl
Size 734.3 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
eaa89d56edb11d3408a3ccb32315f15cf083a006ecc5d0041750e20096c8bfd1
BLAKE2b-256 checksum
How to use checksums
f34177af265c9e058e320a56f10f177e6c083cf0096a827fa19c7d930edae1fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

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

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

Download URL typedunits-0.0.1.dev20250415001833-cp312-cp312-macosx_11_0_arm64.whl
Size 811.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e2110a9149930b12861cf80428fc35e5b58ef511195b51ed2d082f9e15cc2ed7
BLAKE2b-256 checksum
How to use checksums
dc7366740398f1a9bda31598c7beb231b2135cbc42a204688239a52bc9aa1dd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp312-cp312-macosx_10_13_x86_64.whl
Size 851.1 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
ffc9c118cbe82ccab3c44970b4e6e579021e20473f5335e6333239902be31cae
BLAKE2b-256 checksum
How to use checksums
3ea67d8e93264739abdd845386d3ab4695091555808b40b58ee548fae96a405c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp311-cp311-win_amd64.whl
Size 804.1 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
6499430e221cdaa5ba0ece560a48f8ff302f43f2837683ebaede845c6db64dfe
BLAKE2b-256 checksum
How to use checksums
5b3e916d3cf6f132100ca0e645e69ed5e167f7c8787d08ded535512c96993ff7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp311-cp311-win32.whl
Size 769.4 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
ba507758bd73670aa5c3b8bdc571cdb06364e3c330c018fdc15188b9551ee323
BLAKE2b-256 checksum
How to use checksums
b86bb2b3c69080b2dc3d950fac63f763d0cf14903c8eaf939143ab4f82a55f4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

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

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

Download URL typedunits-0.0.1.dev20250415001833-cp311-cp311-macosx_11_0_arm64.whl
Size 828.1 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
47c82b8d93dc7e8d197fae858486ba135c89c8565a9d6fd6d2f20fc079d1014b
BLAKE2b-256 checksum
How to use checksums
2284438e2a9dfca430a820a618eeec83e32a66cb8e3e0ccfcd069193e87d6302
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp311-cp311-macosx_10_9_x86_64.whl
Size 887.8 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ee95c084b4907a89d4b1727933d3a071dbe6bbda32f8d251245fbd938fced746
BLAKE2b-256 checksum
How to use checksums
1dbae4d21ac853059a4b60578eeada49d6184153c6bea2f271882c8ef755e443
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp310-cp310-win_amd64.whl
Size 800.9 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
18722b7086d6f97876aa5c222bc12df9d3ca96eb4de37e9829be261df10cba37
BLAKE2b-256 checksum
How to use checksums
7d6b01b7d731f0df82eb396b477b0aa889be8161c3c910d54b309aa573e2e4bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp310-cp310-win32.whl
Size 736.4 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
9f145d0ba664b009b90ffb0aa38f311c583f851a01f4a56520bd758efc6e497b
BLAKE2b-256 checksum
How to use checksums
1d72f77745d477b51578fd181c4bc994bdfbd97de4a548d629bfc37e849da3e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-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
6c9c9feae2bfc4e094946f0e128433d05804993949fb31695f93309c5d4a3e56
BLAKE2b-256 checksum
How to use checksums
1a40495321420ccaae39cc2ea7b4ce93cfa4f306bcaf108a52c5438d73958ab5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp310-cp310-macosx_11_0_arm64.whl
Size 794.5 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
06f30e2b89382e011d64d066e50c52ca775d08ee5a998bee42a3561b88683261
BLAKE2b-256 checksum
How to use checksums
4923860057a2e5c967c4cb4d7b2ad7c96fb04bab6c2dd393453cdfe5823b9782
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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

Download URL typedunits-0.0.1.dev20250415001833-cp310-cp310-macosx_10_9_x86_64.whl
Size 852.1 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
8b11cce2a2b00dccdb0bcbc2c3a39cdbfa210b888619a35fab74f07b9a9f25fb
BLAKE2b-256 checksum
How to use checksums
1cb3428964abe35c21fedaf981571fa3a32671242ce30f12bd10d471b1a6a22d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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