classification
Title: Misleading error message: global name 'X' is not defined
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Jeremy.Hylton, Ramchandra Apte, cool-RR, eric.araujo, ezio.melotti, ncoghlan, pjenvey, python-dev, terry.reedy
Priority: normal Keywords: patch

Created on 2013-01-25 14:42 by cool-RR, last changed 2013-03-03 13:19 by cool-RR. This issue is now closed.

Files
File name Uploaded Description Edit
cpython_patch1of1_8e9346e7ae87.patch cool-RR, 2013-03-02 15:46 review
Messages (19)
msg180591 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-01-25 14:42
Every single time I see the error message `global name 'X' is not defined` I say to myself, "ah yeah, I mistyped a variable name."

But then it occurred to me, why should I have to do this mental translation from "global name not defined" to "I mistyped a variable name"?

Now, I'm not asking for the error message to say "You mistyped a variable name", because that goes too much into second-guessing the user. But can we at least drop the reference to a global name? I understand that Python first searches the local namespace, and only then in the global namespace, and if the name isn't found there then an error is raised. But that doesn't mean that the error message should just assume that the variable is supposed to be global. It's misleading to say that a variable is global when it's in fact local.

I think that the error message should just say `Variable 'X' not defined`. Maybe add a suggestion `(Typo?)` at the end.


Example:

    >>> def f():
    ...     meow = 0
    ...     return meoow
    ...
    >>> f()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 3, in f
    NameError: global name 'meoow' is not defined

I'd make the error message:

    NameError: Variable 'meoow' is not defined
msg180592 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-25 14:44
Maybe we could just drop the 'global'.
msg180593 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2013-01-25 14:58
+1 on Ezio's idea.
msg180615 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-01-25 19:41
Dropping global sounds good.  (Let’s not replace name with variable though!)
msg181081 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-02-01 14:47
Does fixing this ticket require anything more than making a change in the string that Python uses for this exception?
msg181693 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-02-08 20:16
I made a patch. Is it okay? (I don't normally use Mercurial nor work with patches.)
msg181694 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-02-08 20:17
Patch looks good.  Does the test suite still pass?
msg181695 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-02-08 20:18
I don't program C at all. I have no idea how to compile Python or run the test suite. It took me half an hour just to produce this patch.
msg181696 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2013-02-08 20:20
If you’re using a programmer-friendly OS compiling is not hard.  See http://docs.python.org/devguide for instructions.  Otherwise somebody else will test the patch.  Thanks for your contribution!
msg181697 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-02-08 20:26
I think I'll go for option 2, thanks.
msg182424 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-02-19 20:20
GLOBAL_NAME_ERROR_MSG has been introduced in fd8c7203251f as part of PEP 227 by Jeremy Hylton, so I'm adding him to the nosy to see if he agrees with the change (also adding a couple more devs to see if they have any comments).

There's also a typo in the last chunk of the patch.
msg182470 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-02-20 05:08
+    NAME_ERROR_MSGy, name);
will give a NameError ;-).

The patch undoes the change from 'name' to 'global name'. Skimming the PEP, I do not see this change mandated or justified by the PEP. So my current view: while it is true that it is the final, global name lookup that fails, I agree with Ram that it is not necessarily a global name that was mistyped or is missing. So in absence of better justification of the status quo, I would apply the patch. Backport to 3.3 would be ok but not necessary, as the message is not nearly as wrong as some have been.
msg183324 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-03-02 15:42
Was the patch applied? Is there any reason why it should not be applied?
msg183326 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-03-02 15:44
Not yet, I wanted to make sure that everyone agrees on the change.
If I don't get other replies I'll commit it soon.
msg183327 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-03-02 15:46
(I fixed the patch to not have a typo.)
msg183346 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013-03-02 20:03
I just opened #17339 about another situation where the error message references only the last of several checks that failed.

bytes(object())  # raises
TypeError: 'object' object is not iterable  # or 4 other possibilities

I think any message like this should be fixed as noticed.
msg183369 - (view) Author: Roundup Robot (python-dev) Date: 2013-03-03 13:13
New changeset f1d3fbcd837d by Ezio Melotti in branch 'default':
#17032: The "global" in the "NameError: global name 'x' is not defined" error message has been removed.  Patch by Ram Rachum.
http://hg.python.org/cpython/rev/f1d3fbcd837d
msg183370 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-03-03 13:14
Fixed, thanks for the patch!
msg183372 - (view) Author: Ram Rachum (cool-RR) * Date: 2013-03-03 13:19
Awesome, thanks!
History
Date User Action Args
2013-03-03 13:19:09cool-RRsetmessages: + msg183372
2013-03-03 13:14:07ezio.melottisetstatus: open -> closed
messages: + msg183370

assignee: ezio.melotti
resolution: fixed
stage: commit review -> resolved
2013-03-03 13:13:16python-devsetnosy: + python-dev
messages: + msg183369
2013-03-02 20:03:23terry.reedysetmessages: + msg183346
2013-03-02 19:20:14terry.reedysetstage: patch review -> commit review
2013-03-02 15:46:26cool-RRsetfiles: - cpython_patch1of1_8e9346e7ae87.patch
2013-03-02 15:46:03cool-RRsetfiles: + cpython_patch1of1_8e9346e7ae87.patch

messages: + msg183327
2013-03-02 15:44:26ezio.melottisetmessages: + msg183326
stage: patch review
2013-03-02 15:42:45cool-RRsetmessages: + msg183324
2013-02-20 05:08:56terry.reedysetmessages: + msg182470
2013-02-19 20:39:59pjenveysetnosy: + pjenvey
2013-02-19 20:20:39ezio.melottisetnosy: + terry.reedy, ncoghlan, Jeremy.Hylton
messages: + msg182424
2013-02-08 20:26:42cool-RRsetmessages: + msg181697
2013-02-08 20:20:44eric.araujosetmessages: + msg181696
2013-02-08 20:18:56cool-RRsetmessages: + msg181695
2013-02-08 20:17:22eric.araujosetmessages: + msg181694
2013-02-08 20:16:01cool-RRsetfiles: + cpython_patch1of1_8e9346e7ae87.patch
keywords: + patch
messages: + msg181693
2013-02-01 14:47:38cool-RRsetmessages: + msg181081
2013-01-25 19:41:04eric.araujosetnosy: + eric.araujo

messages: + msg180615
versions: - Python 3.5
2013-01-25 14:58:19Ramchandra Aptesetnosy: + Ramchandra Apte
messages: + msg180593
2013-01-25 14:44:51ezio.melottisetnosy: + ezio.melotti
messages: + msg180592
2013-01-25 14:42:54cool-RRcreate