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

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

Total release size: 22.5 MB

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

Download URL typedunits-0.0.1.dev20250221034458-cp313-cp313-win_amd64.whl
Size 747.2 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
7bd33f071d4d2afa242828181f9c140bed909865d782805d1356c92a975ae4c7
BLAKE2b-256 checksum
How to use checksums
848b12377194ccb4f7014977e87b79f072c2a3817aed4cda8b292b4b160f4bb8
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.dev20250221034458-cp313-cp313-win32.whl

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

Download URL typedunits-0.0.1.dev20250221034458-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
7de17c89e9ae042c7bb65d670e21d1dabb3a377b5763e40c0b75aa3ecadaf715
BLAKE2b-256 checksum
How to use checksums
552326db4719f8b12497d895a7f9d797b882766579171cee2be79d51a7ce9d04
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.dev20250221034458-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221034458-cp313-cp313-macosx_11_0_arm64.whl
Size 773.7 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a2895984c246616a56f1695965f25ff7ab5ae5f44a93f3a60d8a5462f7761c73
BLAKE2b-256 checksum
How to use checksums
2434c5a3b1a489ebce37b32793b80574148f6cecca83241628cdecadd568fc42
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.dev20250221034458-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250221034458-cp312-cp312-win_amd64.whl
Size 748.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
9108171e3c61bbbe72ed94e9a000dbff108cc789a7a94c745496330841712206
BLAKE2b-256 checksum
How to use checksums
6c80f514fa0dfac6a2e6ea2128f92dc62f0934420428697b2639527772bfc482
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.dev20250221034458-cp312-cp312-win32.whl

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

Download URL typedunits-0.0.1.dev20250221034458-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
bfdca817261d189dbb601c96ded5a0a16bbc44ed2ed94ea33ab02be3d08f1b25
BLAKE2b-256 checksum
How to use checksums
868ac900cfbc480d944bd96ddb60e826a5ef12a8d9f547e97ba3c286dcc95805
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.dev20250221034458-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221034458-cp312-cp312-macosx_11_0_arm64.whl
Size 780.7 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
91cb685d975d77e20aae4d893bca9d5acc67c56dcb1695fb49df93631f71a389
BLAKE2b-256 checksum
How to use checksums
ad4c8091d4a0c2ed0a6ed48b90c91fbfd5dd911594de9e0ff6ddb92435ece4ef
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.dev20250221034458-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250221034458-cp311-cp311-win_amd64.whl
Size 775.8 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
1cf9958f9e5d935aba25c10809d36084afff358acd818b94af7f0aff62a81bec
BLAKE2b-256 checksum
How to use checksums
6c28c503bcccc8a68ee49e5ffea6ece7de0ce5127b8fac1e661f8b49a88f3536
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.dev20250221034458-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250221034458-cp311-cp311-win32.whl
Size 741.6 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
9494e1dd178f520ac3cf00ccf8abae72c0515c19d5030c5da08ec0143e272f5b
BLAKE2b-256 checksum
How to use checksums
d4d673bb75ca079e94f426ea519a59e62ec456a7b32f4147c93e0ae90caaf3e2
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.dev20250221034458-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250221034458-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
046f97f331b7c89d8562670143dd24995a41e0491010a048eef20be4cd0e074a
BLAKE2b-256 checksum
How to use checksums
076e9d811cb703d7089c21ae3bac6f6407d8a7619b4b1d1f80f2dae1537a6ab2
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.dev20250221034458-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221034458-cp311-cp311-macosx_11_0_arm64.whl
Size 795.5 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a3a6d4a43fda24791042688c434ddb72ae01c41b8c8b5b6c93287b29447ba6e0
BLAKE2b-256 checksum
How to use checksums
0017ce3f0a104df687ab92255b40b7720a9e819a14e0ac0f768b047e3e30c94e
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.dev20250221034458-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250221034458-cp311-cp311-macosx_10_9_x86_64.whl
Size 859.2 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
eaed1b1c0ffd0c7951bfb41c98213e2db65959947370874067911be2702bdf60
BLAKE2b-256 checksum
How to use checksums
c7e4578868be0fb8255716f9cd396e30fbd1a99cf8a4fb55b8dc644bf8ebfc0e
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.dev20250221034458-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250221034458-cp310-cp310-win_amd64.whl
Size 772.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
06c9677691c30a22727fc2243edf768cdc80a14dc2943f6ac8edca47baadd876
BLAKE2b-256 checksum
How to use checksums
92c4f88b777307f352382106279c96282613355c9153ac81e3592e4e800988d9
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.dev20250221034458-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250221034458-cp310-cp310-win32.whl
Size 709.7 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
0f5540d940125f1b3a12165c1996a0a5893342f07e1ffb5cede22cfb5ed39fff
BLAKE2b-256 checksum
How to use checksums
b2d38ee448a46dcc5321ea452a648f06777858419eab891866a04eea5afc56fc
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.dev20250221034458-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250221034458-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
ab9c0be628dc0991ca5b246965beccb68c51e10fef1ae726eaf87dc0641e5cad
BLAKE2b-256 checksum
How to use checksums
dc03cb653b0d3c53fbced5ad515035da847f40771f68547631fed2c19de2e67d
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.dev20250221034458-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250221034458-cp310-cp310-macosx_11_0_arm64.whl
Size 762.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1f6ffa88e149a01f756d788d6fde531da4ba6e4d4543e0affa321b4ef2e68c60
BLAKE2b-256 checksum
How to use checksums
a03232f6088d0889fa302bdfc038d91466551ea4e3a7dac386f5c7c2752bb1ad
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.dev20250221034458-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250221034458-cp310-cp310-macosx_10_9_x86_64.whl
Size 824.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d1922d5e9e57c0741b5077aeeead41cd6204dc9d79a1cb408200956f7b32be2c
BLAKE2b-256 checksum
How to use checksums
502d426e2682f5637b33b857053659c6aebecf65f032fa397ca50fb6c4cafc78
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