[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread Stéphane Wirtel
Stéphane Wirtel added the comment: Hi Steven, Sure, I am going to explain, I was at the social event of PyCon Germany without my laptop and the code was not really clear on my smartphone. I wanted to know if there was an indentation issue. It's also the reason for my big reply with the

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread James Hewitt
James Hewitt added the comment: So just the fact that somewhere in the function a name is referenced, even if that code isn't actually executed, is enough to change the local namespace. I think I knew that, but didn't know that's what it meant :) I guess the moral is, pay attention to

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread Steven D'Aprano
Steven D'Aprano added the comment: Yes, that's exactly right. That's how local variables work in Python: x = 999 # global x def demo(): if False: x = 1 x # local x has no value does the same thing. This is standard, documented behaviour, regardless of which kind of assignment

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread James Hewitt
James Hewitt added the comment: I don't quite follow... the 'import logging.config' statement should never be executed, and if it is commented out the program works fine as written. It's as if the mere presence of the statement in the code causes 'logging' to be shadowed inside the

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread Steven D'Aprano
Steven D'Aprano added the comment: Stéphane, I'm curious why you asked for a pastebin when James already provided the code right here in the tracker? (Your email even included a copy of that code.) Why split the information into another website? --

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread Steven D'Aprano
Steven D'Aprano added the comment: This is expected behaviour: import is a form of assignment. "import logging", like "logging = 1", tells the compiler to treat logging as a local variable (unless you declare logging as global). As the exception says, you are trying to access the logging

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread James Hewitt
James Hewitt added the comment: Sure, it's at https://pastebin.com/L1RMPD7K -James On 10/25/2018 12:30 PM, Stéphane Wirtel wrote: > > Stéphane Wirtel added the comment: > > Could you share a pastebin? Thank you > >> Le 25 oct. 2018 à 21:28, James Hewitt a écrit : >> >> >> New submission

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread Stéphane Wirtel
Stéphane Wirtel added the comment: Could you share a pastebin? Thank you > Le 25 oct. 2018 à 21:28, James Hewitt a écrit : > > > New submission from James Hewitt : > > Having 'import logging.config' in an if statement in a function causes a > namespace issue, despite the fact that the

[issue35069] Unexecuted import in function causes UnboundLocalError

2018-10-25 Thread James Hewitt
New submission from James Hewitt : Having 'import logging.config' in an if statement in a function causes a namespace issue, despite the fact that the import is not reached. Example code: --- #!/usr/bin/env python3 # Test weird import bug import logging config = {} config['log'] = {}