Skip to content

Commit

Permalink
Bug/various (#26)
Browse files Browse the repository at this point in the history
* disallow bad args

* disallow bad args at Quantity construction

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* disallow bad args at Quantity construction

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
seanpearsonuk and pre-commit-ci[bot] authored Aug 8, 2023
1 parent 3a8ea18 commit 39da0c2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ansys/units/docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Import the pyunits library:

.. code:: python
import ansys.untis as q
import ansys.units as q
Quantities can be instantiated with 1 of 3 methods:

Expand Down
4 changes: 4 additions & 0 deletions src/ansys/units/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,7 @@ def INCOMPATIBLE_DIMENSIONS(cls, from_unit, to_unit):
@classmethod
def INCOMPATIBLE_VALUE(cls, value):
return cls(f"`{value}` is incompatible with the current quantity object.")

@classmethod
def UNKNOWN_UNITS(cls, unit: str):
return cls(f"`{unit}` is an unknown or unconfigured unit.")
12 changes: 10 additions & 2 deletions src/ansys/units/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import yaml

from ansys.units.quantity import Quantity, QuantityError # noqa: F401


class UnitsTable(object):
"""Initializes a UnitsTable object with all table values and unit string
Expand Down Expand Up @@ -54,7 +56,7 @@ def _has_multiplier(self, unit_term: str) -> bool:
Boolean of multiplier within unit_term.
"""
# Check if the unit term is not an existing fundamental or derived unit.
return not (
return unit_term and not (
(unit_term in self._fundamental_units) or (unit_term in self._derived_units)
)

Expand Down Expand Up @@ -135,14 +137,20 @@ def filter_unit_term(self, unit_term: str) -> tuple:
base = unit_term

# strip multiplier and base from unit term
if self._has_multiplier(unit_term):
has_multiplier = self._has_multiplier(unit_term)
if has_multiplier:
for mult in self._multipliers:
if unit_term.startswith(mult):
if not self._has_multiplier(unit_term[len(mult) :]):
multiplier = mult
base = unit_term[len(mult) :]
break

# if we thought it had a multiplier, that's just because the string wasn't
# a known unit on its own. So if we can't actually find its multiplier then
# this string is an invalid unit string
if has_multiplier and not multiplier:
raise QuantityError.UNKNOWN_UNITS(unit_term)
return multiplier, base, power

def si_data(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,3 +938,12 @@ def test_error_messages():

e3 = q.QuantityError.INCOMPATIBLE_VALUE("radian")
assert e3.__str__() == "`radian` is incompatible with the current quantity object."


def test_instantiate_quantity_with_unrecognized_units_causes_exception():
with pytest.raises(q.QuantityError):
q.Quantity(value=10, units="piggies")
with pytest.raises(q.QuantityError):
q.Quantity(value=10, units="piggies s^-1")
with pytest.raises(q.QuantityError):
q.Quantity(value=10, units="piggies^2 m^-3")

0 comments on commit 39da0c2

Please sign in to comment.