Skip to content

Commit

Permalink
Fixup units check (GalacticDynamics#34)
Browse files Browse the repository at this point in the history
* fix units check in pytree
* get rid of units default

Signed-off-by: nstarman <nstarman@users.noreply.github.com>
  • Loading branch information
nstarman committed Dec 10, 2023
1 parent f1155f4 commit 5631661
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/galdynamix/potential/_potential/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

class AbstractPotential(AbstractPotentialBase):
_: KW_ONLY
units: UnitSystem = eqx.field(
default=None, converter=converter_to_usys, static=True
)
units: UnitSystem = eqx.field(converter=converter_to_usys, static=True)
_G: float = eqx.field(init=False, static=True, repr=False, converter=float)

def __post_init__(self) -> None:
Expand Down
9 changes: 8 additions & 1 deletion src/galdynamix/potential/_potential/param/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def __get__( # TODO: use `Self` when beartype is happy

def _check_unit(self, potential: AbstractPotential, unit: Unit) -> None:
"""Check that the given unit is compatible with the parameter's."""
# When the potential is being constructed, the units may not have been
# set yet, so we don't check the unit.
if not hasattr(potential, "units"):
return

# Check the unit is compatible
if not unit.is_equivalent(
potential.units[self.dimensions],
equivalencies=self.equivalencies,
Expand All @@ -98,7 +104,8 @@ def __set__(
if isinstance(value, AbstractParameter):
# TODO: this doesn't handle the correct output unit, a. la.
# potential.units[self.dimensions]
self._check_unit(potential, value.unit) # Check the unit is compatible
# Check the unit is compatible
self._check_unit(potential, value.unit)
elif callable(value):
# TODO: this only gets the existing unit, it doesn't handle the
# correct output unit, a. la. potential.units[self.dimensions]
Expand Down

0 comments on commit 5631661

Please sign in to comment.