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

Implement import tracking

Open FozzieHi opened this issue 2 years ago • 4 comments

I think false positives like those reported in #356 (and probably some false negatives as well) are prevalent across multiple lints and one way to solve them would be to use some form of import tracking.

I've come up with some examples below, but this is just a very rough draft.

from itertools import groupby

imported_modules = {
    "itertools": {
        "name": "itertools",
        "imports": {
            "groupby": "groupby"
        }
    }
}

from itertools import groupby as groupby_as

imported_modules = {
    "itertools": {
        "name": "itertools",
        "imports": {
            "groupby": "groupby_as"
        }
    }
}

import itertools

imported_modules = {
    "itertools": {
        "name": "itertools"
    }
}

import itertools as itertools_as

imported_modules = {
    "itertools": {
        "name": "itertools"
    }
}

Then in the lint to get the itertools id you'd use imported_modules["itertools"]["name"], and to get the groupby id you'd use imported_modules["itertools"]["imports"]["groupby"].

Is this something that we would be interested in adding?

FozzieHi avatar Feb 15 '23 17:02 FozzieHi

@cooperlees What do you think about this?

FozzieHi avatar Mar 13 '23 19:03 FozzieHi

So, just so I understand, you just want to make a generic API to add import tracking to multiple checks in a central place? Any other main goals I missed here?

cooperlees avatar Mar 14 '23 01:03 cooperlees

Yup, just want to effectively create a method to get the import alias for a certain module, which we can then use in the checks.

FozzieHi avatar Mar 14 '23 19:03 FozzieHi

Sounds worth adding to me, especially if we're already using it in multiple places (I forget).

cooperlees avatar Mar 14 '23 19:03 cooperlees