python-lsp / python-lsp-server

Fork of the python-language-server project, maintained by the Spyder IDE team and the community
MIT License
1.9k stars 197 forks source link

Overlapping relative imports are not possible #476

Open piraz opened 11 months ago

piraz commented 11 months ago

While relative importing classes from a module that has the same name of the module I'm editing, completions/references are not available for me.

Steps to reproduce:

Take this file structure:

On my_project/my_project/__init__.py we have:

__version__ = (0, 0, 1)

def get_version():
    return ".".join(map(str, __version__))

On my_project/my_project/protocol.py we have:

class BaseTransport:

    def get(self, path):
        return f"a get stuff with {path}"

From my_project/my_project/dns/__init__.py this will work fine:

from .. import get_version
from ..protocol import BaseTransport

From my_project/my_project/dns/protocol.py this won't complete and references will not work (i.e gd on neovim):

from .. import get_version
from ..protocol import BaseTransport

From my_project/my_project/dns/service.py :

from .. import get_version // <==== not recognized
from ..protocol import BaseTransport // this will work fine

If I rename my_project/my_project/protocol.py to my_project/my_project/base_protocol.py on my_project/my_project/dns/protocol.py I have this situation:

from .. import get_version // <==== this is still not recognized
from ..base_protocol import BaseTransport  // this will work fine

If I don't use relative imports and from my_project.protocol import BaseTransport instead, I cannot observe the issue.

The code with relative imports, will run without issues, and will work on PyCharm, for instance.

The lsp root directory is \<absolute_path>\my_project. Installed python-lsp-server via Mason.

Could you please take a look at this?