One function

last in more-itertools/more-itertools

The author described this change as fix last() when __reversed__ is None. It counts as a record because the check below fails on the code as it stood at c834d6e4a and passes on cca32949f, with nothing else changed between the two runs.

Fix saved2025-07-14
Sharing licenceMIT · LICENSE
Change size+1 1

What the code was meant to do, written into the code itself as a docstring

Return the last item of *iterable*, or *default* if *iterable* is empty. If *default* is not provided and there are no items in the iterable, raise `ValueError`.

The change

1414 if isinstance(iterable, Sequence):
1515 return iterable[-1]
1616 # Work around https://bugs.python.org/issue38525
17- if hasattr(iterable, '__reversed__'):
17+ if getattr(iterable, '__reversed__', None):
1818 return next(reversed(iterable))
1919 return deque(iterable, maxlen=1)[-1]
2020 except (IndexError, TypeError, StopIteration):

The check that tells the two apart

failpass·tests/test_more.py::LastTests::test_reversed_is_none

Check file tests/test_more.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before itc834d6e4a0c4280b7b7750cb0de8dd8acb3d4c2c
Broken version dated2025-07-07
Modulemore_itertools.more
Units changedlast
Fingerprintd2509b7ca26e23a0
Checked2026-08-17 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in more-itertools/more-itertools