8000 gh-82017: Support as_integer_ratio() in the Fraction constructor by serhiy-storchaka · Pull Request #120271 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-82017: Support as_integer_ratio() in the Fraction constructor #120271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Prev Previous commit
Next Next commit
More ducktyping. Add more tests.
  • Loading branch information
serhiy-storchaka committed Jul 18, 2024
commit 07bcf8121f8b394f97e4f1ee81f527cd10fffad5
9 changes: 4 additions & 5 deletions Doc/library/fractions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ another rational number, or from a string.
with value ``numerator/denominator``. If *denominator* is ``0``, it
raises a :exc:`ZeroDivisionError`.

The second version requires that
*number* is an instance of :class:`numbers.Rational` or is an instance
of :class:`numbers.Number` and has the :meth:`!as_integer_ratio` method
The second version requires that *number* is an instance of
:class:`numbers.Rational` or has the :meth:`!as_integer_ratio` method
(this includes :class:`float` and :class:`decimal.Decimal`).
It returns a :class:`Fraction` instance with exactly the same value.
Assumed, that the :meth:`~as_integer_ratio` method returns a pair
Assumed, that the :meth:`!as_integer_ratio` method returns a pair
of coprime integers and last one is positive.
Note that due to the
usual issues with binary floating-point (see :ref:`tut-fp-issues`), the
Expand Down Expand Up @@ -115,7 +114,7 @@ another rational number, or from a string.
now supports fill, alignment, sign handling, minimum width and grouping.

.. versionchanged:: 3.14
The :class:`Fraction` constructor now accepts any numbers with the
The :class:`Fraction` constructor now accepts any objects with the
:meth:`!as_integer_ratio` method.

.. attribute:: numerator
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ ast
fractions
---------

Added support for converting any numbers that have the
Added support for converting any objects that have the
:meth:`!as_integer_ratio` method to a :class:`~fractions.Fraction`.
(Contributed by Serhiy Storchaka in :gh:`82017`.)

Expand Down
2 changes: 1 addition & 1 deletion Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def __new__(cls, numerator=0, denominator=None):
return self

elif (isinstance(numerator, float) or
(isinstance(numerator, numbers.Number) and
(not isinstance(numerator, type) and
hasattr(numerator, 'as_integer_ratio'))):
# Exact conversion
self._numerator, self._denominator = numerator.as_integer_ratio()
Expand Down
36 changes: 24 additions & 12 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,22 +360,34 @@ def __init__(self, ratio):
self._ratio = ratio
def as_integer_ratio(self):
return self._ratio
class RatioNumber(Ratio):
pass
numbers.Number.register(RatioNumber)

self.assertEqual((7, 3), _components(F(RatioNumber((7, 3)))))
# not a number
self.assertRaises(TypeError, F, Ratio((7, 3)))
self.assertEqual((7, 3), _components(F(Ratio((7, 3)))))
errmsg = "argument should be a string or a number"
# the type also has an "as_integer_ratio" attribute.
self.assertRaises(TypeError, F, RatioNumber)
self.assertRaisesRegex(TypeError, errmsg, F, Ratio)
# bad ratio
self.assertRaises(TypeError, F, RatioNumber(7))
self.assertRaises(ValueError, F, RatioNumber((7,)))
self.assertRaises(ValueError, F, RatioNumber((7, 3, 1)))
self.assertRaises(TypeError, F, Ratio(7))
self.assertRaises(ValueError, F, Ratio((7,)))
self.assertRaises(ValueError, F, Ratio((7, 3, 1)))
# only single-argument form
self.assertRaises(TypeError, F, RatioNumber((3, 7)), 11)
self.assertRaises(TypeError, F, 2, RatioNumber((-10, 9)))
self.assertRaises(TypeError, F, Ratio((3, 7)), 11)
self.assertRaises(TypeError, F, 2, Ratio((-10, 9)))

# as_integer_ratio not defined in a class
class A:
pass
a = A()
a.as_integer_ratio = lambda: (9, 5)
self.assertEqual((9, 5), _components(F(a)))

# as_integer_ratio defined in a metaclass
class M(type):
def as_integer_ratio(self):
return (11, 9)
class B(metaclass=M):
pass
self.assertRaisesRegex(TypeError, errmsg, F, B)
self.assertRaisesRegex(TypeError, errmsg, F, B())

def testFromString(self):
self.assertEqual((5, 1), _components(F("5")))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Added support for converting any numbers that have the
Added support for converting any objects that have the
:meth:`!as_integer_ratio` method to a :class:`~fractions.Fraction`.
Loading
0