Skip to content

collections.ChainMap.__iter__ calls __getitem__ on underlying maps #86653

Closed

Description

@ap
mannequin
BPO 42487
Nosy @rhettinger, @ambv, @serhiy-storchaka, @miss-islington, @ap--
PRs
  • bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ #23534
  • [3.9] bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534) #23569
  • 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/rhettinger'
    closed_at = <Date 2020-11-30.21:23:39.144>
    created_at = <Date 2020-11-28.00:50:30.644>
    labels = ['library', '3.9', '3.10', 'performance']
    title = 'collections.ChainMap.__iter__ calls __getitem__ on underlying maps'
    updated_at = <Date 2020-11-30.21:23:39.144>
    user = 'https://github.com/ap'

    bugs.python.org fields:

    activity = <Date 2020-11-30.21:23:39.144>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2020-11-30.21:23:39.144>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2020-11-28.00:50:30.644>
    creator = 'ap--'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42487
    keywords = ['patch']
    message_count = 4.0
    messages = ['381971', '381974', '382159', '382164']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'lukasz.langa', 'serhiy.storchaka', 'miss-islington', 'ap--']
    pr_nums = ['23534', '23569']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue42487'
    versions = ['Python 3.9', 'Python 3.10']

    Activity

    1. ap commented on Nov 28, 2020

      apmannequin
      MannequinAuthor

      Hello,

      I encountered an issue with collections.ChainMap, that was introduced when https://bugs.python.org/issue32792 got fixed.

      Iterating a ChainMap will call __getitem__ on its underlying maps:

      >>> from collections import UserDict, ChainMap
      >>> class MyDict(UserDict):
      ...     def __getitem__(self, k):
      ...         print("expensive computation", k)
      ...         return super().__getitem__(k)
      ... 
      >>> set(ChainMap(MyDict(a=1, b=2, c=3)))
      expensive computation a
      expensive computation b
      expensive computation c
      {'c', 'b', 'a'}
    2. added
      type-bug
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      3.10 (EOL)
      stdlib
      on Nov 28, 2020
    3. removed
      3.7 (EOL)
      3.8 (EOL)
      on Nov 28, 2020
    4. 3 remaining items

    5. added
      performance
      and removed
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      type-bug
      on Nov 28, 2020
    6. rhettinger commented on Nov 28, 2020

      @rhettinger
      Contributor

      I had a discussion with the release manager and we're good to backport to 3.9 but not 3.8.

      Serhiy, would you also take a look at this?

    7. self-assigned this
      on Nov 28, 2020
    8. self-assigned this
      on Nov 28, 2020
    9. rhettinger commented on Nov 30, 2020

      @rhettinger
      Contributor

      New changeset 0be9ce3 by Andreas Poehlmann in branch 'master':
      bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534)
      0be9ce3

    10. rhettinger commented on Nov 30, 2020

      @rhettinger
      Contributor

      New changeset cf22aa3 by Miss Islington (bot) in branch '3.9':
      bpo-42487: don't call __getitem__ of underlying maps in ChainMap.__iter__ (GH-23534) (GH-23569)
      cf22aa3

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

    Assignees

    Labels

    3.10 (EOL)3.9 (EOL)performancestdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions