Name | TypedUnit JSON |
Version |
0.0.7
JSON |
| download |
home_page | None |
Summary | A package wrapping pint for handling typed units in Python. |
upload_time | 2025-08-22 22:21:08 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.10 |
license | MIT License
Copyright (c) 2020 Martin de Sivry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
|
keywords |
typed units
pint
physical quantities
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
TypedUnits
===========
|python| |docs| |coverage| |PyPi| |PyPi_download|
**Type-safe physical units for Python**
TypedUnits is a Python library that extends Pint with type safety and validation capabilities. It allows you to create unit-aware types that work seamlessly with Python's type system and provide runtime validation for physical quantities.
**Key Features:**
- **Type Safety**: Use `isinstance()` checks with unit types like `Energy`, `Time`, `Power`
- **Automatic Validation**: Function decorators for unit validation based on type annotations
- **Pydantic Integration**: Full compatibility with Pydantic dataclasses and models
- **Normal Pint Usage**: Keep your existing Pint code - just add type safety on top
----
**Quick Example**
.. code-block:: python
from TypedUnits import Energy, Time, Power, validate, ureg
# Normal Pint instantiation
energy = 100 * ureg.joule
time = 10 * ureg.second
# But now isinstance works!
assert isinstance(energy, Energy) # True
assert isinstance(time, Time) # True
@validate_units
def calculate_power(energy: Energy, time: Time) -> Power:
"""Function with automatic unit validation."""
return (energy / time).to(ureg.watt)
# This works
power = calculate_power(energy, time)
print(f"Power: {power}")
# This fails with clear error message
length = 5 * ureg.meter
# calculate_power(length, time) # TypeError: Expected Energy, got Length
----
**Installation**
.. code-block:: bash
pip install TypedUnits
----
**Basic Usage**
**Unit Type Checking**
.. code-block:: python
from TypedUnits import Energy, Length, Mass, ureg
# Create quantities the normal way
kinetic_energy = 0.5 * 10 * ureg.kilogram * (5 * ureg.meter / ureg.second)**2
potential_energy = 10 * ureg.kilogram * 9.81 * ureg.meter / ureg.second**2 * 2 * ureg.meter
distance = 100 * ureg.meter
mass = 5 * ureg.kilogram
# Type checking works
assert isinstance(kinetic_energy, Energy)
assert isinstance(potential_energy, Energy)
assert isinstance(distance, Length)
assert isinstance(mass, Mass)
**Function Validation**
.. code-block:: python
from TypedUnits import validate_units, Energy, Time, Power
@validate_units
def calculate_work(power: Power, time: Time) -> Energy:
"""Calculate work from power and time."""
return (power * time).to(ureg.joule)
power = 1500 * ureg.watt
time = 3600 * ureg.second # 1 hour
work = calculate_work(power, time)
print(f"Work done: {work.to(ureg.kilowatt_hour)}")
**Pydantic Integration**
.. code-block:: python
from TypedUnits import Energy, Length, Mass
from pydantic.dataclasses import dataclass
@dataclass
class Projectile:
kinetic_energy: Energy
height: Length
mass: Mass
# Automatic validation on creation
projectile = Projectile(
kinetic_energy=500 * ureg.joule,
height=10 * ureg.meter,
mass=2 * ureg.kilogram
)
----
**Available Unit Types**
TypedUnits includes common physical unit types:
- **Mechanical**: `Energy`, `Power`, `Force`, `Pressure`
- **Spatial**: `Length`, `Area`, `Volume`, `Angle`
- **Temporal**: `Time`, `Frequency`
- **Thermal**: `Temperature`, `ThermalConductivity`
- **Electrical**: `Current`, `Voltage`, `Resistance`, `Capacitance`
- **Optical**: `RefractiveIndex`, `Wavelength`, `ElectricField`
- **Mass**: `Mass`, `Density`, `MolarMass`
----
**Advanced Features**
**Custom Unit Types**
.. code-block:: python
from TypedUnits import create_unit_type
# Create your own unit types
MagneticField = create_unit_type('MagneticField', '[magnetic_flux_density]')
field = 1.5 * ureg.tesla
assert isinstance(field, MagneticField)
**Flexible Validation**
.. code-block:: python
from TypedUnits import validate_enhanced
@validate_enhanced(strict_mode=False, convert_types=True)
def flexible_function(energy: Energy, count: int) -> str:
"""Function with flexible validation options."""
return f"Energy per item: {energy / count}"
----
**Requirements**
- Python ≥ 3.8
- Pint ≥ 0.20
- Pydantic ≥ 2.0 (optional, for dataclass integration)
----
**Testing**
.. code-block:: bash
git clone https://github.com/MartinPdeS/TypedUnits.git
cd TypedUnits
pip install -e ".[testing]"
pytest
----
**Contributing**
TypedUnits is open source and contributions are welcome! Whether you're fixing bugs, adding new unit types, or improving documentation, your help is appreciated.
**Author:** `Martin Poinsinet de Sivry-Houle <https://github.com/MartinPdeS>`_
**Email:** `martin.poinsinet-de-sivry@polymtl.ca <mailto:martin.poinsinet-de-sivry@polymtl.ca?subject=TypedUnits>`_
----
.. |python| image:: https://img.shields.io/pypi/pyversions/typedunits.svg
:target: https://www.python.org/
:alt: Python version
.. |PyPi| image:: https://badge.fury.io/py/TypedUnits.svg
:target: https://pypi.org/project/TypedUnits/
:alt: PyPi
.. |PyPi_download| image:: https://img.shields.io/pypi/dm/typedunits.svg
:target: https://pypistats.org/packages/typedunits
:alt: PyPi download statistics
.. |docs| image:: https://github.com/martinpdes/typedunits/actions/workflows/deploy_documentation.yml/badge.svg
:target: https://martinpdes.github.io/TypedUnits/
:alt: Documentation Status
.. |coverage| image:: https://raw.githubusercontent.com/MartinPdeS/TypedUnits/python-coverage-comment-action-data/badge.svg
:target: https://htmlpreview.github.io/?https://github.com/MartinPdeS/TypedUnits/blob/python-coverage-comment-action-data/htmlcov/index.html
:alt: Unittest coverage
Raw data
{
"_id": null,
"home_page": null,
"name": "TypedUnit",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "typed units, pint, physical quantities",
"author": null,
"author_email": "Martin Poinsinet de Sivry-Houle <martin.poinsinet.de.sivry@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/3f/75/8a6fa5337e887618480cb7e251c5fe63ac1dd6b18663b5ab0ba3f2528421/typedunit-0.0.7.tar.gz",
"platform": null,
"description": "TypedUnits\n===========\n\n|python| |docs| |coverage| |PyPi| |PyPi_download|\n\n**Type-safe physical units for Python**\n\nTypedUnits is a Python library that extends Pint with type safety and validation capabilities. It allows you to create unit-aware types that work seamlessly with Python's type system and provide runtime validation for physical quantities.\n\n**Key Features:**\n\n- **Type Safety**: Use `isinstance()` checks with unit types like `Energy`, `Time`, `Power`\n- **Automatic Validation**: Function decorators for unit validation based on type annotations\n- **Pydantic Integration**: Full compatibility with Pydantic dataclasses and models\n- **Normal Pint Usage**: Keep your existing Pint code - just add type safety on top\n\n----\n\n**Quick Example**\n\n.. code-block:: python\n\n from TypedUnits import Energy, Time, Power, validate, ureg\n\n # Normal Pint instantiation\n energy = 100 * ureg.joule\n time = 10 * ureg.second\n\n # But now isinstance works!\n assert isinstance(energy, Energy) # True\n assert isinstance(time, Time) # True\n\n @validate_units\n def calculate_power(energy: Energy, time: Time) -> Power:\n \"\"\"Function with automatic unit validation.\"\"\"\n return (energy / time).to(ureg.watt)\n\n # This works\n power = calculate_power(energy, time)\n print(f\"Power: {power}\")\n\n # This fails with clear error message\n length = 5 * ureg.meter\n # calculate_power(length, time) # TypeError: Expected Energy, got Length\n\n----\n\n**Installation**\n\n.. code-block:: bash\n\n pip install TypedUnits\n\n----\n\n**Basic Usage**\n\n**Unit Type Checking**\n\n.. code-block:: python\n\n from TypedUnits import Energy, Length, Mass, ureg\n\n # Create quantities the normal way\n kinetic_energy = 0.5 * 10 * ureg.kilogram * (5 * ureg.meter / ureg.second)**2\n potential_energy = 10 * ureg.kilogram * 9.81 * ureg.meter / ureg.second**2 * 2 * ureg.meter\n distance = 100 * ureg.meter\n mass = 5 * ureg.kilogram\n\n # Type checking works\n assert isinstance(kinetic_energy, Energy)\n assert isinstance(potential_energy, Energy)\n assert isinstance(distance, Length)\n assert isinstance(mass, Mass)\n\n**Function Validation**\n\n.. code-block:: python\n\n from TypedUnits import validate_units, Energy, Time, Power\n\n @validate_units\n def calculate_work(power: Power, time: Time) -> Energy:\n \"\"\"Calculate work from power and time.\"\"\"\n return (power * time).to(ureg.joule)\n\n power = 1500 * ureg.watt\n time = 3600 * ureg.second # 1 hour\n work = calculate_work(power, time)\n print(f\"Work done: {work.to(ureg.kilowatt_hour)}\")\n\n**Pydantic Integration**\n\n.. code-block:: python\n\n from TypedUnits import Energy, Length, Mass\n from pydantic.dataclasses import dataclass\n\n @dataclass\n class Projectile:\n kinetic_energy: Energy\n height: Length\n mass: Mass\n\n # Automatic validation on creation\n projectile = Projectile(\n kinetic_energy=500 * ureg.joule,\n height=10 * ureg.meter,\n mass=2 * ureg.kilogram\n )\n\n----\n\n**Available Unit Types**\n\nTypedUnits includes common physical unit types:\n\n- **Mechanical**: `Energy`, `Power`, `Force`, `Pressure`\n- **Spatial**: `Length`, `Area`, `Volume`, `Angle`\n- **Temporal**: `Time`, `Frequency`\n- **Thermal**: `Temperature`, `ThermalConductivity`\n- **Electrical**: `Current`, `Voltage`, `Resistance`, `Capacitance`\n- **Optical**: `RefractiveIndex`, `Wavelength`, `ElectricField`\n- **Mass**: `Mass`, `Density`, `MolarMass`\n\n----\n\n**Advanced Features**\n\n**Custom Unit Types**\n\n.. code-block:: python\n\n from TypedUnits import create_unit_type\n\n # Create your own unit types\n MagneticField = create_unit_type('MagneticField', '[magnetic_flux_density]')\n\n field = 1.5 * ureg.tesla\n assert isinstance(field, MagneticField)\n\n**Flexible Validation**\n\n.. code-block:: python\n\n from TypedUnits import validate_enhanced\n\n @validate_enhanced(strict_mode=False, convert_types=True)\n def flexible_function(energy: Energy, count: int) -> str:\n \"\"\"Function with flexible validation options.\"\"\"\n return f\"Energy per item: {energy / count}\"\n\n----\n\n**Requirements**\n\n- Python \u2265 3.8\n- Pint \u2265 0.20\n- Pydantic \u2265 2.0 (optional, for dataclass integration)\n\n----\n\n**Testing**\n\n.. code-block:: bash\n\n git clone https://github.com/MartinPdeS/TypedUnits.git\n cd TypedUnits\n pip install -e \".[testing]\"\n pytest\n\n----\n\n**Contributing**\n\nTypedUnits is open source and contributions are welcome! Whether you're fixing bugs, adding new unit types, or improving documentation, your help is appreciated.\n\n**Author:** `Martin Poinsinet de Sivry-Houle <https://github.com/MartinPdeS>`_\n\n**Email:** `martin.poinsinet-de-sivry@polymtl.ca <mailto:martin.poinsinet-de-sivry@polymtl.ca?subject=TypedUnits>`_\n\n----\n\n.. |python| image:: https://img.shields.io/pypi/pyversions/typedunits.svg\n :target: https://www.python.org/\n :alt: Python version\n\n.. |PyPi| image:: https://badge.fury.io/py/TypedUnits.svg\n :target: https://pypi.org/project/TypedUnits/\n :alt: PyPi\n\n.. |PyPi_download| image:: https://img.shields.io/pypi/dm/typedunits.svg\n :target: https://pypistats.org/packages/typedunits\n :alt: PyPi download statistics\n\n.. |docs| image:: https://github.com/martinpdes/typedunits/actions/workflows/deploy_documentation.yml/badge.svg\n :target: https://martinpdes.github.io/TypedUnits/\n :alt: Documentation Status\n\n.. |coverage| image:: https://raw.githubusercontent.com/MartinPdeS/TypedUnits/python-coverage-comment-action-data/badge.svg\n :target: https://htmlpreview.github.io/?https://github.com/MartinPdeS/TypedUnits/blob/python-coverage-comment-action-data/htmlcov/index.html\n :alt: Unittest coverage\n",
"bugtrack_url": null,
"license": "MIT License\n \n Copyright (c) 2020 Martin de Sivry\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n ",
"summary": "A package wrapping pint for handling typed units in Python.",
"version": "0.0.7",
"project_urls": {
"Documentation": "https://martinpdes.github.io/TypedUnit/",
"Homepage": "https://github.com/MartinPdeS/TypedUnit",
"Repository": "https://github.com/MartinPdeS/TypedUnit.git"
},
"split_keywords": [
"typed units",
" pint",
" physical quantities"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "f3f8c0505be1062da22b147e6697ae358c8cfe99a46bbd3532934a2f45ee46c0",
"md5": "07163a912e0d5fb1d58f92d229753bac",
"sha256": "a0969b6ed7c79b590aa8cf64b9530f8efadf81aa120245fd54ecd22ae70dea01"
},
"downloads": -1,
"filename": "typedunit-0.0.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "07163a912e0d5fb1d58f92d229753bac",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 9144,
"upload_time": "2025-08-22T22:21:07",
"upload_time_iso_8601": "2025-08-22T22:21:07.626067Z",
"url": "https://files.pythonhosted.org/packages/f3/f8/c0505be1062da22b147e6697ae358c8cfe99a46bbd3532934a2f45ee46c0/typedunit-0.0.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "3f758a6fa5337e887618480cb7e251c5fe63ac1dd6b18663b5ab0ba3f2528421",
"md5": "7e98615791f14f716a54ffe2b3cae1f5",
"sha256": "c9adc1100b9404f28c8e01b9f7d536f815e2c17fa7e9215fd48247025c75246d"
},
"downloads": -1,
"filename": "typedunit-0.0.7.tar.gz",
"has_sig": false,
"md5_digest": "7e98615791f14f716a54ffe2b3cae1f5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 286105,
"upload_time": "2025-08-22T22:21:08",
"upload_time_iso_8601": "2025-08-22T22:21:08.918167Z",
"url": "https://files.pythonhosted.org/packages/3f/75/8a6fa5337e887618480cb7e251c5fe63ac1dd6b18663b5ab0ba3f2528421/typedunit-0.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-22 22:21:08",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "MartinPdeS",
"github_project": "TypedUnit",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "typedunit"
}