Skip to content
Open
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
20 changes: 20 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from tomlkit.items import Bool
from tomlkit.items import Comment
from tomlkit.items import DateTime
from tomlkit.items import Float
from tomlkit.items import InlineTable
from tomlkit.items import Integer
from tomlkit.items import Item
Expand Down Expand Up @@ -116,6 +117,25 @@ def test_integer_unwrap() -> None:
elementary_test(item(666), int)


@pytest.mark.parametrize("method", ["__pow__", "__rpow__"])
@pytest.mark.parametrize("modulus", [0, 3, 0.5])
def test_float_power_rejects_modulus(method: str, modulus: float) -> None:
with pytest.raises(TypeError):
getattr(2.0, method)(3, modulus)

with pytest.raises(TypeError):
getattr(item(2.0), method)(3, modulus)


@pytest.mark.parametrize("method", ["__pow__", "__rpow__"])
def test_float_power_allows_none_modulus(method: str) -> None:
result = getattr(item(2.0), method)(3, None)

assert isinstance(result, Float)
assert result == getattr(2.0, method)(3, None)
assert result.as_string() == str(getattr(2.0, method)(3, None))


def test_float_unwrap() -> None:
elementary_test(item(2.78), float)

Expand Down
4 changes: 2 additions & 2 deletions tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ def __pos__(self) -> Float:
return self._new(float.__pos__(self))

def __pow__(self, other: object, mod: None = None) -> Float:
result = float.__pow__(self, other) # type: ignore[operator]
result = float.__pow__(self, other, mod) # type: ignore[operator]
if result is NotImplemented:
return result # type: ignore[no-any-return]
return self._new(result)
Expand Down Expand Up @@ -974,7 +974,7 @@ def __round__(self, ndigits: int = 0) -> Float: # type: ignore[override]
return self._new(float.__round__(self, ndigits))

def __rpow__(self, other: object, mod: None = None) -> Float:
result = float.__rpow__(self, other) # type: ignore[operator]
result = float.__rpow__(self, other, mod) # type: ignore[operator]
if result is NotImplemented:
return result # type: ignore[no-any-return]
return self._new(result)
Expand Down
Loading