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

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

Total release size: 23.6 MB

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

Download URL typedunits-0.0.1.dev20250425170239-cp313-cp313-win_amd64.whl
Size 783.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
685877b998a1ee42be97257c58fbd80f6cb649104d8eb0200181fbfd61d3018c
BLAKE2b-256 checksum
How to use checksums
e5f56a757471a4236a9ecbc28b33979c548c9a0c7c7246be3a7a5cd0fa753007
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250425170239-cp313-cp313-win32.whl
Size 741.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
222517b0e2e457928a5013c83a2f6323ef2b757fd384f304cc6383f4c9628ce9
BLAKE2b-256 checksum
How to use checksums
d732507220f53d3dc3bfd435b7702ebbd870ac2a93727ba6c12a0f284f966d44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
e4ae2c4f8610ef1ecf0eeb6d66a94f5468b6351baacaea835700b0639ab071ce
BLAKE2b-256 checksum
How to use checksums
78bc4267d4e04c67c2c8c9f76df6c8244b2d6f9ae5d6c0d01cdff6b131bc06df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_11_0_arm64.whl
Size 814.3 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
99b3420f3647c6b05d9340e1edef8307d8d1e7150177d985ba5d22ada2560705
BLAKE2b-256 checksum
How to use checksums
24a2ca55aaa41ba2328d04def9b597827536d75aab3db9d9ecc9ea5198de29da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp313-cp313-macosx_10_13_x86_64.whl
Size 854.9 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
92f980577b2f5db334ed178e2f93764c8bd6b427e37651c2d56e9b2565de7db4
BLAKE2b-256 checksum
How to use checksums
d004ce0c1607d598e5c36b611eb9deae2780167711da1ae2a65311852c5a55e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp312-cp312-win_amd64.whl
Size 784.6 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
5ec374e6557477263f91a7dd720c20c4003390fcbb0246a517054dfd817218af
BLAKE2b-256 checksum
How to use checksums
c44013de151eba0288ee8814f1a13ac00f266ed1c61f1d1818a9fb33d4227f00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250425170239-cp312-cp312-win32.whl
Size 742.9 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
9963462c08f51a2bf6dee6af53e836f50bdd5028d8586f9c1a52a4853f5f2d0e
BLAKE2b-256 checksum
How to use checksums
fb87df0c2295129464ec3de53fe7948c8b05e0e4a7abe0da84f989fdb8b80261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
8126cbc86d8d515ada935732d535ba5e0eedb20da8b2adcc9f0692c1748d4a80
BLAKE2b-256 checksum
How to use checksums
3ef75f8cee8800b7edec1556403de3edbd11f8e225f647501df7b65fdbbeb556
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_11_0_arm64.whl
Size 821.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d578a742553777101f9a6bd79b1f3f4d6e393b5a2930d958b44b7d31ba4ec985
BLAKE2b-256 checksum
How to use checksums
feeb944349ec3870d38f2e5b039d4784eab6552d9e2020d20ae5dfdf538bb55e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp312-cp312-macosx_10_13_x86_64.whl
Size 862.6 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
82892ae7992e2bb92fa8783e1866846da56b13189361867a19ba855bb6cda310
BLAKE2b-256 checksum
How to use checksums
c84b881f08513cbe7cebe2f50de59860ddbf29d7278566f72fc503a8f23bd3c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp311-cp311-win_amd64.whl
Size 813.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
507287f2cd619542a8cd81c676221a9aedfa09e9fca42ace8d88b5b795a644be
BLAKE2b-256 checksum
How to use checksums
2ecba5cac98880f77c0f2b98ddcdc47b00dd6e88d0d206e4981df973c4935ffc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250425170239-cp311-cp311-win32.whl
Size 778.3 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
047b648cdb8c111ffbd83b921e75b7764b9d9538133a8155af671552e99a584d
BLAKE2b-256 checksum
How to use checksums
28e1b9803e4428a9113f34c1af00196a91ff704df51b11189d6ec1f0350edf6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
85f9eb6e7553fe6526bf7c2f2a5ea3438e665595de7c94c5de08456141acffb7
BLAKE2b-256 checksum
How to use checksums
c363a7c716b56b0dd94f440afc998d7779b021a59e8d71434e8f958c52e50d9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp311-cp311-macosx_11_0_arm64.whl
Size 837.2 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6ee1c5b3e84d7bbce014d9ea226fd9629f6e077b89a6d8232075e707159f77da
BLAKE2b-256 checksum
How to use checksums
3d458c98f62dfc24edfae42761233b7d3011dfefb0db9b5354cf2902c4690695
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp311-cp311-macosx_10_9_x86_64.whl
Size 898.8 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ad9da1dde91d93f9f9aa2efd760f25d7e6d9bc596e5c1592c40fa41490b0835c
BLAKE2b-256 checksum
How to use checksums
6ce89c62defa4e6dc8dd27bfb3301df481af0a3eff1a5bdf9d1eed45451ba813
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp310-cp310-win_amd64.whl
Size 810.1 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
28291b9ccdbdbf0556299da854d15347da03760a0bd7b05c3120d6ce201f1fb7
BLAKE2b-256 checksum
How to use checksums
e169271764dea2fbf245000390b10ce889e93e12424ed25db450ed723de4c82e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250425170239-cp310-cp310-win32.whl
Size 745.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
8df55e8185cd7e883aaccf495a281ed3081f902c42031dc69c3ba169950487a4
BLAKE2b-256 checksum
How to use checksums
a035f88cbc246d7bfc21b3805cbc6c19f5569d24a6e64526c528250ed085c39e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-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
ac816885b398dc29594a4c2593e1c4e1d89ac16868eddda778788f00b9bbb93c
BLAKE2b-256 checksum
How to use checksums
296c1d4be12293c4475350967b49e9e35d39b01ac4384eb45021f375ab79c365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp310-cp310-macosx_11_0_arm64.whl
Size 803.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b9cb9c6326ddbc3e083a0dea02cab6b43a5aeaf92be72ba61ee851b0643fa219
BLAKE2b-256 checksum
How to use checksums
0a0c704e9ce5eff94d22aa0da8294a76a05428a5a9f96472f70b74be4f204c37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / typedunits-0.0.1.dev20250425170239-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250425170239-cp310-cp310-macosx_10_9_x86_64.whl
Size 862.0 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ca3a9a35363820319cf96e3f73955611c5c8c3defbcd2d1043c9a7f4f04d2435
BLAKE2b-256 checksum
How to use checksums
8c0cd32274963e744b8fd3c561800e1f8afc2bab06fff5dd3cb6976463bbdf9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

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