This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Conditional import fails and produce UnboundLocalError, if a variable machting import name is used before
Type: Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: stars-of-stras, steven.daprano
Priority: normal Keywords:

Created on 2021-09-26 15:25 by stars-of-stras, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg402663 - (view) Author: arts stars (stars-of-stras) Date: 2021-09-26 15:25
Hello,

I have this situation:
----------------
def test():
    
    if True :
        print("Exception"+DiaObjectFactoryHelper)
    else:
        from . import DiaObjectFactoryHelper
        pass
        
test()
---------------
it generates instead of (like the the line 'from . import' is commented) : 
"NameError: name 'DiaObjectFactoryHelper' is not defined"
this:
UnboundLocalError: local variable 'DiaObjectFactoryHelper' referenced before assignment


PS: The github authentificatiion did not work, did not manage to grab email even if set public
msg402665 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-09-26 15:59
This is not a bug, you are trying to print the value of the local variable DiaObjectFactoryHelper before you have defined the variable.

UnboundLocalError is a subclass of NameError that is used when the undefined name is a local variable instead of a global.
msg402666 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-09-26 16:05
In the future, please remember that this is not a help desk for getting help with your code, it is for reporting bugs. There are many forums that are happy to help you understand why your code is not working as you expect (if you are a beginner to Python, the answer is *almost never* "a bug in Python").

You can find help at Reddit's r/learnpython, or at the forums and mailing lists here:

https://www.python.org/community/forums/

https://www.python.org/community/lists/
msg402716 - (view) Author: arts stars (stars-of-stras) Date: 2021-09-27 14:09
It is not for debuging my code. your answer is really middleclass sorry to say that.

you admit its a due the undefined name of var DiaObjectFactoryHelper
that is exactly I wanted to point out. 

I solved my issue not having answer like in the FAQ like you simply repeated here.

This is a parsing issue error I revealed here. The if... should pe PRIORITARIZED before checking what it is said in the FAQ.
msg402723 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-09-27 14:44
I'm glad that you fixed the bug in your code, but this is not a bug in 
Python. It isn't "a parsing issue", the code is parsed fine. What you 
did was no different from:

    def func():
        print(x)
        x = 1

except that the binding operation was an import, not an assignment. You 
get exactly the same error here:

    def func():
        print(math)
        import math

That's the way the language is defined. Imports and assignments are both 
binding operations and are treated the same by the interpreter. It's not 
an accident or a mistake or a parsing issue or a bug, it is the way the 
language is supposed to work.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89457
2021-09-27 14:44:24steven.dapranosetmessages: + msg402723
2021-09-27 14:09:33stars-of-strassetmessages: + msg402716
2021-09-26 16:05:58steven.dapranosetmessages: + msg402666
2021-09-26 15:59:21steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg402665

resolution: not a bug
stage: resolved
2021-09-26 15:25:13stars-of-strascreate