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.

classification
Title: Error message says dict has no len
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: PySequence_Length() raises TypeError on dict type
View: 32500
Assigned To: serhiy.storchaka Nosy List: rhettinger, serhiy.storchaka, veky
Priority: low Keywords:

Created on 2018-06-21 13:25 by veky, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg320178 - (view) Author: Vedran Čačić (veky) * Date: 2018-06-21 13:25
Look at this:

>>> 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.

At https://bugs.python.org/issue32500, R. David Murray told me to open this.
msg320330 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-06-23 20:03
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 https://github.com/python/cpython/pull/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 issue32500 where it arose.  This would show-up in any C code that calls PySequence_Size() not just bisect.
msg320362 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-06-24 07:09
I think this is an exact duplicate of issue32500.
History
Date User Action Args
2022-04-11 14:59:02adminsetgithub: 78114
2018-06-24 07:09:15serhiy.storchakasetstatus: open -> closed
superseder: PySequence_Length() raises TypeError on dict type
messages: + msg320362

resolution: duplicate
stage: resolved
2018-06-24 06:28:30rhettingersetassignee: serhiy.storchaka

nosy: + serhiy.storchaka
2018-06-23 20:03:06rhettingersetpriority: normal -> low
nosy: + rhettinger
messages: + msg320330

2018-06-21 13:25:21vekycreate