flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

Proposed check: ambiguous variable names

Open sirosen opened this issue 4 years ago • 0 comments

Context: I just upgraded the flake8 version we use in a repo, and noticed new hits for rule E741. This checks for PEP8 names to avoid. Obviously I just changed some variable names and went about my day.

But I realized that there are other ambiguous names -- in some ways, more realistic ambiguous names -- based on this same part of PEP8. For example, myvarO, myvar0, myvar1, myvarl, myvarI are valid identifiers.

These violate the spirit of PEP8, even if they aren't explicitly forbidden by it.

So here's the type of code which I would propose catching with a lint:

xI = 1
xl = 2  # "this variable name is ambiguous when compared with prior definition of xI"

And this code should not fail linting:

xO = 1
xI = 2

Should this be in flake8-bugbear or flake8?

I wasn't sure where to suggest this. It feels similar to B007 to me, but I'm perfectly happy to reopen against flake8 if that seems more appropriate.

Challenges

This would require that when checking a variable, it is compared with each pre-existing variable name in scope. Putting the visible names into a clever data structure might make this more efficient, but it will always be somewhat expensive, especially on large blocks.

sirosen avatar Jun 11 '20 15:06 sirosen