Error messages on import

Hi,

few ideas came to my mind when writing GH-116292, so I rather moved it here hoping to get some feedback.

Let’s consider two possible errors in import argparse

  1. A typo in import keyword
>>> ipmort argparse
  File "<stdin>", line 1
    ipmort argparse
           ^^^^^^^^
SyntaxError: invalid syntax
  1. A typo in the package name
>>> import agrparse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    import agrparse
ModuleNotFoundError: No module named 'agrparse'

I was wondering if it would be possible to change the error messages a bit to something like this

  1. Point to the error place and provide suggestion
>>> ipmort argparse
  File "<stdin>", line 1
    ipmort argparse
    ^^^^^^
NameError: name 'ipmort' is not defined. Did you mean 'import'?
  1. Provide a suggestion about the importable
>>> import agrparse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    import agrparse
ModuleNotFoundError: No module named 'agrparse'. Did you mean 'argparse'?

No idea though whether someone would find this useful. Any feedback is warmly welcome.

ipmort argparse should definitely remain a SyntaxError, not a NameError. That piece of code is two non-keywords next to each other, which is a syntax error in Python. That’s something very different from import agrparse, which is syntactically fine and meaningful; it just throws an error at runtime because you refer to a module that doesn’t exist (unless you happen to have created a module named agrparse!).

I wouldn’t be opposed to adding a “did you mean” suggestion in cases like ipmort argparse, but I don’t know how easy that would be in the parser.

I agree that import agrparse would ideally suggest importing argparse. I actually thought we already had that, but we don’t, even on current main:

Python 3.13.0a4+ (heads/pep696v2-dirty:7a69204202, Feb 29 2024, 19:21:11) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import agrparse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    import agrparse
ModuleNotFoundError: No module named 'agrparse'
4 Likes

Thanks a lot for explaining this. I think I’ll go and read about the different error types, as I didn’t know about the “two non-keywords”.

One way to think about that is to the parser, ipmort argparse is exactly as if you wrote a b—just two names with no special meaning.

1 Like

Well, I think you can close the issue as not planned, if you like.
EDIT: already closed

I don’t think it would make much difference to me. I’m already going to
know what I meant.

IMO a “Did you mean…” type of question only makes sense if you can
immediately answer it, as for example in Google search results where you
get a link to search for the suggested alternative.

I suppose it would be possible for an IDE to make use of it by letting
you click on it to automatically make the appropriate edit to your code.
That would certainly be useful.

cc @pablogsal for idea 2.