File size: 1,751 Bytes
d40299b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## `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.