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

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

Total release size: 22.9 MB

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

Download URL typedunits-0.0.1.dev20250301002457-cp313-cp313-win_amd64.whl
Size 763.9 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
577af7cdedc49866b4215d2ccd10ef24053a528bae75623895be3da9c21ec479
BLAKE2b-256 checksum
How to use checksums
5f581b7f2235cbf9762898d3ea49c243d2eced073c1f8d7101e71ad4457d4a12
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.dev20250301002457-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250301002457-cp313-cp313-win32.whl
Size 721.5 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
5af818a758805fb08af095581688e5f2ef076a41e40d762d6720708ac8b4d9d0
BLAKE2b-256 checksum
How to use checksums
bc9a2b2b12ae0fe3792c2e5f6635bf52cdf6665ded28b6e6026eb3c02fb7464e
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.dev20250301002457-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250301002457-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
6761291efe376a9b0153044dc9ed305abc7bac3aabf9553b01482484bfd2b5cf
BLAKE2b-256 checksum
How to use checksums
eb838e8c235fa81859fdf664e437f269f1c2daaa23a626d4485e43f9c3dc7019
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.dev20250301002457-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250301002457-cp313-cp313-macosx_11_0_arm64.whl
Size 791.8 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
8e2d98c4cb0ff0deea48f1a23c2092695c01d15faf8cbf0a14938f18891eead9
BLAKE2b-256 checksum
How to use checksums
821bca90203fb9deca54a5d4cca93c7e4faa976e3c17226546630a2b69286ef4
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.dev20250301002457-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250301002457-cp312-cp312-win_amd64.whl
Size 765.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
0205b29f2814b1e9ebd74201513ebe56cef4cf4ce522cc45e5174f2372be84c1
BLAKE2b-256 checksum
How to use checksums
a52272afb8c7ef0636cef7ec127fb794aef4af3deeffece1df46d19ae6fb0f87
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.dev20250301002457-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250301002457-cp312-cp312-win32.whl
Size 722.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
0f279bdf073eaada4a18c7f317cbf1d20d9731117397f4ec7e7e6e22a5bdad12
BLAKE2b-256 checksum
How to use checksums
c69886bd20f47a2a5c8961495b707c9a37e0012a757d68d81cee7b8ce3c48460
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.dev20250301002457-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250301002457-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
6e83615299bff40b1b23708dbea90c683e9b39379d35042994c93edb19be5123
BLAKE2b-256 checksum
How to use checksums
47f0e0b42c1775640437b2c0037ff2570f2891265fa22f489b9889314a7be76a
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.dev20250301002457-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250301002457-cp312-cp312-macosx_11_0_arm64.whl
Size 799.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9d2f7be76a16d44c3adb23895d7167757fc832e1a41057a3926c02e448a4ab94
BLAKE2b-256 checksum
How to use checksums
a2a788c4eb25be22b3d6fbac125b08c6830cedb8915eba09b1f212197547cec7
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.dev20250301002457-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250301002457-cp311-cp311-win_amd64.whl
Size 792.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
1ad92b2bee01fd9d1968b201ce16c997276209f5645d14ca2f6adb558c0944ca
BLAKE2b-256 checksum
How to use checksums
3e60c8257507a925127ad2ffdff637bb33faedbb5c7247d502908717f303b201
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.dev20250301002457-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250301002457-cp311-cp311-win32.whl
Size 757.0 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
977c402c1d43b9bfe9b1af1574c238f7e4467b199bbf0dbb51240469533f58c3
BLAKE2b-256 checksum
How to use checksums
401868b774011f354f72f1bc8db1a7b2cb67eb12b4ee9b4d477ace0b666c8527
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.dev20250301002457-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250301002457-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
12aeda0d6b9184e99d994f6ba2ed7682b75975fe27aa21492bca2c3e6422a4cb
BLAKE2b-256 checksum
How to use checksums
7e21a810769e6d8ca0468e429f9e9596aa62b0a67d4a118ed444b8a9378885e8
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.dev20250301002457-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250301002457-cp311-cp311-macosx_11_0_arm64.whl
Size 816.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a65fa0e7730076f605586b7fc8ede84f1b061290ca23aa9be0eba6821e5f4880
BLAKE2b-256 checksum
How to use checksums
026bbe44baf23c15e931891a78ecbb9b12796f8c8ecf4ba3be980fae403a09db
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.dev20250301002457-cp311-cp311-macosx_10_9_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250301002457-cp310-cp310-win_amd64.whl
Size 788.7 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
c925f2c2c3d3a83d5c63f11dc34ea122e8bc392a23e496e2ef3dd371d2d15c0b
BLAKE2b-256 checksum
How to use checksums
e0a4e29e64924501b9885d2f401cba486b4cd7a0f11eb79d4465349a6f3b2987
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.dev20250301002457-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250301002457-cp310-cp310-win32.whl
Size 726.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
582f9b7d8c6abce5172c76956c624f220ef1cf04762ba203683ef040d7ef7fa4
BLAKE2b-256 checksum
How to use checksums
d891cc7f0cba4df51667522bc9af009c4394418b0b2adea83e145304eada8055
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.dev20250301002457-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250301002457-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
8697ed5817cebc5a27d581d7d9e7ea0c39010acec25d2209aa662070b0f21d0a
BLAKE2b-256 checksum
How to use checksums
2c47ccab4dd9fc3009154cc1974b9ad0b443e9ce9bedd11663eb3d009d3f56c5
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.dev20250301002457-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250301002457-cp310-cp310-macosx_11_0_arm64.whl
Size 784.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
407531b564628fcfb852d3c556b1630ac51a50ff0c9b189f5a8eb34653fb8c88
BLAKE2b-256 checksum
How to use checksums
4b8504991f228c743d920b2bcbb1a8838f482bc183f492bff041899c749855d1
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.dev20250301002457-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250301002457-cp310-cp310-macosx_10_9_x86_64.whl
Size 841.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
93c77dcdb8056d16de60f5fe567b65cc525f2ac33f0944bf5e290086b11b6203
BLAKE2b-256 checksum
How to use checksums
2303b4c337d5bd5b4448201ec604238e966984ee091b2a28093d900de881b441
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