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
Document that TestCase.assertCountEqual() can take iterables #686
Conversation
The docstring for `assertCountEqual()` says "An unordered sequence comparison", but in fact it works fine with general Iterables, since the first thing it does is make a defensive copy. Since order does not matter, we are not indexing into the arguments, it is necessary to read all elements to make a comparison, and a defensive copy is generally a good practice, there does not seem to be a reason to forbid iterables. That is, it is hard to imagine a situation where a list is fine but an iterable is not. (The only potential issue is if the same iterable object is passed as both arguments, but of course that is the nature of iterables.) I noticed the issue when using mypy, and typeshed has updated their stubs: python/typeshed#1006
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA. This is necessary for legal reasons before we can look at your contribution. Please follow these steps to help rectify the issue:
Thanks again to your contribution and we look forward to looking at it! |
@jkleint, thanks for your PR! By analyzing the history of the files in this pull request, we identified @benjaminp, @voidspace and @serhiy-storchaka to be potential reviewers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring wasn't well written to begin with. The proposed while technically correct makes in sound even more strange. I suggest a more thorough rewording, perhaps something like, "Asserts that two iterables have the same elements, the same number of times, without regard to order", or "Asserts that two iterables have the same elements, regardless of order and that if the same element occurs more than once, it occurs the same number of times."
Verbiage from @rhettinger.
Went with the first suggestion; thanks. |
The docstring for
assertCountEqual()
says "An unordered sequence comparison," but in fact it works fine with general Iterables, since the first thing it does is make a defensive copy. Since order does not matter, we are not indexing into the arguments, it is necessary to read all elements to make a comparison, and a defensive copy is generally a good practice, there does not seem to be a reason to forbid iterables. That is, it is hard to imagine a situation where a list is fine but an iterable is not. (The only potential issue is if the same iterator object is passed as both arguments, but of course that is the nature of iterators.) This is, however, a change to the method contract and the implementation would have to remain consistent in the future.I noticed the issue when using mypy, and typeshed has updated their stubs: python/typeshed#1006