Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Improve unit checking. Avoids error on construction w/ quantities tha…
Browse files Browse the repository at this point in the history
…t mismatch the unit's registry
  • Loading branch information
mawildoer committed Nov 15, 2024
1 parent 7acf134 commit f70a067
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/faebryk/libs/library/L.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging

from faebryk.core.module import Module # noqa: F401
from faebryk.core.moduleinterface import ModuleInterface # noqa: F401
from faebryk.core.node import ( # noqa: F401
InitVar,
Node,
Expand Down
4 changes: 2 additions & 2 deletions src/faebryk/libs/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,14 @@ def __init__(
self.units = units or min_unit or max_unit
self.range_units = base_units(self.units)

if isinstance(min, Quantity):
if HasUnit.check(min):
num_min = min.to_base_units().magnitude
if not (isinstance(num_min, float) or isinstance(num_min, int)):
raise ValueError("min must be a float or int quantity")
else:
num_min = min

if isinstance(max, Quantity):
if HasUnit.check(max):
num_max = max.to_base_units().magnitude
if not (isinstance(num_max, float) or isinstance(num_max, int)):
raise ValueError("max must be a float or int quantity")
Expand Down
3 changes: 2 additions & 1 deletion src/faebryk/libs/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Any

from pint import Quantity as _Quantity # noqa: F401
from pint import UndefinedUnitError, Unit, UnitRegistry # noqa: F401
from pint import UndefinedUnitError, UnitRegistry # noqa: F401
from pint.util import UnitsContainer as _UnitsContainer

from faebryk.libs.util import cast_assert
Expand All @@ -14,6 +14,7 @@

UnitsContainer = _UnitsContainer | str
Quantity = P.Quantity
Unit = P.Unit
dimensionless = cast_assert(Unit, P.dimensionless)


Expand Down

0 comments on commit f70a067

Please sign in to comment.