Skip to content

Commit 22f779f

Browse files
Workaround for float('nan') != float('nan')
1 parent dde4beb commit 22f779f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/geophires_x_client/geophires_x_result.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,8 @@ def _parse_number(self, number_str, field='string') -> int | float:
862862
return None
863863

864864
try:
865-
number_str = number_str.replace(',', '')
866-
if '.' in number_str or number_str.lower() == 'nan':
865+
number_str = number_str.replace(',', '').lower()
866+
if '.' in number_str or number_str == 'nan':
867867
# TODO should probably ideally use decimal.Decimal to preserve precision,
868868
# i.e. 1.00 for USD instead of 1.0
869869
return float(number_str)

tests/test_geophires_x.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import math
12
import os
23
import tempfile
34
import uuid
@@ -194,6 +195,22 @@ def get_output_file_for_example(example_file: str):
194195
del expected_result.result['metadata']
195196
del expected_result.result['Simulation Metadata']
196197

198+
def sanitize_nan(r: GeophiresXResult) -> None:
199+
"""
200+
Workaround for float('nan') != float('nan')
201+
See https://stackoverflow.com/questions/51728427/unittest-how-to-assert-if-the-two-possibly-nan-values-are-equal
202+
203+
TODO generalize beyond Project IRR
204+
"""
205+
try:
206+
if math.isnan(r.result['ECONOMIC PARAMETERS']['Project IRR']['value']):
207+
r.result['ECONOMIC PARAMETERS']['Project IRR']['value'] = 'NaN'
208+
except TypeError:
209+
pass
210+
211+
sanitize_nan(geophires_result)
212+
sanitize_nan(expected_result)
213+
197214
try:
198215
self.assertDictEqual(
199216
expected_result.result, geophires_result.result, msg=f'Example test: {example_file_path}'

0 commit comments

Comments
 (0)