Skip to content
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

Conditional import fails and produce UnboundLocalError, if a variable machting import name is used before #89457

Closed
stars-of-stras mannequin opened this issue Sep 26, 2021 · 5 comments
Labels
3.9 only security fixes

Comments

@stars-of-stras
Copy link
Mannequin

stars-of-stras mannequin commented Sep 26, 2021

BPO 45294
Nosy @stevendaprano

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-09-26.15:59:21.127>
created_at = <Date 2021-09-26.15:25:13.279>
labels = ['invalid', '3.9']
title = 'Conditional import fails and produce UnboundLocalError, if a variable machting import name is used before'
updated_at = <Date 2021-09-27.14:44:24.673>
user = 'https://bugs.python.org/stars-of-stras'

bugs.python.org fields:

activity = <Date 2021-09-27.14:44:24.673>
actor = 'steven.daprano'
assignee = 'none'
closed = True
closed_date = <Date 2021-09-26.15:59:21.127>
closer = 'steven.daprano'
components = []
creation = <Date 2021-09-26.15:25:13.279>
creator = 'stars-of-stras'
dependencies = []
files = []
hgrepos = []
issue_num = 45294
keywords = []
message_count = 5.0
messages = ['402663', '402665', '402666', '402716', '402723']
nosy_count = 2.0
nosy_names = ['steven.daprano', 'stars-of-stras']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue45294'
versions = ['Python 3.9']

@stars-of-stras
Copy link
Mannequin Author

stars-of-stras mannequin commented Sep 26, 2021

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

@stars-of-stras stars-of-stras mannequin added 3.9 only security fixes labels Sep 26, 2021
@stevendaprano
Copy link
Member

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.

@stevendaprano
Copy link
Member

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/

@stars-of-stras
Copy link
Mannequin Author

stars-of-stras mannequin commented Sep 27, 2021

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.

@stevendaprano
Copy link
Member

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes
Projects
None yet
Development

No branches or pull requests

1 participant