Skip to content

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.

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.

@arvi18
Copy link
Author

arvi18 commented Sep 2, 2025

/refacto-test

Copy link

refacto-test bot commented Sep 2, 2025

Refacto is reviewing this PR. Please wait for the review comments to be posted.

Copy link

refacto-test bot commented Sep 2, 2025

Code Review: Iterator Processing Evaluation Order Fix

👍 Well Done
Execution Order Fix

Corrected evaluation order for iterator tuple assignment prevents data corruption and ensures Python semantics are properly maintained.

📌 Files Processed
  • mypyc/test-data/irbuild-statements.test
  • mypyc/irbuild/builder.py
📝 Additional Comments
mypyc/irbuild/builder.py (4)
Duplicate List Iteration

The code iterates through 'reversed(post_star_vals)' twice - once to read values and again to assign them. This creates O(n) additional iterations that could be avoided. While the impact is minimal for small lists, it's an unnecessary performance cost. Consider combining the read and assign operations if evaluation order permits, or use a more efficient approach that avoids the double iteration pattern.

Standards:

  • ISO-IEC-25010-Performance-Time-Behaviour
  • Algorithm-Opt-Loop-Efficiency
Consistent Pattern Application

The same pattern is correctly applied to starred values, separating reading from assignment. This consistent approach improves maintainability by establishing a clear pattern throughout the codebase.

Standards:

  • Clean-Code-Consistency
  • Design-Pattern-Template-Method
Value Assignment Reordering

Separating read and assign operations changes execution order. Consider documenting this behavior change to prevent future regressions.

Standards:

  • ISO-IEC-25010-Functional-Correctness-Appropriateness
  • DbC-Resource-Mgmt
Reordered Value Assignment

Original code assigns values directly after reading, causing incorrect evaluation order. Attacker could exploit inconsistent behavior in security-sensitive unpacking operations. Fixes by reading all values first, then assigning.

Standards:

  • CWE-696
  • OWASP-A04

Comment on lines +620 to 622
values = []
for litem in reversed(post_star_vals):
ritem = self.call_c(list_pop_last, [iter_list], line)
Copy link

Choose a reason for hiding this comment

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

Post-Star Assignment Order

Similar to the first issue, values after the starred expression are assigned immediately after reading, violating Python's evaluation-then-assignment order. The fix correctly reads all values first, then performs assignments separately.

Standards
  • Algorithm-Correctness-Execution-Order
  • Language-Compliance-Python-Semantics
  • Logic-Verification-Side-Effects

Comment on lines +602 to 603
# Read the starred value and all values after it
if target.star_idx is not None:
Copy link

Choose a reason for hiding this comment

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

Star Unpack Testing

Test case for star unpacking exists but comment indicates implementation change. Ensure test coverage validates both reading and assignment phases for starred values.

Standards
  • ISO-IEC-25010-Reliability-Maturity
  • ISO-IEC-25010-Functional-Correctness-Appropriateness

@visz11
Copy link
Collaborator

visz11 commented Sep 17, 2025

/refacto-test-arvi

Copy link

Refacto is reviewing this PR. Please wait for the review comments to be posted.

Copy link

Code Review: Iterator Assignment Execution Order

👍 Well Done
Improved Execution Order

Successfully fixed iterator unpacking to match Python's left-to-right evaluation semantics by separating value reading from assignment.

📌 Files Processed
  • mypyc/test-data/irbuild-statements.test
  • mypyc/irbuild/builder.py
📝 Additional Comments
mypyc/irbuild/builder.py (3)
Simplify Value Collection

The code collects values in a list and then assigns them, but the collection loop doesn't need to iterate through litem values since they're not used during collection. Consider simplifying by using a range-based loop or list comprehension for value collection.

Standards:

  • Algorithm-Correctness-Code-Clarity
  • Logic-Verification-Simplification
Defensive Iterator Handling

Similar to the first issue, the code now separates reading values from assigning them for post-starred values. Consider adding additional validation to ensure iter_list is not modified during the reading phase, as external modifications between reading and assignment could lead to inconsistent state.

Standards:

  • CWE-696
  • OWASP-A04
Batch Value Reading

Reading all values before assignment improves error handling by ensuring all required values exist before any assignments occur. This pattern avoids partial state changes and improves performance by preventing unnecessary assignments that might need to be undone if an exception occurs later.

Standards:

  • ISO-IEC-25010-Performance-Efficiency-Behavior-Predictability
  • Optimization-Pattern-Batch-Processing
  • Algorithmic-Complexity-Exception-Handling

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.

Reversed Post-Star Assignment

The post-star values are collected in reverse order (using list_pop_last), but the assignment loop also uses reversed(post_star_vals), causing double reversal. This incorrectly assigns values to the wrong variables compared to Python's expected behavior.

Suggested change
# Assign the read values to target lvalues
for litem, ritem in zip(reversed(post_star_vals), values):
self.assign(litem, ritem, line)
# Assign the read values to target lvalues
for litem, ritem in zip(post_star_vals, reversed(values)):
self.assign(litem, ritem, line)
Standards
  • Algorithm-Correctness-Execution-Order
  • Logic-Verification-Data-Flow
  • Business-Rule-State-Consistency

Comment on lines +620 to 627
values = []
for litem in reversed(post_star_vals):
ritem = self.call_c(list_pop_last, [iter_list], line)
values.append(ritem)

# 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.

Inconsistent Pattern Implementation

The code correctly implements the read-then-assign pattern for post-star values, but uses a different approach than for pre-star values. This inconsistency could lead to maintenance issues and potential bugs when modifying either section. The pattern should be consistently applied across both code paths.

Standards
  • ISO-IEC-25010-Reliability-Maturity
  • ISO-IEC-25010-Functional-Correctness-Consistency
  • DbC-Implementation-Consistency

Comment on lines +620 to 622
values = []
for litem in reversed(post_star_vals):
ritem = self.call_c(list_pop_last, [iter_list], line)

Choose a reason for hiding this comment

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

Redundant Value Processing

Values are immediately assigned after being popped from the list, creating the same evaluation order issue as above. This pattern leads to inconsistent state if an exception occurs during iteration, causing performance overhead from unnecessary partial assignments that may need to be rolled back.

Standards
  • ISO-IEC-25010-Performance-Efficiency-Resource-Utilization
  • Optimization-Pattern-Batch-Processing
  • Algorithmic-Complexity-Exception-Handling

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.

Two-Phase Assignment Pattern

The code implements a clear two-phase pattern: first reading all values, then assigning them. This separation improves maintainability by making the execution order explicit and matching Python's evaluation semantics for unpacking assignments.

Standards
  • Clean-Code-Clarity
  • Design-Pattern-Separation
  • Maintainability-Quality-Consistency

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
3 participants