Skip to content

Add Py3k warning for list comprehension target rebinding and regression test#45

Merged
ltratt merged 3 commits intosoftdevteam:migrationfrom
Eddy114514:listcomp_new
Mar 12, 2026
Merged

Add Py3k warning for list comprehension target rebinding and regression test#45
ltratt merged 3 commits intosoftdevteam:migrationfrom
Eddy114514:listcomp_new

Conversation

@Eddy114514
Copy link
Collaborator

List comprehensions that rebind an outer name (e.g. x in [x = ...; [x for x in ...]] can cause 2.x vs 3.x behavior differences. Specifically, in python 2, it will rebind the outer variable, but in 3.x, it will not.

I added a symbol-table check in symtable.c to detect listcomp target rebinding and emit the warning. I add test in test_grammer.py and it passed on my local machine.

return 1;
}

static int
Copy link
Member

Choose a reason for hiding this comment

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

AFAICS the return int is either 0 or 1 so a bool in disguise? I'm not sure what true/false mean here though: there was/wasn't an error?

@Eddy114514
Copy link
Collaborator Author

Eddy114514 commented Mar 12, 2026

I followed the convention of symtable_warn , where the return value indicates whether the check completed successfully or hit an error, not whether a warning was found. So symtable_warn_listcomp_target() and symtable_warn_listcomp_shadowing() return 1 if analysis can continue and 0 only if fail at issuing warning or a recursive check fails.

My comments shows "pending" so it wasn't visible to you as the picture shown below:
image

@ltratt

@ltratt
Copy link
Member

ltratt commented Mar 12, 2026

Got it, thanks! [Pending means I didn't see the original comment, alas!].

@ltratt ltratt added this pull request to the merge queue Mar 12, 2026
Merged via the queue into softdevteam:migration with commit 723e0de Mar 12, 2026
2 checks passed
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