Skip to content

fix: .over was mutating expressions in some cases#3638

Merged
MarcoGorelli merged 3 commits into
narwhals-dev:mainfrom
MarcoGorelli:fix-expr-mutability
May 19, 2026
Merged

fix: .over was mutating expressions in some cases#3638
MarcoGorelli merged 3 commits into
narwhals-dev:mainfrom
MarcoGorelli:fix-expr-mutability

Conversation

@MarcoGorelli
Copy link
Copy Markdown
Member

thanks @dangotbanned for spotting this on discord https://discord.com/channels/1235257048170762310/1272835922924273694/1492563536696578248

Description

What type of PR is this? (check all applicable)

  • 💾 Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🔧 Optimization
  • 📝 Documentation
  • ✅ Test
  • 🐳 Other

Related issues

  • Related issue #<issue number>
  • Closes #<issue number>

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

Comment thread src/narwhals/expr.py
Comment on lines -66 to +67
new_nodes = list(self._nodes)
new_nodes = deepcopy(list(self._nodes))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

based on the name, it looks like i was under the impression that list would copy the tuple self._nodes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That is usually enough when trying to avoid the dictionary changed size during iteration issue - which is probably more common to run into

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

not sure what you mean here sorry

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I meant that if self._nodes were a dict, then the code you wrote would have avoided an error related to mutation.

I was trying to say that it wasn't unreasonable to expect it would work for tuple too 🙂

@dangotbanned
Copy link
Copy Markdown
Member

Thanks for following up on this @MarcoGorelli

Since I kinda fell into this one by chance, do you think there might be any other places since introducing ExprNode that may have a similar bug?

when could be another one to try out?

@MarcoGorelli MarcoGorelli marked this pull request as ready for review May 17, 2026 11:00
@MarcoGorelli
Copy link
Copy Markdown
Member Author

sure, added a test for when/then

Copy link
Copy Markdown
Member

@dangotbanned dangotbanned left a comment

Choose a reason for hiding this comment

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

Thanks @MarcoGorelli

squeeze cheeks narwhal

@MarcoGorelli MarcoGorelli merged commit 7eaa875 into narwhals-dev:main May 19, 2026
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants