Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collections.Counter drops key if value is 0 and updating using += operator #90094

Closed
crypdick mannequin opened this issue Nov 30, 2021 · 3 comments
Closed

collections.Counter drops key if value is 0 and updating using += operator #90094

crypdick mannequin opened this issue Nov 30, 2021 · 3 comments
Assignees
Labels
3.8 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@crypdick
Copy link
Mannequin

crypdick mannequin commented Nov 30, 2021

BPO 45936
Nosy @rhettinger, @sweeneyde

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 2021-11-30.03:46:38.855>
created_at = <Date 2021-11-30.02:43:03.801>
labels = ['invalid', 'type-bug', '3.8']
title = 'collections.Counter drops key if value is 0 and updating using += operator'
updated_at = <Date 2021-11-30.03:47:26.253>
user = 'https://bugs.python.org/crypdick'

bugs.python.org fields:

activity = <Date 2021-11-30.03:47:26.253>
actor = 'rhettinger'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2021-11-30.03:46:38.855>
closer = 'rhettinger'
components = []
creation = <Date 2021-11-30.02:43:03.801>
creator = 'crypdick'
dependencies = []
files = []
hgrepos = []
issue_num = 45936
keywords = []
message_count = 3.0
messages = ['407348', '407349', '407351']
nosy_count = 3.0
nosy_names = ['rhettinger', 'Dennis Sweeney', 'crypdick']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45936'
versions = ['Python 3.8']

@crypdick
Copy link
Mannequin Author

crypdick mannequin commented Nov 30, 2021

In brief:

from collections import Counter
x = Counter({'a': 0, 'b': 1})
x.update(x)  # works: Counter({'a': 0, 'b': 2})
x += x  # expected: Counter({'a': 0, 'b': 3}) actual: Counter({'b': 3})

I expect += and .update() to be synonymous. However, the += operator is deleting keys if the source Counter has a zero count to begin with:

x = Counter({'a': 1})
x += Counter({'a': 0})  # ok: Counter({'a': 1})

y = Counter({'a': 0})
y += y  # expected: Counter({'a': 0}) actual: Counter()

@crypdick crypdick mannequin added 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Nov 30, 2021
@sweeneyde
Copy link
Member

This is consistent with the docstrings of the methods:

---------------------------------------------------------------------

>>> help(Counter.__iadd__)
Help on function __iadd__ in module collections:
__iadd__(self, other)
    Inplace add from another counter, keeping only positive counts.
    
    >>> c = Counter('abbb')
    >>> c += Counter('bcc')
    >>> c
    Counter({'b': 4, 'c': 2, 'a': 1})
>>> help(Counter.update)
Help on function update in module collections:
update(self, iterable=None, /, **kwds)
    Like dict.update() but add counts instead of replacing them.
    
    Source can be an iterable, a dictionary, or another Counter instance.
    
    >>> c = Counter('which')
    >>> c.update('witch')           # add elements from another iterable
    >>> d = Counter('watch')
    >>> c.update(d)                 # add elements from another counter
    >>> c['h']                      # four 'h' in which, witch, and watch
    4

However, it seems Counter.__iadd__ is not mentioned at all at https://docs.python.org/3/library/collections.html#collections.Counter. I think there could be a doc update.

@rhettinger
Copy link
Contributor

I don't think there is a need to list the inplace methods. They were put in to optimize what was already occurring when only the __add__ method was defined. Also, other container typically don't specifically call out the inplace methods.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants