astroid icon indicating copy to clipboard operation
astroid copied to clipboard

AstroidManager.ast_from_module_name returns incorrect module when a pyz is in the PYTHONPATH

Open grayjk opened this issue 5 months ago • 1 comments

Create a pyz x.zip:

$ unzip -t x.zip 
Archive:  x.zip
    testing: xxx/                     OK
    testing: xxx/test.py              OK
    testing: xxx/__init__.py          OK
No errors detected in compressed data of x.zip.

xxx/test.py contains a function named func

Run python:

$ PYTHONPATH=x.zip python3
>>> from astroid.manager import AstroidManager
>>> AstroidManager().ast_from_module_name('xxx.test').file
'/usr/lib/python3.12/test/__init__.py'

The module returned is from the python stdlib but should be from within the pyz

Expected result:

>>> AstroidManager().ast_from_module_name('xxx.test').file
'/home/user/pyztest/x.zip/xxx/test.py'

Tested with astroid 3.3.10

grayjk avatar Jun 25 '25 17:06 grayjk

Affect on pylint 3.3.7:

$ PYTHONPATH=x.zip python3 
>>> import astroid
>>> from pylint.checkers import utils
>>> x = astroid.extract_node('''
... from xxx.test import func
... func  #@
... ''')
>>> x
<Name.func l.3 at 0xf4d3d84f72f0>
>>> utils.safe_infer(x)
Uninferable

grayjk avatar Jun 25 '25 17:06 grayjk