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: Syntax checking confuses Try: class_instance_name as ... is used before glabal declaration
Type: compile error Stage: resolved
Components: Interpreter Core, macOS Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Davidt, ned.deily, ronaldoussoren, steven.daprano
Priority: normal Keywords:

Created on 2017-07-10 00:18 by Davidt, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg298012 - (view) Author: David (Davidt) Date: 2017-07-10 00:18
when I do anything like this:

import flask ...



try:
    current_user
except NameError:
    global current_user
    current_user = User(request.form['parameter1'], request.form['parameter2'], '')


I get the error that the_user was 'used' before the global declaration. The try ... except ... is so when the user enters this route / function the first time, a class instance is instantiated, but if they return the second time, the existing class instance is updated in lines of code downstream rather than declared again with the same name, and therefore overwritten.
msg298013 - (view) Author: David (Davidt) Date: 2017-07-10 00:21
when I do anything like this:

import flask ...

class User......
.
.
.

try:
    current_user
except NameError:
    global current_user
    current_user = User(request.form['parameter1'], request.form['parameter2'], '')


I get the error that current_user_user was 'used' before the global declaration. The try ... except ... is so when the user enters this route / function the first time, a class instance is instantiated, but if they return the second time, the existing class instance is updated in lines of code downstream rather than declared again with the same name, and therefore overwritten.
msg298019 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-07-10 02:42
Please copy and paste (don't retype from memory!) the *exact* error you are getting.

You should be seeing something like:

SyntaxWarning: name 'current_user' is used prior to global declaration

(at least that's the warning I'm getting in Python 3.5), which is correct behaviour. The name is used prior to the global declaration.

The Python interpreter is now discouraging the use of the "global" keyword anywhere except at the top of the function. (It may some day become an error.)

If your code is unindented module-level code, as your code snippet suggests, you don't need the global declaration since current_user is automatically global.

If it is function-level code, then move the global declaration to the top of the function, as the SyntaxWarning suggests.

I don't believe this is a bug, I believe that what you are seeing is expected, so I am closing this bug report. If I have misunderstood what you are experiencing, then please re-open the ticket and give us some more information. Preferably give us some code that we can run that demonstrates the error. (Since the error doesn't have anything to do with flask, the `import flask` line is unnecessary.)
msg298020 - (view) Author: David (Davidt) Date: 2017-07-10 03:23
I made the syntax error go away by moving the global current_user above the
try......

I will have to revisit this one when time permits and write a script that
recreates the error. This will probably be later this week.

Thanks for your volunteer service.

David

On Jul 9, 2017 10:42 PM, "Steven D'Aprano" <report@bugs.python.org> wrote:

>
> Steven D'Aprano added the comment:
>
> Please copy and paste (don't retype from memory!) the *exact* error you
> are getting.
>
> You should be seeing something like:
>
> SyntaxWarning: name 'current_user' is used prior to global declaration
>
> (at least that's the warning I'm getting in Python 3.5), which is correct
> behaviour. The name is used prior to the global declaration.
>
> The Python interpreter is now discouraging the use of the "global" keyword
> anywhere except at the top of the function. (It may some day become an
> error.)
>
> If your code is unindented module-level code, as your code snippet
> suggests, you don't need the global declaration since current_user is
> automatically global.
>
> If it is function-level code, then move the global declaration to the top
> of the function, as the SyntaxWarning suggests.
>
> I don't believe this is a bug, I believe that what you are seeing is
> expected, so I am closing this bug report. If I have misunderstood what you
> are experiencing, then please re-open the ticket and give us some more
> information. Preferably give us some code that we can run that demonstrates
> the error. (Since the error doesn't have anything to do with flask, the
> `import flask` line is unnecessary.)
>
> ----------
> nosy: +steven.daprano
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue30887>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:58:48adminsetgithub: 75070
2017-07-10 03:23:35Davidtsetmessages: + msg298020
2017-07-10 02:43:37steven.dapranosetstatus: open -> closed
resolution: not a bug
stage: resolved
2017-07-10 02:42:51steven.dapranosetnosy: + steven.daprano
messages: + msg298019
2017-07-10 00:21:48Davidtsetmessages: + msg298013
2017-07-10 00:18:29Davidtcreate