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

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

Total release size: 22.3 MB

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

Download URL typedunits-0.0.1.dev20250212002305-cp313-cp313-win_amd64.whl
Size 741.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
1872811fc1c2a95668ff8682e3eacc4df89437bb9ca4ebbcb74d319dea2e0098
BLAKE2b-256 checksum
How to use checksums
828e862a086d31228d85467eb5c983d5a257956b602d84ca790ef64313e4439a
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.dev20250212002305-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250212002305-cp313-cp313-win32.whl
Size 699.1 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
d7694926d083be1e37dd6fa94abd301e53debb44a4a7e91fd51b217ef6cdbb99
BLAKE2b-256 checksum
How to use checksums
bc3e7618cd32b963a8703b1be3c18b8aa47460f2684632f10bc62ed05787b00d
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.dev20250212002305-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250212002305-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
c8d9ba9a34ded6f17b17307dfe5bd4b777ebf31e209ef79970fc585fc835812b
BLAKE2b-256 checksum
How to use checksums
ff2f07b4e93ff62eaeda6ae80f409313418ec36e8053136ba89db2650c5ae32b
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.dev20250212002305-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp313-cp313-macosx_11_0_arm64.whl
Size 769.1 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
67dd2c5f9f2ce78888d992f26d4cbff85983032bcfcc069b0d731d2079b548f3
BLAKE2b-256 checksum
How to use checksums
de9cc4ed0a9e4fa7ed9dd5b102afb45d836482d497834a5127f43d617fff78f8
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.dev20250212002305-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp313-cp313-macosx_10_13_x86_64.whl
Size 808.3 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
4b0ae95a4607db6e3f295ede005134b3d626ce4d1c580ef5e92d5c599817bd05
BLAKE2b-256 checksum
How to use checksums
705362e31f56b82500597d7230adb40a9b6094ed7480da26176537af83e66c9d
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.dev20250212002305-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp312-cp312-win_amd64.whl
Size 742.6 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
8392634bf1f4661bb2f331baaa50a35b50e46ce277c7d8e1da9be0f1e8fce367
BLAKE2b-256 checksum
How to use checksums
ba751146ef3ce70c3c9c83ccd84d753b89f35c55f23ec4e1bef7b022e5e44645
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.dev20250212002305-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250212002305-cp312-cp312-win32.whl
Size 700.1 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
f8cb744cfae938c9a20f4e3fdb4c5b8ad7374b4b54fd19bb0aa4822ff274374f
BLAKE2b-256 checksum
How to use checksums
682edd76c344b4b2c9bde368b9ca810bf8b2a6a6d5eab156b9978addff47c636
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.dev20250212002305-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250212002305-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
878450ee53c9379f328afde4304bf98ef7e972e24191180578edc4b780eff61b
BLAKE2b-256 checksum
How to use checksums
c8fad506fad559b47f679107b78e17a5178cd7eb5e2cc52d6798d86efd88cb76
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.dev20250212002305-cp312-cp312-macosx_11_0_arm64.whl

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

Download URL typedunits-0.0.1.dev20250212002305-cp312-cp312-macosx_10_13_x86_64.whl
Size 814.4 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
e12f9ef64c5fb75f39aabe5f2ab728af786bb8ee028f51409f4723e646715115
BLAKE2b-256 checksum
How to use checksums
562638efba64b4386561d6463a0c4ad42e9da4e875b17b7617249d0f1f3baed8
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.dev20250212002305-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp311-cp311-win_amd64.whl
Size 769.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
a781d118759cad8b9340d4785330a2b28217daa95abf808762bd7bba403cee30
BLAKE2b-256 checksum
How to use checksums
57527a83cf528d383e009677911e23305c5556771e8aba5a9185b5d1e680adc6
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.dev20250212002305-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250212002305-cp311-cp311-win32.whl
Size 735.1 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
2ff781fce302db88d9a36b5c8ac85ec878ce3c3ce0c4c38747599f3d8c92fff5
BLAKE2b-256 checksum
How to use checksums
a298e45376216ca86d57ffbff84cf88a0e290b6574206164ea248976cbb9907b
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.dev20250212002305-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250212002305-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
eb67bf74e684fd5c3fe16a5bd6cc8c2eb5f6c129d9b0e61e4903cd2ec2742c45
BLAKE2b-256 checksum
How to use checksums
89a718175b22bf2a5b957c58fa333aba2daa8c4335a87418b621d75576479f1c
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.dev20250212002305-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp311-cp311-macosx_11_0_arm64.whl
Size 790.3 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
97b21881014eca256131c951942638e5e867aea7d079eb2b6699eaab99e66a37
BLAKE2b-256 checksum
How to use checksums
8bdd4a9da993ec27b4236b406eeba549858b68a43cfb4b41c60d5dd675123073
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.dev20250212002305-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp311-cp311-macosx_10_9_x86_64.whl
Size 851.3 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
d5d0c82e35eb32219ff6e20a9bbf88f2f26ea42494eaee11e0bc6ef868d20214
BLAKE2b-256 checksum
How to use checksums
e63c1cc45809b240f280f6379e428166739d983597e6e8dc75d111b73ffa3a4d
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.dev20250212002305-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp310-cp310-win_amd64.whl
Size 766.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
056b58278de4c1460d6bf2e6b399f027aa61f2c668f7a9a66fa8b5743ffe1231
BLAKE2b-256 checksum
How to use checksums
db8f6a582a291e139bc13f3ff578852b54b891a913d3abd8d0d5c8b303399bde
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.dev20250212002305-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250212002305-cp310-cp310-win32.whl
Size 703.5 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
03e9705d1e8dc8bee4750102487fe8f94775a583770e85f07a472045e8bf5a66
BLAKE2b-256 checksum
How to use checksums
fb123e312b35ba155a9770132ca82fc65683533a16f109b59cba2ec4927065f3
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.dev20250212002305-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250212002305-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
88b89abdafb526537b15f460c94b04634f441661f6c5df7081b36f28d8a4c791
BLAKE2b-256 checksum
How to use checksums
8ae44255d03918b64af1f4e1dca10f066b8e33d03eebdc7d7224f8a58e77bb96
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.dev20250212002305-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp310-cp310-macosx_11_0_arm64.whl
Size 756.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a29d89114d1f381ce96380e9e292b8dfa82dc14d5b1b61ae5ba18db21bbc1407
BLAKE2b-256 checksum
How to use checksums
a71addbb3cc6f8f9a32cd862d98edd60bb7053db69e9ee29a8fd6bd8690f19a6
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.dev20250212002305-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250212002305-cp310-cp310-macosx_10_9_x86_64.whl
Size 815.9 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
eec5909caccca533899f004da4001de253c4de546fe002ddc16f281337b61181
BLAKE2b-256 checksum
How to use checksums
745ca5bd18452ff1da33a24ed75ae8b8f0759131c40406994500dc2387c208dd
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