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

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

Total release size: 23.3 MB

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

Download URL typedunits-0.0.1.dev20250404020538-cp313-cp313-win_amd64.whl
Size 775.3 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
0c6a5ab804aa6f78df5e0d18e19e23670c001fdd87440791e1abb90b5e01b273
BLAKE2b-256 checksum
How to use checksums
3085a71cd8974f9c0c93e4ac05a5777c4bb748c0525fa1fa254d3d0a7ecf8528
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.dev20250404020538-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250404020538-cp313-cp313-win32.whl
Size 733.0 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
95f6fcf10568d9e68d8bb564bee880c150b55d98c767a8a0596ab47119c43e37
BLAKE2b-256 checksum
How to use checksums
bd8be21fdd9d14e697b18fb1317a56bff72a40a43959862bfd4fbf81de8d4eed
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.dev20250404020538-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250404020538-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
2a918bc1ba219cb781f3ba69920ec459c9840c217ebccfa1c7107f1e2f260a8f
BLAKE2b-256 checksum
How to use checksums
8ee543d1e7bfcf16f71b2e3e82fcc572c63eba2797015bd17f4a1b8985e58093
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.dev20250404020538-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250404020538-cp313-cp313-macosx_11_0_arm64.whl
Size 805.8 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
64687be55059a4264a4ebbd028306e8e685d0fbe5300c0bcad2e2d2a949c40b7
BLAKE2b-256 checksum
How to use checksums
e41a974190948d12a89912c8810e808be36f39061c2cfab0c638e3a3c8277ef6
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.dev20250404020538-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250404020538-cp312-cp312-win_amd64.whl
Size 776.4 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
822fabac27164d09b8a2b2994cdc2e5d735bd820ccd8de95425279b1ab3cdbcb
BLAKE2b-256 checksum
How to use checksums
af784c00b1aff73cfa1615f90487eb127c956a53c451ae92d33f290e96a1a44c
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.dev20250404020538-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250404020538-cp312-cp312-win32.whl
Size 734.3 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
f0791606029cd00bc55c741e27bff516271749fbde12654c171db4fa716f020f
BLAKE2b-256 checksum
How to use checksums
759e153ed2800d804cf5597fe8f799284bd3ec228f6b71580472d78737cfd667
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.dev20250404020538-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250404020538-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
4ad5cdc1de1694be1f6ecbd5f261f08eed894ad769c2dacaf52fb17a9256d125
BLAKE2b-256 checksum
How to use checksums
c6867017eb5bd7689aa79e67933ec26e094b19b326923f33aa57437c88c12fb6
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.dev20250404020538-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250404020538-cp312-cp312-macosx_11_0_arm64.whl
Size 811.5 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
53a7fe51a296e9fd2fed8a90d48c3ce7d91550abd6bce348efe0fe9f9e5edfcb
BLAKE2b-256 checksum
How to use checksums
9b6e809857cf60e598f3e997a4ce43bc729da0d29aa33c891f53706e4aa2250c
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.dev20250404020538-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250404020538-cp311-cp311-win_amd64.whl
Size 804.1 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
a509bb3e296f2f084d1e9617beb1e1db927fa8015bbea28d0779fa0e26d43c30
BLAKE2b-256 checksum
How to use checksums
5123497eebd16eb85b82b80161064a5ae2d05b105d6b28697c2c9eb9d9702047
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.dev20250404020538-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250404020538-cp311-cp311-win32.whl
Size 769.4 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
4966e591e2234f01e488e1d7981cfa4ca0c967c2c779e8c4a29f6a373b9784e6
BLAKE2b-256 checksum
How to use checksums
5f8ccc52d5df12aa880b91fca6a37c8933eb1d4cac9f832bd15b55f0be0192d2
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.dev20250404020538-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250404020538-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
31691b7d535685116668403f6e37aedb5e1899a6d24bbe8d0297ef8f583637b6
BLAKE2b-256 checksum
How to use checksums
ba1128ee29c49345bb0870a1534f4850736ba409504dcbc1882221cf7b3e5da0
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.dev20250404020538-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250404020538-cp311-cp311-macosx_11_0_arm64.whl
Size 828.1 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
ceaa2def52bb7040478a8373d08e8be50d205122243902d83935c067d727fa89
BLAKE2b-256 checksum
How to use checksums
b2247dcc4075d0cc6db23cb24f2c461f7a62d653b6a69dd5eb6dfec3bb770a44
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.dev20250404020538-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250404020538-cp311-cp311-macosx_10_9_x86_64.whl
Size 887.8 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
c953caded41e2b0b3b2510e363f5bbd1c6a7ffa40745c89f81395a5758e0e544
BLAKE2b-256 checksum
How to use checksums
8a46b886bc6133ccb79700d7ab6c4d6176ab5f0a108723490fd2501bb1e426ed
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.dev20250404020538-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250404020538-cp310-cp310-win_amd64.whl
Size 800.9 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
73bd5efe72880ca8c4f7148f3731f0854f83309a7221027497dabe412143d30f
BLAKE2b-256 checksum
How to use checksums
e720a6d5240ea6697b0bae3634852a4629730f053342e8699562901751248acf
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.dev20250404020538-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250404020538-cp310-cp310-win32.whl
Size 736.4 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
d5b4d8ec55955a51aa9414129f7259222973be2297975ca3080fab46d39ac829
BLAKE2b-256 checksum
How to use checksums
9337d5545c1575b7a02f3673443fea62be57367e52dc356d04c55ec55a385e67
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.dev20250404020538-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250404020538-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
446941f1fa5b46a1e4fdb9aa809ca45958dca99dc23d3c627c340eef16204b1a
BLAKE2b-256 checksum
How to use checksums
25248b13c52e3ac13aa6c19f127ff3b5a150d89347744f70414e0c8f1be7bfdb
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.dev20250404020538-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250404020538-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
f9065eab2bbe14c0b2926d39619d625bc045b94bfcfec8a116dc92696bc78232
BLAKE2b-256 checksum
How to use checksums
ece41291a56d442352e1ace44abcd6b40f91ba32bd6c483dd9554ee9e3a8585d
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.dev20250404020538-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250404020538-cp310-cp310-macosx_10_9_x86_64.whl
Size 852.1 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
eda1d5cd00aee6e000280c392e1c4e054f33c479f4ca7da4fa87cda61895ba63
BLAKE2b-256 checksum
How to use checksums
8fc6b8da5ca3d2591646df93e529e816146afb629bd689c2d73f2f8084fddc54
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