Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused tag applied to too big range #253

Open
rchl opened this issue Aug 24, 2022 · 6 comments
Open

Unused tag applied to too big range #253

rchl opened this issue Aug 24, 2022 · 6 comments

Comments

@rchl
Copy link
Contributor

rchl commented Aug 24, 2022

In #229 support for DiagnosticTag was introduced for flake8 but the range in those diagnostics is not quite right.

With a file like (requires "pylsp.plugins.flake8.enabled": true):

from typing import Any, List


def foo() -> Any:
    return 1

the whole from typing line is marked with "unused" tag while only the List import is unused.

Here is how it looks with pylsp:

Screenshot 2022-08-24 at 08 29 48

Here is how it looks with pyright:

Screenshot 2022-08-24 at 08 30 19

@krassowski @ccordoba12

@rchl
Copy link
Contributor Author

rchl commented Aug 24, 2022

Diagnostic payload:

{
  "diagnostics": [
    {
      "code": "F401",
      "message": "F401 'typing.List' imported but unused",
      "range": {
        "end": {
          "character": 29,
          "line": 0
        },
        "start": {
          "character": 0,
          "line": 0
        }
      },
      "severity": 1,
      "source": "flake8",
      "tags": [
        1
      ]
    }
  ],
  "uri": "file:///Users/rafal/Documents/testpy/test.py"
}

@rchl
Copy link
Contributor Author

rchl commented Aug 24, 2022

Looking at the result, I suppose it might be more flake8 issue that it reports diagnostic for the whole line.

@krassowski
Copy link
Contributor

Yes, nothing with support for diagnostic tags, just the old issue of missing precise positions for diagnostics themselves. Note: flake8 is just a wrapper around pyflakes and friends.

Over a year ago I opened a PR to expose the metadata we need (start and end position) upstream PyCQA/pyflakes#649 for an issue which is even older. It was meat with rather little enthusiasm by the maintainers at the time, suggesting that they would reconsider once they can drop Python 3.7 (EOL is June 2023) but they still support 3.6 (which is EOL for 8 months now). You can try to nudge/rebase that PR with hope they would reconsider earlier - after all this is not a user facing change (not visible in pyflakes reports).

Otherwise, we could just monkeypatch here (being very defensive - wrapping everything in big try-except) if @ccordoba12 agrees.

@ccordoba12
Copy link
Member

Otherwise, we could just monkeypatch here (being very defensive - wrapping everything in big try-except) if @ccordoba12 agrees.

Yeah, I think it'd be very nice to have support for this here because it's confusing for users not being able to see the exact position where an error/warning occurs.

So, I'm quite +1 on this @krassowski.

@Wuestengecko
Copy link
Contributor

Hi! I noticed that something very similar applies to the pylint plugin as well, when it marks unused variables and function parameters. It does correctly set the start position in this case, but the highlighting extends until end of line, which can be very confusing:

grafik

This one might be easier to fix; considering we already have an accurate start position, we could derive the end position by looking for the end of the "word" – which, if I'm not missing something, should always be an identifier (i.e. word.isidentifier() is True).

@ccordoba12
Copy link
Member

@Wuestengecko, we're very busy at the moment but you're welcome to send us a pull request with a possible fix to take a look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants