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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 6.3.0
* Support allow_nan in message JSON output [#183](https://github.com/singer-io/singer-python/pull/183)

## 6.2.3
* Default type for non-standard data types is string [#182](https://github.com/singer-io/singer-python/pull/182)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import subprocess

setup(name="singer-python",
version='6.2.3',
version='6.3.0',
description="Singer.io utility library",
author="Stitch",
classifiers=['Programming Language :: Python :: 3 :: Only'],
Expand Down
13 changes: 9 additions & 4 deletions singer/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,17 @@ def parse_message(msg):
return None


def format_message(message, ensure_ascii=True):
return json.dumps(message.asdict(), use_decimal=True, ensure_ascii=ensure_ascii)
def format_message(message, ensure_ascii=True, allow_nan=False):
return json.dumps(
message.asdict(),
use_decimal=True,
ensure_ascii=ensure_ascii,
allow_nan=allow_nan
)


def write_message(message, ensure_ascii=True):
sys.stdout.write(format_message(message, ensure_ascii=ensure_ascii) + '\n')
def write_message(message, ensure_ascii=True, allow_nan=False):
sys.stdout.write(format_message(message, ensure_ascii=ensure_ascii, allow_nan=allow_nan) + '\n')
sys.stdout.flush()


Expand Down
60 changes: 59 additions & 1 deletion tests/test_transform.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import io
import sys
import unittest
import decimal
import simplejson as json
import singer.messages as messages
from singer import transform
from singer.transform import *


class TestTransform(unittest.TestCase):
def test_integer_transform(self):
schema = {'type': 'integer'}
Expand Down Expand Up @@ -486,3 +489,58 @@ def test_pattern_properties_match_multiple(self):
dict_value = {"name": "chicken", "unit_cost": 1.45, "SKU": '123456'}
expected = dict(dict_value)
self.assertEqual(expected, transform(dict_value, schema))

class DummyMessage:
"""A dummy message object with an asdict() method."""
def __init__(self, value):
self.value = value

def asdict(self):
return {"value": self.value}


class TestAllowNan(unittest.TestCase):
"""Unit tests for allow_nan support in singer.messages."""

def test_format_message_allow_nan_true(self):
"""Should serialize NaN successfully when allow_nan=True."""
msg = DummyMessage(float("nan"))
result = messages.format_message(msg, allow_nan=True)

# The output JSON should contain NaN literal (not quoted)
self.assertIn("NaN", result)

# Replace NaN with null to make it valid JSON for parsing check
json.loads(result.replace("NaN", "null"))

def test_format_message_allow_nan_false(self):
"""Should raise ValueError when allow_nan=False and value is NaN."""
msg = DummyMessage(float("nan"))
with self.assertRaises(ValueError):
messages.format_message(msg, allow_nan=False)

def test_write_message_allow_nan_true(self):
"""Should write to stdout successfully when allow_nan=True."""
msg = DummyMessage(float("nan"))
fake_stdout = io.StringIO()
original_stdout = sys.stdout
sys.stdout = fake_stdout
try:
messages.write_message(msg, allow_nan=True)
output = fake_stdout.getvalue()
self.assertIn("NaN", output)
self.assertTrue(output.endswith("\n"))
finally:
sys.stdout = original_stdout

def test_write_message_allow_nan_false(self):
"""Should raise ValueError when allow_nan=False and message has NaN."""
msg = DummyMessage(float("nan"))
fake_stdout = io.StringIO()
original_stdout = sys.stdout
sys.stdout = fake_stdout
try:
with self.assertRaises(ValueError):
messages.write_message(msg, allow_nan=False)
finally:
sys.stdout = original_stdout