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

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

Total release size: 23.3 MB

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

Download URL typedunits-0.0.1.dev20250314004035-cp313-cp313-win_amd64.whl
Size 774.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
0f587793c325bea0fb47d33666a5b1f2b501df82d389e5c2cd1cfc8d8a914571
BLAKE2b-256 checksum
How to use checksums
5eec23d4a041ae5e5910cbd3d246c0eac9a2c5b061c007d42c3ff02f363d2b13
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.dev20250314004035-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250314004035-cp313-cp313-win32.whl
Size 732.3 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
766a6c82e63313d94014cab837f320301fe0df062130c214e049356a5a8d5368
BLAKE2b-256 checksum
How to use checksums
5478e7468027f988089afeba9847ce7034b181a8fe62cde5a98eaac89970eeeb
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.dev20250314004035-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250314004035-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
a126f42aa8ee70650d6c2847052d07b3316eafb288ff8fa6d34cfe6682229841
BLAKE2b-256 checksum
How to use checksums
f33e5979c96c6d4136f589c460202b271132b2875e231844cc8abe3bb6487d74
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.dev20250314004035-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp313-cp313-macosx_11_0_arm64.whl
Size 806.5 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
902f0b37929fc338d023fc41d926947f5ce00626f0f28bf1d1cb8251d539d69c
BLAKE2b-256 checksum
How to use checksums
d9fdd11f5f271d5ed6e93c3a6dc92c6f2bd5303df328d90f1f6e8088e1bf01de
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.dev20250314004035-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250314004035-cp312-cp312-win_amd64.whl
Size 775.9 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
b1b401e498c7528e8784c4b3716e2233704f0a82fac0d1e7062a3235e7693f0a
BLAKE2b-256 checksum
How to use checksums
32b1f30477a6c8bc1068fb1a8fc2dbeff8967be4ca6d88a8ad74f5b3f33a6866
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.dev20250314004035-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250314004035-cp312-cp312-win32.whl
Size 733.5 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
baaa9ecebe8c7c213b98aecff81aa007fc206751e18edd1630ad881c9443448d
BLAKE2b-256 checksum
How to use checksums
8b0b860e66ccee5ceb2309c855d107007e6344b6a9caf772750fb9f9e8217f07
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.dev20250314004035-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250314004035-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
3d52f824f833b17c16bdd53d5e9653cddcd0a9d6a0db87e23aeceb94d5ffa6e6
BLAKE2b-256 checksum
How to use checksums
f336833b012bb47211ca24f28d56b7456d66f426af985e91b950be72f34b1333
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.dev20250314004035-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp312-cp312-macosx_11_0_arm64.whl
Size 813.0 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e5352507a7aa1c2d3eaaefd8b4bb2e189a7d7af163099223442043c0cc2b45c5
BLAKE2b-256 checksum
How to use checksums
1b243b191652788b01cf5f279a2a7b0ebbb4ec77085d2eb445752106df180523
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.dev20250314004035-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp312-cp312-macosx_10_13_x86_64.whl
Size 850.3 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
a8bbb79107d30a8eeef0cd0a7516efa795bf8bc26fcd5801ce41cce2471a4363
BLAKE2b-256 checksum
How to use checksums
28700e45e9ca429963db9fd6cbb09ea00ca66b513bf17b8874a100494c5b6825
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.dev20250314004035-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp311-cp311-win_amd64.whl
Size 803.5 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
00fd90fccc8ed9616b2e6c97365698492a23d7ca086eb862c9195388a9aa4904
BLAKE2b-256 checksum
How to use checksums
3cff5d36d20c2c2384a5405727a948b9d57dd4e3dd50a7229c822db3c5a476a1
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.dev20250314004035-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250314004035-cp311-cp311-win32.whl
Size 768.0 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
415b5c32d47e3463703eb1db9e2ebfeff603f24624a3e826edfc2965ee365465
BLAKE2b-256 checksum
How to use checksums
8d66e40e574ab1c697f8b4e76b0a6298c2ac01d7349c713bab2b29bdcad53762
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.dev20250314004035-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250314004035-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
8a160ecd8ac674bbadcd453138b5cf930ffab2c813a8b97d3fe5b7489274c3d4
BLAKE2b-256 checksum
How to use checksums
9e87a204ede26369f6ec20488e0259d6d7f3c8e36c5efb8eccd3d96af8a3a85d
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.dev20250314004035-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp311-cp311-macosx_11_0_arm64.whl
Size 825.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
8ec9791c7d315d06db79216217217c08e8b8b1672b8faba8a5d5979be7a160cd
BLAKE2b-256 checksum
How to use checksums
894f9563dc6b83e6b0459ebe9b67db47dc559d845de03271b1ddcfd657c495d8
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.dev20250314004035-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp311-cp311-macosx_10_9_x86_64.whl
Size 889.5 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
8df8f1696e2cfd7a2bc997cc47149938a851b04f5d0bdb91dec8e7c78b5ee473
BLAKE2b-256 checksum
How to use checksums
2af0e1fc02aab24138b48e6566e1d912d3748371bffa2bc55059ace53304900c
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.dev20250314004035-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp310-cp310-win_amd64.whl
Size 800.3 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
2fccc4afcdfa2e21a3c89f981160cab8422870a8bbb5b05f0455c24151198a6f
BLAKE2b-256 checksum
How to use checksums
6dd03f1169a3acd1d82314f2eec89124d036fac6bbff0f00952e336678079d42
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.dev20250314004035-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250314004035-cp310-cp310-win32.whl
Size 735.1 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
756ae931df6e8b1be532eb3db5dfb5c06f0d85667f315760e6c5cdbe101d9ae9
BLAKE2b-256 checksum
How to use checksums
b7c604fc8067c1664064dffbc55b4aee30b7e107dd3066e4cfd474329c824d9c
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.dev20250314004035-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250314004035-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
2e5d079ae18850c668bb4582946e0ed01bfc8c9ba79961c1799f083a91156f0a
BLAKE2b-256 checksum
How to use checksums
24f7b355052c9486ca9c5463282d173c6fc31f74debfb5c8f0911309d41c4521
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.dev20250314004035-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp310-cp310-macosx_11_0_arm64.whl
Size 792.2 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4273b9b518639921b685248d1f36f1ec8734fea7ab7753d0bf1a03c8d5092e62
BLAKE2b-256 checksum
How to use checksums
726682c0b301653044e11ab99744ec2d7c2c2186ec7b3296f630a8f8945d4aff
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.dev20250314004035-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250314004035-cp310-cp310-macosx_10_9_x86_64.whl
Size 852.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
f46dcb931f93822be14eb6692c9a367d380df0aaaec2c91e6a0ea0c5f8485e5c
BLAKE2b-256 checksum
How to use checksums
cb0245df45136cc18bb1e6150693434e511a27e5fba3c079e8977d1b2e121af4
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