From 626ca91cc5d6ce5729a5f16dee1155a9473dc47a Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 23 Dec 2022 14:31:05 -0600 Subject: [PATCH 1/3] gh-100488: Add is_integer method to fractions.Fraction --- Doc/library/fractions.rst | 6 ++++++ Lib/fractions.py | 4 ++++ Lib/test/test_fractions.py | 13 +++++++++++++ 3 files changed, 23 insertions(+) diff --git a/Doc/library/fractions.rst b/Doc/library/fractions.rst index c46d88b2297aa1..d6aa726865c704 100644 --- a/Doc/library/fractions.rst +++ b/Doc/library/fractions.rst @@ -117,6 +117,12 @@ another rational number, or from a string. .. versionadded:: 3.8 + .. method:: is_integer() + + Return True if the Fraction is an integer. + + .. versionadded:: 3.12 + .. classmethod:: from_float(flt) Alternative constructor which only accepts instances of diff --git a/Lib/fractions.py b/Lib/fractions.py index 75c7df14e1b9c7..4302f3f1b98dea 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -225,6 +225,10 @@ def from_decimal(cls, dec): (cls.__name__, dec, type(dec).__name__)) return cls(*dec.as_integer_ratio()) + def is_integer(self): + """Return True if the Fraction is an integer.""" + return self._denominator == 1 + def as_integer_ratio(self): """Return the integer ratio as a tuple. diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 7fa9dbea905b59..819d680fd88e4e 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -340,6 +340,19 @@ def testFromDecimal(self): ValueError, "cannot convert NaN to integer ratio", F.from_decimal, Decimal("snan")) + def test_is_integer(self): + self.assertTrue(F(1, 1).is_integer()) + self.assertTrue(F(-1, 1).is_integer()) + self.assertTrue(F(1, -1).is_integer()) + self.assertTrue(F(2, 2).is_integer()) + self.assertTrue(F(-2, 2).is_integer()) + self.assertTrue(F(2, -2).is_integer()) + + self.assertFalse(F(1, 2).is_integer()) + self.assertFalse(F(-1, 2).is_integer()) + self.assertFalse(F(1, -2).is_integer()) + self.assertFalse(F(-1, -2).is_integer()) + def test_as_integer_ratio(self): self.assertEqual(F(4, 6).as_integer_ratio(), (2, 3)) self.assertEqual(F(-4, 6).as_integer_ratio(), (-2, 3)) From db51a7ea83cec055e8932ae871fd0b11bc6ee92c Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 24 Dec 2022 04:13:57 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-12-24-04-13-54.gh-issue-100488.Ut8HbE.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-12-24-04-13-54.gh-issue-100488.Ut8HbE.rst diff --git a/Misc/NEWS.d/next/Library/2022-12-24-04-13-54.gh-issue-100488.Ut8HbE.rst b/Misc/NEWS.d/next/Library/2022-12-24-04-13-54.gh-issue-100488.Ut8HbE.rst new file mode 100644 index 00000000000000..f7c07c0ab4c2d5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-12-24-04-13-54.gh-issue-100488.Ut8HbE.rst @@ -0,0 +1 @@ +Add :meth:`Fraction.is_integer` to check whether a :class:`fractions.Fraction` is an integer. This improves duck type compatibility with :class:`float` and :class:`int`. From c542a852ae64c4287d0760f23ecf03d31640a49b Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sun, 1 Jan 2023 01:18:39 -0700 Subject: [PATCH 3/3] Update Doc/library/fractions.rst Co-authored-by: Mark Dickinson --- Doc/library/fractions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/fractions.rst b/Doc/library/fractions.rst index d6aa726865c704..dc9d5fc18215c4 100644 --- a/Doc/library/fractions.rst +++ b/Doc/library/fractions.rst @@ -119,7 +119,7 @@ another rational number, or from a string. .. method:: is_integer() - Return True if the Fraction is an integer. + Return ``True`` if the Fraction is an integer. .. versionadded:: 3.12