Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
LeszekSwirski opened this issue Nov 10, 2021 · 6 comments
Closed

Comments

@LeszekSwirski
Copy link

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
@dlashua
Copy link
Contributor

dlashua commented Nov 10, 2021

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.

@LeszekSwirski
Copy link
Author

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

@dlashua
Copy link
Contributor

dlashua commented Nov 11, 2021

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

@craigbarratt
Copy link
Member

craigbarratt commented Nov 11, 2021

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

@ALERTua
Copy link
Contributor

ALERTua commented Sep 25, 2023

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

@LeszekSwirski
Copy link
Author

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

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

No branches or pull requests

4 participants