Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion json5/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
7 changes: 7 additions & 0 deletions tests/lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down