hylang / hy

A dialect of Lisp that's embedded in Python
http://hylang.org
Other
5.14k stars 372 forks source link

Surprising statement pull-out in multi-item `with` #2605

Closed Kodiologist closed 2 months ago

Kodiologist commented 2 months ago

Consider:

(import
  contextlib [nullcontext])

(with [a (nullcontext)  b (do a (nullcontext))])

This raises NameError: name 'a' is not defined. How this happens is clear enough to the experienced Hy programmer, but that it happens at all can be surprising considering that e.g.

(for [a [1]  b (do a [1])])

works fine. And the Python language reference specifies that

With more than one item, the context managers are processed as if multiple with statements were nested:

with A() as a, B() as b:
    SUITE

is semantically equivalent to:

with A() as a:
    with B() as b:
        SUITE

So it's surprising that this equivalency doesn't hold for Hy.