I have a custom module loader that basically does some redirection. I would like pylint to recognize this custom loader. This is my situation:
root/
__init__.py
new/
__init__.py
foo.py
bar.py
old/
__init__.py
I have a lot of clients importing old.foo
. I wrote a custom loader in old/__init__.py
to redirect these to import new.foo
under the hood. How do I get pylint to recognize this? When it lints import old.foo
, it complains that it can't find old.foo
. This is only a problem with pylint. I can get client code to recognize the custom loader without any issue.
You have to remember that pylint is a static analyser and as such doesn't actually load python file (except in some cases where it can't do otherwise, e.g. compiled code). As such it's not aware of custom importer or other tricks taking part of python's high dynamicity.
That being said:
you may still write a "brain" plugin for astroid (the library under pylint) that will help pylint understand your code's specificity
by relying on standard mecanism such as
__path__
manipulation you'll get more chance to avoid such need, either because at some point pylint may understand this or because someone else will have contributed a plugin for that purpose.from the documentation on modules:
So if I understand correctly you want to redirect any references to
old
to redirect tonew
, so all you would need to do is replace theold
folder withold.py
that contains this:Then when anything tries to import
old.foo
it will end up importingnew.foo
.