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

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

Total release size: 22.9 MB

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

Download URL typedunits-0.0.1.dev20250224204949-cp313-cp313-win_amd64.whl
Size 763.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
3c0f724976e06eb7a5ede4eb70b38a818beab0a1258695889cb68dd5e62b7bba
BLAKE2b-256 checksum
How to use checksums
1393e1112b151a68f6c1688482b6d8a17b4126a0299b0e8b004f0424249eab55
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.dev20250224204949-cp313-cp313-win32.whl

Download URL typedunits-0.0.1.dev20250224204949-cp313-cp313-win32.whl
Size 721.7 kB
Tags CPython 3.13 Windows x86-32
SHA-256 checksum
How to use checksums
a5c9022b1721997fc274321a56ece940888da705d184bfd44cbc1562a15ac842
BLAKE2b-256 checksum
How to use checksums
07237504a3d928fb72dddfb85f9b2b80954dd3297a7c4e4072c45975e2a0ebb7
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.dev20250224204949-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250224204949-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
7e0f7ba740aec0a66e4521a3f8fa17c6cb661dc2f7da93845a5ffb62e3d7668d
BLAKE2b-256 checksum
How to use checksums
fad9ebe488b18b8fba7db96dd3e4087b5d4a4af8c445824660b0048597ca43f9
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.dev20250224204949-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250224204949-cp313-cp313-macosx_11_0_arm64.whl
Size 791.8 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bb5e646c20620481fd3b6d2577fcaadb58e7899fb5f2c9ebfc4379a2c6153ca3
BLAKE2b-256 checksum
How to use checksums
bb25ff63ac8d9b862c6aa8ad8e6e9c8a048577508d10ec1944284957bf13956a
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.dev20250224204949-cp313-cp313-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250224204949-cp312-cp312-win_amd64.whl
Size 765.1 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
34773b7f306849cb1adaa4abf9da920e59766682bca8d3897dba2242f553dbfd
BLAKE2b-256 checksum
How to use checksums
32bf845629d176cdb1adbbc9e0e5634499ec38d364248de3b11af1058cd8b47b
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.dev20250224204949-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250224204949-cp312-cp312-win32.whl
Size 722.8 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
164a9e35d856efff8b3ce923c8b96ff7d0ddf500f6570d06104b0b9c8b1147e1
BLAKE2b-256 checksum
How to use checksums
3785e40047c395ed387c1fc57641ca9a7040f468ed5fa6f8b22d5591e15cfa2d
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.dev20250224204949-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250224204949-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
41b59e35a6e7ebeb57808da8ce624885aca40a390bba6a7aac570efdc8822f19
BLAKE2b-256 checksum
How to use checksums
159fbc29f6b4e10ddf469b9ae2e17588dc89840956c4aa0fbdf995f930968d82
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.dev20250224204949-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250224204949-cp312-cp312-macosx_11_0_arm64.whl
Size 799.4 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
c35d51093748f3d5aaa2a2bcf906c975b06c160aeae1993aafb0ab30be28cb4a
BLAKE2b-256 checksum
How to use checksums
8bce90092468da270e871a7df4919bdf4a53f541901b4afb20613579be564a3d
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.dev20250224204949-cp312-cp312-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250224204949-cp312-cp312-macosx_10_13_x86_64.whl
Size 837.3 kB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
388a5530c386a1ad1f478418df65da6c3ee8097fff076c005eb6677258a39514
BLAKE2b-256 checksum
How to use checksums
a412374bbb5185dd6cdf81e7dbe5ca53c1dd82793b8934ff424cb49e777c50d4
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.dev20250224204949-cp311-cp311-win_amd64.whl

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

Download URL typedunits-0.0.1.dev20250224204949-cp311-cp311-win32.whl
Size 756.8 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
63c39abfa5bdca8b32471c4d04f6e9eb63f2e177c8d09a5cde8f52e1d5a817df
BLAKE2b-256 checksum
How to use checksums
0e64077580e1b09dd19de83ce8e76c7f11a6f7fb9a4f6b7a4b93e1b9d408ab8e
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.dev20250224204949-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250224204949-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
1ab08659df3d5eb9819cb26832112d1b19f1dd6171cf831a11794939ba5f7b57
BLAKE2b-256 checksum
How to use checksums
6fb97364606a3d9fb65525a63a3263339b3729f59b17ba4aed183bab08c40ddf
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.dev20250224204949-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250224204949-cp311-cp311-macosx_11_0_arm64.whl
Size 816.8 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
5dd462d7c17f28c869281622e1a14227ba792200ca349d20cd6c60ff6f42ce0f
BLAKE2b-256 checksum
How to use checksums
a9dc91c40d28d5623ac6f21a48d66b65b5219c125123eaa8a4b7f053cad8364d
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.dev20250224204949-cp311-cp311-macosx_10_9_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250224204949-cp310-cp310-win_amd64.whl
Size 788.9 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
d5c18261c30ab3c180c2fe79a04879ee34a6c0a12404ec5e445db697e693524e
BLAKE2b-256 checksum
How to use checksums
fe238ea6aef0c4346acad87d24797377c64e12ee96967049d7a53653d88e5eac
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.dev20250224204949-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250224204949-cp310-cp310-win32.whl
Size 726.2 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
3211503c1df8fce55132ed44a1555b72dc4094fb1b62dd6319189a4f53f4fa4e
BLAKE2b-256 checksum
How to use checksums
59b12fb767e851d535ec90e6ddb2e94d0c12fd4c89156128aade059981e14a0b
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.dev20250224204949-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250224204949-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
df58d1fa7b85be3ec90c8ac9e4177eb9e8f7b7ea66e833d58c03a98116cbe2ab
BLAKE2b-256 checksum
How to use checksums
8fc630f918a20891c20e43a79782fee5de74d42f0887b29e3e495c2dbfaef178
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.dev20250224204949-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250224204949-cp310-cp310-macosx_11_0_arm64.whl
Size 784.0 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
88058c0f5c26f574e50b41ac9d07ad8c2eff4fab78baff4995620125e44c0dc0
BLAKE2b-256 checksum
How to use checksums
1c4515dd821eed970b513a3bd39fa52b4c3ebc4e60aa29d3ecb9524d2d67260f
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.dev20250224204949-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250224204949-cp310-cp310-macosx_10_9_x86_64.whl
Size 841.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
b4f8423355bde5be9c8d61fc2fc622a3ef3d88c42f9deab042594e6f21f59e0a
BLAKE2b-256 checksum
How to use checksums
5599aaef5ae41197583b93e9bb23731df69ea3d1c2dc52f55b4846e8f19a8e79
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