Repository navigation
[mypyc] Fix loops on reversed lists with Any item types - #22445
PratyushD21 wants to merge 3 commits into
Conversation
|
Hi @p-sawicki, @JukkaL, I have made the change to how the reversed function is overloaded based on the Python documentation. If you feel that the previous implementation for Happy to discuss your inputs! Thanks a lot! |
chrikrah
left a comment
There was a problem hiding this comment.
The fix works, see the mypyc run. blocking: the test passes without it, as both fixture overloads return Iterator[Any].
# git archive 6ae7ef26c (base b83696ea8), CPython 3.12.14, mypyc imported from the export
$ python -m mypyc crash.py # for_helpers.py from b83696ea8
crash.py:4: AssertionError: Any
# same command at head: compiles; the program returns [None, 'x', 1] and [2, 1]
$ pytest -q -n 0 mypyc/test/test_run.py -k testForReversedAny
1 passed # head
1 passed # head, for_helpers.py from b83696ea8
1 failed native.py:7: AssertionError: Any # fixture's second overload -> Iterable[_T], for_helpers.py from b83696ea8
1 passed # same fixture, for_helpers.py from head
$ pytest -q -n 4 mypyc/test # head, unmodified
2190 passed, 1 skipped, 1 xfailed, 50 subtests passed in 579.30s
Could the second fixture overload return Iterable[_T], disagreeing like typeshed's reversed[_T]?
|
Hi @chrikrah Thanks for pointing that out. I have updated the PR with this fix, and now it correctly handles regression. |
chrikrah
left a comment
There was a problem hiding this comment.
8ffc5f7 closes it: the regression test now fails without the fix. From my perspective this can be merged.
# git archive 8ffc5f74d (base b83696ea8), CPython 3.12.14, mypyc imported from the export
$ pytest -q -n 0 mypyc/test/test_run.py -k testForReversedAny
1 passed # head
1 failed native.py:7: AssertionError: Any # head, for_helpers.py from b83696ea8
$ pytest -q -n 4 mypyc/test # head
2190 passed, 1 skipped, 1 xfailed, 50 subtests passed in 775.33s
Fixes #22354
As shared in the issue, if there is a for loop on a reversed iterator, then the
mypyccompiler crashes. This was happening because, in thefor_helpers.pyfile, the call was being inferred asAny, instead of inferring the type of the iterator item.This fix uses the existing iterable analysis method and fetches the
rtype.Regression tests (test name:
testForReversedAny) cover lists and tuples containingAny, comprehensions, tuple unpacking, and aNewTypebacked by a list. TheNewTypewas introduced to showcase why the suggested fix in the original issue will not work. (The suggested fix in the issue was to look up for the argument passed toreversed(). Instead, the current fix is a more general way to find the item type.)Tests:
.venv/bin/python -m pytest -n0 mypyc/test/test_run.py -k testForReversedAnypython -m pytest mypyc/test -rxIn addition, while trying to check why the existing test suite had missed this issue, I noticed that the lightweight builtins fixture in
mypyc/test-data/fixtures/ir.pyhas a simplified definition ofreversed()which is not accurate as per the python docs. As per Python data model documentation ofobject.__reversed__(),reversed()should be using__reversed__()when available or otherwise falling back to__len__()and__getitem__().I have fixed this by overloading the
reversed()function to be in-line with the Python object model documentation.