Skip to content

Commit

Permalink
fix: change items.py to add float and int (#215)
Browse files Browse the repository at this point in the history
* change items.py to add float and int

* Update items.py

* Update

* Update tomlkit/items.py

Co-authored-by: Frost Ming <mianghong@gmail.com>

* run lint

Co-authored-by: Frost Ming <mianghong@gmail.com>
  • Loading branch information
SamirPS and frostming committed Aug 2, 2022
1 parent 0449c53 commit a639224
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 7 additions & 0 deletions tests/test_items.py
Expand Up @@ -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)

Expand Down
4 changes: 1 addition & 3 deletions tomlkit/items.py
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit a639224

Please sign in to comment.