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

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

Total release size: 23.6 MB

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

Download URL typedunits-0.0.1.dev20250422050902-cp313-cp313-win_amd64.whl
Size 783.5 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
88dd976e207d4f1399eb535af39eb772ef52759be7384bb38b330e85d82735b6
BLAKE2b-256 checksum
How to use checksums
e12f187db1b774578aad74904e630a833ee2d5aa6c0c766d76cdb903e849cc0e
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.dev20250422050902-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250422050902-cp313-cp313-win32.whl
Size 741.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
3e8248c54b408c209db3abffbaf1bebe4de7db6f93d69bd5dcee10193b88fa29
BLAKE2b-256 checksum
How to use checksums
0ffde40374c3cf66aeb0089b4a7572eedbb2e5d1bd76a39023ae59c5170dd115
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.dev20250422050902-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
58e194fbd829bf825e1ff29ff65101dd0bc009cc6751f53c3fa2fbc2d2749ab8
BLAKE2b-256 checksum
How to use checksums
4ef334f43a6442ebdc0659457ebf610d40d271035a8e549c5735d6d6958d918c
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.dev20250422050902-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp313-cp313-macosx_11_0_arm64.whl
Size 814.4 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
8d6f3d011c50486cb68415312f05e8d1482defa9eb366e85f4fb097556bd7dce
BLAKE2b-256 checksum
How to use checksums
5a51e98c9fe94db869e7ce1fabccb0f424828bbbe98c39e26594ada474f0bf0a
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.dev20250422050902-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250422050902-cp312-cp312-win_amd64.whl
Size 784.6 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
2080bc818776d9561fc2591cd6adbfe5301de4b421494a232ec3dc8936970838
BLAKE2b-256 checksum
How to use checksums
e4a3285174f6da328a3688f475febefffa47308671c9400bd88bf1d76236dac0
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.dev20250422050902-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250422050902-cp312-cp312-win32.whl
Size 742.9 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
35abf06d94b1840f8a50c463a8327dbe0d9d838fd2f5c00deca82ee2ab916201
BLAKE2b-256 checksum
How to use checksums
32abdca95a86ed7d6ca25594c93e0612ccb2127cf6df6ce3592a6511a2662ee0
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.dev20250422050902-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
545f8ae7b66fab8cf39f766124554ffcfccc4354f4d8285215de566bfee24e5e
BLAKE2b-256 checksum
How to use checksums
d8224ad4f9182c38a71dcdbe31bf9e771be8fdddac444f2ba5f5313b12586e7f
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.dev20250422050902-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp312-cp312-macosx_11_0_arm64.whl
Size 821.7 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2c48810255cbe4fca95338cc697c6940208afa9dc9cfe5fb4b4afb83fecfdf78
BLAKE2b-256 checksum
How to use checksums
36119c698ba2ee7bff38bd537bcec6695d83501271707adf3eb4167a900fae25
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.dev20250422050902-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250422050902-cp311-cp311-win_amd64.whl
Size 813.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
9bdedccfa533e8542173e750849b846d0e8345824e71d3717b57e927bd82b3c4
BLAKE2b-256 checksum
How to use checksums
a8aac478736d351a55162657fe6de5926e76cb1ec2b5be5214f8f37d5c185826
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.dev20250422050902-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250422050902-cp311-cp311-win32.whl
Size 778.3 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
15362f0ad916c9fc196a1484e2db509b2951f670fbe6dd2b490cc2dc7e7cd60c
BLAKE2b-256 checksum
How to use checksums
f25e9a41e20286656cace819f944b76c114186f4fd32fd8f4b10fbde57fa6e99
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.dev20250422050902-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250422050902-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
ea630ccca9656599de7c8062bc488aab1602d0933e098fb4783be5a6b785fd91
BLAKE2b-256 checksum
How to use checksums
d5981cb2b058fa06d61d5e4413a72ffb72fcbba0dc43ca860872a78fcc1798ef
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.dev20250422050902-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp311-cp311-macosx_11_0_arm64.whl
Size 837.2 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d8db2c38e9084b11e218be5abbc1a5a783b8bf690dee7a8f14e5a3cd3ce285e1
BLAKE2b-256 checksum
How to use checksums
5966777dba043d347819c2ee90b89b32e1e73a3f60f8f1da9f10d9fb4e0c8376
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.dev20250422050902-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp311-cp311-macosx_10_9_x86_64.whl
Size 898.8 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
556ffd65c954e5c4e8c64ef973bd4000d77d242b4f676650e6705a9719164b5b
BLAKE2b-256 checksum
How to use checksums
22c2cb5d3140c3bf3f9fe36377662ea6fe406b41769ef0812989db2d05b0b2a8
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.dev20250422050902-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp310-cp310-win_amd64.whl
Size 810.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
eacf886b2330c1495156d5d091c444528f1c8528793d0557846ba0c2aa73d377
BLAKE2b-256 checksum
How to use checksums
48f294bacfdd9f47ef5ba7b8f461a0cc0a43c475bd9c03a667e2ff7539d9d7fd
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.dev20250422050902-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250422050902-cp310-cp310-win32.whl
Size 745.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
c533081d0cdc8c4358498bb8823d8e1574bef290bd2deaf5aaf64e74acc5bd1f
BLAKE2b-256 checksum
How to use checksums
82a40da2de8925ff59d43703e2896e83123d974c7805c43fcccb2e497dae8427
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.dev20250422050902-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250422050902-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
e1782bb6371489660e6b4134cb9792fa6cb682be5822aa33d72aad5e2418b850
BLAKE2b-256 checksum
How to use checksums
df8299c8a148fedbba5e8469d48da3554c7a9f17d6121ec1d5b8712c65bdbc54
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.dev20250422050902-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp310-cp310-macosx_11_0_arm64.whl
Size 803.2 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
55b9ae0750a9bd447837123940e7530f849a0be942d3abf391111956ffe2c75f
BLAKE2b-256 checksum
How to use checksums
42add332f0cdb1a05c9b511ffd02697de8eb84d708a5ebc4cecfc4b272d49e4c
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.dev20250422050902-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250422050902-cp310-cp310-macosx_10_9_x86_64.whl
Size 862.1 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
21b0988767a4d6d2fc482327949f1a87308c1839b3ea112ac6847e0c9f742c23
BLAKE2b-256 checksum
How to use checksums
bf8a80f228112f9ee2110052267d871b7f8228ba2c668c1136bd1f5f5c2045d2
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