This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Counter.elements() wrong error message
Type: behavior Stage: resolved
Components: Versions: Python 3.10
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: bytebites, eric.smith, rhettinger
Priority: normal Keywords:

Created on 2021-09-28 17:26 by bytebites, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg402795 - (view) Author: (bytebites) Date: 2021-09-28 17:26
aCounter.elements(1) gives wrong error message:
TypeError: Counter.elements() takes 1 positional argument but 2 were given
msg402796 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-28 17:53
When reporting a bug, please give a full example that we can run. I assume aCounter is of type collections.Counter?

Assuming so, the "1 positional argument" is "self". The second argument is the number 1, which is an error. So the error is correct.
msg402816 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-09-29 00:43
The elements() method is working correctly:

    >>> from collections import Counter
    >>> aCounter = Counter('abracadabra')
    >>> list(aCounter.elements())
    ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'r', 'r', 'c', 'd']

No arguments should be passed into elements, so this code is incorrect:

    aCounter.elements(1)

You may have expected that the error message would say:

    TypeError: Counter.elements() takes 0 positional argument but 1 was given

Instead, you observed the counts were one higher than you expected.

This is due to how Python implements object oriented programming.

The call:

    aCounter.elements(1)

is effectively translated to:

    Counter.elements(aCounter, 1)

which does in fact have two arguments.

No one really likes the error message, but it has been deemed hard to fix and we just live with it.

Note, this isn't specific to Counter.  All pure python classes work this way:

    >>> class A:
            def m(self, x):
                pass
        
    >>> a = A()
    >>> a.m(10, 20)
    Traceback (most recent call last):
      File "<pyshell#10>", line 1, in <module>
        a.m(10, 20)
    TypeError: m() takes 2 positional arguments but 3 were given
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89476
2021-09-29 00:43:03rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg402816

resolution: wont fix
stage: resolved
2021-09-28 17:53:01eric.smithsetnosy: + eric.smith
messages: + msg402796
2021-09-28 17:26:47bytebitescreate