On Fri, Aug 21, 2020, at 15:13, Gustav O wrote:
> In the beginning of the programming journey, getting a message about 
> circular imports when they were testing out tkinter in a file names 
> "tkinter.py" would probably not be helpful. Getting a message that 
> hints that you may have accidentally imported itself, however, will 
> probably help significantly.
> 
> Maybe an error like this would be better, even though the wording could 
> be worked on:
> "AttributeError: partially initialized module 'tkinter' has no 
> attribute 'Tk' (most likely
> due to trying to import the file that is being executed)"

Detecting being partially loaded won't be enough for the shadowed file case, 
consider this minimal example of a file named tkinter.py:

import tkinter

if name == '__main__':
    root = tkinter.Tk()

This file is loaded twice, once as __main__ and once as tkinter. The one that 
is loaded as tkinter and imported is *not* partially initialized, it finishes 
initializing with no errors.

It may be necessary to do more introspection (does the import machinery have a 
way to see if a module name exists twice on the path?) to determine when an 
AttributeError is caused by shadowing. It can also happen deep inside stdlib 
code, if one module whose name is not shadowed imports a shadowed module.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YJYJBRSUGBZ4MJCFP56OE34TCGHXEGYX/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to