Closed JulienPalard closed 3 years ago
Yay, an easy to implement and useful new explanation to add! :-)
I already have an alternative to identify importable modules: https://github.com/aroberge/friendly/blob/master/friendly/runtime_errors/stdlib_modules.py It might not cover absolutely everything, but it should be good enough. It definitely includes this case.
I currently only use it for ModuleNotFoundError (https://github.com/aroberge/friendly/blob/master/friendly/runtime_errors/module_not_found_error.py) but it would definitely make sense to use this for NameError cases like you report.
I agree about not running typo-corrections for this. My "philosophy" is to attempt to identify a single cause for an error. A missing import AND a typo are two different causes. Perhaps the only exception I would make is to check if changing the name to a lowercase version (Tkinter -> tkinter; there might be other) as some Python 2 modules were using uppercase letters.
Given that it is a module name, I think I will also require that the unknown name be followed by a dot if I am going to provide this information.
Thus:
... name.attribute .... # See if name is a known module
... name + something ... # Never suggest that 'name' could be a module
Fixed and new version uploaded to PyPI.
I will wait a bit before closing.
Hi André, you asked for more feedback, here's one:
Describe the solution you'd like
Here I think we can safely propose something like « You forgot to
import unicodedata
».Describe alternatives you've considered
To discover known importable modules I bet there's alternatives, like:
One is slower but list every available modules, the other one is faster but list only builtins modules... I'd go for the slow path, we're not in production, we're trying to help, better put all chances on our side to understand the problem.
We could also run typo-correction on all importable modules, but gut feeling say it's a bad idea: It'll always match some obscure module, from a far far sub-ependency, leading to obscure and wrong hints.