pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

Functions defined inside if blocks don't do variable lookup correctly

Open LeszekSwirski opened this issue 2 years ago • 4 comments

If I define an inner function inside an if block, it fails to look up values that it closes over, doing e.g. a global lookup instead:

value = 4
def make_func(value):
    if value == 0:
        def inner():
            log.info(f"value = {value}")
        return inner

make_func(0)()
---
value = 4

Interestingly, however, if there is an inner function defined outside the if block, the one inside the if block works as it should.

value = 4
def make_func(value):
    if value == 0:
        def inner():
            log.info(f"value = {value}")
        return inner
    
    def fix():
        pass

make_func(0)()
---
value = 0

LeszekSwirski avatar Nov 10 '21 14:11 LeszekSwirski

This is a side effect of the way function definitions work in pyscript. Under def inner you can try adding nonlocal value. I've had some luck with this.

dlashua avatar Nov 10 '21 23:11 dlashua

Nope:

value = 4
def make_func(value):
    if value == 0:
        def inner():
            nonlocal value
            log.info(f"value = {value}")
        return inner
    
make_func(0)()
---
value = 4

LeszekSwirski avatar Nov 11 '21 10:11 LeszekSwirski

I have tested this myself (and every variation I can think of). It is indeed broken. Hopefully @craigbarratt can fix it.

dlashua avatar Nov 11 '21 12:11 dlashua

Yikes. It doesn't correctly detect inner functions inside nested statements, which then means the correct variable scoping isn't applied. I pushed a fix f4f32f4.

A workaround for the existing code is to define a dummy inner function at the top-level, eg:

value = 4
def make_func(value):
    if 1:
        def real_inner():
            log.info(f"value = {value}")
        return real_inner
    def dummy_inner():
       pass

craigbarratt avatar Nov 11 '21 18:11 craigbarratt

@LeszekSwirski Has your problem been fixed? Could you please update the ticket or close it? Thank you

ALERTua avatar Sep 25 '23 07:09 ALERTua

Honestly, I still just have the workaround in my own config, but from the looks of it it's fixed.

LeszekSwirski avatar Sep 25 '23 08:09 LeszekSwirski