From 67f306f059d6caba37556b35c53602057fc3cd2c Mon Sep 17 00:00:00 2001 From: Matthew Newville Date: Sun, 5 Mar 2023 21:47:14 -0600 Subject: [PATCH] ENH: better handling of non-numeric value in gformat() Closes: #845 --- lmfit/printfuncs.py | 5 +++++ tests/test_printfuncs.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lmfit/printfuncs.py b/lmfit/printfuncs.py index 520536a61..d77e4ab48 100644 --- a/lmfit/printfuncs.py +++ b/lmfit/printfuncs.py @@ -58,10 +58,15 @@ def gformat(val, length=11): Positive values will have leading blank. """ + if val is None or isinstance(val, bool): + return f'{repr(val):>{length}s}' try: expon = int(log10(abs(val))) except (OverflowError, ValueError): expon = 0 + except TypeError: + return f'{repr(val):>{length}s}' + length = max(length, 7) form = 'e' prec = length - 7 diff --git a/tests/test_printfuncs.py b/tests/test_printfuncs.py index b41bf7d2e..7b8f13417 100644 --- a/tests/test_printfuncs.py +++ b/tests/test_printfuncs.py @@ -306,11 +306,11 @@ def test_report_modelpars(fitresult): def test_report_parvalue_non_numeric(fitresult): - """Verify that a non-numeric value is caught (can this ever happens?).""" + """Verify that a non-numeric value is handled gracefully.""" fitresult.params['center'].value = None fitresult.params['center'].stderr = None report = fitresult.fit_report() - assert 'center: Non Numeric Value?' in report + assert len(report) > 50 def test_report_zero_value_spercent(fitresult):