neoclide / coc-python

Python extension for coc.nvim, fork of vscode-python
574 stars 51 forks source link

Improper Jedi environment initialization prevents older Jedi versions from working #36

Closed randrej closed 5 years ago

randrej commented 5 years ago

Sometimes you want to use an older version of Jedi in a virtualenv with coc-python, because PyQT5 / PySide2 don't work with Jedi versions >12.1.

Here's the Jedi issue that outlines what happens: davidhalter/jedi#1287.

So, until they fix that, people who work with PyQT5 / PySide2 have to use jedi = "0.12.1". But that doesn't work with coc-python because the code in this project (forked from the vscode plugin) initializes the jedi environment the wrong way:

    def __init__(self):
        self.default_sys_path = sys.path
        self.environment = jedi.api.environment.Environment(sys.executable)

which is clearly against the recommendation in the Jedi project

class Environment(_BaseEnvironment):
    """
    This class is supposed to be created by internal Jedi architecture. You
    should not create it directly. Please use create_environment or the other
    functions instead. It is then returned by that function.
    """

This, coupled with an API change in Jedi, renders coc-python unable to interface with the legacy Jedi version, since jedi.api.environment.Environment constructor's arguments have changed in the meantime.

But, if we use the recommended way of creating the environment, jedi.api.environment.Environment, the issue goes away and coc-python works properly with both jedi = "0.12.1" and jedi = "0.13.3" (latest).

This is the change:

    def __init__(self):
        self.default_sys_path = sys.path
        self.environment = jedi.api.environment.create_environment(
            sys.executable, safe=False
        )

I'll put up a pull request with this fix right away.