astroid
astroid copied to clipboard
Resolving symlinks considered harmful
Steps to reproduce
- Get https://github.com/M-o-a-T/moat
- git submodule update --init util micro
- cd micro
- pylint
Current behavior
The problem which this archive exhibits is that moat/micro/link.py is a symlink that refers to moat/micro/_embed/lib/moat/micro/link.py. The directory moat/micro has an _init__.py file while moat/micro/_embed/lib/moat/micro does not.
Now, modpath_from_file_with_callback, in astroid/mdutils.py, calls astroid's _get_relative_base_path function which calls realpath and thus resolves this symlink. As a result, pylint's "is this a proper module" callback looks for the __init__.py file in the wrong place, decides that this is not a module, and refuses to process the import.
Expected behavior
IMHO there's no good reason to call realpath on anything. Why not simply assume that the symlink is there for a good reason?
python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output
2.14.2, but applies to master
Did you search for the reason in the git history ? It feel like a Chesterton's fence (we should not remove it until we know why it's here in the first place)
Both absolute and real path lookups were added in cd76b495d
modutils is using realpath at the moment which will resolve the symlink
and don't allow the import as the file is detected outside of the
available paths.
This change added two tests – which incidentally still work after removing that realpath call.
Both absolute and real path lookups were added in cd76b49
modutils is using realpath at the moment which will resolve the symlink and don't allow the import as the file is detected outside of the available paths.This change added two tests – which incidentally still work after removing that
realpathcall.
As you can see from the diff we used realpath before it. I added a comment in your PR with the first introduction of realpath, which sadly doesn't include a test.
Ah, yes, sorry, the real reason realpath was added seems to be PyCQA/pylint#791
I'll ask there whether somebody can test this.