Repository navigation
[mypyc] Fix loops on reversed lists with Any item types - #22445
Open
PratyushD21 wants to merge 2 commits into
Open
PratyushD21 wants to merge 2 commits into
PratyushD21 wants to merge 2 commits into
Conversation
Author
|
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.