Skip to content

Commit 0ea1b2e

Browse files
authored
Merge pull request #7 from mtscanlan/fix-recursive-hasattr
1758 : fix recursive hasattr call
2 parents db97f46 + cf14fde commit 0ea1b2e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

mygeotab/serializers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ def object_serializer(obj):
4545
4646
:param obj: The object.
4747
"""
48-
return dates.format_iso_datetime(obj) if hasattr(obj, "isoformat") else obj
48+
if hasattr(obj, 'isoformat'):
49+
return dates.format_iso_datetime(obj)
50+
else:
51+
# Let the base class default method raise the TypeError
52+
return json.JSONEncoder.default(obj)
4953

5054

5155
def object_deserializer(obj):

tests/test_serializers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
from datetime import date, datetime
55

6+
import pytest
67
import pytz
78

89
from mygeotab import serializers, dates
@@ -42,6 +43,10 @@ def test_only_date(self):
4243
data_str = json_serialize(data)
4344
assert data_str == expected_str
4445

46+
def test_unparsable_data_throws(self):
47+
with pytest.raises(TypeError):
48+
json_serialize({''})
49+
4550

4651
class TestDeserialization:
4752
def test_top_level_datetime(self):

0 commit comments

Comments
 (0)