Skip to content

Commit

Permalink
🩹 fix-simple(dimensions): add missing dispatches for dimension_of (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
nstarman authored Feb 26, 2025
1 parent 7396e6d commit 8709c16
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
license.file = "LICENSE"
dependencies = [
"astropy>=7.0.0",
"dataclassish>=0.5",
"dataclassish>=0.8.0",
"equinox>=0.11.8",
"is-annotated>=1.0",
"jax>0.4.32",
Expand Down
21 changes: 20 additions & 1 deletion src/unxt/_src/dimensions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

__all__: list[str] = []

from typing import Any
from typing import Any, NoReturn

import astropy.units as apyu
from plum import dispatch
Expand Down Expand Up @@ -85,3 +85,22 @@ def dimension_of(obj: AbstractDimension, /) -> AbstractDimension:
"""
return obj


@dispatch
def dimension_of(obj: type, /) -> NoReturn:
"""Get the dimension of a type.
Examples
--------
>>> import unxt as u
>>> try:
... u.dimension_of(u.quantity.BareQuantity)
... except ValueError as e:
... print(e)
Cannot get the dimension of <class 'unxt._src.quantity.unchecked.BareQuantity'>.
"""
msg = f"Cannot get the dimension of {obj}."
raise ValueError(msg)
34 changes: 31 additions & 3 deletions src/unxt/_src/quantity/register_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

from typing import Any

import equinox as eqx
from astropy.units import UnitConversionError
from jaxtyping import Array
from plum import dispatch

from .api import ustrip
from .base import AbstractQuantity
from .quantity import Quantity
from unxt._src.units import AstropyUnits
from unxt.dims import AbstractDimension, dimension_of
from unxt.units import unit
Expand All @@ -26,15 +28,41 @@ def dimension_of(obj: AbstractQuantity, /) -> AbstractDimension:
Examples
--------
>>> from unxt import dimension_of, Quantity
>>> q = Quantity(1, "m")
>>> dimension_of(q)
>>> import unxt as u
>>> q = u.Quantity(1, "m")
>>> u.dimension_of(q)
PhysicalType('length')
"""
return dimension_of(obj.unit)


@dispatch
def dimension_of(obj: type[Quantity], /) -> AbstractDimension:
"""Return the dimension of a quantity.
Examples
--------
>>> import unxt as u
>>> try:
... u.dimension_of(u.Quantity)
... except Exception as e:
... print(e)
can only get dimensions from parametrized Quantity -- Quantity[dim].
>>> u.dimension_of(u.Quantity["length"])
PhysicalType('length')
"""
obj = eqx.error_if(
obj,
not hasattr(obj, "_type_parameter"),
"can only get dimensions from parametrized Quantity -- Quantity[dim].",
)
return obj._type_parameter # noqa: SLF001


# ===================================================================
# Get units

Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8709c16

Please sign in to comment.