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

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

Total release size: 21.6 MB

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

Download URL typedunits-0.0.1.dev20250122222505-cp313-cp313-win_amd64.whl
Size 720.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
fe6a50b22e4cdcda59073c7605b3ed2d2ace415ce7688eae58bbdfb32f5b09ed
BLAKE2b-256 checksum
How to use checksums
c181a1fce8c1e0e47e769ba258910e03053e01f781d13975d22afb468fbfb2f4
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.dev20250122222505-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250122222505-cp313-cp313-win32.whl
Size 680.7 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
8f73c5793b4263fc818120ed0b2ae720f2baf527f3e4f39e7dc727292ad1c766
BLAKE2b-256 checksum
How to use checksums
b009b7761f6bb980d7000af23fdb77ad0a348aa19303aa061cc1c5db9b1e57a7
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.dev20250122222505-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250122222505-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
e5f9755bcae53c55c034f497b3fb9fab07b98c7456d864532eb77df81cc75eaa
BLAKE2b-256 checksum
How to use checksums
d598db4a7ea2a502e25cd72c3a8d75b6c660bfce0f67bc02b2c57b4ef4be8391
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.dev20250122222505-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250122222505-cp313-cp313-macosx_11_0_arm64.whl
Size 747.4 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
293974f6ec4545debf0a500171e4d2924eb8524de869bac620cf1a9df0561b2b
BLAKE2b-256 checksum
How to use checksums
43dde3d8e9189aa1149f3b534d3da862e92701cc915ab745e1f1100a4f3613f4
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.dev20250122222505-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250122222505-cp313-cp313-macosx_10_13_x86_64.whl
Size 783.8 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
647434591c8e20f0954980fb406204543beef41c7247ce87fca71c815a972f26
BLAKE2b-256 checksum
How to use checksums
18a5d0607a0ee22ade1ea3ef4f9a9fd62cfc381929e821114fdf75f63f65be55
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.dev20250122222505-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250122222505-cp312-cp312-win_amd64.whl
Size 721.8 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
32af38084d0718e60112f5980352f26271cbb5dfc8daa84bd4d57b8e8fef8396
BLAKE2b-256 checksum
How to use checksums
f74b2d5a8b89d229b057cc93e7a932680403a330dde16bda25d2e69d527b3af4
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.dev20250122222505-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250122222505-cp312-cp312-win32.whl
Size 681.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
01b1202f23f078846147fe2b77feeca671217ff5e60c3521ef14039d21b239bb
BLAKE2b-256 checksum
How to use checksums
de4c4ddf37d2df0696996e0e19a27063169f0b82ca48239ac650ed17e63c8e56
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.dev20250122222505-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250122222505-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
a5d78da6b61a4413eb76574cfd8aa77d9ac6b36b5d387fb4fcc52e4cfad92a5e
BLAKE2b-256 checksum
How to use checksums
8b4b338e7de18cd6535b30bfce7551c0e45ff90495c211a1295220cdc90f3388
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.dev20250122222505-cp312-cp312-macosx_11_0_arm64.whl

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

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

Download URL typedunits-0.0.1.dev20250122222505-cp311-cp311-win_amd64.whl
Size 749.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
0764eabd07435349bcc8f6f2ae3289f2fffd0a70db35cf5ce5c4cabbcc4acb2a
BLAKE2b-256 checksum
How to use checksums
251ddf927b0b1f053dac479ed1af73375b35dbee7204d0d585c6b1d7033e13e0
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.dev20250122222505-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250122222505-cp311-cp311-win32.whl
Size 714.4 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
6779ce42492ebc217f30663394de2765de7e63fbf60b1324582270f82ee17b8d
BLAKE2b-256 checksum
How to use checksums
98be60f2fa263ba58495e7650ed2229236257aa5058d55079347e78c5a7ebb9f
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.dev20250122222505-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250122222505-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
c59dea672c938c644a0b650bc4c1078c94c5f9cf720e6182783ce02b0c95862e
BLAKE2b-256 checksum
How to use checksums
2af18063912a20c6690f39f608d8d69b4f27547792da14f0397cc18e8c5f7541
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.dev20250122222505-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250122222505-cp311-cp311-macosx_11_0_arm64.whl
Size 767.0 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a6f1fb513e82ac2030e5feecc388bf73c6620e1c9e9f7f51a76af0e0828d3a30
BLAKE2b-256 checksum
How to use checksums
4879ab1b1bcc0dae7749a7ca9aa5029609c125a7bdb5a0019178582d00ce355b
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.dev20250122222505-cp311-cp311-macosx_10_9_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250122222505-cp310-cp310-win_amd64.whl
Size 745.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
6ca4a59ff6182d4dc6d56a7eb666f129a196c41e3daeabf69a1f81e927224258
BLAKE2b-256 checksum
How to use checksums
e8524cdce63197a9a9c932c74d2c4046d50fcdee400ad3a94f979d8c56645d16
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.dev20250122222505-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250122222505-cp310-cp310-win32.whl
Size 683.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
e2c08e9702e5fc990cd6581220412688a9b3de2cced272cb3d31b11e0ff7dc5f
BLAKE2b-256 checksum
How to use checksums
8ef789f7fbefdb4f2e2576c14ba58ff1ca55bde56c3cd77f23effcefd74f0a60
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.dev20250122222505-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250122222505-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
4b912d2b7218b97c61304f82a2bcaa9afb200dabeed7e6dc5965e9c9a51d6615
BLAKE2b-256 checksum
How to use checksums
66a956f87e50e6bd8d3d2b1511e0af30566b9c3fe38391a8e31723c0af44bacb
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.dev20250122222505-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250122222505-cp310-cp310-macosx_11_0_arm64.whl
Size 734.1 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
37fdc6660deab395a45fb7607190b008b6a9cbc856b33025d93ffddbf8620cf9
BLAKE2b-256 checksum
How to use checksums
242fc11454a3907903768273543021ffe9383d18e50d0bd0ed9b839d4417fd08
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.dev20250122222505-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250122222505-cp310-cp310-macosx_10_9_x86_64.whl
Size 791.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
301afc4e77d31afd82b12a83dff0b171fe96fc36aea6a71e2c2519e89b1a76e7
BLAKE2b-256 checksum
How to use checksums
531ba462d6a8aabbc238e364c8d05d0b997363ba0f28e270c707209f2e5548c4
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