bpo-17576: Strict __int__ and __index__ return types; operator.index always uses __index__ by mdickinson · Pull Request #13740 · python/cpython
Conversation
This PR:
- turns the
DeprecationWarnings introduced by @serhiy-storchaka in Python 3.4 for non-integer return values from__int__and__index__intoTypeErrors - makes
operator.indexandPyNumber_Indexalways give the same return value as__index__. This fixes the inconsistency reported by @warsaw in bpo-17576.
Still working on doc updates and what's-new entries, but making the PR now so that others can review.
This PR should also allow simplifications to many parts of the codebase that currently need to check what type they got back from a PyNumber_Index call.
| self.assertRaises(TypeError, getargs_n, 3.14) | ||
| self.assertEqual(99, getargs_n(Index())) | ||
| self.assertEqual(0, getargs_n(IndexIntSubclass())) | ||
| self.assertEqual(getargs_n(IndexIntSubclass()), 99) |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: inconsistent arg ordering. Will fix.
| operator_index = operator.index(my_int) | ||
| self.assertEqual(direct_index, 8) | ||
| self.assertEqual(operator_index, 7) | ||
| self.assertEqual(operator_index, 8) |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the key change: for a subclass of int, we take the __index__ result to be the true value instead of the integer's value.
| return item; | ||
| } | ||
|
|
||
| /* In general, use __index__ */ |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: redundant comment. I'll remove it.
The test failure appears to be due to test_asyncio altering the test environment. It seems unlikely that it's related to the changes in this PR (but anything's possible).
The test failure appears to be due to test_asyncio altering the test environment. It seems unlikely that it's related to the changes in this PR (but anything's possible).
Could you please try rebasing on master to see if it helps. One of the assertion errors in test_asyncio was fixed with #13754 .
Some of these warnings were added in 3.8.
Are you sure? Which ones? The PR removes 3 DeprecationWarning blocks: one in abstract.c and two in longobject.c, and it looks to me as though all three were introduced in 31a6554
mdickinson
deleted the
make-int-and-index-return-types-strict
branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters