Skip to content

Unit tests for collections.Counter comparisons need cases for missing keys #143586

@heikkitoivonen

Description

@heikkitoivonen

Feature or enhancement

Proposal:

I was trying to find optimizations in collections.Counter comparison operations, and found something that passed existing unit tests:

@@ -814,7 +814,7 @@ def __le__(self, other):
         'True if all counts in self are a subset of those in other.'
         if not isinstance(other, Counter):
             return NotImplemented
-        return all(self[e] <= other[e] for c in (self, other) for e in c)
+        return all(self[e] <= other[e] for e in self)
 
     def __lt__(self, other):
         'True if all counts in self are a proper subset of those in other.'
@@ -826,7 +826,7 @@ def __ge__(self, other):
         'True if all counts in self are a superset of those in other.'
         if not isinstance(other, Counter):
             return NotImplemented
-        return all(self[e] >= other[e] for c in (self, other) for e in c)
+        return all(self[e] >= other[e]  for e in other)
 
     def __gt__(self, other):
         'True if all counts in self are a proper superset of those in other.'

However, this breaks the semantics of the class, because documentation says that missing keys are handled as if they had count 0.

I will make a PR to add more tests to catch this case.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions