classification
Title: bisect raises a TypeError when hi is None
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Mike Lenzen, rhettinger, serhiy.storchaka, xiang.zhang
Priority: low Keywords:

Created on 2016-03-28 04:00 by Mike Lenzen, last changed 2016-03-31 03:42 by Mike Lenzen. This issue is now closed.

Messages (6)
msg262551 - (view) Author: Mike Lenzen (Mike Lenzen) Date: 2016-03-28 04:00
>>> bisect.bisect([1, 2, 3], 1, hi=None)
TypeError: 'NoneType' object cannot be interpreted as an integer

I'm assuming this is an error in the C implementation because the Python source has:

    if hi is None:
        hi = len(a)
msg262552 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-03-28 04:34
The C version bisect_right can only accept number as hi, which conflicts with the python version using None as the default value.
msg262559 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-03-28 06:14
The pure python code used None as a placeholder for an optional argument.  It is not an intended input value.  The documented signature
is:  bisect.bisect_left(a, x, lo=0, hi=len(a)).
msg262576 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-03-28 16:32
May be change the placeholder? For example to sys.maxsize, float('inf') or ['len(a)'].
msg262590 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-03-29 05:37
Sorry, I don't think that is a good idea.  The use of None as a placeholder for optional arguments is legitimate and well enshrined in Python history.  The code is bisect is very old, stable, and battle-tested.  There isn't a real bug here, nothing is broken; instead, we have a near pedantic notion that the C-version must be forced to replicate a minor and irrelevant implementation detail of the Python version that goes beyond what the docs guarantee.  I recommend closing this as "not a bug".
msg262686 - (view) Author: Mike Lenzen (Mike Lenzen) Date: 2016-03-31 03:42
Sounds good, but then I think the docs could be clearer about this. I scratched my head at the function signature bisect.bisect_left(a, x, lo=0, hi=len(a)) because it isn't valid (in Python, maybe in C?), so I checked the source and saw hi=None. I'm not sure how to concisely describe it though. Listing two signatures doesn't seem ideal.
History
Date User Action Args
2016-03-31 03:42:15Mike Lenzensetmessages: + msg262686
2016-03-30 07:30:01rhettingersetstatus: open -> closed
resolution: not a bug
2016-03-29 05:37:16rhettingersetmessages: + msg262590
2016-03-28 16:32:04serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg262576
2016-03-28 06:14:46rhettingersetpriority: normal -> low

messages: + msg262559
2016-03-28 06:08:55rhettingersetassignee: rhettinger
2016-03-28 04:34:27xiang.zhangsetnosy: + xiang.zhang
messages: + msg262552
2016-03-28 04:00:48Mike Lenzencreate