Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change items.py to add float and int #215

Merged
merged 5 commits into from
Aug 2, 2022
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
7 changes: 7 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ def test_item_array_of_dicts_converted_to_aot():
)

SamirPS marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
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