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: Teach "import *" to warn when overwriting globals or builtins
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Windson Yang, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2019-04-30 22:31 by rhettinger, last changed 2022-04-11 14:59 by admin.

Messages (5)
msg341166 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-04-30 22:31
One reason we usually suggest that people don't use star imports is that it is too easy to shadow a builtin or overwrite an existing global.  Momma Gump always used to say, "import star is like a box of chocolates, you never know what you've going to get".

>>> from os import *
Warning (from warnings module):
  File "__main__", line 1
ImportWarning: The 'open' variable in the 'os' module shadows a variable in the 'builtins' module

>>> alpha = 2.0
>>> beta = 3.0
>>> gamma = 4.5
>>> delta = 5.5
>>> from math import *
>>> from os import *
Warning (from warnings module):
  File "__main__", line 8
ImportWarning: The 'gamma' variable in the 'math' overwrites an existing variable in the globals.
msg341229 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-01 19:23
What if existing globals are overwritten intentionally? For example, the stat module defines some constant and then use the star import from _stat. Other example is the datetime module.
msg341237 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-05-02 00:33
> What if existing globals are overwritten intentionally?

That's why it would be a warning instead of an error.  Also, with warnings we can suppress specific instances (by module or by message).
msg341243 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-05-02 01:26
Another question will be are we going to replace the * in our source code in the future? Since I found lots of our code use 'from xxx import *' pattern.
msg341247 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-02 04:56
This will force legal third-party code to add special code to suppress warnings. This is breaking change.

Other problem is with shadowing builtins. This increases chance of breakage when introduce new builtins. Currently adding a new builtin does not affect existing code which does not use this builting, but all will be changed if implement this feature.

Since shadowing a builtin and overwriting an existing global is not always an error, I think this warning should not be the part of the interpreter, but rather a part of third-party linters (which can be configured to ignore some warnings for particular projects).
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 80943
2019-05-02 04:56:17serhiy.storchakasetmessages: + msg341247
2019-05-02 01:26:32Windson Yangsetnosy: + Windson Yang
messages: + msg341243
2019-05-02 00:33:37rhettingersetmessages: + msg341237
2019-05-01 19:23:42serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg341229
2019-04-30 22:31:42rhettingercreate