You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bpo-32500: PySequence_Length() raises TypeError on dict type
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/serhiy-storchaka'closed_at=<Date2018-06-24.07:09:15.001>created_at=<Date2018-06-21.13:25:21.228>labels= ['interpreter-core', '3.7']
title='Error message says dict has no len'updated_at=<Date2018-06-24.07:09:15.000>user='https://github.com/vedgar'
>>> import bisect
>>> bisect.bisect({}, None)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
bisect.bisect({}, None)
TypeError: object of type 'dict' has no len()
Of course, objects of type 'dict' do have len. The problem is that bisect considers its first argument a sequence, which is very sensible to do (although, with ordered dicts, it's not the only sensible choice), but it gives a very wrong error message given that context.
This probably doesn't warrant another tracker entry. Serhiy already had a PR for this and attached it to the original bug report for https://bugs.python.org/issue32500 . See #7846
My suggestion would be to just change the error message in PySequence_Size() from "object of type '%.200s' has no len()" to "object of type '%.200s' has no len() or does not support the sequence protocol (consider using a list instead)".
Marking this as low priority because the behavior has existed for a very long time and hasn't bothered anyone until now (possibly because the bisect module is documented to only search lists). Also, this is a specific instance of a more general, pervasive, and hard problem where low level routines raise exceptions that have error messages than may not make much sense from the point of view someone calling higher level APIs.
Suggest closing this as a duplicate and continuing the discussion back in bpo-32500 where it arose. This would show-up in any C code that calls PySequence_Size() not just bisect.
Activity
vedgar commentedon Jun 21, 2018
Look at this:
Of course, objects of type 'dict' do have len. The problem is that bisect considers its first argument a sequence, which is very sensible to do (although, with ordered dicts, it's not the only sensible choice), but it gives a very wrong error message given that context.
At https://bugs.python.org/issue32500, R. David Murray told me to open this.
rhettinger commentedon Jun 23, 2018
This probably doesn't warrant another tracker entry. Serhiy already had a PR for this and attached it to the original bug report for https://bugs.python.org/issue32500 . See #7846
My suggestion would be to just change the error message in PySequence_Size() from "object of type '%.200s' has no len()" to "object of type '%.200s' has no len() or does not support the sequence protocol (consider using a list instead)".
Marking this as low priority because the behavior has existed for a very long time and hasn't bothered anyone until now (possibly because the bisect module is documented to only search lists). Also, this is a specific instance of a more general, pervasive, and hard problem where low level routines raise exceptions that have error messages than may not make much sense from the point of view someone calling higher level APIs.
Suggest closing this as a duplicate and continuing the discussion back in bpo-32500 where it arose. This would show-up in any C code that calls PySequence_Size() not just bisect.
serhiy-storchaka commentedon Jun 24, 2018
I think this is an exact duplicate of bpo-32500.