Skip to content

Error message says dict has no len #78114

Closed

Description

@vedgar
mannequin
BPO 33933
Nosy @rhettinger, @serhiy-storchaka, @vedgar
Superseder
  • 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 = <Date 2018-06-24.07:09:15.001>
    created_at = <Date 2018-06-21.13:25:21.228>
    labels = ['interpreter-core', '3.7']
    title = 'Error message says dict has no len'
    updated_at = <Date 2018-06-24.07:09:15.000>
    user = 'https://github.com/vedgar'

    bugs.python.org fields:

    activity = <Date 2018-06-24.07:09:15.000>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2018-06-24.07:09:15.001>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2018-06-21.13:25:21.228>
    creator = 'veky'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33933
    keywords = []
    message_count = 3.0
    messages = ['320178', '320330', '320362']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'serhiy.storchaka', 'veky']
    pr_nums = []
    priority = 'low'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '32500'
    type = None
    url = 'https://bugs.python.org/issue33933'
    versions = ['Python 3.7']

    Activity

    1. vedgar commented on Jun 21, 2018

      vedgarmannequin
      MannequinAuthor

      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.

    2. rhettinger commented on Jun 23, 2018

      @rhettinger
      Contributor

      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.

    3. serhiy-storchaka commented on Jun 24, 2018

      @serhiy-storchaka
      Member

      I think this is an exact duplicate of bpo-32500.

    4. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    3.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions