From 75ae73ee97f5a31be06a00da31d0188ea2e5b876 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 9 Mar 2020 17:27:51 +0100 Subject: [PATCH] Load -stubs packages properly in _load_python_module --- jedi/inference/imports.py | 13 +++++++++++-- test/test_api/test_project.py | 11 ++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/jedi/inference/imports.py b/jedi/inference/imports.py index 3432e171..5d88d40b 100644 --- a/jedi/inference/imports.py +++ b/jedi/inference/imports.py @@ -19,6 +19,7 @@ from parso.tree import search_ancestor from jedi._compatibility import ImplicitNSInfo, force_unicode, FileNotFoundError from jedi import debug from jedi import settings +from jedi.file_io import FolderIO from jedi.parser_utils import get_cached_code_lines from jedi.inference import sys_path from jedi.inference import helpers @@ -439,9 +440,17 @@ def _load_python_module(inference_state, file_io, if is_stub: folder_io = file_io.get_parent_folder() - python_file_io = folder_io.get_file_io(import_names[-1] + '.py') + if folder_io.path.endswith('-stubs'): + folder_io = FolderIO(folder_io.path[:-6]) + if file_io.path.endswith('__init__.pyi'): + python_file_io = folder_io.get_file_io('__init__.py') + stub_import_names = import_names + else: + python_file_io = folder_io.get_file_io(import_names[-1] + '.py') + stub_import_names = import_names[:-1] + try: - v = load_module_from_path(inference_state, python_file_io, import_names[:-1]) + v = load_module_from_path(inference_state, python_file_io, stub_import_names) values = ValueSet([v]) except FileNotFoundError: values = NO_VALUES diff --git a/test/test_api/test_project.py b/test/test_api/test_project.py index 6c47ed2d..9cbc4561 100644 --- a/test/test_api/test_project.py +++ b/test/test_api/test_project.py @@ -89,11 +89,9 @@ def test_load_save_project(tmpdir): 'examples.implicit_namespace_package.ns2'], dict(complete=True)), - # With namespace & stub - ('with_python.module', ['stub:examples.stub_packages.with_python.module', - 'examples.stub_packages.with_python.module'], {}), - ('with_python.modul', ['stub:examples.stub_packages.with_python.module', - 'examples.stub_packages.with_python.module'], + # With stubs + ('with_python.module', ['examples.stub_packages.with_python.module'], {}), + ('with_python.modul', ['examples.stub_packages.with_python.module'], dict(complete=True)), ('no_python.foo', ['stub:examples.stub_packages.no_python.foo'], {}), ('no_python.fo', ['stub:examples.stub_packages.no_python.foo'], @@ -102,8 +100,7 @@ def test_load_save_project(tmpdir): ('no_python-stubs.foo', [], {}), # Both locations are given, because they live in separate folders (one # suffixed with -stubs. - ('with_python', ['stub:examples.stub_packages.with_python', - 'examples.stub_packages.with_python'], {}), + ('with_python', ['examples.stub_packages.with_python'], {}), ('no_python', ['stub:examples.stub_packages.no_python'], {}), # Completion stubs ('stub_only', ['stub:completion.stub_folder.stub_only',