Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions Tests/test_tiff_ifdrational.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import math
from fractions import Fraction
from pathlib import Path

Expand Down Expand Up @@ -38,6 +39,20 @@ def test_sanity() -> None:
_test_equal(7, 5, 1.4)


def test_float() -> None:
# float() must agree with ==, int() and repr(), which all use the
# normalized fraction. For a non-integral numerator (1.5 / 3 == 0.5) the
# inherited Rational.__float__ used int(numerator) / int(denominator) and
# returned 1/3 instead.
Comment thread
radarhere marked this conversation as resolved.
Outdated
r = IFDRational(1.5, 3)
assert r == 0.5
assert float(r) == 0.5

# Integral and 0/0 (nan) cases stay correct.
Comment thread
radarhere marked this conversation as resolved.
Outdated
assert float(IFDRational(4, 2)) == 2.0
assert math.isnan(float(IFDRational(0, 0)))


def test_ranges() -> None:
for num in range(1, 10):
for denom in range(1, 10):
Expand Down
1 change: 1 addition & 0 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def __setstate__(self, state: list[float | Fraction | IntegralLike]) -> None:
__ceil__ = _delegate("__ceil__")
__floor__ = _delegate("__floor__")
__round__ = _delegate("__round__")
__float__ = _delegate("__float__")
# Python >= 3.11
if hasattr(Fraction, "__int__"):
__int__ = _delegate("__int__")
Expand Down
Loading