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

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

Total release size: 23.5 MB

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

Download URL typedunits-0.0.1.dev20250416214736-cp313-cp313-win_amd64.whl
Size 781.7 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
35ffc742ca80cf5611c700558b025c4a523edabd4701a324716e24ca84bf1a2d
BLAKE2b-256 checksum
How to use checksums
251fcbffede4c8d83986ebb7722d6b3fa40595d4aa348a116638276834e3ee0f
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.dev20250416214736-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250416214736-cp313-cp313-win32.whl
Size 740.1 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
60ab22e58329a8d5e6ae2b85775551fe5e889a0496fe43e754f3ea6e2588c9d0
BLAKE2b-256 checksum
How to use checksums
d14cc2aaa2cd5d04acc15cf1a6a0eee31f1101fe3f37f10f9211dd030217ce52
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.dev20250416214736-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250416214736-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
f6bc090b3344795f7093feb7214056a0d3da8ed7755468eaf4f2691dbeedde19
BLAKE2b-256 checksum
How to use checksums
8bf50ea46e9801b3ea89d26a0fa5607a750f7931fff5537df13c4a19c41cd8d2
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.dev20250416214736-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp313-cp313-macosx_11_0_arm64.whl
Size 810.3 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
67cfa512df8c01c075aadb38c14c83e5c59a0381091975d2b1e5b628664e5861
BLAKE2b-256 checksum
How to use checksums
778a6679fa70150cd57a11600452234ed736f1b58952759f7eac1aab3a48a347
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.dev20250416214736-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250416214736-cp312-cp312-win_amd64.whl
Size 782.7 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
9802d61e3743b81d4526b37b16c0d451db2b4854d0ddde202af9cd7e58a29681
BLAKE2b-256 checksum
How to use checksums
377a090f5928baf9fda1b11793d2934c8caa6293e156e20a854d12ad33b0b522
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.dev20250416214736-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250416214736-cp312-cp312-win32.whl
Size 741.3 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
89ad303c7bdbba50bbbe655b253c8346944e9d5372e3ed8a252992ee66630834
BLAKE2b-256 checksum
How to use checksums
21d0d91826b8f35f8c3d0f387f67bbf5878ac87017d61e757824d38d73afeadc
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.dev20250416214736-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250416214736-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
0b85bfacaf7d518aab4cc93a37f2ec6a61d913fb957fd8db435c66c766b5855c
BLAKE2b-256 checksum
How to use checksums
7d97dae918fa0dd88e9eb2da09b9ba17516b9a0f423204f13ea3af670cdfc5fb
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.dev20250416214736-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp312-cp312-macosx_11_0_arm64.whl
Size 819.8 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a4947eada379ef96eaf9b8a761f21a155e4a9dc6e38480570f513d9a57ddcd71
BLAKE2b-256 checksum
How to use checksums
ab8bae543db95500bdd54ab1e11aba3b8f407fdf3e103d5eaee92cbb72cc2871
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.dev20250416214736-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp312-cp312-macosx_10_13_x86_64.whl
Size 857.6 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
c0155b184330ac60311d6878a9d658ac1c3d75f48f70941e11c7ad2a0a259335
BLAKE2b-256 checksum
How to use checksums
0bc5de9203003295294efed4eb85784e0ff540af4fbfa4db231fc7d0a10c11c6
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.dev20250416214736-cp311-cp311-win_amd64.whl

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

Download URL typedunits-0.0.1.dev20250416214736-cp311-cp311-win32.whl
Size 775.5 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
4f14f9bd128d9057e87548c31b9c99aa1640eee2c794101ea465183f4a980dc1
BLAKE2b-256 checksum
How to use checksums
6a2ce8d1b58908c0d8787ba3686f22d6b3bacae9c1440f838ad24797b9c293cd
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.dev20250416214736-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250416214736-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
3746bac4894435874a65362f8f4dbcfa6737b1c88557fdf88986b4f72f0093b2
BLAKE2b-256 checksum
How to use checksums
c9c0916086a31330ff86c4e9e1de8632f864ed2cecda1a3a34a4fd8bfd46c298
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.dev20250416214736-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp311-cp311-macosx_11_0_arm64.whl
Size 832.6 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e98186ac1023c9f5e1514b11eb1e3eec30b0ce5a99c3fd783f284b41843ad789
BLAKE2b-256 checksum
How to use checksums
3869c3af7d764e23748650927ec10bc97d89af2d911dbc4c9ccd634575ee0dac
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.dev20250416214736-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp311-cp311-macosx_10_9_x86_64.whl
Size 894.8 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
738a7b4485a2e18f9381ff34d2a2517038ef495d6b7285f4796d95752d399461
BLAKE2b-256 checksum
How to use checksums
0bb774cf103577b59f45b358d69c23ada2fab0f8c3243b9396d1507f298abbdc
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.dev20250416214736-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp310-cp310-win_amd64.whl
Size 808.5 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
21581f9fa0388aebc27dc59211838ba2df908ad73a99479785eaa5befe8ac0dd
BLAKE2b-256 checksum
How to use checksums
2ea13f1c87a3404b4b9eb9e0456666c81e4582fa3ed46d77577b5d8d63c16c4b
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.dev20250416214736-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250416214736-cp310-cp310-win32.whl
Size 743.4 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
88843f74b740b9c338d00052a2e6ef5c98506cc83e60011e2badd7c08d9c4303
BLAKE2b-256 checksum
How to use checksums
14e541f4f2f9b02dffa6e4623b5bbc94a13839e7304a5d193a25c79f56150427
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.dev20250416214736-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250416214736-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
e0ed98674cde9ad62778ba1aabeed30dd9796e8485f563f6492536a654c798fc
BLAKE2b-256 checksum
How to use checksums
2f0921da652e15cd1be9aea1b334013a5e1ac4a345da481c6cf96a434a524ba5
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.dev20250416214736-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp310-cp310-macosx_11_0_arm64.whl
Size 798.3 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
b3d2dc5757ec6e79361c526c9295008db58767543d69bf41318ef9013d1753d2
BLAKE2b-256 checksum
How to use checksums
0af5c30c5ad7b98f55427164f58234048a044ddbb1d3edffa14cb46c5884ba8d
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.dev20250416214736-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250416214736-cp310-cp310-macosx_10_9_x86_64.whl
Size 858.5 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
8685220f404be97bb20a5d1fd84c61cdbb5ef9afb941a4fe6c49e004c41c4e64
BLAKE2b-256 checksum
How to use checksums
eaee901bde9379e5ac05b5e69e375cbf5dc6b2e2d246c03135cf5fd995068c7a
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