Skip to content

Commit b11e143

Browse files
committed
Fix tests and mypy errors
1 parent a2960dd commit b11e143

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/component_model/model.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from abc import abstractmethod
88
from enum import Enum
99
from math import log
10+
from numbers import Real
1011
from pathlib import Path
1112
from typing import Generator, Sequence, TypeAlias
1213

@@ -139,7 +140,7 @@ def __init__(
139140
if guid is not None:
140141
self.guid = guid
141142
# use a common UnitRegistry for all variables:
142-
self.ureg = UnitRegistry(system=unit_system)
143+
self.ureg: UnitRegistry = UnitRegistry(system=unit_system)
143144
self.copyright, self.license = self.make_copyright_license(copyright, license)
144145
self.guid = guid if guid is not None else uuid.uuid4().hex
145146
# print("FLAGS", flags)
@@ -538,8 +539,14 @@ def xml_unit_definitions(self):
538539
"substance": "mol",
539540
"luminosity": "cd",
540541
}.items():
541-
if "[" + key + "]" in dim:
542-
exponents.update({value: str(int(dim["[" + key + "]"]))})
542+
dim_key = f"[{key}]"
543+
if dim_key not in dim:
544+
continue
545+
dim_value = dim[dim_key]
546+
if not isinstance(dim_value, Real):
547+
logger.debug("Skipping non-real dimensionality entry for %s", dim_key)
548+
continue
549+
exponents.update({value: str(int(float(dim_value)))})
543550
if (
544551
"radian" in str(ubase.units)
545552
): # radians are formally a dimensionless quantity. To include 'rad' as specified in FMI standard this dirty trick is used

tests/test_pint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
logger = logging.getLogger(__name__)
99

10-
_reg = UnitRegistry(system="SI", autoconvert_offset_to_baseunit=True) # , auto_reduce_dimensions=True)
10+
_reg: UnitRegistry = UnitRegistry(system="SI", autoconvert_offset_to_baseunit=True) # , auto_reduce_dimensions=True)
1111

1212

1313
def test_needed_functions():
14-
_reg = UnitRegistry(system="SI", autoconvert_offset_to_baseunit=True) # , auto_reduce_dimensions=True)
14+
_reg: UnitRegistry = UnitRegistry(system="SI", autoconvert_offset_to_baseunit=True) # , auto_reduce_dimensions=True)
1515
print("AVAILABLE UNITS", dir(_reg.sys.SI))
1616
print(
1717
"degrees_Celsius defined?",

tests/test_utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ def dicts_equal(d1: dict, d2: dict):
2828
assert isinstance(d2, dict), f"Dict expected. Found {d2}"
2929
for key in d1:
3030
assert key in d2, f"Key {key} not found in {d2}"
31-
if key != "copyright": # copyright changes with the year!
32-
assert d1[key] == d2[key], f"Value of key {key} {d1[key]} != {d2[key]}"
31+
if key == "copyright":
32+
continue # copyright changes with the year!
33+
if key == "license":
34+
assert " ".join(str(d1[key]).split()) == " ".join(str(d2[key]).split()), (
35+
f"Value of key {key} differs after whitespace normalization\n{d1[key]!r}\n!=\n{d2[key]!r}"
36+
)
37+
continue
38+
assert d1[key] == d2[key], f"Value of key {key} {d1[key]} != {d2[key]}"
3339
for key in d2:
3440
assert key in d1, f"Key {key} not found in {d1}"
35-
if key != "copyright": # copyright changes with the year!
36-
assert d1[key] == d2[key], f"Value of key {key} {d1[key]} != {d2[key]}"
41+
if key == "copyright":
42+
continue # copyright changes with the year!
43+
if key == "license":
44+
assert " ".join(str(d1[key]).split()) == " ".join(str(d2[key]).split()), (
45+
f"Value of key {key} differs after whitespace normalization\n{d1[key]!r}\n!=\n{d2[key]!r}"
46+
)
47+
continue
48+
assert d1[key] == d2[key], f"Value of key {key} {d1[key]} != {d2[key]}"
3749

3850

3951
def test_xml_to_python_val():

0 commit comments

Comments
 (0)