Skip to content

Commit

Permalink
Make _default.default bound method; fix #323
Browse files Browse the repository at this point in the history
Previously, our JSON encoder monkey patch was to an unbound method.
This resulted in throwing a misleading `TypeError` when trying to encode
something non-encodable.
  • Loading branch information
brainix committed Jan 25, 2021
1 parent 5a7ce75 commit 245f214
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pottery/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _default(self: Any, obj: Any) -> Union[Dict[str, Any], List[Any], str]:
return return_value

import json # isort:skip
_default.default = json.JSONEncoder.default # type: ignore
_default.default = json.JSONEncoder().default # type: ignore
json.JSONEncoder.default = _default # type: ignore

_logger.info(
Expand Down
20 changes: 20 additions & 0 deletions tests/test_monkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# --------------------------------------------------------------------------- #
# test_monkey.py #
# #
# Copyright © 2015-2021, Rajiv Bakulesh Shah, original author. #
# All rights reserved. #
# --------------------------------------------------------------------------- #


import json

from tests.base import TestCase


class MonkeyKeyTests(TestCase):
def test_json_encoder(self):
try:
json.dumps(object())
except TypeError as error:
assert str(error) == \
'Object of type object is not JSON serializable'

0 comments on commit 245f214

Please sign in to comment.