Skip to content

Commit

Permalink
build: adopt spec-0 (#208)
Browse files Browse the repository at this point in the history
* build: adopt spec-0 and bump py, deps
* ci: don’t check pypy until 3.11 support

Signed-off-by: nstarman <[email protected]>
  • Loading branch information
nstarman authored Dec 1, 2024
1 parent a5a1d9f commit c78177e
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 348 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
python-version: ["3.11", "3.12", "3.13"]
runs-on: [ubuntu-latest, macos-latest, windows-latest]

include:
- python-version: pypy-3.10
runs-on: ubuntu-latest
# TODO: check when pypy3.11 is available
# include:
# - python-version: pypy-3.11
# runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -89,7 +90,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10"]
python-version: ["3.11"]
runs-on: [ubuntu-latest]

steps:
Expand Down Expand Up @@ -124,7 +125,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13"]
python-version: ["3.11", "3.12", "3.13"]
runs-on: [ubuntu-latest]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.10
3.11
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<a href="https://github.com/GalacticDynamics/unxt/actions"> <img alt="CI status" src="https://github.com/GalacticDynamics/unxt/workflows/CI/badge.svg" /> </a>
<a href="https://unxt.readthedocs.io/en/"> <img alt="ReadTheDocs" src="https://readthedocs.org/projects/unxt/badge/?version=latest" /> </a>
<a href="https://codecov.io/gh/GalacticDynamics/unxt"> <img alt="codecov" src="https://codecov.io/gh/GalacticDynamics/unxt/graph/badge.svg" /> </a>
<a href="https://docs.astral.sh/ruff/"> <img alt="ruff" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/format.json" /> </a>
<a href="https://scientific-python.org/specs/spec-0000/"> <img alt="ruff" src="https://img.shields.io/badge/SPEC-0-green?labelColor=%23004811&color=%235CA038" /> </a>
<a href="https://docs.astral.sh/ruff/"> <img alt="ruff" src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json" /> </a>
<a href="https://pre-commit.com"> <img alt="pre-commit" src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit" /> </a>
</p>

Expand Down Expand Up @@ -148,7 +149,9 @@ except ValueError as e:
JAX. For convenience we use the [`quaxed`][quaxed] library, which is just a
`quax.quaxify` wrapper around `jax` to avoid boilerplate code.

> [!NOTE] Using `quaxed` is optional. You can directly use `quaxify`, and even
> [!NOTE]
>
> Using [`quaxed`][quaxed] is optional. You can directly use `quaxify`, and even
> apply it to the top-level function instead of individual functions.
```python
Expand Down Expand Up @@ -180,6 +183,7 @@ consider citing this work.
[![Actions Status][actions-badge]][actions-link]
[![Documentation Status][rtd-badge]][rtd-link]
[![codecov][codecov-badge]][codecov-link]
[![SPEC 0 — Minimum Supported Dependencies][spec0-badge]][spec0-link]
[![pre-commit][pre-commit-badge]][pre-commit-link]
[![ruff][ruff-badge]][ruff-link]

Expand All @@ -204,6 +208,8 @@ We welcome contributions!
[rtd-link]: https://unxt.readthedocs.io/en/
[ruff-badge]: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json
[ruff-link]: https://docs.astral.sh/ruff/
[spec0-badge]: https://img.shields.io/badge/SPEC-0-green?labelColor=%23004811&color=%235CA038
[spec0-link]: https://scientific-python.org/specs/spec-0000/
[zenodo-badge]: https://zenodo.org/badge/734877295.svg
[zenodo-link]: https://zenodo.org/doi/10.5281/zenodo.10850455

Expand Down
9 changes: 4 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
dynamic = ["version"]
description = "Quantities in JAX"
readme = "README.md"
requires-python = ">=3.10,<3.14"
requires-python = ">=3.11,<3.14"
authors = [
{ name = "GalacticDynamics", email = "[email protected]" },
{ name = "Nathaniel Starkman", email = "[email protected]" },
Expand All @@ -17,7 +17,6 @@
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
Expand All @@ -27,7 +26,7 @@
]
license.file = "LICENSE"
dependencies = [
"astropy>=6.0",
"astropy>=7.0.0",
"dataclassish>=0.3",
"equinox>=0.11.8",
"is-annotated>=1.0",
Expand Down Expand Up @@ -133,7 +132,7 @@
disallow_untyped_defs = false
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
files = ["src", "tests"]
python_version = "3.10"
python_version = "3.11"
strict = true
warn_return_any = false
warn_unreachable = true
Expand Down Expand Up @@ -238,7 +237,7 @@
"wrong-import-order", # handled by ruff
"wrong-import-position",
]
py-version = "3.10"
py-version = "3.11"
reports.output-format = "colorized"
similarities.ignore-imports = "yes"

Expand Down
49 changes: 3 additions & 46 deletions src/unxt/_interop/unxt_interop_astropy/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
import astropy.units as apyu
from astropy.coordinates import Angle as AstropyAngle, Distance as AstropyDistance
from astropy.units import Quantity as AstropyQuantity
from jaxtyping import Array
from packaging.version import Version
from plum import conversion_method, dispatch, type_unparametrized as type_up

import quaxed.numpy as jnp
from dataclassish import field_items, replace

from unxt._interop.optional_deps import OptDeps
from unxt.dims import dimension_of
from unxt.quantity import AbstractQuantity, Quantity, UncheckedQuantity

Expand Down Expand Up @@ -176,46 +173,6 @@ def convert_astropy_quantity_to_unxt_uncheckedquantity(
)


if Version("7.0") <= OptDeps.ASTROPY.version:

def _apy7_unit_to(self: AstropyUnit, other: AstropyUnit, value: Array, /) -> Array:
return self.to(other, value)

else:

def _apy7_unit_to(self: AstropyUnit, other: AstropyUnit, value: Array, /) -> Array:
"""Convert the value to the other unit."""
# return self.get_converter(Unit(other), equivalencies)(value)
# First see if it is just a scaling.
try:
scale = self._to(other)
except apyu.UnitsError:
pass
else:
return scale * value

# if that doesn't work, maybe we can do it with equivalencies?
try:
return self._apply_equivalencies(
self, other, self._normalize_equivalencies([])
)(value)
except apyu.UnitsError as exc:
# Last hope: maybe other knows how to do it?
# We assume the equivalencies have the unit itself as first item.
# TODO: maybe better for other to have a `_back_converter` method?
if hasattr(other, "equivalencies"):
for funit, tunit, _, b in other.equivalencies:
if other is funit:
try:
converter = self.get_converter(tunit, [])
except Exception: # noqa: BLE001, S110 # pylint: disable=W0718
pass
else:
return b(converter(value))

raise exc # noqa: TRY201


@dispatch # type: ignore[misc]
def uconvert(unit: AstropyUnit, x: AbstractQuantity, /) -> AbstractQuantity:
"""Convert the quantity to the specified units.
Expand All @@ -233,21 +190,21 @@ def uconvert(unit: AstropyUnit, x: AbstractQuantity, /) -> AbstractQuantity:
>>> with apyu.add_enabled_equivalencies(apyu.temperature()):
... y = x.uconvert("deg_C")
>>> y
Quantity['temperature'](Array([-272.15, -271.15, -270.15], dtype=float32), unit='deg_C')
Quantity['temperature'](Array([-272.15, -271.15, -270.15], dtype=float32, ...), unit='deg_C')
>>> x = Quantity([1, 2, 3], "radian")
>>> with apyu.add_enabled_equivalencies(apyu.dimensionless_angles()):
... y = x.uconvert("")
>>> y
Quantity['dimensionless'](Array([1., 2., 3.], dtype=float32), unit='')
Quantity['dimensionless'](Array([1., 2., 3.], dtype=float32, ...), unit='')
""" # noqa: E501
# Hot-path: if no unit conversion is necessary
if x.unit == unit:
return x

# Compute the value. Used in all subsequent branches.
value = _apy7_unit_to(x.unit, unit, x.value)
value = x.unit.to(unit, x.value)

# If the dimensions are the same, we can just replace the value and unit.
if dimension_of(x.unit) == dimension_of(unit):
Expand Down
2 changes: 1 addition & 1 deletion src/unxt/_src/quantity/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from unxt._src.units.core import AbstractUnits

if TYPE_CHECKING:
from typing_extensions import Self
from typing import Self


FMT = TypeVar("FMT")
Expand Down
2 changes: 1 addition & 1 deletion src/unxt/_src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import TYPE_CHECKING, Any, cast

if TYPE_CHECKING:
from typing_extensions import Self
from typing import Self


_singleton_insts: dict[type, object] = {}
Expand Down
Loading

0 comments on commit c78177e

Please sign in to comment.