You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve error message when trying to delete builtin #138890
changed the title [-][enchancement] Fix error message when trying to delete builtin[/-][+]Improve error message when trying to delete builtin[/+]on Sep 17, 2025
Not sure about the wording here, since it's not entirely true - you can delete builtins from the module, just not via global scopes. On the other hand it's probably not the best to encourage people to delete them.
I don't see a real issue here and think that this could be closed.
As documented, del only deletes name in the current local namespace (unless the name is declared global).
Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block. Trying to delete an unbound name raises a NameError exception.
At global scope, the global namespace is the local namespace. The builtins namespace is separate from globals. Beginners should be taught this, along with the automagic lookup behavior.
To delete from builtins, one must import the module. import builtins; delete builtins.all. (I not sure this should be advertised.)
Perhaps 'unbound name' could be expanded to 'name not bound in the appropriate namespace', but it is not completely unreasonable to expect people to understand this.
Discourse thread proposed a change to all name error messages. The more restrained proposal here looks better to me, but I propose even less change on the PR.
Still, the premise of this issue is that the user really meant to delete the builtin all rather than something else. Consider
ale = 3
del all
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
del all
NameError: name 'all' is not defined. Did you mean: 'ale'?
The current message is correct and gives a helpful hint. The PR will degrade it to something irrelevant.
The current message is correct and gives a helpful hint. The PR will degrade it to something irrelevant.
However, the next isn't:
Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit (AMD64)] on win32
>>> del next
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
del next
^^^^
NameError: name 'next' is not defined. Did you mean: 'anext'?
Maybe it need an attribute to distinguish whether the NameError raised from getting or deleting to decide whether add f_builtins to the list of possible suggestion.
Activity
Tapeline commentedon Sep 14, 2025
We could add a clearer error message, e.g.:
StanFromIreland commentedon Sep 14, 2025
Prior art: in b86ed8e a range of cases was covered, e.g.:
sergey-miryanov commentedon Sep 14, 2025
Hm, I'm not sure it should be handled while parsing. Or I don't get what you mean.
StanFromIreland commentedon Sep 14, 2025
It is currently handled in the parser,
cpython/Parser/pegen.h
Lines 238 to 258 in 8b5ce31
sergey-miryanov commentedon Sep 14, 2025
Why are you talking about literals? Builtin
allis not a literal. And following is a valid Python code:So
allis not an invalid target. Or I still not get you, sorry.StanFromIreland commentedon Sep 14, 2025
I am talking about the existing improved cases.
sergey-miryanov commentedon Sep 14, 2025
Ough, ok. Thanks!
StanFromIreland commentedon Sep 14, 2025
Anyhow, a quick patch:
Though I worry about performance cost.
sobolevn commentedon Sep 14, 2025
I think that we can't do that in compiler, because users can shadow buitlins:
This must continue to work.
DELETE_NAMEis the most probable place for this.Tapeline commentedon Sep 15, 2025
Maybe the best place to put this check is in err ≠ 0 block of DELETE_NAME instruction:
sergey-miryanov commentedon Sep 15, 2025
You also have to do same check in
DELETE_GLOBALandDELETE_FAST.Tapeline commentedon Sep 17, 2025
I'd like to solve this issue :)
sergey-miryanov commentedon Sep 17, 2025
Yeah, go ahead. Feel free to ask if you have some questions!
[-][enchancement] Fix error message when trying to delete builtin[/-][+]Improve error message when trying to delete builtin[/+]TeamSpen210 commentedon Oct 14, 2025
Not sure about the wording here, since it's not entirely true - you can delete builtins from the module, just not via global scopes. On the other hand it's probably not the best to encourage people to delete them.
terryjreedy commentedon Oct 27, 2025
I don't see a real issue here and think that this could be closed.
As documented,
delonly deletes name in the current local namespace (unless the name is declared global).At global scope, the global namespace is the local namespace. The builtins namespace is separate from globals. Beginners should be taught this, along with the automagic lookup behavior.
To delete from builtins, one must import the module.
import builtins; delete builtins.all. (I not sure this should be advertised.)Perhaps 'unbound name' could be expanded to 'name not bound in the appropriate namespace', but it is not completely unreasonable to expect people to understand this.
terryjreedy commentedon Oct 27, 2025
Discourse thread proposed a change to all name error messages. The more restrained proposal here looks better to me, but I propose even less change on the PR.
terryjreedy commentedon Oct 27, 2025
Still, the premise of this issue is that the user really meant to delete the builtin
allrather than something else. ConsiderThe current message is correct and gives a helpful hint. The PR will degrade it to something irrelevant.
Tapeline commentedon Oct 29, 2025
I have checked, surprisingly, it does not:
Locked-chess-official commentedon Oct 30, 2025
However, the next isn't:
Maybe it need an attribute to distinguish whether the NameError raised from getting or deleting to decide whether add
f_builtinsto the list of possible suggestion.merwok commentedon Jul 8, 2026
I agree with Terry, the current message is technically correct and clear enough.