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

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

Total release size: 22.4 MB

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

Download URL typedunits-0.0.1.dev20250131003844-cp313-cp313-win_amd64.whl
Size 742.2 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
44289dc848840995e035d43d165f18883285a44abb383b515ba44c8b502b018e
BLAKE2b-256 checksum
How to use checksums
38614cc6fec7c03bc70b749f6d5ce3370b6ee936f23c580cfea6ec956dc3b952
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.dev20250131003844-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250131003844-cp313-cp313-win32.whl
Size 699.6 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
724275f04775d3339b74580b32c5e253566eb2ddb48afe29c925a66b52ae4dfb
BLAKE2b-256 checksum
How to use checksums
53b85b01fce8036d25142b40c0931a2f64033222bae580c46b403b00acb165e4
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.dev20250131003844-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
36bc75084c52ef95880193a951a7afa4726a602d7bb9c82e354b274afc05939b
BLAKE2b-256 checksum
How to use checksums
ea2c456ed7fe535e28186e1e76b2cdb32b618eccc615d2161781e8b197a33683
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.dev20250131003844-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp313-cp313-macosx_11_0_arm64.whl
Size 769.7 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5b5f82fbb3ee129f4d003edabd4c8a4715d745995863d1dc96a0dd0685856b34
BLAKE2b-256 checksum
How to use checksums
87b7bb22ceda821bc272915587bac1eadae6fc3abad0733c75d16c98778c013f
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.dev20250131003844-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250131003844-cp312-cp312-win_amd64.whl
Size 743.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
db545ca7a2a4af1a9fb9b829b3694a310505a811f0f232ca44e6a3855ef8efdc
BLAKE2b-256 checksum
How to use checksums
e25e7be17696f3279df85de4e142f1e8ad8a616d15645e8c9838446568faa6e3
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.dev20250131003844-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250131003844-cp312-cp312-win32.whl
Size 700.6 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
fa1a67f05ee40c9df279ae2d6f6415986d699e75ae9039cd1f8a06e99272ce00
BLAKE2b-256 checksum
How to use checksums
973982539fe1af86ed5131fed8d99e4c71998d478234b01c6ef0b7385bfe6660
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.dev20250131003844-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.5 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
dcae13faace854a07bebcae808eab6b524f363b2d30863a67e0b73077062c0b2
BLAKE2b-256 checksum
How to use checksums
531a3eab8c13d113d4779d689e249ebe9c4442607ea35f7260ee8cc88e681fd5
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.dev20250131003844-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp312-cp312-macosx_11_0_arm64.whl
Size 776.9 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
9460d7cf4cad039e2e43107e83e59a314dc707b603f8a0d9dd92c34bb9f48a09
BLAKE2b-256 checksum
How to use checksums
ee7c846b469fc22c56d2f0cc8bf0efc6b1b03be2665fcbc5e684e8b76a5836e4
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.dev20250131003844-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp312-cp312-macosx_10_13_x86_64.whl
Size 815.1 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
db2face9afe0ba62b84b8f3c6153380c3c0b3794e64e80c99459c712b2a3c9f1
BLAKE2b-256 checksum
How to use checksums
4aca1aae71c58d1e89ce993770d6396a256cc995a0e246ec3392bd87bab8960e
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.dev20250131003844-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp311-cp311-win_amd64.whl
Size 770.3 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
903cd71f134a72fc41e919941734bb43b2255e4c2315eb044ba61c7aef6c624f
BLAKE2b-256 checksum
How to use checksums
35d64f56f296114ab7a4323f59112e70b8bd272750cc495dfad496ed273e5f35
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.dev20250131003844-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250131003844-cp311-cp311-win32.whl
Size 735.5 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
9daae16f3eae1ae96689d91f7dece913917c7a46730901818b8d0a40ee6d8610
BLAKE2b-256 checksum
How to use checksums
4e35c32402e1bc9243e4a18c537ac0deba58040466edf808a12362e9022e4e9a
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.dev20250131003844-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.6 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
0fa038e4f84b5cea30bae034a1b10c40028ef9e836d8d9f2444f1e4abb4ddf87
BLAKE2b-256 checksum
How to use checksums
468d1b085a68c3c8ccdf4ec3c5c2959711db103325d9db72648923e35ea35c45
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.dev20250131003844-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp311-cp311-macosx_11_0_arm64.whl
Size 790.9 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a353b619c609cc24a716dbf0c1b07f345f847e05daade3c719fbcda724b9204f
BLAKE2b-256 checksum
How to use checksums
2ce4f6b69ca0dd2d0363ac4ecb0b67287f08108de51de5a88a62dd49e2ce87f4
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.dev20250131003844-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp311-cp311-macosx_10_9_x86_64.whl
Size 852.2 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
142f4b82058cb1d6709cab2ca7cf7c9d9616a3d55b11e129ad8f51fbe0d0bfed
BLAKE2b-256 checksum
How to use checksums
1f8008e195f4156f4e73afeb299d9482d19341a73ef253d552eec969a62ba17f
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.dev20250131003844-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp310-cp310-win_amd64.whl
Size 766.8 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
3135975258c1e93fe6a0733e1069d32ebc96574d0d5d65570853b273801e398b
BLAKE2b-256 checksum
How to use checksums
f28fdf445d6fb2957e4366d6acac42c82cc8c3f234146ada7ba8f9f8ac885e61
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.dev20250131003844-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250131003844-cp310-cp310-win32.whl
Size 703.9 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
f5aa7c38c46d7d6f675cdb811fde555e9351551d8d2cd86a30322f2d2939dd8e
BLAKE2b-256 checksum
How to use checksums
64315945fcd1660de11872d51ee8cc3ce9ae3190eb88c37408c0947c39283bde
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.dev20250131003844-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250131003844-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
bd124cc898a679dca27a298283d4b5f3e8d255ebe2df8d2f748bf5b8b1de73a1
BLAKE2b-256 checksum
How to use checksums
75d26beade2e4d740c1b2bbb754fee2699614c3843a0a3379233bc31fc6c2f77
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.dev20250131003844-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp310-cp310-macosx_11_0_arm64.whl
Size 757.6 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2723f96a22fb06812c80835b0c80b9d6f329c13f8ef8ac9e02d8ca85b80f643c
BLAKE2b-256 checksum
How to use checksums
391a8d3cf166cb86676f0353c8a76401b4c75ac6a118e2006834cf2300c67988
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.dev20250131003844-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250131003844-cp310-cp310-macosx_10_9_x86_64.whl
Size 816.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
7a2e83fac100a40b05b01d8045dbd88d0b3b3e86852d381b88a47a2efe1a04ce
BLAKE2b-256 checksum
How to use checksums
92e2421867e9234bdcea3439b52c5bea9ebc00b8660546c7d9894f0e424d5d3b
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