From 58e98a77a39beb6a5f3ae7348082e1b2e94bea78 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Mon, 27 May 2024 13:12:23 -0400 Subject: [PATCH] fix: Normalize decimals for to_json(key=...), closes wireservice/csvkit#1248 --- CHANGELOG.rst | 5 +++++ agate/table/to_json.py | 3 +++ docs/conf.py | 2 +- setup.py | 2 +- tests/test_table/test_to_json.py | 11 +++++++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a3e39464..86cb9bbe 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +1.11.0 - May 27, 2024 +--------------------- + +- fix: The `key` argument to :meth:`.Table.to_json` errors if two values are equal, even if their CSV representation is different: for example, "1/1/2020" and "01/01/2020". However, until now, this was not the case for numbers: for example, "3.0" was treated as unequal to "3.00". + 1.10.2 - April 28, 2024 ----------------------- diff --git a/agate/table/to_json.py b/agate/table/to_json.py index 51afdc87..e4cc7f09 100644 --- a/agate/table/to_json.py +++ b/agate/table/to_json.py @@ -1,6 +1,7 @@ import json import os from collections import OrderedDict +from decimal import Decimal def to_json(self, path, key=None, newline=False, indent=None, **kwargs): @@ -65,6 +66,8 @@ def dump_json(data): for row in self._rows: if key_is_row_function: k = key(row) + elif isinstance(row[key], Decimal): + k = str(row[key].normalize()) else: k = str(row[key]) diff --git a/docs/conf.py b/docs/conf.py index 5efe5f82..786ed519 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,7 +12,7 @@ project = 'agate' copyright = '2017, Christopher Groskopf' -version = '1.10.2' +version = '1.11.0' release = version # -- General configuration --------------------------------------------------- diff --git a/setup.py b/setup.py index c5070b51..41c86b38 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='agate', - version='1.10.2', + version='1.11.0', description='A data analysis library that is optimized for humans instead of machines.', long_description=long_description, long_description_content_type='text/x-rst', diff --git a/tests/test_table/test_to_json.py b/tests/test_table/test_to_json.py index ad023a44..efba1a0a 100644 --- a/tests/test_table/test_to_json.py +++ b/tests/test_table/test_to_json.py @@ -1,6 +1,7 @@ import json import os import sys +from decimal import Decimal from io import StringIO from agate import Table @@ -63,6 +64,16 @@ def test_to_json_non_string_key(self): self.assertEqual(js1, js2) + def test_to_json_key_decimal(self): + table = Table([[Decimal('3')], [Decimal('3.0')], [Decimal('3.00')]], ['a'], [Number()]) + + output = StringIO() + + with self.assertRaises(ValueError) as exc: + table.to_json(output, key='a', indent=4) + + assert str(exc.exception) == 'Value 3 is not unique in the key column.' + def test_to_json_key_func(self): table = Table(self.rows, self.column_names, self.column_types)