From a1a37bdaff125225f242a25db456acdf5d94c53a Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Wed, 24 Jun 2026 14:49:01 +0200 Subject: [PATCH] Fix missing f-string prefix in allow_nan=False error message When dumps() is called with allow_nan=False and a non-finite float (inf, -inf, nan) is encountered, the error message was always the literal string 'Illegal JSON5 value: f{obj}' instead of interpolating the actual value. Add the missing f-string prefix so callers see the offending value in the exception message. --- json5/lib.py | 2 +- tests/lib_test.py | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/json5/lib.py b/json5/lib.py index 7284003..d14c410 100644 --- a/json5/lib.py +++ b/json5/lib.py @@ -680,7 +680,7 @@ def _encode_float(self, obj: float, *, as_key: bool) -> str: s = float.__repr__(obj) if not allowed: - raise ValueError('Illegal JSON5 value: f{obj}') + raise ValueError(f'Illegal JSON5 value: {obj}') return f'"{s}"' if as_key else s def _encode_str(self, obj: str, *, as_key: bool) -> str: diff --git a/tests/lib_test.py b/tests/lib_test.py index 72b85da..d48a82a 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -583,15 +583,10 @@ def test_numbers(self): self.check(float('-inf'), '-Infinity') self.check(float('nan'), 'NaN') - self.assertRaises( - ValueError, json5.dumps, float('inf'), allow_nan=False - ) - self.assertRaises( - ValueError, json5.dumps, float('-inf'), allow_nan=False - ) - self.assertRaises( - ValueError, json5.dumps, float('nan'), allow_nan=False - ) + for v in (float('inf'), float('-inf'), float('nan')): + with self.assertRaises(ValueError) as ctx: + json5.dumps(v, allow_nan=False) + self.assertIn(str(v), str(ctx.exception)) def test_null(self): self.check(None, 'null')