Implement import tracking
FozzieHi opened this issue · 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?
@cooperlees What do you think about this?
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?
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.
Sounds worth adding to me, especially if we're already using it in multiple places (I forget).