GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — TypeError in bisect when called with key argument · Issue #117848 · python/cpython

TypeError in bisect when called with key argument #117848

New issue
Copy link
New issue
Copy link
Closed
Closed
TypeError in bisect when called with key argument#117848
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@c-m-m-kobayashi
c-m-m-kobayashi
opened on Apr 13, 2024
Issue body actions

Bug report

Bug description:

bisect functions raise TypeError when called with key argument.
Here is a minimum code to reproduce the issue:

import bisect
import operator

a = [('one', 1), ('two', 2)]
print(bisect.bisect(a, ('one', 1), key=operator.itemgetter(0)))

This results in the following error:

Traceback (most recent call last):
  File "/path/to/bisect_test.py", line 5, in <module>
    print(bisect.bisect(a, ('one', 1), key=operator.itemgetter(0)))
          ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '<' not supported between instances of 'tuple' and 'str'

It seems that picking an item in a tuple has a problem.

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Activity

  1. c-m-m-kobayashi
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 13, 2024
  2. nineteendo commented on Apr 13, 2024

    @nineteendo
    nineteendo
    on Apr 13, 2024
    Contributor
    More actions

    You need to apply key() to the element:

    key=operator.itemgetter(0)
    print(bisect.bisect(a, key(('one', 1)), key=key))
  3. nineteendo commented on Apr 13, 2024

    @nineteendo
    nineteendo
    on Apr 13, 2024
    Contributor
    More actions

    See #20556 (comment)

  4. nineteendo commented on Apr 13, 2024

    @nineteendo
    nineteendo
    on Apr 13, 2024
    Contributor
    More actions

    @AlexWaygood, could you close this? This works as intended. Sorry for the ping.

  5. c-m-m-kobayashi commented on Apr 13, 2024

    @c-m-m-kobayashi
    c-m-m-kobayashi
    on Apr 13, 2024
    Author
    More actions

    @nineteendo Thank you for your comment.
    Now I understand that giving the field to be compared is better than the entire record in bisect search.

  6. c-m-m-kobayashi
    closed this as completedon Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.