Skip to content

Commit

Permalink
fix: the return type is wrong for int +/- float (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Mar 27, 2023
1 parent 0240995 commit 14230ea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
submodules: "recursive"

Expand All @@ -20,7 +20,7 @@ jobs:
run: echo ::set-output name=tag::${GITHUB_REF#refs/tags/}

- name: Set up Python 3.9
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: "3.9"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Install Poetry
shell: bash
run: curl -fsSL https://install.python-poetry.org | python - -y
run: curl -fsSL https://install.python-poetry.org | python - -y --version 1.4.0

- name: Update PATH
if: ${{ matrix.os != 'Windows' }}
Expand Down
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 14230ea

Please sign in to comment.