Skip to content

Print source line with error and underline target of error #7411

Closed

Description

@JukkaL

Now that mypy can usually report the column number of errors in a useful manner, we could print out the contents of the source line for each error and underline the token the error points to (based on line/column information). This can be either enabled by default or through a flag -- I'm not sure which option I'd prefer yet.

Arguments for doing this:

  • This makes it easier to see what is wrong, especially if the error message happens to be terse, or if there is a lot of things going on in a single line.
  • Consistency with Python: Python tracebacks show the source line.
  • Not everybody has configured their editor to highlight the location of the error.
  • If a user edits the file with the error, the line number will get out of alignment. Having the source line in the output makes it easier to see where the error points to even if there have been edits.
  • Many other tools can do this, so many users are used to this behavior.

Arguments for not doing this (at least by default):

  • The length of mypy output is increased significantly. If we show a source line and a separate line with underlining, the number of lines may increase by a factor of three.
  • The output may be harder to scan or look noisy, since there will be more stuff. Using colors should help with this (Use colors in mypy output #7410).
  • If the reported line/column number is not logically correct, this may be confusing. We may want to fix the line/column numbers of most errors first. (I've already done some of this relatively recently.)

Thoughts?

Activity

  1. ilevkivskyi commented on Aug 29, 2019

    @ilevkivskyi
    Member

    I think reporting reliably correct line/column numbers is a blocker for this. And it is still not perfect unfortunately.

    Also end_lineno and end_col_offset will simplify finding better spans, but they are Python 3.8+ only (there is even nice helper).

  2. gvanrossum commented on Aug 29, 2019

    @gvanrossum
    Member

    Well, even if it only shows the line that can sometimes help (e.g. if you're looking at slightly stale mypy output and have trouble matching up the line number with the actual source line in your editor). So I'm not so hung up on correct column numbers as Ivan. Having this will probably motivate users to report cases where the column number (or even the line number) is off.

    I would not like this by default, because I often look at long lists of mypy errors where it would just be noise, and in an Emacs *compilation* buffer going to the corresponding line is easy.

    Interestingly, this is an area where compilers and linters seem to differ -- I know of no linter that shows the text of the line (at least not by default), while all compilers I know of do that. I think mypy is still more like a linter than like a compiler (mypyc notwithstanding :-).

    Also, let's not get into bikeshedding. Let's just do it.

  3. ilevkivskyi commented on Aug 31, 2019

    @ilevkivskyi
    Member

    I have played with this a bit and I think I have got a decent readability with help of colors and word-wrap in errors. I would however still make this optional, since the output is quite massive and as you can see even in this small sample columns are not ideal. I will make a PR later today.

    Screenshot from 2019-08-31 18:26:49

  4. gvanrossum commented on Aug 31, 2019

    @gvanrossum
    Member

    I am not near a laptop, but don’t gcc/clang show the offending line before the error message?

  5. gvanrossum commented on Aug 31, 2019

    @gvanrossum
    Member

    don’t gcc/clang show the offending line before the error message

    Scratch that, I just checked and it also prints the message before the text of the line. I guess I was thinking of Python's SyntaxError formatting.

  6. JukkaL commented on Sep 2, 2019

    @JukkaL
    CollaboratorAuthor

    I think that we should fix at least the most obvious problems with column numbers before documenting this as a feature. I have a half-finished PR that fixes column numbers of arguments and may be able to finish that up soon. Also, assignments should probably point to the rvalue.

    Other comments:

    • I don't like the new location for the error code. I'd keep that in the original location after the message.
    • It might look better if we'd only underline the target token, i.e. the length of the underlining would vary. Maybe the underlining should cover a simple member expression (say, underline foo.bar in foo.bar.zar).
    • If the column number is missing, we should perhaps point to the first non-space token on the line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions