Skip to content

[mypyc] Match evaluation order of multiple assignment from iterable (#793) #8

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
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

arvi18
Copy link

@arvi18 arvi18 commented Apr 25, 2025

Description

Refactored to iterate rvalues first before performing any assignments to match with Python semantics.

Closes mypyc/mypyc#793.

Test Plan

Added a new test case to test the unpacking with *x type lvalues

Summary by CodeRabbit

  • Refactor

    • Improved the handling of unpacking assignments with starred expressions for more consistent value assignment and error handling.
  • Tests

    • Updated existing test cases to reflect the new unpacking assignment logic.
    • Added a new test case to verify correct behavior of extended unpacking assignments with starred expressions.

…ith Python (python#793)

Refactored to iterate rvalues first before performing any assignments.

Closes mypyc/mypyc#793.
Copy link

coderabbitai bot commented Apr 25, 2025

Walkthrough

The changes refactor the logic for multiple assignment from iterators in the IR builder. The updated implementation first collects all values from the iterator before performing assignments to the target variables, rather than interleaving iteration and assignment. This adjustment ensures that the evaluation order matches Python semantics, especially in cases involving starred expressions. The test suite is updated to reflect the new evaluation order, and a new test case is added to verify unpacking with a starred target.

Changes

File(s) Change Summary
mypyc/irbuild/builder.py Refactored process_iterator_tuple_assignment to separate value collection from assignment for unpacking logic.
mypyc/test-data/irbuild-statements.test Updated test cases to reflect new evaluation order; added a new test for starred unpacking assignments.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant IRBuilder
    participant Iterator
    participant TargetVars

    Caller->>IRBuilder: process_iterator_tuple_assignment(it, targets)
    IRBuilder->>Iterator: Collect all values (prefix, starred, suffix)
    Iterator-->>IRBuilder: Return values as list
    IRBuilder->>TargetVars: Assign prefix values
    IRBuilder->>TargetVars: Assign starred value
    IRBuilder->>TargetVars: Assign suffix values
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure evaluation order of multiple assignment from iterable matches Python (python#793)
Correct handling of starred lvalues in unpacking assignments (python#793)

Poem

In the warren of code, a shuffle anew,
Assignments now wait till all values are through.
From iterator's burrow, each element hops,
Collected in baskets before assignment drops.
With starred and suffix, the order's just right—
Pythonic precision, a coder's delight!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @arvi18, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request addresses issue python#793 in the mypyc project, focusing on the evaluation order of multiple assignments from iterables. The primary goal is to refactor the code to ensure that rvalues (right-hand side values) are iterated before any assignments are made to lvalues (left-hand side values), aligning with standard Python semantics. This involves changes to mypyc/irbuild/builder.py to adjust the assignment logic and the addition of a new test case in mypyc/test-data/irbuild-statements.test to verify the correct behavior with starred expressions.

Highlights

  • Evaluation Order Fix: The core change involves modifying the process_iterator_tuple_assignment function in mypyc/irbuild/builder.py to first read all rvalues from the iterator before assigning them to the corresponding lvalues. This ensures correct evaluation order, especially when dealing with iterators that might have side effects.
  • Test Case Addition: A new test case, testStarUnpack, has been added to mypyc/test-data/irbuild-statements.test to specifically test the unpacking of iterables with starred lvalues. This test verifies that the assignment order is correct when using the *x syntax.
  • Code Refactoring: The code has been refactored to use temporary lists (values) to store the rvalues before assigning them to the lvalues. This change improves the clarity and correctness of the assignment process.

Changelog

  • mypyc/irbuild/builder.py
    • Modified process_iterator_tuple_assignment to iterate through rvalues before assigning to lvalues.
    • Introduced temporary values list to store rvalues during iteration.
    • Adjusted assignment logic to use the stored values list for assigning to lvalues.
  • mypyc/test-data/irbuild-statements.test
    • Added a new test case testStarUnpack to verify correct unpacking with starred lvalues.
    • Modified existing test cases from_any to adjust variable declarations and assignment order to align with the intended behavior.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


From iterable's flow,
Values unpack, order known,
Python's way is shown.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request refactors the code to ensure the correct evaluation order for multiple assignments from iterables in mypyc. The changes involve reading all rvalues before assigning them to lvalues, which aligns with Python semantics. A new test case has been added to verify the unpacking with *x type lvalues. Overall, the changes seem well-structured and address the identified issue.

Summary of Findings

  • Potential performance impact: The changes introduce temporary lists (values) to store rvalues before assignment. While this ensures correct evaluation order, it might introduce a slight performance overhead due to the list operations. It would be beneficial to analyze the performance impact of these changes, especially for large iterables.
  • Error handling in starred assignments: The code includes error handling for cases where there are not enough values to unpack. However, it might be beneficial to add more specific error messages or handling for cases where the iterable contains non-compatible types, especially when dealing with starred assignments.

Merge Readiness

The pull request addresses an important correctness issue and includes a new test case. However, it's recommended to analyze the potential performance impact of the introduced temporary lists and consider adding more specific error handling for non-compatible types in starred assignments. I am unable to approve this pull request, and users should have others review and approve this code before merging. Given the potential performance impact, it would be best to address this before merging.

@@ -579,7 +579,8 @@ def process_iterator_tuple_assignment(self,
# This may be the whole lvalue list if there is no starred value
split_idx = target.star_idx if target.star_idx is not None else len(target.items)

# Assign values before the first starred value
# Read values before the first starred value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider clarifying in the comment that the purpose of reading values first is to ensure correct evaluation order, matching Python semantics.

Comment on lines +598 to 600
# Assign read values to target lvalues
for litem, ritem in zip(target.items[:split_idx], values):
self.assign(litem, ritem, line)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It might be beneficial to add a comment explaining why the zip function is used here. Is it to handle cases where the lengths of target.items[:split_idx] and values might be different due to some edge case?

Comment on lines +625 to 627
# Assign the read values to target lvalues
for litem, ritem in zip(reversed(post_star_vals), values):
self.assign(litem, ritem, line)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the previous zip usage, a comment explaining the purpose of using zip with reversed here would improve readability.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
mypyc/irbuild/builder.py (1)

621-623: Unused loop variable in value collection

The loop control variable litem is not used within the loop body. Consider using _ to indicate an unused variable.

-for litem in reversed(post_star_vals):
+for _ in reversed(post_star_vals):
    ritem = self.call_c(list_pop_last, [iter_list], line)
    values.append(ritem)
🧰 Tools
🪛 Ruff (0.8.2)

621-621: Loop control variable litem not used within loop body

(B007)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 380cb8d and 32d2aad.

📒 Files selected for processing (2)
  • mypyc/irbuild/builder.py (3 hunks)
  • mypyc/test-data/irbuild-statements.test (5 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
mypyc/irbuild/builder.py (2)
mypyc/test-data/fixtures/ir.py (6)
  • values (175-175)
  • append (144-144)
  • zip (259-259)
  • zip (261-261)
  • items (176-176)
  • reversed (242-242)
mypyc/irbuild/ll_builder.py (2)
  • self (129-134)
  • call_c (1012-1069)
🪛 Ruff (0.8.2)
mypyc/irbuild/builder.py

621-621: Loop control variable litem not used within loop body

(B007)

🔇 Additional comments (5)
mypyc/irbuild/builder.py (2)

583-584: Improvement: Properly separating value collection from assignment in tuple unpacking

The refactoring correctly separates the reading of values from the iterator from their assignment to target lvalues. This ensures that all right-hand side values are fully evaluated before any assignments occur, matching Python's semantics for unpacking operations.

Also applies to: 596-597, 599-600

🧰 Tools
🪛 Ruff (0.8.2)

584-584: Loop control variable litem not used within loop body

(B007)


625-627: Correctly implemented assignment phase for tuple suffix

The code now properly separates reading suffix values (in reverse order) from assigning them to the targets, maintaining consistent semantics with the rest of the implementation.

mypyc/test-data/irbuild-statements.test (3)

522-544: Well-aligned IR code for basic unpacking

The test case IR ordering has been updated to match the new implementation where values are read from the iterator first before any assignments occur. The modified IR correctly reflects the evaluation order: first fetching both values (r1 and r3), then assigning them to variables x and y.


578-602: Correctly updated IR for unpacking with type coercion

The IR code for type coercion during unpacking has been properly updated to match the new evaluation order. This test case correctly illustrates that all values are read first before any assignments or type coercions happen, which is essential for matching Python's semantics.


610-693: Excellent new test case for starred expressions

This new test case effectively verifies the updated tuple unpacking implementation for the more complex scenario with a starred expression. The test covers:

  1. Obtaining the iterator from a global variable
  2. Reading prefix elements (a.x, a.y)
  3. Converting the remainder to a list for starred assignment
  4. Checking if there are enough elements for unpacking
  5. Popping suffix elements in reverse order (a.w, a.u)
  6. Error handling for insufficient values

The test case aligns well with the PR objective to match Python's evaluation order in unpacking operations, especially for scenarios with starred expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Evaluation order of multiple assignment from iterable doesn't match Python
2 participants