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

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

Download URL typedunits-0.0.1.dev20250213042250-cp313-cp313-win_amd64.whl
Size 741.6 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
23f0c1dad75982642d9268165d2699b18e7ab5b009705e50823202dcc805dd63
BLAKE2b-256 checksum
How to use checksums
7ca3007e4544d072e38440390cad2f9cce05293823e6f3eba974519555b6c08d
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.dev20250213042250-cp313-cp313-win32.whl

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

Download URL typedunits-0.0.1.dev20250213042250-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
69caaa96da3854f57ff9a4137d980f854219e0564150e00ae155efceb3afe196
BLAKE2b-256 checksum
How to use checksums
266600ea06494addab69b3ce1f06909b02ad10361b710dc7d77fc1b4a74492e0
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.dev20250213042250-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
6600e5d56c8fe258a992a2d5378080ae06de801b4862a4a0a2236c8d8777adea
BLAKE2b-256 checksum
How to use checksums
ecfee6d537113c8d23fba3b58be60f2a612c09718fe359b8fbf9fd1cb2f6a692
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.dev20250213042250-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
9cc8366c2d29d777bbf6aae20db02afa3c7feaa098d09a3d16f84eeb44df634c
BLAKE2b-256 checksum
How to use checksums
f6fb28520ba234abcaa35df0520a91a86b0ce416072f0d9875bf7caa147daab5
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.dev20250213042250-cp312-cp312-win_amd64.whl

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

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

Download URL typedunits-0.0.1.dev20250213042250-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
1707e7e3f59ffe8e59f21ecdb4cb72383f65e27876bf7a8b7ad682cbdb2634b3
BLAKE2b-256 checksum
How to use checksums
6f59d1d93e17c05c33e7b8b8e7d8adfb5f7e9dfdab1505d426aa1294248817bd
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.dev20250213042250-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
a66a7133c70219d812ec3c65372e342a197a0c79e3a59d58bf3ee0904d282f00
BLAKE2b-256 checksum
How to use checksums
9a7466f584f9a3d9610c8096acb77bc31d3106998f8de6c6e647a3fbd4928218
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.dev20250213042250-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
5e5276c4a1199a0f6ebf82fb9d76d66ba3ff57eb0fcb1ab306e424f4246fc5b7
BLAKE2b-256 checksum
How to use checksums
a22b4a987924457b2e97f91b3f427a6bb615adce6b1e379fac4a7de839da2b5b
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.dev20250213042250-cp311-cp311-win_amd64.whl

Download URL typedunits-0.0.1.dev20250213042250-cp311-cp311-win_amd64.whl
Size 769.6 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
759b46350331eadc0fbf96042a090ac2a227350af3bf7dd457d05425876ab284
BLAKE2b-256 checksum
How to use checksums
992879514056261c1edd07d996247fa7cc2bb517810e70adf6e86414524c9b6d
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.dev20250213042250-cp311-cp311-win32.whl

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

Download URL typedunits-0.0.1.dev20250213042250-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
37f577ca5a50f66c4a7e88bc496f774da81e6b27070d76c3cabb23d0009cf04f
BLAKE2b-256 checksum
How to use checksums
9dbf7a1226527bd1d28ef299fb324710bb49470106cc12355fcb503b58d8c487
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.dev20250213042250-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
e2cf221457d6cb57e14a98a09d1a269f61dd6ba29bdc964509a30fe77857c891
BLAKE2b-256 checksum
How to use checksums
670c88544c600fff134c218de1efcd5779425d6cea46abc264832fa5cc7cb922
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.dev20250213042250-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
70e4487a94d6285956fa694620bae5e855f971afb9177c8c4779664751af31d5
BLAKE2b-256 checksum
How to use checksums
2b8e0573f01b8f483df816debae9f0fce41e5bd66caecd92f8d2c1928e0fafa2
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.dev20250213042250-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250213042250-cp310-cp310-win_amd64.whl
Size 766.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
514db2999ecfe1316ce40340eacc9741ffcd4e1717f1492ffbd75542838c4d3f
BLAKE2b-256 checksum
How to use checksums
4c04f6a42e206c9703e4bc31301c08e034f9b01303a664378a25f3231a0bb9cd
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.dev20250213042250-cp310-cp310-win32.whl

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

Download URL typedunits-0.0.1.dev20250213042250-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
ec6b35017a191358e2a71785bcb26035ea8509af645797d99d3d54cd172b6928
BLAKE2b-256 checksum
How to use checksums
5e3f5cf6cbeb21a301cb86d644e7b9c22784c77947ebb4a67c1977db0f310f6c
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.dev20250213042250-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
22da70e59455549c5b922f11bc29a42e0401dad20cbca0ab7807677dbb67586b
BLAKE2b-256 checksum
How to use checksums
a60ab2fa432106ec2b8f59762bfc4a884900bb0b266d9d1fb82971423b3d9e1f
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.dev20250213042250-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250213042250-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
e3e3bb591a0223650cd3f52e9b1c180fdf75061465f6d5eecf4a9f5c7a6b8dbe
BLAKE2b-256 checksum
How to use checksums
c51451f040a7ae5ac892697f5c0e36315bb4ff670427f154920d13a5e8129c12
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