pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.26k stars 1.12k forks source link

Support disabling possibly-used-before-assignment in-line for specific variables #9738

Closed avylove closed 3 months ago

avylove commented 3 months ago

Current problem

With the introduction of possibly-used-before-assignment we've seen some errors come up in existing code. They are all cases where the code imports different libraries depending on if it is running on Windows or *nix. The guards in these cases are not direct. In one case, the Windows code is subclassed and in another the code is

I don't expect Pylint to figure out every case, but the current implementation only provides two options: disable the check universally or disable the check in-line. Of course there are some cases the code could be refactored, but that's not always possible or practical. In either case, one could easily mask legitimate issues.

Desired solution

Disable the check in-line for specific variables This may look something like this:

if not IS_WINDOWS:
    import termios
...
if not IS_WINDOWS:
  attrs = termios.tcgetattr(fd)  # pylint: disable=possibly-used-before-assignment(termios)

In this case, for this line possibly-used-before-assignment is ignored for termios, but not for fd

I believe this also introduces a new general capability, i.e. the ability to fine tune in-line disables. This seems very useful as a general purpose capability, thought, without being familiar with the code, I can't be sure how difficult this would be to implement.

Additional context

No response

DanielNoord commented 3 months ago

Although this is an interesting idea I don't think it is feasible and would make our already overly complicated disable logic even more complicated.

If we were to redesign it from scratch I would definitely take it into consideration and if you're using ruff I'd recommend explaining this use case in https://github.com/astral-sh/ruff/issues/3711 as it seems their issue of generating feedback about disables.

Pierre-Sassoulas commented 3 months ago

We have a somewhat related issue to add reasoning in disables https://github.com/pylint-dev/pylint/issues/5253