Proposed check: ambiguous variable names
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 123
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 5
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the existing E741 and B007 checks to understand how flake8-bugbear reports variable-name issues and how scope is represented. Define the comparison behavior for names such as xI and xl, account for the performance concern described in the issue, and add coverage showing ambiguous pairs are reported while xO and xI are not.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100