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

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

Total release size: 22.9 MB

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

Download URL typedunits-0.0.1.dev20250305020553-cp313-cp313-win_amd64.whl
Size 763.9 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
b28c9cbe3977af4220ca81c037bf7743fa8ad4414fd88a796520e8c20dd10e85
BLAKE2b-256 checksum
How to use checksums
0af90e4c85c3b4df4782b9f26d2c56a4ceb7330724f56a121fc0c417cf582d39
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.dev20250305020553-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250305020553-cp313-cp313-win32.whl
Size 721.5 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
6ef26ac9ac6794914eb8591cb70ebd4b71c004f5807c96837e06c7bd04ddea5c
BLAKE2b-256 checksum
How to use checksums
aeed9427cb893d93179ec9043add9cfe31369b19198a0644a42c3a652ad6f2ba
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.dev20250305020553-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305020553-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
cf055355749095f738ea2bc112d7f090b54e883df5e7c52d40331b98915e341e
BLAKE2b-256 checksum
How to use checksums
aa7757e550f324b70fc3d18658fb740cdd97327ea482f05c0970059f168dcf43
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.dev20250305020553-cp313-cp313-macosx_11_0_arm64.whl

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

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

Download URL typedunits-0.0.1.dev20250305020553-cp312-cp312-win_amd64.whl
Size 765.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
c67a87a4d3008c94966a0949d918cc7b1c2e1265b20570123c7a85bf8f28a3bd
BLAKE2b-256 checksum
How to use checksums
589d7942001b9c361816cad97c5d62412e7f1cc0bbb495d60de32406dbb315ff
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.dev20250305020553-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250305020553-cp312-cp312-win32.whl
Size 722.7 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
43139e7fb98521c09bf1cd19fa596d8ada4c9c9ab703062c5bd9f1e5e134fd99
BLAKE2b-256 checksum
How to use checksums
652a407d2089039e7c3eb844a19954c95ccd82d4bdeaaab18b47c4ac975a04dd
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.dev20250305020553-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305020553-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
d1d4dd319b887c87c49df02cf64c866fa6e1c63be4382b3111f3d0d896385006
BLAKE2b-256 checksum
How to use checksums
4171f56a90aa433542b6b0b1f4b625d51adff9ab5705dd8b73d4531bb1b24cbe
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.dev20250305020553-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305020553-cp312-cp312-macosx_11_0_arm64.whl
Size 799.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
661d32dc5a41ff73a621974ea4ff5fd0301923d24c8cc096a2f915b4f6b43cdd
BLAKE2b-256 checksum
How to use checksums
df652e7a5afe01fbae0ad82bc5c04507cb56e1627856e920fc72e39eebaa7b8a
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.dev20250305020553-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250305020553-cp312-cp312-macosx_10_13_x86_64.whl
Size 837.3 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
266199fef579515707f5b56a3963e1cd5b1e74e74042dab7d5fb064f3773c45c
BLAKE2b-256 checksum
How to use checksums
1dc12045d9562d09de1043e1ddab4568fa18fe526545e4dcbf4777a51bf3414a
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.dev20250305020553-cp311-cp311-win_amd64.whl

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

Download URL typedunits-0.0.1.dev20250305020553-cp311-cp311-win32.whl
Size 757.0 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
1334691ae324edf874914bba07bc5afbb2bebf57319e9415e24c63c5adee60d2
BLAKE2b-256 checksum
How to use checksums
73dfd80e57677c5401988851f9231a0584e92b6bb2855f93e2fea77151f55d83
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.dev20250305020553-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305020553-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
d26cc602e98734e5944b6ac51bb80a1c1f4945c3a2ea8e6d614ca76618cb65a1
BLAKE2b-256 checksum
How to use checksums
7fa4901f69b82c3d8acc70eec3b9b5a4cf9ef19ba01c1878e41024545c7f3988
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.dev20250305020553-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305020553-cp311-cp311-macosx_11_0_arm64.whl
Size 816.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d6746ab9a86f3caa0279c33d1bf1c5e72f7c4d156e75e5a54c05e617eb522e92
BLAKE2b-256 checksum
How to use checksums
6fabadd45d7b63080a81db12f8d6c43e38504cd7de273ad350747039b9d5615d
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.dev20250305020553-cp311-cp311-macosx_10_9_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250305020553-cp310-cp310-win_amd64.whl
Size 788.7 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
283e47e14bbd38403eca8a8ff9dc8880b5072943e8c35fd1715d972699f87109
BLAKE2b-256 checksum
How to use checksums
f3bd91c0122c35046a31fdff4c41cd821dc47ee0c56fafb55804de13271477d1
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.dev20250305020553-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250305020553-cp310-cp310-win32.whl
Size 726.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
67c81e2d21b66b03cbd60a7baaf6712b9672472c809cabebebaa6c5910fbe0fc
BLAKE2b-256 checksum
How to use checksums
d1e50b61fbde7e41574f63d805cf3f9b8a10750665180614adcb5f371a6c8a9e
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.dev20250305020553-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250305020553-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
603b75c5c3c2aea02bcbdb721a64a48de606606f1c329bc4b6e14ac15d07092c
BLAKE2b-256 checksum
How to use checksums
780595d7f7ae92f1c18163edfc579605c4481db473d0a778002cb3e9b34f2db4
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.dev20250305020553-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250305020553-cp310-cp310-macosx_11_0_arm64.whl
Size 784.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7d3d625a95c6c43528963137b23ef5296a123182749f7a3293d6b49270ada810
BLAKE2b-256 checksum
How to use checksums
442429cf85c5d87165070ed1eff0805cc8ee221cf00e5a1114babdef301b841c
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.dev20250305020553-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250305020553-cp310-cp310-macosx_10_9_x86_64.whl
Size 841.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
c6fda2a2c92b425fe8808b986606eeaf52f23130a26a5f00fcc5271deb5e174a
BLAKE2b-256 checksum
How to use checksums
9356a12f2f4aff945fb2f16a530c657465d30081650212fc0a46f4eb5d724035
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