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

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

Total release size: 21.7 MB

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

Download URL typedunits-0.0.1.dev20250128022528-cp313-cp313-win_amd64.whl
Size 721.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
97a492e4a9cbc391467d897f5ec7ef630ef49baf6b419977e86d7fa0e6aa36e0
BLAKE2b-256 checksum
How to use checksums
fb0b87a2598a4ed5949b1fa889f6c0ee7dd1c67b5a6cee4fc657a0ebc962179e
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.dev20250128022528-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250128022528-cp313-cp313-win32.whl
Size 681.7 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
41e59e3d3f331185e26d07cf48d7ffc635cfada12adf42ddaa72ea0976650924
BLAKE2b-256 checksum
How to use checksums
a1bee20203548099e1302a85fe68ba2bc5edfb61662b4e618253f7d2eedeb613
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.dev20250128022528-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250128022528-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e758f7e1f2b50c79e1f4d4691a0ee984f3caf2ee775692b4f609692981c634b2
BLAKE2b-256 checksum
How to use checksums
28df96db439747ef4f10784508530102fbfa892572492a3483e3e0679d508e5b
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.dev20250128022528-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250128022528-cp313-cp313-macosx_11_0_arm64.whl
Size 748.3 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0fb9d72d0b002e2a6e8bd6000681f319e28ce63133b261f5813cca2f743329f3
BLAKE2b-256 checksum
How to use checksums
67188a639382871f7ce7daf3a035b71cfcec2188a869e4a3b58618693fadea5b
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.dev20250128022528-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250128022528-cp312-cp312-win_amd64.whl
Size 723.3 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
c134594e46756c28d435f7e800967c2812af6b1b5e3980274f5797e36425c4c2
BLAKE2b-256 checksum
How to use checksums
3a504bc8b48e42844a195fd08b2c4e9b4c770a9dee7b810412564e38fd11b342
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.dev20250128022528-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250128022528-cp312-cp312-win32.whl
Size 682.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
1d074a948769ab1ddccfbc29ddba17917847667477078ed0d24744fe22d9112e
BLAKE2b-256 checksum
How to use checksums
4dc2fe5f92281f66aa3024f9246854b8cbed9918f3ee0336486c918c7b52c8b1
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.dev20250128022528-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250128022528-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.4 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
ae8702c24d62b58ca2690fb1972ef34feb7cc12be1f6a2edf4c82a9b72bf4847
BLAKE2b-256 checksum
How to use checksums
017a872dca7cfaeaf67779cfc3d85fa64ee2d10d3f817ddfaf8a39eb69e644aa
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.dev20250128022528-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250128022528-cp312-cp312-macosx_11_0_arm64.whl
Size 756.2 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b502e05b2c2d10bdb5af873441d1e8a46774517bbaa36366d84c492dc4a26a4e
BLAKE2b-256 checksum
How to use checksums
fa87659841e52e9ecbc1301d432f4319c4f9138d5d256aecae8666b97dd262bd
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.dev20250128022528-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250128022528-cp311-cp311-win_amd64.whl
Size 750.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
c493d62aae1cdeca6f58c1042a008f5bfb53a193ab5976944391d55697ed1162
BLAKE2b-256 checksum
How to use checksums
01381141f1c0fb1d370e06b5f7120722bfe65af94c75e5ce23b2bd892658968b
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.dev20250128022528-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250128022528-cp311-cp311-win32.whl
Size 715.9 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
6c412a735a9e59ea70f61ab6342dd540cdc4d29a99bd0473deb7bd9a7de5b837
BLAKE2b-256 checksum
How to use checksums
b3f58f39361206f3965809a559d00f8e130c47adca31fb27015576a20ca9e4cb
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.dev20250128022528-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250128022528-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
6f8ea522a05c780d8781716d5f809c3a6c348d124b7af3df2e6698e6d5cba2a7
BLAKE2b-256 checksum
How to use checksums
1aae0be432270dc5ef74596c35abd66cc2a3675b332717a41a66806abd0d0840
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.dev20250128022528-cp311-cp311-macosx_11_0_arm64.whl

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

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

Download URL typedunits-0.0.1.dev20250128022528-cp310-cp310-win_amd64.whl
Size 746.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
39df1f2e058f70250c2e1cb7b60b119ab3e47a3b98ea366897857a5e4bf6b0af
BLAKE2b-256 checksum
How to use checksums
d61f361333f66a55f6925dc031ff36fdf8f5efe512a0d44c5852e3af13051207
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.dev20250128022528-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250128022528-cp310-cp310-win32.whl
Size 684.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
789f9de2c1a667128f1dce1de6b99ad917206c26928f61d879a091ab661656c4
BLAKE2b-256 checksum
How to use checksums
bed2079951079b6dfbbe6c5943d4645d0bcbf4c75083b08f7a5aee34c9d08c72
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.dev20250128022528-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250128022528-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.3 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
4525858ef391d5f6369a8826817fbcdcd66a48762f6baecd627dc8a001969946
BLAKE2b-256 checksum
How to use checksums
050651b5ea2ce4b0cb73720695338b2c1898deb1e99e47e029ddae32c7f7100f
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.dev20250128022528-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250128022528-cp310-cp310-macosx_11_0_arm64.whl
Size 735.4 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a75113290a24ede32abcc94ee25381519af265c556de2501e3c5fc42a36267b7
BLAKE2b-256 checksum
How to use checksums
af7206587a4957f8d958557d14869a89fb77730581a8c4a75cef550224e01e10
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.dev20250128022528-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250128022528-cp310-cp310-macosx_10_9_x86_64.whl
Size 792.3 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
53c8b0e2845ac3a7cfe2ecaebd5c7514f76f393529e7d879ab6610987bb8fd31
BLAKE2b-256 checksum
How to use checksums
bd3de4e94069c5817e404344f2c02304341eda880244ae72ff74aa6793e0fae9
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