Any() not searching in locals in debugger environment


I don’t understand if this is a bug, a feature, or me just misunderstanding something:

(Pdb) pp excluded_nodes
(Pdb) pp any(parent in excluded_nodes for parent in src_node.parents)
*** NameError: name 'excluded_nodes' is not defined
(Pdb) pp "excluded_nodes" in locals()

Why excluded_nodes is not found from inside the any() call?


How to reproduce?

$ python
Python 3.11.2 (main, Feb  8 2023, 00:00:00) [GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...     lst = [1, 2, 3]
...     breakpoint()
>>> f()
> <stdin>(3)f()->None
(Pdb) pp lst
[1, 2, 3]
(Pdb) pp any(x == 2 for x in lst)
(Pdb) pp "lst" in locals()

It’s because you are using a generator expression inside the any() call, and genexps and comprehensions are compiled as nested functions. More details at list comprehensions don't see local variables in pdb in python3 · Issue #65360 · python/cpython · GitHub

PEP 709 would fix this for list/dict/set comprehensions, and potentially in the future for non-escaping genexps (as in your example) too.

1 Like

To reproduce, you need to reference the outer variable in the value or if clause of the comprehension, not just as the iterated sequence.

Awesome, thanks! Subscribed to that bug.

:man_facepalming: Sorry for not seeing the obvious.