From d72e68c81b290058c14f24cfdab5d45785dc38b5 Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Wed, 5 Feb 2025 15:14:38 +0000 Subject: [PATCH 1/5] initial `Unit` protocol --- .gitattributes | 2 ++ .gitignore | 4 ++++ pyproject.toml | 17 +++++++++++++++++ src/unit_api/__init__.py | 22 ++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 pyproject.toml create mode 100644 src/unit_api/__init__.py diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..8f61a8e --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# SCM syntax highlighting +pixi.lock linguist-language=YAML linguist-generated=true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..740bb7d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +# pixi environments +.pixi +*.egg-info diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..3814110 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,17 @@ +[project] +authors = [{ name = "quantity-dev contributors" }] +dependencies = [] +name = "unit-api" +requires-python = ">= 3.12" +version = "0.0.1.dev0" + +[build-system] +build-backend = "hatchling.build" +requires = ["hatchling"] + +[tool.pixi.project] +channels = ["https://prefix.dev/conda-forge"] +platforms = ["win-64", "linux-64", "osx-64", "osx-arm64"] + +[tool.pixi.pypi-dependencies] +unit_api = { path = ".", editable = true } diff --git a/src/unit_api/__init__.py b/src/unit_api/__init__.py new file mode 100644 index 0000000..cf92870 --- /dev/null +++ b/src/unit_api/__init__.py @@ -0,0 +1,22 @@ +"""Unit API.""" + +from typing import Any, Protocol, Self, runtime_checkable + +__version__ = "0.0.1.dev0" +__all__ = ["Unit"] + +type Dimension = Any # TODO: dimension-api +type Quantity = Any # TODO: quantity-api + +@runtime_checkable +class Unit(Protocol): + @property + def dimension(self) -> Dimension: ... + + def __eq__(self, other: Self, /) -> bool: ... + def __mul__(self, other: Self, /) -> Self: ... + def __div__(self, other: Self, /) -> Self: ... + def __pow__(self, other: int | Self, /) -> Self: ... + + # debatable whether this should be standardised + def __rlshift__[V](self, other: V) -> Quantity[V, Self]: ... From a581e91ddbd27499635cf766ba9a2c6aa662514b Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Wed, 5 Feb 2025 15:26:18 +0000 Subject: [PATCH 2/5] improvements --- src/unit_api/__init__.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/unit_api/__init__.py b/src/unit_api/__init__.py index cf92870..ec1ef0f 100644 --- a/src/unit_api/__init__.py +++ b/src/unit_api/__init__.py @@ -1,9 +1,9 @@ """Unit API.""" -from typing import Any, Protocol, Self, runtime_checkable +from typing import Any, Protocol, Final, Self, runtime_checkable -__version__ = "0.0.1.dev0" -__all__ = ["Unit"] +__version__: Final = "0.0.1.dev0" +__all__ = ["__version__", "Unit"] type Dimension = Any # TODO: dimension-api type Quantity = Any # TODO: quantity-api @@ -13,9 +13,8 @@ class Unit(Protocol): @property def dimension(self) -> Dimension: ... - def __eq__(self, other: Self, /) -> bool: ... def __mul__(self, other: Self, /) -> Self: ... - def __div__(self, other: Self, /) -> Self: ... + def __truediv__(self, other: Self, /) -> Self: ... def __pow__(self, other: int | Self, /) -> Self: ... # debatable whether this should be standardised From 043c001d2b3a9e06e81e9ce0d173346ebe69ec5e Mon Sep 17 00:00:00 2001 From: Lucas Colley Date: Wed, 5 Feb 2025 15:29:16 +0000 Subject: [PATCH 3/5] tweak --- src/unit_api/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unit_api/__init__.py b/src/unit_api/__init__.py index ec1ef0f..3894c48 100644 --- a/src/unit_api/__init__.py +++ b/src/unit_api/__init__.py @@ -1,6 +1,6 @@ """Unit API.""" -from typing import Any, Protocol, Final, Self, runtime_checkable +from typing import Any, Final, Protocol, Self, runtime_checkable __version__: Final = "0.0.1.dev0" __all__ = ["__version__", "Unit"] From 8a662d4d8ea5ae91c2b272462cfe8f2972af3a4e Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sun, 9 Mar 2025 22:12:26 +0000 Subject: [PATCH 4/5] Update src/unit_api/__init__.py Co-authored-by: Lucas Colley --- src/unit_api/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unit_api/__init__.py b/src/unit_api/__init__.py index 3894c48..8993363 100644 --- a/src/unit_api/__init__.py +++ b/src/unit_api/__init__.py @@ -15,7 +15,7 @@ def dimension(self) -> Dimension: ... def __mul__(self, other: Self, /) -> Self: ... def __truediv__(self, other: Self, /) -> Self: ... - def __pow__(self, other: int | Self, /) -> Self: ... + def __pow__(self, other: int | float, /) -> Self: ... # debatable whether this should be standardised def __rlshift__[V](self, other: V) -> Quantity[V, Self]: ... From c06f3b67c66dc2d18f0c8f6d42fd4059a9fc2010 Mon Sep 17 00:00:00 2001 From: andrewgsavage Date: Sun, 9 Mar 2025 23:07:51 +0000 Subject: [PATCH 5/5] add reverse ops, remove rlshift (#1) * add reverse ops * remove rlshift * Update src/unit_api/__init__.py --------- Co-authored-by: Lucas Colley --- src/unit_api/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/unit_api/__init__.py b/src/unit_api/__init__.py index 8993363..7aa77cf 100644 --- a/src/unit_api/__init__.py +++ b/src/unit_api/__init__.py @@ -17,5 +17,6 @@ def __mul__(self, other: Self, /) -> Self: ... def __truediv__(self, other: Self, /) -> Self: ... def __pow__(self, other: int | float, /) -> Self: ... - # debatable whether this should be standardised - def __rlshift__[V](self, other: V) -> Quantity[V, Self]: ... + def __rmul__(self, other: Self, /) -> Self: ... + def __rtruediv__(self, other: Self, /) -> Self: ... + def __rpow__(self, other: int | float, /) -> Self: ...