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

Fix numpy go-to-definition by taking it off autoimport list for this case #447

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions pylsp/plugins/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from __future__ import annotations
import logging
from typing import Any, Dict, List, TYPE_CHECKING

import jedi

from pylsp import hookimpl, uris, _utils

if TYPE_CHECKING:
Expand Down Expand Up @@ -43,12 +46,19 @@ def pylsp_definitions(
settings = config.plugin_settings("jedi_definition")
code_position = _utils.position_to_jedi_linecolumn(document, position)
script = document.jedi_script(use_document_path=True)
definitions = script.goto(
follow_imports=settings.get("follow_imports", True),
follow_builtin_imports=settings.get("follow_builtin_imports", True),
**code_position,
)
definitions = [_resolve_definition(d, script, settings) for d in definitions]
auto_import_modules = jedi.settings.auto_import_modules

try:
smacke marked this conversation as resolved.
Show resolved Hide resolved
jedi.settings.auto_import_modules = []
definitions = script.goto(
follow_imports=settings.get("follow_imports", True),
follow_builtin_imports=settings.get("follow_builtin_imports", True),
**code_position,
)
definitions = [_resolve_definition(d, script, settings) for d in definitions]
finally:
jedi.settings.auto_import_modules = auto_import_modules

follow_builtin_defns = settings.get("follow_builtin_definitions", True)
smacke marked this conversation as resolved.
Show resolved Hide resolved
return [
{
Expand Down
12 changes: 12 additions & 0 deletions test/plugins/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def my_func():
inception = my_list[2]

inception()

import numpy
numpy.ones
"""


Expand Down Expand Up @@ -89,6 +92,15 @@ def test_definition_with_multihop_inference_goto(config, workspace):
)


def test_numpy_definition(config, workspace):
# Over numpy.ones
cursor_pos = {"line": 29, "character": 8}

doc = Document(DOC_URI, workspace, DOC)
defns = pylsp_definitions(config, doc, cursor_pos)
assert len(defns) > 0, defns


def test_builtin_definition(config, workspace):
# Over 'i' in dict
cursor_pos = {"line": 8, "character": 24}
Expand Down