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

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

Download URL typedunits-0.0.1.dev20250224221743-cp313-cp313-win_amd64.whl
Size 751.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
ecc428214c9d926e270492a6e9341136137577941853699a4c068765ed005aa5
BLAKE2b-256 checksum
How to use checksums
04afcdbec1bd7a97dbbf70dbfa6b396153782f00d90a558af8d44fdc95644350
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.dev20250224221743-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250224221743-cp313-cp313-win32.whl
Size 709.5 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
3c1ee5a06a462d38de0ac62d99b14caea450274be819374b82ca46b2fd586218
BLAKE2b-256 checksum
How to use checksums
d0ba4877725a5d349ed1d42d72b8bcb3ca492584a653bcd26887d78f06d0ba8f
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.dev20250224221743-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
2ce3941766223e8665ae0d6421365021504a2a016058c10e288ad6cde35f0f63
BLAKE2b-256 checksum
How to use checksums
7ac64848012b1262fbd63c0d9910185f153b8566bcafc5324a998ed14412f512
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.dev20250224221743-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
9c3931840b5b846aa8860a17cb771d5dd197a6184a275f25c87519e40b459f73
BLAKE2b-256 checksum
How to use checksums
5027d97fb9c0f37fe8c31b33209bd8e1ef2ab4c243a03cc4f75fa8a1d1e97745
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.dev20250224221743-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
bb55b2815b117a0cb2f4c024abbfe0875ff939f824a4eb7ddccd6aebb2fca8d6
BLAKE2b-256 checksum
How to use checksums
dc26c28369f64e167d3b8526828c471f2e0312c16a845f9809c0e1c7e7bc7d32
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.dev20250224221743-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250224221743-cp312-cp312-win_amd64.whl
Size 753.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
219e13fd82ba887e6f9c3dfa4a181599e9dae1585dda4d322939398b98afe94d
BLAKE2b-256 checksum
How to use checksums
c3c38c8924a343674d7ac05481828a4e84c9ba893d692e20a84714e555a62e86
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.dev20250224221743-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250224221743-cp312-cp312-win32.whl
Size 710.6 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
a5c0e14f95633190e8af63d773d3cbe7e75c0c360490236fd7c2d8bc9a800e2a
BLAKE2b-256 checksum
How to use checksums
3f82dd4e3883c3261201a555dc5752fa80babc1c4697f471d908d4596c1fbe71
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.dev20250224221743-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250224221743-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
04d948f3ab50642dc9232cbbf71a97f0648200884c27ad627b22d6ad272ea8d8
BLAKE2b-256 checksum
How to use checksums
d044301c51cb862250aede00f495f64eb30398a2b6c1d7800c9fefcf64e0a6cc
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.dev20250224221743-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
63afedbdc7c08228f1837d62e03a827f946d3bc30f09aacbc0753a58594ab3c2
BLAKE2b-256 checksum
How to use checksums
416120b9fb364414d56ba5cc646cf3255bff6910a2e181a9466f124962277750
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.dev20250224221743-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250224221743-cp311-cp311-win_amd64.whl
Size 780.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
df28a8d3e846ddebb1abce756d415015fe2ba422b6d8bf6b288e05879218e6f1
BLAKE2b-256 checksum
How to use checksums
c398e90fd9f3c24260c429446577e158a7e2605d0bc27ede7fa1eb1640572f9c
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.dev20250224221743-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250224221743-cp311-cp311-win32.whl
Size 744.7 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
309db3c77a5a7520f672e83ee34203385bcdf182797c2ce702e4045c0357b0f2
BLAKE2b-256 checksum
How to use checksums
5b69e3635250d2568165349859b903c9ca5c38902f725ae23b209cfe81865a51
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.dev20250224221743-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
a4934c3e9190f85883770ffe10866f4a9771f15c8a54466d087f29bb76ea6ad7
BLAKE2b-256 checksum
How to use checksums
8c52e3e0d2202224660c608be9e37469b0c2cee5d0a910756a2338fd91982709
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.dev20250224221743-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
1898fea9099e621e70c0d1210f68ec042a7b2a031b50e76d9f4f857a279222af
BLAKE2b-256 checksum
How to use checksums
4862cca621f4330cf43309bbd3ad63c7aba2b3a6f3e7716db06cda3266d844b2
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.dev20250224221743-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
566c4f6324da29d052c80e2573f0d6b2b74eeca764ff36afc2dcc4d2140dba66
BLAKE2b-256 checksum
How to use checksums
265054260b50b42b5d52fbaa339c43416f0d737421fd552d001738618ba00876
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.dev20250224221743-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250224221743-cp310-cp310-win_amd64.whl
Size 776.7 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
6ddaf4ad18df8cba6028c99996024bad99f0395f70c78d4743f4ca05a895e594
BLAKE2b-256 checksum
How to use checksums
0625a64005f00d4accc8fb42be6fa3e430356ce58a733732aa023317f7c494e1
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.dev20250224221743-cp310-cp310-win32.whl

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

Download URL typedunits-0.0.1.dev20250224221743-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
e8e7960d2edcb5289a46294adee44ab7d2ec4dff90d210c8aede4051799c6ed3
BLAKE2b-256 checksum
How to use checksums
63421c05e5ccadb602a319fdc57988b9cad2a3b7779b3d338d53b2116039d10e
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.dev20250224221743-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
91a4c6175211ad568fcf7fded6863293f08a3e7efe70ca7b060f2d817a6965f4
BLAKE2b-256 checksum
How to use checksums
019a2102907396892d8fca13571e6f4dd54ec45d12d906da5aeea72f91fcc0b3
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.dev20250224221743-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250224221743-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
59a0972367f0277f27736196363f87677b5be17b9afdc59003f69cf093422b3b
BLAKE2b-256 checksum
How to use checksums
7d11167d4d4f5a4dd9c31a38295a5188cceb0e98cbbd8dd536c9fca5e88e4aaf
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