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

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

Total release size: 22.4 MB

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

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

Download URL typedunits-0.0.1.dev20250218222530-cp313-cp313-win32.whl
Size 702.2 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
d764e21dd53eaec2a0d4f506aed2e35980a0160cfb8a028aa12674916069b8bf
BLAKE2b-256 checksum
How to use checksums
79685147fb472a9b2e07794c204914c51130e81c54131378cc63415aac88bcd7
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.dev20250218222530-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
b928a22e04d661c60df4cd838dd3e7363728fe8c8fc89499bd197a410a9c523f
BLAKE2b-256 checksum
How to use checksums
6d8bdf7952a22fd9c4e282738d1b1202fd4292cb64d9b7166a7acf79c4b8bacb
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.dev20250218222530-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp313-cp313-macosx_11_0_arm64.whl
Size 771.1 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
eb821a04cdb41776a3152281849479de3be283fc48c33b9c67d854988d987888
BLAKE2b-256 checksum
How to use checksums
ce061ea33a9fe6cbe5959300865abf7ea0e555d2969dde7c56097684c24ae768
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.dev20250218222530-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250218222530-cp312-cp312-win_amd64.whl
Size 745.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
ffb9dc376de9cffda7f3dc7cb723343f6a1150b17d49f58867bed691128240c8
BLAKE2b-256 checksum
How to use checksums
1d7c45d5cbfd05b91faf7b86e24560427d709c9e4d1b29773a2fd6a81af6054e
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.dev20250218222530-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250218222530-cp312-cp312-win32.whl
Size 703.4 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
8e5fb8748c98eff56f128078e64f034fb0b5b9f17fcd22e43d8a9a724852243d
BLAKE2b-256 checksum
How to use checksums
18148454383e1bd51e48b3f5b1105b6741f2e9d78036a89fd06f0e02ba49c8f4
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.dev20250218222530-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
423b8b9297c016780302469dfb4bd64b37a725d64f6cc222e88311e9c1f38010
BLAKE2b-256 checksum
How to use checksums
5a897a740b14784c0c4ead18a335bc75bfbe1dad9cfa55ab89b86ad361148254
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.dev20250218222530-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp312-cp312-macosx_11_0_arm64.whl
Size 778.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c00bc44d33bbbeb6b79e5cdaaef538429f51f931076c963e6382f2e16c0c7f05
BLAKE2b-256 checksum
How to use checksums
6866486a37a2425a845090415d70f72bc02d532194482d5be48ead3c46abd2e3
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.dev20250218222530-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250218222530-cp311-cp311-win_amd64.whl
Size 772.1 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
1111c4c59cd9be34bf6d40c337ae108a7541ef1fa526e535a290fb40b35eaf14
BLAKE2b-256 checksum
How to use checksums
71bad2ec5cff4c918ce06997445336e5db89181d10514a878a8076cf6e1e9763
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.dev20250218222530-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250218222530-cp311-cp311-win32.whl
Size 738.1 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
ead526264e286ad244aa45d7c893085fd16c3b13608e262fb6dc0f855fa6fbaf
BLAKE2b-256 checksum
How to use checksums
ccf67c3c85ddd6fcdc8bde9e293af3ff7027d2ca859d8ced12030f4a4472d7dd
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.dev20250218222530-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250218222530-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
486891b6f487a4a7ab30826b214865e8a11d5c879ec255192b7302fab68079ac
BLAKE2b-256 checksum
How to use checksums
40bceff7262c56c7f8e535510d0af044e1443339d1e8646cc5499b1447829ab6
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.dev20250218222530-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp311-cp311-macosx_11_0_arm64.whl
Size 796.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a1dc5392e87784eb4c06c0b78b4b059c9319dcbde6a2267b4d1c6acccf570b4f
BLAKE2b-256 checksum
How to use checksums
04ce41cc0e3c0bbd49c1c1834c1bd22b39369d7090fc6ff9526e71a90015eb48
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.dev20250218222530-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp311-cp311-macosx_10_9_x86_64.whl
Size 856.2 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
1b58e45722a4fa8d953f64a3a4030419093bf8071546fb9caa7919b94cf760dc
BLAKE2b-256 checksum
How to use checksums
75c24ef4e98bf6ad2ae7e7e95592c05fc40cb8954f989704596dfeaba9f01b5f
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.dev20250218222530-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp310-cp310-win_amd64.whl
Size 768.8 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
761bc6c6a102833ee258b19f9ac40b4fc7737f3a3aafbe35896361a57626e591
BLAKE2b-256 checksum
How to use checksums
2f2845ebc2b195c25a9b36c1cf1e0fed7a14747c4c9c630c83a16ecb9a0123c8
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.dev20250218222530-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250218222530-cp310-cp310-win32.whl
Size 707.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
e39847c4d9390a3e3c773230bdcf07896fe389aa35acef96594deeb768a11c1e
BLAKE2b-256 checksum
How to use checksums
2ed7ad90d156003a4bb42b4ec70fd1a108a41b9bd200bd9eb355ee10c9ae8c40
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.dev20250218222530-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250218222530-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
f6bdd7e3e74f245957edae70240e9b35b46bd960ed01c8c52b0b7ceee85deca8
BLAKE2b-256 checksum
How to use checksums
1d5315b0341e58582f04fe31ec2ae1a0d8d2dc4c5cd4a4af9567db8e16780123
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.dev20250218222530-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp310-cp310-macosx_11_0_arm64.whl
Size 758.8 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6cccb6bc9bd99106d58a42fb4b329f9a3c28cabbcda35465f2ab4831cd94df35
BLAKE2b-256 checksum
How to use checksums
7d8de16a2e3948a6679a2ec6c972ae957714a136520248de8d48319ab31a1f9b
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.dev20250218222530-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250218222530-cp310-cp310-macosx_10_9_x86_64.whl
Size 821.4 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
04cf9a1ba3d39673d60d57531dacae813950ccc8843046cefbbdeefbeb660229
BLAKE2b-256 checksum
How to use checksums
1b5d6f04da0d44795e4257cdbf631b6748943b9e893e79ca17410bb69c1c172a
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