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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changelog
## 3.0.2 (2026-04-22)
* Handle Decimal objects in messages

## 3.0.1 (2026-04-21)
* Fixed PyPI publisher

## 3.0.0 (2026-04-20)
* Support for Python 3.12

## 2.0.1 (2021-11-23)
* Fixed an issue when `format_message` returned newline character
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
long_description = fh.read()

setup(name="pipelinewise-singer-python",
version='3.0.1',
version='3.0.2',
description="Singer.io utility library - PipelineWise compatible",
python_requires=">=3.12, <3.13",
long_description=long_description,
Expand Down
3 changes: 2 additions & 1 deletion singer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
write_schema,
write_state,
write_version,
write_batch
write_batch,
handler_for_decimal_object
)

from singer.transform import (
Expand Down
7 changes: 6 additions & 1 deletion singer/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytz
import orjson
import ciso8601
import decimal

import singer.utils as u
from .logger import get_logger
Expand Down Expand Up @@ -291,9 +292,13 @@ def parse_message(msg):

return None

def handler_for_decimal_object(obj):
if isinstance(obj, decimal.Decimal):
return float(obj)
Comment thread
amofakhar marked this conversation as resolved.
raise TypeError

def format_message(message, option=0):
return orjson.dumps(message.asdict(), option=option)
return orjson.dumps(message.asdict(), option=option, default=handler_for_decimal_object)
Comment thread
amofakhar marked this conversation as resolved.


def write_message(message):
Expand Down
42 changes: 42 additions & 0 deletions tests/test_singer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import orjson
import unittest
import dateutil
import decimal

from unittest.mock import MagicMock


class TestSinger(unittest.TestCase):
Expand Down Expand Up @@ -206,6 +209,45 @@ def test_format_message(self):
self.assertEqual(b'{"type":"RECORD","stream":"users","record":{"name":"foo"}}\n',
singer.format_message(record_message, option=orjson.OPT_APPEND_NEWLINE))

def test_default_handler_decimal(self):
"""Test that decimal.Decimal is converted to a string."""
d = decimal.Decimal("10.50")
result = singer.handler_for_decimal_object(d)
self.assertEqual(result, 10.50)
self.assertIsInstance(result, float)

def test_default_handler_error(self):
"""Test that unsupported types still raise a TypeError."""
with self.assertRaises(TypeError):
singer.handler_for_decimal_object(range(5))

def test_format_message_with_decimals(self):
"""Test that format_message correctly serializes a message containing decimals."""
# Mocking the message object
mock_message = MagicMock()
mock_message.asdict.return_value = {
"id": 1,
"price": decimal.Decimal("99.99"),
"status": "active"
}

# We expect the Decimal to become a string in the resulting bytes
expected_output = b'{"id":1,"price":99.99,"status":"active"}'

result = singer.format_message(mock_message)
self.assertEqual(result, expected_output)

def test_format_message_options(self):
"""Test that orjson options (like appending a newline) work."""
mock_message = MagicMock()
mock_message.asdict.return_value = {"key": "val"}

# orjson.OPT_APPEND_NEWLINE is an integer bitmask
result = singer.format_message(mock_message, option=orjson.OPT_APPEND_NEWLINE)

# Should end with a newline byte (10)
self.assertTrue(result.endswith(b'\n'))


if __name__ == '__main__':
unittest.main()
Loading