AdithyaSK's picture
AdithyaSK HF Staff
Publish 100 swe_next Harbor tasks
d40299b verified
|
Raw History Blame Contribute Delete
1.75 kB
## `iequals` incorrectly returns `True` when comparing iterables of different lengths with equality-wildcard objects
### Description
When using `iequals` to compare two iterables of different lengths, it can incorrectly return `True` if one of the iterables contains an object that compares equal to everything (such as `unittest.mock.ANY`).
Here's a minimal reproduction:
```python
from unittest import mock
import more_itertools as mi
# An empty list vs a list containing mock.ANY
result = mi.iequals([], [mock.ANY])
print(result) # prints True, but should be False
```
The two iterables clearly have different lengths (0 vs 1), so `iequals` should return `False`. But it returns `True`.
### Root cause
The current implementation uses `zip_longest` with a private sentinel `fillvalue=object()` to detect length mismatches:
```python
return all(map(all_equal, zip_longest(*iterables, fillvalue=object())))
```
The idea is that the sentinel won't compare equal to real values. However, `mock.ANY` (and any object that overrides `__eq__` to always return `True`) compares equal to the sentinel too, so the length mismatch goes undetected and `iequals` returns `True` instead of `False`.
### Expected behavior
`mi.iequals([], [mock.ANY])` should return `False`, since the two iterables have different lengths and cannot be considered equal regardless of the values they contain.
### Actual behavior
`mi.iequals([], [mock.ANY])` returns `True`.
Work in `/workspace`. Submit your fix in the existing Python source files under `more_itertools`. Preserve the other public behavior. The environment is offline; dependencies are preinstalled. Grading runs the repository's test suite in a fresh environment, using your submitted source files.