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

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

Total release size: 23.1 MB

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

Download URL typedunits-0.0.1.dev20250305185237-cp313-cp313-win_amd64.whl
Size 768.2 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
b199d5560802e636a365a753d7a1bc845fab5cf87d7ee83d3db16d69912268d5
BLAKE2b-256 checksum
How to use checksums
167699767e7d8875718d68543259153898c41b23ab8d84faeed230d4e47d9ab0
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.dev20250305185237-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250305185237-cp313-cp313-win32.whl
Size 725.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
6768823153f1a0a7543f55ffed722ef1200d716bcdae736753a074b798c87ee0
BLAKE2b-256 checksum
How to use checksums
033ba6f52e65bbcf1f6aa0dfdc6722a238990152403f85266902f160c91c5d29
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.dev20250305185237-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305185237-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
6aba85ea4b7b4dc579b282705f988e6e9a2ca4b8785d2bcc7abbd6fdfb47a536
BLAKE2b-256 checksum
How to use checksums
ffd8199f64ce6d8f5751ce76060a3274d6d3b7842bf8bcf81a2e1202c8edc6e8
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.dev20250305185237-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305185237-cp313-cp313-macosx_11_0_arm64.whl
Size 795.6 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3fa17f24dc33c14ff5848a68a515c7405b5ff7c47ae312ed8afba330d83c827e
BLAKE2b-256 checksum
How to use checksums
03c8d0387aef901509e0aa564b8934076c604d98345d7f068bda9db52f78902f
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.dev20250305185237-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250305185237-cp312-cp312-win_amd64.whl
Size 769.7 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
731d6a15fd4763adcb5d2e88ff80ed00c930ae2f419eb8256571c3fcca59ac9e
BLAKE2b-256 checksum
How to use checksums
69608d9debf7b5e67435b12c1fd2ec3a7b48f0cf061f939e55d909157463e4f5
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.dev20250305185237-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250305185237-cp312-cp312-win32.whl
Size 726.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
d996e837208cf741aa75dde81df9da1363496c7edfef661a6301c6cf2b478f19
BLAKE2b-256 checksum
How to use checksums
4a1f6cd2add1e5179f0afd1ac4b330da97371c0ef985694b0f37a064f701e781
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.dev20250305185237-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305185237-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
a7686232dd7c1c8b28e9c657ac0ce1578566269dcaef7e8f9b2fad40d5eed7ec
BLAKE2b-256 checksum
How to use checksums
78fa923c29dd44530a08b10381980eff0c1b9ddd66838e8456a39dddbf449354
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.dev20250305185237-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305185237-cp312-cp312-macosx_11_0_arm64.whl
Size 804.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6075352546fad06fdca6457bcfb44689c17e02aceceee90c802f29aa185b313e
BLAKE2b-256 checksum
How to use checksums
ed683b918ecae62f6c070fb31985507f21ab62ff0f073a5b12956ed9ef26ae86
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.dev20250305185237-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250305185237-cp311-cp311-win_amd64.whl
Size 797.0 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
11f9dd4639d161c1de9cf68f80fc66560295437b89a19b5acab2482049ab4b67
BLAKE2b-256 checksum
How to use checksums
d4aeaa7f2ee55473d5ba14c0eccb1b74f0227fb27f9e64f98af10cb30e94d89e
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.dev20250305185237-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250305185237-cp311-cp311-win32.whl
Size 761.6 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
59bc0d8f8e1e88e5b50ec38b36f60ebee096661258509cde14bfa078ea0d40d2
BLAKE2b-256 checksum
How to use checksums
ce367cf9c64e08fcbac473715542a086b26ab9d688d510a6739ac7085054eb7d
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.dev20250305185237-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305185237-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
d782166fd51f5e3d9a7c60743f534c9b273d4ec4472b451616fd98666a17e768
BLAKE2b-256 checksum
How to use checksums
b29743bac23b42c4e08713a2f275d60aaf71ac2165537b5874265a25d7d5e420
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.dev20250305185237-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305185237-cp311-cp311-macosx_11_0_arm64.whl
Size 821.5 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0b9e228fe38a48c31a2ce010ac261f3ef7e862eacbddcad935f2f0025cfe737a
BLAKE2b-256 checksum
How to use checksums
ce7e35fcbcfa944a65913354044fe86e5617d33b72caf3d1ca625ea08f83fbdd
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.dev20250305185237-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305185237-cp311-cp311-macosx_10_9_x86_64.whl
Size 880.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
65dd1550b08139bfe1deeb49f15acd1001c5c90055f57491fad5655f88e579a2
BLAKE2b-256 checksum
How to use checksums
c798fdf18ec5451bb047a556fe8d40ed8ecebb6d78e6e8133198b8c73661c734
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.dev20250305185237-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250305185237-cp310-cp310-win_amd64.whl
Size 793.4 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
de733c8b30a75c1fc99da34b4201fe5964c0cb0a527c4255bb59b645dfd1d980
BLAKE2b-256 checksum
How to use checksums
5aeea0a2454d2ca27400a5688fc1fcdac0b217bcd25ebbf5bad98082ae71baf8
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.dev20250305185237-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250305185237-cp310-cp310-win32.whl
Size 730.3 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
a44ab79f9dce71a2d80b4f10e71ddd6d0be3179865db9a026e8e6c113802b9be
BLAKE2b-256 checksum
How to use checksums
19f524bc0da2f78d7073eb24b9be8e57ccfe6c7a40dbe4e5629a1d2c7d159b4a
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.dev20250305185237-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305185237-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
24ee366306f947b9125f3ef6a859a4732c5e5e11b439393195890dd20cd9a59d
BLAKE2b-256 checksum
How to use checksums
0a765fea762f75f99d45342599df6cd2a423df90442a045f731ed6266320fe23
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.dev20250305185237-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305185237-cp310-cp310-macosx_11_0_arm64.whl
Size 788.6 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a49ce6fecdb9db90ce5ba9370e1aac2e63cca21ead781eda744048ec3ee1a9a0
BLAKE2b-256 checksum
How to use checksums
28c4ecf842163f3f3cc6408c8fe3ec030b2170f9a515865a721f5136472dfd16
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.dev20250305185237-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305185237-cp310-cp310-macosx_10_9_x86_64.whl
Size 845.5 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
01c01481e4cdb31bdb2e679cda54802181fd39e11503d6dc8efcf145339040fc
BLAKE2b-256 checksum
How to use checksums
eb301695faa8070dd2bbb0f0a1183a55275250c2b4f49219d0e2c464d184d938
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