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

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

Total release size: 23.4 MB

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

Download URL typedunits-0.0.1.dev20250307001849-cp313-cp313-win_amd64.whl
Size 777.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
3f7d6943f36df126cf7268c3d360f5033618c996c65b17e84d7a887e575a874b
BLAKE2b-256 checksum
How to use checksums
806ca58cbb88ff24012c1c85196cbd7c3de83719db0422260823f8885342e954
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.dev20250307001849-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250307001849-cp313-cp313-win32.whl
Size 735.2 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
14dd51bed532299fb755d3598a72a98f24ce4d25ec299cd739270d4cf07112ea
BLAKE2b-256 checksum
How to use checksums
3de88529e2973d74c85fa6b1799495eb4b04d76a81ffd57d143ea597d4a0d88c
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.dev20250307001849-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250307001849-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
b92a99eee8bc254eba1a2d11eb8b1f5f709bd8b1b250fc5e7ae13cff41506b76
BLAKE2b-256 checksum
How to use checksums
0eafa7fb0079fdfc894ce09aceb3b16da0995cf6c2e03e94ad529cf0dbed7250
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.dev20250307001849-cp313-cp313-macosx_11_0_arm64.whl

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

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

Download URL typedunits-0.0.1.dev20250307001849-cp312-cp312-win_amd64.whl
Size 779.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
c688f3586652ab18f3b3c3265351a3c5183a74d4278672ec47c789e37aec2d49
BLAKE2b-256 checksum
How to use checksums
a16252404468b65d3ca18d9347f1692732c54121cb60c738a92927d1471ebd5d
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.dev20250307001849-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250307001849-cp312-cp312-win32.whl
Size 736.4 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
834cd439186cca95a82d568a0360270e45e4540eca97048d4de67072b70f6a6a
BLAKE2b-256 checksum
How to use checksums
ef75699907cccd4c0ebb1b5b5d841032018f1ba277bbd9f0bb03dac2572fbfd2
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.dev20250307001849-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250307001849-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
3b5ec632d9340965364cdf1df144ddc148ca37ff1c70a287c17673d9a2ddabbd
BLAKE2b-256 checksum
How to use checksums
13721ebe094edfe8d88fc6a91416014f26d666386b88b4302fb95710d75858a7
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.dev20250307001849-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250307001849-cp312-cp312-macosx_11_0_arm64.whl
Size 812.3 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2751a9d02e9b56b3550e64eea80ad6018d2947d11a8fa728ddf65f4681708148
BLAKE2b-256 checksum
How to use checksums
3caa318513835c93d33bcaaca42c9e2fe2000a089de7519edefbbbbc714ed9ab
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.dev20250307001849-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250307001849-cp311-cp311-win_amd64.whl
Size 807.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
ea6fde997e919bf45c27603f1f3cf0d60985b15c32fc0015e9b83971b2159617
BLAKE2b-256 checksum
How to use checksums
be23b8b6de5479ac84c6e16e103649609fd24a1c074aaabeaa6684bf227de2ae
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.dev20250307001849-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250307001849-cp311-cp311-win32.whl
Size 771.5 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
426d3b93c54aa966aacc7afe51ae77f05f688ef4e8ebed2be347be931182a3df
BLAKE2b-256 checksum
How to use checksums
1b6498d3c15b96751da5c85d778ff76187fa75dc23559857866eb497e9ec7dba
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.dev20250307001849-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250307001849-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
19912b51d0d71ef3e8a14b950bf5d2824e2222404a70d0035c026835aaf6cb38
BLAKE2b-256 checksum
How to use checksums
3afece54b1d558080aa61254ad38a64140e5c267b19cc00f7abebe13b4f9c104
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.dev20250307001849-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250307001849-cp311-cp311-macosx_11_0_arm64.whl
Size 827.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c68622d4f8abaf9cd25812110946c94ae6a53d9fd546f927ee468a1e4b205803
BLAKE2b-256 checksum
How to use checksums
53cca8c5de4c8d97822239fc8a995d3cee51d0b624c221eef78cff6db3d34842
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.dev20250307001849-cp311-cp311-macosx_10_9_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250307001849-cp310-cp310-win_amd64.whl
Size 803.8 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
973d563a727e48df81c998f369b76d1ece8ee63c935ccc5295b57ee928dfd331
BLAKE2b-256 checksum
How to use checksums
e8115013fbe85926d69e0d619f78bf23e2c0066a63d3c1407c2185ef9adc8ec0
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.dev20250307001849-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250307001849-cp310-cp310-win32.whl
Size 739.1 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
ec2fa6b49695f018544f61a50a5326bc66d215ff8f037fb1f1d4d12ca8a8db36
BLAKE2b-256 checksum
How to use checksums
6706abdedfb1c9b970a035c2d3a8b394a9a2a7a5e42b3c19d959cb008a055da5
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.dev20250307001849-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250307001849-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
0d9777fa4928244a2c94218891ad55b9126ea57b1be0ee83b06db8a0c083e020
BLAKE2b-256 checksum
How to use checksums
e244e66b66217383fbd235bedb8c236b936cdfeefa1c7df382f160756c6b6a30
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.dev20250307001849-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250307001849-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
534a69d346c27a04436333899b93e95bc29b9d9c307c65c92f60fa7cd49705b8
BLAKE2b-256 checksum
How to use checksums
171d5316f60bbbf3e5d4d0c34406633f8783160623ed60b47590a7840fceda65
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.dev20250307001849-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250307001849-cp310-cp310-macosx_10_9_x86_64.whl
Size 854.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
53269f95897c8c6faa65fa68db58e2ff61c688ecbd8cf5271cae65b7c9a3e59b
BLAKE2b-256 checksum
How to use checksums
424d233ad806fbed0797915e5684b673d5bbabb343e1f33ea42711451734d0b4
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