← 返回首页
gh-87447: Fix walrus comprehension rebind checking by sobolevn · Pull Request #100581 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

gh-87447: Fix walrus comprehension rebind checking#100581

Merged
emilyemorehouse merged 8 commits into
python:mainfrom
sobolevn:issue-87447
Jan 8, 2023
Merged

gh-87447: Fix walrus comprehension rebind checking#100581
emilyemorehouse merged 8 commits into
python:mainfrom
sobolevn:issue-87447

Conversation

sobolevn commented Dec 28, 2022
edited by AlexWaygood
Loading

Copy link
Copy Markdown
Member

I went with the most straightforward approach. I am not very qualified in this module, so I would love to hear about any possible drawbacks this solution might have!

Copy link
Copy Markdown
Member Author

Sorry, I did not click on the second reviewer the first time! :(

hauntsaninja left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Thank you! I'm not very familiar with symtable, but left one comment.

I think it might also need a little more consensus that this is something that should be fixed; I tried to restart discussion in #87447 (comment)

Comment thread Python/symtable.c Outdated
long target_in_scope = _PyST_GetSymbol(ste, target_name);
if (target_in_scope & DEF_COMP_ITER) {
if ((target_in_scope & DEF_COMP_ITER) &&
(target_in_scope & (DEF_LOCAL | DEF_GLOBAL))) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Hmm, should this also check DEF_NONLOCAL?

Actually, looking closer, I think DEF_GLOBAL might be impossible, because we already check for it when setting DEF_COMP_ITER. Or at least, tests pass without checking DEF_GLOBAL here.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Yes, you are right.

Comment thread Python/symtable.c Outdated Show resolved Hide resolved

hauntsaninja left a comment
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Thanks! With the fix, the code seems reasonable to me, but I'm not comfortable enough with this module for that to mean much.

I think this merits a What's New entry, along the lines of https://docs.python.org/3/whatsnew/3.11.html#other-language-changes

Comment thread Python/symtable.c Show resolved Hide resolved
Comment thread Lib/test/test_named_expressions.py Outdated
exec(f"lambda: {code}", {}) # Function scope

def test_named_expression_valid_rebinding_list_comprehension_iteration_variable(self):
cases = [

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Can you add a comment at the start of the test explaining that we are cheching that anything that is not directly a comprehension iteration variable (a or b) can be assigned to?

Also, we probably should ensure ALL comprehensions are covered by the test (add another for loop that changes the brackets).

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Done! Except for dict comprehensions. Looks like they are not tested in test_named_expressions at all. I've opened a new task for it here: #100746

pablogsal left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Fantastic work @sobolevn!

Let's leave some time for @emilyemorehouse in case she wants to take a look and we can land it then. If this is not landed in a week or so, please ping me and I will land it.

Comment thread Doc/whatsnew/3.12.rst Outdated Show resolved Hide resolved
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Comment thread Doc/whatsnew/3.12.rst Outdated Show resolved Hide resolved
Comment thread Misc/NEWS.d/next/Core and Builtins/2022-12-28-15-02-53.gh-issue-87447.7-aekA.rst Outdated Show resolved Hide resolved

emilyemorehouse left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

The implementation of this looks great, thanks @sobolevn!

I went back and forth a bit on whether the underlying issue should be fixed in this way and ultimately agree – the current behavior is certainly confusing and while I'm not highly motivated by the use cases, it makes sense given that we allow the use of local variables in assignment expressions in other scenarios. I'm also in favor of leaving this as a change for 3.12 and not backporting.

Thanks, all!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

5 participants

Footer

© 2026 GitHub, Inc.