Any() not searching in locals in debugger environment

Hello!

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

(Pdb) pp excluded_nodes
{PosixPath('/tmp/pytest-of-facundo/pytest-7/test_copyproject_deeptree0/src/dir1/secret1'),
 PosixPath('/tmp/pytest-of-facundo/pytest-7/test_copyproject_deeptree0/src/dir2/cache'),
 PosixPath('/tmp/pytest-of-facundo/pytest-7/test_copyproject_deeptree0/src/dir2/secret2.txt')}
(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()
True

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

Thanks!

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()
--Return--
> <stdin>(3)f()->None
(Pdb) pp lst
[1, 2, 3]
(Pdb) pp any(x == 2 for x in lst)
True
(Pdb) pp "lst" in locals()
True

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.