fix: the return type is wrong for int +/- float (#277)
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 22bd503..3a850f7 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -11,7 +11,7 @@
steps:
- name: Checkout code
- uses: actions/checkout@v2
+ uses: actions/checkout@v3
with:
submodules: "recursive"
@@ -20,7 +20,7 @@
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"
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 6108d6b..ebe25ae 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -48,7 +48,7 @@
- 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' }}
diff --git a/tests/test_items.py b/tests/test_items.py
index 57eba32..45aea25 100644
--- a/tests/test_items.py
+++ b/tests/test_items.py
@@ -579,6 +579,29 @@
)
+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)
diff --git a/tomlkit/items.py b/tomlkit/items.py
index 5990eea..da40880 100644
--- a/tomlkit/items.py
+++ b/tomlkit/items.py
@@ -647,28 +647,28 @@
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)