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: collections.counter update method without argument gives misleading error
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, serhiy.storchaka, xtreak
Priority: normal Keywords:

Created on 2018-06-19 07:54 by xtreak, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg319935 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-06-19 07:54
There was a change made in collections.Counter that made it accept self as the first parameter and hence while doing *args in the method signature args[0] is always set as the self. Hence the check for `not args` becomes a no-op during collection_object.update() calls and the type error is not raised. This is only triggered when it's called as a class method like `Counter.update` and the error message is misleading "descriptor 'update' of 'Counter' object "needs an argument" which says update method of Counter object needs an argument though we are calling the method on class and also calling update on Counter object doesn't need an argument since counter_object.update() works fine as no-op. I think the error message could be improved since argument is not necessary for counter_object.update.

Relevant commit : https://github.com/python/cpython/commit/ae5cb214d2cd41d96943a0ef43a4e95bd9a10b7a#diff-f30cb98704c3ccaf71deaf5503f7f4a1R587
No argument to counter object update test case : https://github.com/python/cpython/blob/ae5cb214d2cd41d96943a0ef43a4e95bd9a10b7a/Lib/test/test_collections.py#L1077
Calling update on Counter class test case : https://github.com/python/cpython/commit/20994f1e27c38973f1854dbdcf9b29fe8e3cd6be#diff-b5f669eab2fa576572b6115caa24427fR928

# Python 3

➜  cpython git:(master) ✗ ./python
Python 3.8.0a0 (heads/bpo33830-httplib-docs-fix-dirty:9bf0fb8, Jun 18 2018, 17:09:54)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import Counter
>>> c = Counter("Hello")


>>> Counter.update()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/user/karthik/cpython/Lib/collections/__init__.py", line 638, in update
    raise TypeError("descriptor 'update' of 'Counter' object "
TypeError: descriptor 'update' of 'Counter' object needs an argument
>>> Counter.update(1) # No error since we check for the first argument to be iterable
>>> c.update() # No args works fine though there was an above error with TypeError: descriptor 'update' of 'Counter' object needs an argument when called on class

>>> Counter.update(1, foo=1) # 1 is considered as self here, a valid counter object works fine and is updated
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/user/karthik/cpython/Lib/collections/__init__.py", line 655, in update
    self.update(kwds)
AttributeError: 'int' object has no attribute 'update'


Thanks
msg319939 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-06-19 08:38
This error is correct. It is the same as for dict.update.

>>> dict.update()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'update' of 'dict' object needs an argument

Perhaps you missed the difference between a descriptor and a method.
msg319940 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-06-19 08:46
Okay, got it. Sorry for the noise. Closing it.

Thanks
History
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 78081
2018-06-19 08:46:37xtreaksetstatus: open -> closed
resolution: not a bug
messages: + msg319940

stage: resolved
2018-06-19 08:38:31serhiy.storchakasetnosy: + rhettinger, serhiy.storchaka
messages: + msg319939
2018-06-19 07:54:27xtreakcreate