Skip to content

Evaluation order of multiple assignment from iterable doesn't match Python #793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JukkaL opened this issue Dec 29, 2020 · 3 comments · May be fixed by python/mypy#10444 or coderabbit-test/mypy#8
Labels
bug good first issue python compat Mypyc doesn't match CPython or documented semantics.

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 29, 2020

In code like this we should iterate over the rvalue first before performing any assignments to match Python semantics:

o.x, o.y = it

We currently interleave assignments and iteration in some cases. The evaluation order is correct if the rvalue is a list or a tuple, but not for arbitrary iterables. Assignments and iteration can have side effects, so in some edge cases these two approaches will produce different results.

Dealing with *x lvalues may make the implementation somewhat tricky (not sure though).

For more context, see python/mypy#9800 (review).

@JukkaL JukkaL added the bug label Dec 29, 2020
@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 20, 2021

Here's a full example where we use the wrong evaluation order:

it = iter(['x', 'y'])

def f(a) -> None:
    a.x, a.y = it

Currently f is compiled to something like this (pseudocode, simplified):

_tmp = iter(it)
a.x = next(_tmp)
a.y = next(_tmp)

We should compile it to something like this (pseudocode, simplified):

_tmp = iter(it)
_tmp2 = next(_tmp)
_tmp3 = next(_tmp)
a.x = _tmp2
a.y = _tmp3

The relevant code can be found by starting from mypyc.irbuild.statement.transform_assignment_stmt.

@starrohan999
Copy link

hii @JukkaL i want to work on this.
please assign me.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 21, 2021

@starrohan999 You can go ahead and work on this. We don't use issue assignments very consistently, since they get easily out of date. They mostly act as reminders for the assignees.

@JukkaL JukkaL added the python compat Mypyc doesn't match CPython or documented semantics. label Apr 19, 2021
ChetanKhanna added a commit to ChetanKhanna/mypy that referenced this issue May 7, 2021
…ith Python (python#793)

Refactored to iterate rvalues first before performing any assignments.

Closes mypyc/mypyc#793.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue python compat Mypyc doesn't match CPython or documented semantics.
Projects
None yet
2 participants