diff --git a/json5/lib.py b/json5/lib.py index 2eaed55..ecf375a 100644 --- a/json5/lib.py +++ b/json5/lib.py @@ -719,7 +719,7 @@ def encode( return s if as_key: - raise TypeError(f'Invalid key f{obj}') + raise TypeError(f'Invalid key: {obj!r}') return self._encode_non_basic_type(obj, seen, level) def _encode_basic_type(self, obj: Any, *, as_key: bool) -> Optional[str]: diff --git a/tests/lib_test.py b/tests/lib_test.py index 6379510..fa85e7e 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -827,6 +827,13 @@ def test_invalid_collection(self): # work. self.assertRaises(TypeError, json5.dumps, {1, 2, 3}) + def test_invalid_key_type_message(self): + # Unencodable keys must fail with a clean, correctly-formatted + # message, not one with a stray ``f`` in it. + with self.assertRaises(TypeError) as ctx: + json5.dumps({(1, 2): 'x'}) + self.assertEqual(str(ctx.exception), "Invalid key: (1, 2)") + def test_custom_numbers(self): # See https://github.com/dpranke/pyjson5/issues/57: we # need to ensure that we use the bare int.__repr__ and