-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
bpo-26701: Add documentation for __trunc__ #6022
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! Your changes look good as far as __trunc__
goes, but they also make it clear that it's not only the __trunc__
path that isn't currently documented properly, but also the __index__
path.
Would you be amenable to adjusting the PR to cover __index__
as well?
@@ -711,8 +711,11 @@ are always available. They are listed here in alphabetical order. | |||
|
|||
Return an integer object constructed from a number or string *x*, or return | |||
``0`` if no arguments are given. If *x* is a number, return | |||
:meth:`x.__int__() <object.__int__>`. For floating point numbers, this | |||
truncates towards zero. | |||
:meth:`x.__int__() <object.__int__>`. If *x* defines |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're updating this paragraph anyway, it makes sense to mention __index__
as well, perhaps by converting to a bulleted list of conversion methods in order of priority: x.__index__()
, x.__int__()
, x.__trunc__()
.
Called to implement :meth:`math.trunc`. Should return the value of the | ||
object truncated to a :class:`numbers.Integral` (typically an | ||
:class:`int`). If a class defines :meth:`__trunc__` but not | ||
:meth:`__int__`, then :meth:`__trunc__` is called to implement the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mention __index__
here as well.
@ncoghlan - yes, I'll update this tonight and incorporate your suggestions. |
@ncoghlan So I did some experimentation to try to ensure that I had the order of fallbacks correct, and after further investigation it seems that you can't call
This appears to be odd behavior in that something that is obviously designed to be interpreted as an int for the purposes of indexing and slicing can't be coerced into an int, and it seems backwards relative to the aim of PEP-357. The source code for Maybe I am misreading the code, could you verify? |
@appeltel Huh, very strange. I guess the history there is that all of the objects that |
Since the handling of |
`int` fails back to `__trunc__` is `__int__` isn't defined, so cover that in the docs. (cherry picked from commit 308eab9) Co-authored-by: Eric Appelt <eric.appelt@gmail.com>
GH-6049 is a backport of this pull request to the 3.7 branch. |
`int` fails back to `__trunc__` is `__int__` isn't defined, so cover that in the docs. (cherry picked from commit 308eab9) Co-authored-by: Eric Appelt <eric.appelt@gmail.com>
GH-6050 is a backport of this pull request to the 3.6 branch. |
`int` fails back to `__trunc__` is `__int__` isn't defined, so cover that in the docs.
Add explicit documentation of the
__trunc__
method beyond the single mention in the math module.Place the documentation for this method near to
__int__
, and explain that there is a fallback tox.__trunc__()
ifx.__int__()
is not available.https://bugs.python.org/issue26701