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

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

Download URL typedunits-0.0.1.dev20250228032247-cp313-cp313-win_amd64.whl
Size 751.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
8241aad06c173b84dc92ed26beb0f0973185481a8a71f50e1c7f9a2ccffee4c4
BLAKE2b-256 checksum
How to use checksums
2f177ee0098d3ced5cbe5fa0ce33ecc41c21b1eace0a14c3c7adb7d455a6b3c0
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.dev20250228032247-cp313-cp313-win32.whl

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

Download URL typedunits-0.0.1.dev20250228032247-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
c33c860b7215661e502639b070777ab3aea8371da5bd24d18df9ec7a7785d69c
BLAKE2b-256 checksum
How to use checksums
08e85b662960e483bcfb21bf9e789668fcd1920dec6d1b721eeac6848c26bee6
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.dev20250228032247-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
356b5444fdbba108f318193bd6b84e7956c365e693250d05ea4b8464ae35a5da
BLAKE2b-256 checksum
How to use checksums
0320f8f84b6da10b34f91f3da305bb54624b38c3ae6659ba858c1939d0f3c6f5
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.dev20250228032247-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
9c007b27174035eaa1ea1f995a962447cd4815f5e72c46462654e5c8a386a9f7
BLAKE2b-256 checksum
How to use checksums
74b5609c5fe4f92b44bb4e90628579b5955a172f384598f48b953f05758cc103
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.dev20250228032247-cp312-cp312-win_amd64.whl

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

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

Download URL typedunits-0.0.1.dev20250228032247-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
213d0d5a0a9bd145712a9180e4029f2dce646046554756fc6bd96569a8681632
BLAKE2b-256 checksum
How to use checksums
8631ed2942290a17e0e16c5afc9287a1c1f645d2f6ad414d37eba7f791156f3e
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.dev20250228032247-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
4462f82e8a50bc52ba53a4f0fd05a430a242c3111c42adab07ae72075b7faf77
BLAKE2b-256 checksum
How to use checksums
6c0a78d9483213f6da641fc0133494316240854c8005f77e72d1c8ec3388129d
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.dev20250228032247-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
ab29a7099cb5ba2ee756882e90ca4a442054387e4e663472681837882bd38230
BLAKE2b-256 checksum
How to use checksums
700d1d24bd80cb39a2bf06d68052ea0a18c77e34b44248b88b38f265bd5bf617
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.dev20250228032247-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250228032247-cp311-cp311-win_amd64.whl
Size 780.5 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
97cc2b3f3d103cae6e836888d45a3006fc70f23c33d743967372d8068b201f0a
BLAKE2b-256 checksum
How to use checksums
b7af82769cc454fe63d7c3e96108091e2cb953fd13a48fb1c0cae3e37a22418e
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.dev20250228032247-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250228032247-cp311-cp311-win32.whl
Size 744.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
17b5037efad3b548fb68045c1aa8f9e629134814123d2ac4aa8f48684d730a83
BLAKE2b-256 checksum
How to use checksums
88205b8b41c30e898c54434d7abc11f3b2c779413d55988adb9a5bef82b64fd0
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.dev20250228032247-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
9d7c2bca48565d442091f76961a3553fc48859efffff980715093b020d025fd1
BLAKE2b-256 checksum
How to use checksums
d49d6f7b6bbeb53a0099b517ac630c556db5d5dc9105681c0805f048cbac2cc6
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.dev20250228032247-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
68597643290fca942f3d98b69ea670d5cb7dd5c8ae49bbb5951cab48d65cb5f8
BLAKE2b-256 checksum
How to use checksums
9547fe16b7d81887bc7ef0391244321f1b5275553b60f59e91eb7d3738c4437a
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.dev20250228032247-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
58d7b289fd5b919400f14b44f710a129b4814d91e076d46511cd8dc3707b578a
BLAKE2b-256 checksum
How to use checksums
43d3a27981507120a1e1b85121f3cee20bda2c264ab1251625892e4bd1206700
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.dev20250228032247-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250228032247-cp310-cp310-win_amd64.whl
Size 776.6 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
943f63da20a1e5e2882c821916ce6db6e8294ffa6acd815d1c496ba02ae7fa22
BLAKE2b-256 checksum
How to use checksums
5fb498c9f13f77eb47e03f9f554fdb774cef3f04edd11c2204f91ec2cd9dfa2c
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.dev20250228032247-cp310-cp310-win32.whl

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

Download URL typedunits-0.0.1.dev20250228032247-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
88834ab32b4dfd16f1a42ec27e5d65933f248698997ca47669cac2a29612a713
BLAKE2b-256 checksum
How to use checksums
f7229178d03ebf54d00624e494c2340ae376ab4df12a86c16a07815d429b5f80
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.dev20250228032247-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
fe66b96e0c8599cbff3540e8b0eaf6252f7950644b28a0a6d42f6fef7d210d21
BLAKE2b-256 checksum
How to use checksums
06dae6a76cd8921cdb1fb6e4fcde6201d2849d8d0f05ec0d89e1d6aaf085e40f
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.dev20250228032247-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250228032247-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
395071225744b95505d629f3e0ee592b2928a8db68c32bc4587673da3ef193f4
BLAKE2b-256 checksum
How to use checksums
b23e7da4c8a30c67257b6bd24f2d198493ae184b4819e0bd33e114218487633d
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