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

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

Total release size: 23.4 MB

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

Download URL typedunits-0.0.1.dev20250310230954-cp313-cp313-win_amd64.whl
Size 777.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
50dba00328a4d404db54ea28e5a97504d87d97869c92c60a00f2d196c321787a
BLAKE2b-256 checksum
How to use checksums
b99f40a58c12964641b098c03a27e08b32a9e27584081bd0051c71e6aa3dd21f
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.dev20250310230954-cp313-cp313-win32.whl

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

Download URL typedunits-0.0.1.dev20250310230954-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
e6a88b4bc3322ecf2aec2ee3a9a76cb093f9bc97af17e5ac873159f9fc794b4a
BLAKE2b-256 checksum
How to use checksums
88078296efb8a29407c41e397c6276ae1a86d708785a663b2c20b8cad2ea94db
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.dev20250310230954-cp313-cp313-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp313-cp313-macosx_11_0_arm64.whl
Size 804.9 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
913d1d86e17c7879c9ec28d5d9472b706b4dafbac416b4ca36b21e9213d05b7d
BLAKE2b-256 checksum
How to use checksums
7739929c607d7156de401d81702f7228f91f51ee5d472836a6d20c543f192b64
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.dev20250310230954-cp313-cp313-macosx_10_13_x86_64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp313-cp313-macosx_10_13_x86_64.whl
Size 845.1 kB
Tags CPython 3.13 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
34eb1ca7766930b34c9fb2f984db50e53b764e9d2db3b202280c09bbdcbb7fd0
BLAKE2b-256 checksum
How to use checksums
63478118668dd7d99c8a2407bcc153e740eef0eb160a2539776920c00c531681
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.dev20250310230954-cp312-cp312-win_amd64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp312-cp312-win_amd64.whl
Size 779.0 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
6be24c704ac457572ff9c510e3c4ada27ea1ac7492c0ba2283fb2a3057477287
BLAKE2b-256 checksum
How to use checksums
87e6ecdb1f19f97656cef25f9436a62c5c576d6126fde981f0ff73558d02dd61
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.dev20250310230954-cp312-cp312-win32.whl

Download URL typedunits-0.0.1.dev20250310230954-cp312-cp312-win32.whl
Size 736.3 kB
Tags CPython 3.12 Windows x86-32
SHA-256 checksum
How to use checksums
c6983460f1f937ba720bf66f3cf7c6915b8831760c3ac78545774925ffee86fd
BLAKE2b-256 checksum
How to use checksums
b6e06f750ecda620dc10803371999fe6e45294c716ec3a9f7038864cc67d5316
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.dev20250310230954-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250310230954-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
398d9c27c7aec1c3008a0ff603a39ecec786fe271029126587a953e8112fb66b
BLAKE2b-256 checksum
How to use checksums
7fd45e61df585141acd84b4563463ad79840a4b1c054e562fe8c420ce3fd669c
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.dev20250310230954-cp312-cp312-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp312-cp312-macosx_11_0_arm64.whl
Size 812.3 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
55f6b5245908a0f8d1b88b5199003742105cbe0fa2b2085c7a621cb5ebfa3418
BLAKE2b-256 checksum
How to use checksums
16c48192ceb442e88cc14200f6895a730efedb12c24a71480818d85be4ae8ed5
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.dev20250310230954-cp312-cp312-macosx_10_13_x86_64.whl

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

Download URL typedunits-0.0.1.dev20250310230954-cp311-cp311-win_amd64.whl
Size 807.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
2b388c21e35cb0c1b3ff5c57b0f04c4dbd244bc3af636e91f67412fac7677406
BLAKE2b-256 checksum
How to use checksums
5fc7cfbc40079d133eb04552f8602c6de1d127e5df69bdb8cebb2a94124d1f4a
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.dev20250310230954-cp311-cp311-win32.whl

Download URL typedunits-0.0.1.dev20250310230954-cp311-cp311-win32.whl
Size 771.5 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
d82349fc693ef91aaa5a743a0160e13a9cd49c54b9bd15e7cf4fc51fe78e79da
BLAKE2b-256 checksum
How to use checksums
05ce19a77b75fb1a0ec99c96e5b87d7185dd2268c9efd6c307cebdce05e592e2
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.dev20250310230954-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250310230954-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
813fa7717f1116c23061cce3f216c26e94b62fb56795357078410eb348b97a77
BLAKE2b-256 checksum
How to use checksums
4af7c75a47fddb4801c11cf0903755a7e1d56c1eea0df5b2b97afe2b9e475db4
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.dev20250310230954-cp311-cp311-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp311-cp311-macosx_11_0_arm64.whl
Size 827.7 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1ded893c6ac39b985073f1135d12a2128f466e5c7829d74316b34e707beb4ea6
BLAKE2b-256 checksum
How to use checksums
e248a9ae4d88614db2c427be03d51d3009dfdd124c54c03fc51fd5dccbfd8e43
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.dev20250310230954-cp311-cp311-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp311-cp311-macosx_10_9_x86_64.whl
Size 890.1 kB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
00977d98fb32d6d21ed985067d4334ccf7e70f7609885ae54d16f1c65a6a3999
BLAKE2b-256 checksum
How to use checksums
4242ab67467516a9518d6307fe44f4235ffdf162e2946b6575aeef4572a68ed0
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.dev20250310230954-cp310-cp310-win_amd64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp310-cp310-win_amd64.whl
Size 803.8 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
70491ce27a7a82d6ac44ea7940f4dc3a6cdc5d8e15c565cb10c9fcd3128d4f2f
BLAKE2b-256 checksum
How to use checksums
58b7880e524db6de5fca11f55492c20b409ea0a4e527e18fb1dd9e9bf56d4269
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.dev20250310230954-cp310-cp310-win32.whl

Download URL typedunits-0.0.1.dev20250310230954-cp310-cp310-win32.whl
Size 739.1 kB
Tags CPython 3.10 Windows x86-32
SHA-256 checksum
How to use checksums
04c83d4763026ea2f6fa6230d4f694f69f577cd9713e387d8404570da7702a62
BLAKE2b-256 checksum
How to use checksums
f9569f1b50ebac4369372997a77ee3127784bca38c96955f9916bb45f00efe50
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.dev20250310230954-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typedunits-0.0.1.dev20250310230954-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
2069607baefbdf673a59bd6c5485f6cbcc0286644694ccf15eee297a7f7e8040
BLAKE2b-256 checksum
How to use checksums
072f714f4416099c7febc0f87d17bbb97ddb519b0c51599ac07cf6c3d87f3976
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.dev20250310230954-cp310-cp310-macosx_11_0_arm64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp310-cp310-macosx_11_0_arm64.whl
Size 794.6 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6b840f0065d4fc3024d9195d69863c51e7004d00e64eae0cd1fd564996cf9a1d
BLAKE2b-256 checksum
How to use checksums
2e0c38674eea28bf6396ee236ff178e97011349b30d660146fc56bc5020ef5da
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.dev20250310230954-cp310-cp310-macosx_10_9_x86_64.whl

Download URL typedunits-0.0.1.dev20250310230954-cp310-cp310-macosx_10_9_x86_64.whl
Size 854.6 kB
Tags CPython 3.10 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
f7c8caad28bab2139ed8ee7256f61c737303fc9a472061b9ff395fff5ce614e6
BLAKE2b-256 checksum
How to use checksums
46e200393b4f5506ff9f68c8c58126338cbd9a990769d3cc9f19c82101ed3360
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