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

Editor opens internal typeshed file instead of real file in venv #264

Open
Morikko opened this issue Apr 5, 2023 · 1 comment
Open

Editor opens internal typeshed file instead of real file in venv #264

Morikko opened this issue Apr 5, 2023 · 1 comment
Labels
bug Something isn't working jedi-issue

Comments

@Morikko
Copy link

Morikko commented Apr 5, 2023

Expected vs. Actual

Expected:
When Ctrl-clicking on a function from a given package, the editor opens the actual file in which the function definition is found in the virtual environment.

Actual:
When Ctrl-clicking on a function from a given package, the editor opens the typeshed file matching the definition installed within the extension itself.

How to reproduce

  1. Create a new folder/workspace and open vscode;
  2. Create a new Python file with eg.:
import flask

assert flask.__version__ == "2.2.3"

class Route(flask.Blueprint):
    ...

r = Route("test", "test")
r.add_url_rule

(Using Flask here as an example)

  1. Create a new venv: python -m venv env && source env/bin/activate
  2. Install flask: pip install flask
  3. Mouse hover over from_app and click.

The editor will bring up the typeshed in the extension directory, ~/.vscode-oss/extensions/ms-python.python-2023.4.1-universal/pythonFiles/lib/jedilsp/jedi/third_party/typeshed/third_party/2and3/flask/blueprints.pyi instead of opening env/lib64/python3.X/site-packages/flask/blueprints.py, which is what eg. mypy will use to check the project against.

The hover I see:

flask-hover

Technical details

The Flask v2 signature: https://github.com/pallets/flask/blob/main/src/flask/blueprints.py#L408

    def add_url_rule(
        self,
        rule: str,
        endpoint: t.Optional[str] = None,
        view_func: t.Optional[ft.RouteCallable] = None,
        provide_automatic_options: t.Optional[bool] = None,
        **options: t.Any,
    ) -> None:

While it is the old Flask v1 here: https://github.com/davidhalter/typeshed/blob/jedi/third_party/2and3/flask/blueprints.pyi#L55

    def add_url_rule(self, rule: str, endpoint: Optional[str] = ..., view_func: _ViewFunc = ..., **options: Any) -> None: ...

Jedi looks to act normally, so I believe the problem is how it is used by jedi-language-server:

import jedi

assert jedi.__version__ == '0.18.2'

def filter_function_params(completions):
    return [c for c in completions if c.complete.endswith("=")]

def get_function_function_params_completion(func):
    return filter_function_params(
        jedi.Interpreter(
            f"{func.__name__}(",
            [
                {
                    func.__name__: func,
                }
            ],
        ).complete()
    )


print(get_function_function_params_completion(r.add_url_rule))
# [<Completion: endpoint=>, <Completion: provide_automatic_options=>, <Completion: rule=>, <Completion: view_func=>]

provide_automatic_options parameter is present and only Flask v2.

Note: The issue was originally open here: microsoft/vscode-python#20988

@pappasam pappasam added the bug Something isn't working label Apr 6, 2023
@pappasam
Copy link
Owner

pappasam commented Apr 6, 2023

I'm able to reproduce and believe this is an issue with how Jedi's Script.goto logic prioritizes the Jedi-managed typeshed stubs.

Note: we don't use jedi.Interpreter within the editing environment. Instead, we rely on jedi.Script. See: https://jedi.readthedocs.io/en/latest/docs/api.html?highlight=interpreter#script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working jedi-issue
Projects
None yet
Development

No branches or pull requests

2 participants