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: Python should warn when a global/local has the same name as a builtin
Type: Stage: resolved
Components: Interpreter Core Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.smith, reuven
Priority: normal Keywords:

Created on 2020-01-05 22:18 by reuven, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg359384 - (view) Author: Reuven Lerner (reuven) Date: 2020-01-05 22:18
Newcomers to Python are often frustrated and surprised when they define variables such as "sum" and "list", only to discover that they've masked access builtins of the same name.

External code checkers do help, but those don't work in Jupyter or other non-IDE environments.

It would be nice if defining a global/local with the same name as a builtin would generate a warning.  For example:

list = [10, 20, 30]
RedefinedBuiltinWarning: "list" is a builtin, and should normally not be redefined. 

I'm sure that the wording could use a lot of work, but something like this would do wonders to help newbies, who encounter this all the time. Experienced developers are surprised that these terms aren't reserved words.
msg359389 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-01-05 22:52
On the other hand, this would generate a lot of warnings for users of libraries, who aren't able to do anything about it. I've got tons of existing code that uses the names of builtins as locals, to no harm. And as builtins are added, new warnings would pop up.

A linter is really the best way to handle this. Maybe Jupyter could add its own warning.
msg359445 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-01-06 18:18
I agree with Eric that this is the realm of linters as there are legitimate reasons sometimes to mask and redefine built-ins.

Thanks for the idea, Reuven, but I'm closing the issue.
History
Date User Action Args
2022-04-11 14:59:24adminsetgithub: 83406
2020-01-06 18:18:32brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg359445

resolution: rejected
stage: resolved
2020-01-05 22:52:47eric.smithsetnosy: + eric.smith
messages: + msg359389
2020-01-05 22:18:09reuvencreate