aroberge / friendly

Aimed at Python beginners: replacing standard traceback by something easier to understand
https://friendly-traceback.github.io/docs/index.html
MIT License
325 stars 9 forks source link

NameError on missing stdlib imports #215

Closed JulienPalard closed 3 years ago

JulienPalard commented 3 years ago

Hi André, you asked for more feedback, here's one:

## Exception Python

    :::pytb
    Traceback (most recent call last):
      File "./check.py", line 166, in main
        response = is_anagram(a, b)
      File "solution.py", line 13, in is_anagram
        mot1= "".join([c for c in unicodedata.normalize("NFD", mot1) if not unicodedata.combining(c)])
    NameError: name 'unicodedata' is not defined

Une exception `NameError` indique que le nom d'une variable
ou d'une fonction n'est pas connue par Python.
Habituellement, ceci indique une simple faute d'orthographe.
Cependant, cela peut également indiquer que le nom a été
utilisé avant qu'on ne lui ait associé une valeur.

### Cause probable basée sur les informations données par Python

Dans votre programme, `unicodedata` est un nom inconnu.
Je n’ai pas d’informations supplémentaires pour vous.

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.

aroberge commented 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
aroberge commented 3 years ago

Fixed and new version uploaded to PyPI.

I will wait a bit before closing.