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

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

Total release size: 22.7 MB

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

Download URL typedunits-0.0.1.dev20250228051437-cp313-cp313-win_amd64.whl
Size 751.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
6fa5affcb18ea2d265c8629aeaaeb3a6de050a0eb93f05abea647f795cdec4d5
BLAKE2b-256 checksum
How to use checksums
219e77bb42468f06a93968d71d5ff56139a58562b6d0cabb0f58ea14c712cf92
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.dev20250228051437-cp313-cp313-win32.whl

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

Download URL typedunits-0.0.1.dev20250228051437-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
4dc7528624e5a3896ab260f79a77a9fcf8211934b2d2e7204f0fcc81d2882313
BLAKE2b-256 checksum
How to use checksums
86b860795c1a1ca3fe5f67eed15eee689e9c510a3fa96a0870d74f2a9f660abc
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.dev20250228051437-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250228051437-cp313-cp313-macosx_11_0_arm64.whl
Size 779.7 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f3cb0ee4379bb93a43b71789d3a26b544a472c6c237b32af84873897096623eb
BLAKE2b-256 checksum
How to use checksums
8ddb4482c29fc4e20fea8ecf1e21e462f742d27a5b3a4554161649ac5d184645
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.dev20250228051437-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250228051437-cp313-cp313-macosx_10_13_x86_64.whl
Size 819.8 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
5688149eba59540dec5fcb03118019cd09f9aaf14ef534ce0777765d2fe53a81
BLAKE2b-256 checksum
How to use checksums
1f7c23648d17dd1db767658aeda4258682f654e75b1532e4eba130beedfa4d06
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.dev20250228051437-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250228051437-cp312-cp312-win_amd64.whl
Size 753.1 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
abfae3318b6a5c67ef4024d15f9aaebde3e92abf0f95aa318f2ecf3487722d2e
BLAKE2b-256 checksum
How to use checksums
7334cde222237b63dc1898483eeaa6417491b3e77e06bcb58bb5a9f0130b869c
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.dev20250228051437-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250228051437-cp312-cp312-win32.whl
Size 710.5 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
c2b119ee0e1cd083a795709ccaec02342c28bf7c97666d05bed7869a72e7325c
BLAKE2b-256 checksum
How to use checksums
2f5260733d5d22be3623c5fe58da55f0ac69395054e41841e3243ea170a756ac
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.dev20250228051437-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250228051437-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
0ffdf3839e72cbe5d6f28b70c87dbb0b2142da2dc26fc3641dc2e955955fd4b7
BLAKE2b-256 checksum
How to use checksums
b7e4d7dd181e7ad7418ee7026badf34d68878b317dcbaa5cecdd83ed5e3a4859
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.dev20250228051437-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250228051437-cp312-cp312-macosx_11_0_arm64.whl
Size 787.3 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0704f99d6dbffdc6c57b49b5ccc06d5ac95a63f586910457b0826d6842b8b8f8
BLAKE2b-256 checksum
How to use checksums
fdffa6ae0aa786b926c554d5279537d4887c59a8e800524303fca6a6d5562bd6
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.dev20250228051437-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250228051437-cp311-cp311-win_amd64.whl
Size 780.5 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
6498ea285b12260e98b5b176f88ceb64be29586fdb91a7ce887ab9e82b631283
BLAKE2b-256 checksum
How to use checksums
6f8b598babeced92e15359631e21455e1dd2ce0c677d2fd8926d1d1b3ccc106a
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.dev20250228051437-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250228051437-cp311-cp311-win32.whl
Size 744.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
cd7730d40581f1d8f038abed166a461d933ef46ea416e1c68b3414c744bce622
BLAKE2b-256 checksum
How to use checksums
71320918e740a60abead40ef99a400c54810fda1466d08c68d7d4ab2721ddb91
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.dev20250228051437-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250228051437-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
0447b7c71fd70282f099f4c104923985f11ff86b9139ccfa1ed3f6b104126ce8
BLAKE2b-256 checksum
How to use checksums
7b063c5a99df491b82c1d1f6f40702ec38f69e4ed0ee3660657ff556743fe4a6
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.dev20250228051437-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250228051437-cp311-cp311-macosx_11_0_arm64.whl
Size 804.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5eec5c6356d9f0eae7f6ca54772ccb36960db42675c5847e8e2ee1bb2e99faac
BLAKE2b-256 checksum
How to use checksums
a21cad20489cbb6ef5712b1472491eb2db4e6334dfa5cab758ef96e0103964ab
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.dev20250228051437-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250228051437-cp311-cp311-macosx_10_9_x86_64.whl
Size 863.6 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
53df3944b7e7281f9eccc15c10e934751543c9dff30a662973dfb4a99178e229
BLAKE2b-256 checksum
How to use checksums
3e84e6a891c4122c0ca77d50406f9d15a665de2273b84bdff5f91c6088020724
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.dev20250228051437-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250228051437-cp310-cp310-win_amd64.whl
Size 776.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
199792c676d5e627d21597da53c5b6b57e95f0a4faa85a8aabb1540c4a53195f
BLAKE2b-256 checksum
How to use checksums
6e84dc270271b73afb033c627f7e4ffcc6ac2c92127b2c1500468d35c342772f
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.dev20250228051437-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250228051437-cp310-cp310-win32.whl
Size 714.0 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
f63a8bb322377250613046c1258a23723a2d98beed75722584570a1773a1cd6e
BLAKE2b-256 checksum
How to use checksums
14058f3f0a959573722ccc8209680cb91620f61b0e75687fd7f4c007fcfa535d
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.dev20250228051437-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250228051437-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
eca37b287e6ab998f51d0916b01ccf7967d6ee076e0f33d01b7af4e42763a381
BLAKE2b-256 checksum
How to use checksums
403f0ed8d18b90d2a0a3ce04745f1a9fa52e9847b5efc7fc3963cba8b276ca01
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.dev20250228051437-cp310-cp310-macosx_11_0_arm64.whl

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

Download URL typedunits-0.0.1.dev20250228051437-cp310-cp310-macosx_10_9_x86_64.whl
Size 829.5 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
c831b991e1769aee9c4f314106259eb9a87ab2d279265c215bbfa292760687b9
BLAKE2b-256 checksum
How to use checksums
d1d3a668461d5ac31093ffe2dce2c3d16faf9e38d89d7acf91cee983f3974408
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