Skip to content

Commit

Permalink
fix: the return type is wrong for int +/- float
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Mar 27, 2023
1 parent 0240995 commit 972cd1d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
23 changes: 23 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,29 @@ def test_item_array_of_dicts_converted_to_aot():
)


def test_add_float_to_int():
content = "[table]\nmy_int = 2043"
doc = parse(content)
doc["table"]["my_int"] += 5.0
assert doc["table"]["my_int"] == 2048.0
assert isinstance(doc["table"]["my_int"], float)


def test_sub_float_from_int():
content = "[table]\nmy_int = 2048"
doc = parse(content)
doc["table"]["my_int"] -= 5.0
assert doc["table"]["my_int"] == 2043.0
assert isinstance(doc["table"]["my_int"], float)


def test_sub_int_from_float():
content = "[table]\nmy_int = 2048.0"
doc = parse(content)
doc["table"]["my_int"] -= 5
assert doc["table"]["my_int"] == 2043.0


def test_add_sum_int_with_float():
content = "[table]\nmy_int = 2048.3"
doc = parse(content)
Expand Down
24 changes: 12 additions & 12 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,28 +647,28 @@ def as_string(self) -> str:
return self._raw

def __add__(self, other):
return self._new(int(self._raw) + other)
result = super().__add__(other)
if result is NotImplemented:
return result
return self._new(result)

def __radd__(self, other):
result = super().__radd__(other)

if isinstance(other, Integer):
return self._new(result)

return result
if result is NotImplemented:
return result
return self._new(result)

def __sub__(self, other):
result = super().__sub__(other)

if result is NotImplemented:
return result
return self._new(result)

def __rsub__(self, other):
result = super().__rsub__(other)

if isinstance(other, Integer):
return self._new(result)

return result
if result is NotImplemented:
return result
return self._new(result)

def _new(self, result):
raw = str(result)
Expand Down

0 comments on commit 972cd1d

Please sign in to comment.