Skip to content

Bug in ChainMap if one of the items is empty defaultdict #117576

Closed as not planned

Description

@erastogi

Bug report

Bug description:

from collections import defaultdict, ChainMap
a = ChainMap({1: {2: "a"}}, defaultdict(dict), {3: {4 : "b"}})
print(dict(a))
# Output: {3: {}, 1: {2: 'a'}}
# Expected Output: {3: {4: 'b'}, 1: {2: 'a'}}

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Activity

  1. pochmann3 commented on Apr 6, 2024

    @pochmann3
    Contributor

    You're not using ChainMap and there is no output and you're doing the same thing twice...

  2. added
    pendingThe issue will be closed if no feedback is provided
    on Apr 6, 2024
  3. self-assigned this
    on Apr 6, 2024
  4. rhettinger commented on Apr 6, 2024

    @rhettinger
    Contributor

    I'm think the example was meant to look something like this:

    >>> a = ChainMap({1: {2: "a"}}, defaultdict(dict), {3: {4 : "b"}})
    >>> a[3]
    {}
    >>> a
    ChainMap({1: {2: 'a'}}, defaultdict(<class 'dict'>, {3: {}}), {3: {4: 'b'}})
    

    If that is the case, then this isn't a bug. A defaultdict always shadows downstream mappings because is was specifically designed to always return some value whether or not a key is present.

  5. erastogi commented on Apr 11, 2024

    @erastogi
    Author

    Thanks for clarifying. When I was converting ChainMap to dict, it was overriding the latest value for key 3.

  6. ericvsmith commented on Apr 14, 2024

    @ericvsmith
    Member

    Since this doesn't seem to be a bug, I'm going to close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions