This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients ammar2, mark.dickinson, seberg, serhiy.storchaka, tcaswell, terry.reedy, vstinner
Date 2019-08-30.07:41:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1567150878.74.0.965686998996.issue37980@roundup.psfhosted.org>
In-reply-to
Content
Yes, I think there's a lot of history here that obscures the picture.

We have mechanisms in Python to allow 3rd party objects to be interpreted as floats (via __float__) or as integers (via __index__). 

So np.int64 (for example) doesn't subclass int (on Python 3), but is usable as an integer almost everywhere that that makes sense because it defines __index__ (and __int__, but it's __index__ that really matters here).

If you're writing something bool-like in a 3rd party library, and you want it to be duck-typeable as a boolean in calls that expect a boolean flag, what special method should you implement? It seems clear to me that the answer should be __bool__, but the current error message appears to be telling the NumPy folks that if they want this to work then np.bool_ should (continue to) implement __index__. That feels odd to me: it's a completely legitimate and sensible choice on NumPy's part to decide that np.bool_ objects *aren't* integers, and shouldn't implement __index__.

The NumPy bug report demonstrates that the concern isn't hypothetical: np.bool_ really *is* a boolean type, and it's easy and natural for a NumPy user to end up passing an np.bool_ instance instead of a bool instance where a flag is expected, and then rather surprising when that doesn't work as expected. I've run into this myself on occasion (turns out that PyQt doesn't like np.bool_ objects either, but that's another story).

The one disadvantage that I can see of allowing arbitrary objects to be used as flags is the potential for surprising failure modes when a function is accidentally misused. For example, it could be surprising if something like:

> "aaaXbbbXccc".splitlines("X")

returned ["aaaXbbbXccc"] instead of raising as it currently does. But many flags are keyword-only, which would make it harder to run into this sort of thing accidentally.
 
In short, I think this particular case _is_ a bug that should be fixed.
History
Date User Action Args
2019-08-30 07:41:18mark.dickinsonsetrecipients: + mark.dickinson, terry.reedy, vstinner, serhiy.storchaka, seberg, tcaswell, ammar2
2019-08-30 07:41:18mark.dickinsonsetmessageid: <1567150878.74.0.965686998996.issue37980@roundup.psfhosted.org>
2019-08-30 07:41:18mark.dickinsonlinkissue37980 messages
2019-08-30 07:41:17mark.dickinsoncreate