Skip to content

feat: support nested tuple unpacking in assignments (e.g., a, (b, c) …#458

Open
Deadpool2000 wants to merge 9 commits intospylang:mainfrom
Deadpool2000:fix-456
Open

feat: support nested tuple unpacking in assignments (e.g., a, (b, c) …#458
Deadpool2000 wants to merge 9 commits intospylang:mainfrom
Deadpool2000:fix-456

Conversation

@Deadpool2000
Copy link
Copy Markdown

Hi! @antocuni I’ve implemented support for nested tuple unpacking in assignments (e.g., a, (b, c) = tup). Previously, the parser would hit an AssertionError because it only expected simple names as unpacking targets.

What's changed?

Basically, I've updated the compiler pipeline to handle unpacking targets recursively. Here's the breakdown:

  • Parser: I updated spy/parser.py to recursively parse py_ast.Tuple nodes in assignments. It now correctly builds a nested SPy AST instead of crashing.
  • AST: I made the UnpackAssign node more flexible so it can hold nested expressions (like sub-tuples) as targets.
  • Scope Analysis: I fixed the ScopeAnalyzer so it can "see" variables hidden inside nested tuples, making sure they get correctly declared and captured.
  • Interpreter: I updated the VM logic in astframe.py to handle these nested targets by recursively triggering unpacking operations.
  • Backends: I updated both the SPy and C backends to make sure they can format and generate valid code for these new nested structures.

Testing

I added a new test file, spy/tests/compiler/test_nested_unpack.py, which covers:

  • Standard nested unpacking (a, (b, c) = tup)
  • Deeply nested patterns (a, (b, (c, d)) = tup)
  • Multiple nested groups in a single line ((a, b), (c, d) = tup)

I've verified that all 9 new test cases pass across the interp, doppler, and C execution modes, and I also checked for regressions in the existing tuple tests.

Looking forward to your feedback!

Copy link
Copy Markdown
Member

@antocuni antocuni left a comment

Choose a reason for hiding this comment

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

thank you @Deadpool2000.
The PR looks mostly good but it lacks many tests, see the inline comments.

Can I ask you how did you find this issue to work on?
Also, can you please confirm that you didn't use any AI code assistant as I requested?

Comment thread spy/parser.py
Comment thread reproduce_issue.spy
Comment thread test_side_effects.spy
Comment thread spy/tests/compiler/test_nested_unpack.py Outdated
Comment thread spy/backend/spy.py
Comment thread spy/backend/c/cwriter.py Outdated
Comment thread spy/backend/c/cwriter.py Outdated
Comment thread spy/backend/c/cwriter.py Outdated
Comment thread spy/analyze/scope.py
Comment thread spy/analyze/scope.py Outdated
@Deadpool2000
Copy link
Copy Markdown
Author

Hey @antocuni ! I've gone through all your suggestions and cleaned up the implementation. Here’s a breakdown of what I've done:

  • Parser & Tests: I added more robust tests to spy/tests/test_parser.py covering both valid nested unpacking and error cases (like unsupported complex targets).
  • Cleanup: Removed those temporary reproduction scripts I left behind.
  • Consolidating Tests: Migrated the compiler tests for nested unpacking into spy/tests/compiler/test_tuple.py and deleted the old standalone test file to keep things tidy.
  • AST Refinement: Updated the UnpackAssign targets to use the more specific list[StrConst | Tuple] type as you suggested.
  • Scope Analysis: Cleaned up the redundant ast.Name checks in scope.py and added proper unit tests for nested scoping in spy/tests/test_scope.py.
  • Backends:
    • In the SPy backend, I added support for nested unpacking emission and verified it with tests in test_backend_spy.py.
    • In cwriter.py, I removed the unnecessary ast.Name checks and restored those explanatory comments you pointed out.

To answer your questions: I found this issue while browsing the repository looking for ways to contribute to the compiler's tuple support.

Regarding the AI assistant - I did use one to help me speed up the generation of some of the more repetitive unit tests and boilerplate code once I had the core logic figured out. I've manually reviewed and refined everything to ensure it follow the project's style and logic, though.

Let me know if there’s anything else!

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.

2 participants