python / python/cpython

Improve suggestion in NameError when deleting builtins

Open
#153,374 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

When deleting a name, python will only find the name in globals and locals, no builtins. However, the suggestion generating code just combined these dict, which makes the suggestion cycle:

>>> 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'?
>>> del anext
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    del anext
        ^^^^^
NameError: name 'anext' is not defined. Did you mean: 'next'?

According to DPO, the best way to fix this bug is using dis to analyse the last traceback to get the final operation.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-141389

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with del next and del anext, then inspect the NameError suggestion-generation path and use dis to identify the final operation from the traceback. Compare the approach with linked PR gh-141389; done means deletion no longer suggests the deleted builtin as an alternative.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.