Skip to content

Commit

Permalink
Make _default.default bound method; fix #323 (#324)
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 authored Jan 25, 2021
1 parent 5a7ce75 commit 550bda1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pottery/monkey.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# --------------------------------------------------------------------------- #
# monkey.py #
# #
# Copyright © 2015-2021, Rajiv Bakulesh Shah, original author. #
# Copyright © 2015-2021, original authors. #
# All rights reserved. #
# --------------------------------------------------------------------------- #
'Monkey patches.'
Expand Down 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
22 changes: 22 additions & 0 deletions tests/test_monkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# --------------------------------------------------------------------------- #
# test_monkey.py #
# #
# Copyright © 2015-2021, original authors. #
# All rights reserved. #
# --------------------------------------------------------------------------- #


import json

from tests.base import TestCase


class MonkeyPatchTests(TestCase):
def test_json_encoder(self):
try:
json.dumps(object())
except TypeError as error:
assert str(error) in {
"Object of type 'object' is not JSON serializable", # Python 3.6
'Object of type object is not JSON serializable', # Python 3.7+
}

0 comments on commit 550bda1

Please sign in to comment.