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
Prev Previous commit
Next Next commit
Updates as_integer_ratio to be one line.
  • Loading branch information
lisroach committed Aug 16, 2018
commit d1daadcd21b4e9516314661e50adddcfc90bf3b3
7 changes: 4 additions & 3 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,11 @@ class`. In addition, it provides a few more methods:

.. method:: int.as_integer_ratio()

Return a pair of integers whose ratio is exactly equal to the original integer
and with a positive denominator. The integer ratio of integers (whole numbers)
is always the integer as the numerator and 1 as the denominator.
Return a pair of integers whose ratio is exactly equal to the original integer
and with a positive denominator. The integer ratio of integers (whole numbers)
is always the integer as the numerator and 1 as the denominator.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A versionadded directive needs to be added. Also, the developer's guide states that reST files should use an indentation of 3 spaces.

.. versionadded:: 3.8

Additional Methods on Float
---------------------------
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ def test_shift_bool(self):
def test_as_integer_ratio(self):
tests = [10, 0, -10, 1, 3]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so much similar cases are needed?

Add tests for booleans and other int subclasses. Check types of numerator and denominator.

for value in tests:
self.assertEqual((value).as_integer_ratio(), (value, 1))
self.assertEqual(value.as_integer_ratio(), (value, 1))


if __name__ == "__main__":
Expand Down
11 changes: 1 addition & 10 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5268,8 +5268,6 @@ Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original int
and with a positive denominator.

Raise OverflowError on infinities and a ValueError on NaNs.

>>> (10).as_integer_ratio()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need so much examples in a docstring?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's useful to have at least one positive and one negative example, so that it's obvious at a glance that the behaviour for negatives is to give (for example) (-5, 1) rather than (5, -1). I could imagine users thinking that 0 was somehow a special case, too, so I like that we have the 0 example there.

(10, 1)
>>> (0).as_integer_ratio()
Expand All @@ -5284,14 +5282,7 @@ static PyObject *
int_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=e60803ae1cc8621a input=ce9c7768a1287fb9]*/
{
PyObject *denominator = NULL;
PyObject *result_pair = NULL;

denominator = PyLong_FromLong(1);
result_pair = PyTuple_Pack(2, self, denominator);

Py_DECREF(denominator);
return result_pair;
return PyTuple_Pack(2, self, _PyLong_One)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a missing semicolon at the end of this line; I think that's why the continuous integration builds are failing.

Also, please change the indentation of this line to 4 spaces instead of 2 spaces, following PEP7. Thanks!

}

/*[clinic input]
Expand Down