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: assertCountEqual is horribly misleading, sounds like only counts are being compared
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: duplicate
Dependencies: Superseder: unittest.TestCase.assertCountEqual is a very misleading name
View: 27071
Assigned To: Nosy List: ammar2, rhettinger, terry.reedy, vitaly.krug
Priority: normal Keywords:

Created on 2019-09-20 21:55 by vitaly.krug, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg352901 - (view) Author: Vitaly Kruglikov (vitaly.krug) Date: 2019-09-20 21:55
assertCountEqual is a horribly misleading name because it misleads the programmer and reader of the test code into thinking that it only compares the counts of the elements.

It's name misrepresents what it does in a really bad way.
msg352902 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2019-09-20 22:01
Hey Vitaly, not sure if you're the author of the original bug here: https://bugs.python.org/issue27071

Could you re-open that so the discussion is kept in one place.
msg376437 - (view) Author: Vitaly Kruglikov (vitaly.krug) Date: 2020-09-05 18:49
Re-opened, thanks!

On Fri, Sep 20, 2019 at 3:01 PM Ammar Askar <report@bugs.python.org> wrote:

>
> Ammar Askar <ammar@ammaraskar.com> added the comment:
>
> Hey Vitaly, not sure if you're the author of the original bug here:
> https://bugs.python.org/issue27071
>
> Could you re-open that so the discussion is kept in one place.
>
> ----------
> nosy: +ammar2
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue38240>
> _______________________________________
>
msg376438 - (view) Author: Vitaly Kruglikov (vitaly.krug) Date: 2020-09-05 18:49
Reopened per request from ammar2
msg376440 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-09-05 20:02
This should stay closed as a duplicate.

If you must, reopen of the original 27071, but it was closed for a reason.  The majority of respondents voted for the status quo.  Each alternative that was considered had its own share of problems.

BTW, it is hyperbolic to call the name "horribly misleading".  The method literally runs Counter and checks for equality.  The docstring is clear about this:

    def assertCountEqual(self, first, second, msg=None):
        """Asserts that two iterables have the same elements, the same number of
        times, without regard to order.

            self.assertEqual(Counter(list(first)),
                             Counter(list(second)))

         Example:
            - [0, 1, 1] and [1, 0, 1] compare equal.
            - [0, 0, 1] and [0, 1] compare unequal.

        """
msg376442 - (view) Author: Vitaly Kruglikov (vitaly.krug) Date: 2020-09-05 20:17
Well, when you do tab-completion in unit tests on `self.` and
`assertCountEqual` pops up, it sure sounds a lot like it's going to compare
just the count of the items. The point is that the name of the function is
not self-documenting, hence "misleading".

A better name could be `assertItemsEqual` or `assertCountAndItemsEqual`

On Sat, Sep 5, 2020 at 1:02 PM Raymond Hettinger <report@bugs.python.org>
wrote:

>
> Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
>
> This should stay closed as a duplicate.
>
> If you must, reopen of the original 27071, but it was closed for a
> reason.  The majority of respondents voted for the status quo.  Each
> alternative that was considered had its own share of problems.
>
> BTW, it is hyperbolic to call the name "horribly misleading".  The method
> literally runs Counter and checks for equality.  The docstring is clear
> about this:
>
>     def assertCountEqual(self, first, second, msg=None):
>         """Asserts that two iterables have the same elements, the same
> number of
>         times, without regard to order.
>
>             self.assertEqual(Counter(list(first)),
>                              Counter(list(second)))
>
>          Example:
>             - [0, 1, 1] and [1, 0, 1] compare equal.
>             - [0, 0, 1] and [0, 1] compare unequal.
>
>         """
>
> ----------
> nosy: +rhettinger
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue38240>
> _______________________________________
>
msg376739 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-09-11 21:44
Vitaly, when you repost to posts by email, please delete the post you are responding to, expect possible for a particular line.  The full post is already here on the web page above the response, and a duplicate copy is just noise that wastes screen space.
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82421
2020-09-11 21:54:59terry.reedysetversions: + Python 3.10, - Python 3.7
2020-09-11 21:44:50terry.reedysetnosy: + terry.reedy

messages: + msg376739
versions: - Python 3.5, Python 3.6
2020-09-05 20:17:41vitaly.krugsetmessages: + msg376442
2020-09-05 20:02:41rhettingersetstatus: open -> closed


messages: + msg376440
nosy: + rhettinger
2020-09-05 18:49:55vitaly.krugsetmessages: + msg376438
2020-09-05 18:49:17vitaly.krugsetmessages: + msg376437
2020-09-05 18:48:28vitaly.krugsetstatus: closed -> open
2020-09-04 12:55:06mark.dickinsonsetstatus: open -> closed
2019-09-20 22:01:30ammar2setnosy: + ammar2
messages: + msg352902
2019-09-20 21:58:32ammar2settype: behavior
superseder: unittest.TestCase.assertCountEqual is a very misleading name
resolution: duplicate
stage: resolved
2019-09-20 21:55:35vitaly.krugcreate