Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no inference for call whose containing module is imported relatively #850

Open
ltcmelo opened this issue Nov 3, 2020 · 3 comments
Open
Labels

Comments

@ltcmelo
Copy link
Contributor

ltcmelo commented Nov 3, 2020

version: 2.4.2

Consider this project structure:

project
    |__ app.py
    |__ lib.py

And lib.py is as follows:

def setup():
    pass

There appears to exist a bug in the inference with relative from … import of lib.py. Nothing is inferred for setup() in the snippet below.

from .lib import setup

def f():
    setup()  # bad

If the relative import is replaced by an absolute one (assume the directory is in sys path), the inference works as expected for setup().

from lib import setup

def f():
    setup()  # good

A possible cause of this error is the level not being taken into account here.

@hippo91
Copy link
Contributor

hippo91 commented Nov 17, 2020

@ltcmelo thanks for the report.

@earonesty
Copy link

i got this to work in the latest astroid. just had to do this:

astroid.parse(open(fp, encoding="utf8").read(), module_name=derive_module_name(fp), path=fp)

as long as the derived module name is correct.... everything worked fine with relative imports

@earonesty
Copy link

earonesty commented Oct 29, 2021

for posterity, my crappy module name function was this:

def derive_module_name(fp):
    fp = os.path.splitext(fp)[0]
    fp = fp.replace("/", ".")
    fp = fp.replace("\\", ".")
    return fp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants