David Wilson <d...@botanicus.net> added the comment:

Having considered this for a few hours, it seems the following is clear:

- 3.6 introduces ModuleImportError
- 3.7 begins using it within importlib
- 3.8 first instance of stdlib code catching it

- PEP-302 and PEP-451 are the definitive specifications for how sys.meta_path 
is supposed to work, and neither makes mention of ModuleNotFoundError. It seems 
at least this should have been flagged during review of the original change, 
but apparently the name of the exception was more important.

- The recent work done to move most of the import machinery on to sys.meta_path 
has exposed a set of import hooks that don't quite comply to the documented 
import hook interface

- The newly advertised ModuleNotFoundError appearing in stack traces for 
everyone means that more people will continue to write new cases of "except 
ModuleNotFoundError:", which while following best practice (catch most specific 
relevant exception), at present it amounts to relying on an implementation 
detail of the default importer. GitHub search reveals this to be an accurate 
reading: https://github.com/search?q=%22except+ModuleNotFoundError%22&type=Code

Therefore we are in a position where:

- Natural developer habit will cause much more protocol-violating code to exist 
over time, there is no option to stop this process
- New import hooks written against the existing documentation will break in the 
face of developer habit
- Existing import hooks were broken in Python 3.6 and this is not documented 
anywhere
- Python's own import machinery contravenes its specification

Options:

- Updating PEP-302 to mention introduction of the new exception type
- Updating ModuleNotFoundError/ImportError documentation to somehow capture the 
compatibility issue

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue35486>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to