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.

Title: assertCountEqual doesn't work as expected for dictionary elements
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: cansarigol, eric.smith, rhettinger, serhiy.storchaka, sobolevn
Priority: normal Keywords: patch

Created on 2022-02-02 13:19 by cansarigol, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31077 closed cansarigol, 2022-02-02 13:42
Messages (5)
msg412356 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-02-02 13:36
@cansarigol, can you please specify what do you expect and how it works?
msg412362 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-02-02 14:04
The PR changes the meaning of assertCountEqual. says: Equivalent to: assertEqual(Counter(list(first)), Counter(list(second))) ...

At the very least, the documentation would need to be updated. But since this could be a breaking change, I don't think it will be accepted.
msg412363 - (view) Author: Can (cansarigol) * Date: 2022-02-02 14:23
@sobolevn I was expecting the following test case to be covered.

self.assertRaises(self.failureException, self.assertCountEqual,
                          {'a': 1}, {'a': 2})

what I understand from the doc of the assertion is that elements have to be the same without regard to order. The doc can be extended for dict.

In [10]: d = {"key": "value"}

In [11]: collections.Counter(list(d))
Out[11]: Counter({'key': 1})

In [12]: collections.Counter(d.items())
Out[12]: Counter({('key', 'value'): 1})
msg412368 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-02-02 15:46
I agree with Eric that this should not be changed.  It is working as documented and intended.

In its original incarenation, assertCountEqual was documented as being equivalent to ``assertEqual(sorted(expected), sorted(actual))``.  Either way, the goal was to count the values produced by iterating.

To compare two dicts or two Counters including both keys and values, just use assertEqual.
msg412428 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-03 09:08
Concur with Eric and Raymond.
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90768
2022-02-03 09:08:52serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg412428

resolution: not a bug
stage: patch review -> resolved
2022-02-02 15:46:38rhettingersetnosy: + rhettinger
messages: + msg412368
2022-02-02 14:23:01cansarigolsetmessages: + msg412363
2022-02-02 14:04:32eric.smithsetnosy: + eric.smith
messages: + msg412362
2022-02-02 13:42:16cansarigolsetkeywords: + patch
stage: patch review
pull_requests: + pull_request29261
2022-02-02 13:36:27sobolevnsetcomponents: + Library (Lib)
versions: + Python 3.9, Python 3.10, Python 3.11
2022-02-02 13:36:15sobolevnsetnosy: + sobolevn
messages: + msg412356
2022-02-02 13:19:38cansarigolcreate