From a639224f3b7d49f45576c2c036ae4bc90e4dec77 Mon Sep 17 00:00:00 2001 From: SamirAk <44325916+SamirPS@users.noreply.github.com> Date: Tue, 2 Aug 2022 02:29:14 +0200 Subject: [PATCH] fix: change items.py to add float and int (#215) * change items.py to add float and int * Update items.py * Update * Update tomlkit/items.py Co-authored-by: Frost Ming * run lint Co-authored-by: Frost Ming --- tests/test_items.py | 7 +++++++ tomlkit/items.py | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_items.py b/tests/test_items.py index 3dcd509..aad33b4 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -562,6 +562,13 @@ def test_item_array_of_dicts_converted_to_aot(): ) +def test_add_sum_int_with_float(): + content = "[table]\nmy_int = 2048.3" + doc = parse(content) + doc["table"]["my_int"] += 5 + assert doc["table"]["my_int"] == 2053.3 + + def test_integers_behave_like_ints(): i = item(34) diff --git a/tomlkit/items.py b/tomlkit/items.py index c956407..8c6889c 100644 --- a/tomlkit/items.py +++ b/tomlkit/items.py @@ -637,9 +637,8 @@ def as_string(self) -> str: return self._raw def __add__(self, other): - result = super().__add__(other) - return self._new(result) + return self._new(int(self._raw) + other) def __radd__(self, other): result = super().__radd__(other) @@ -664,7 +663,6 @@ def __rsub__(self, other): def _new(self, result): raw = str(result) - if self._sign: sign = "+" if result >= 0 else "-" raw = sign + raw