From cc2279452145c30aff32139131ffa8b3275cf414 Mon Sep 17 00:00:00 2001 From: mgtm98 Date: Tue, 3 Sep 2024 22:23:31 +0300 Subject: [PATCH 1/3] Make abs_path point to the real python file if possible --- .../passes/main/py_collect_dep_pass.py | 8 ++++++++ .../tests/fixtures/pygame_mock/__init__.pyi | 3 +++ .../passes/main/tests/test_import_pass.py | 20 +++++++++---------- 3 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 jaclang/compiler/passes/main/tests/fixtures/pygame_mock/__init__.pyi diff --git a/jaclang/compiler/passes/main/py_collect_dep_pass.py b/jaclang/compiler/passes/main/py_collect_dep_pass.py index f58eac43e..1f9362a7d 100644 --- a/jaclang/compiler/passes/main/py_collect_dep_pass.py +++ b/jaclang/compiler/passes/main/py_collect_dep_pass.py @@ -7,6 +7,8 @@ from __future__ import annotations +import os + import jaclang.compiler.absyntree as ast from jaclang.compiler.passes import Pass from jaclang.settings import settings @@ -28,11 +30,15 @@ def enter_node(self, node: ast.AstNode) -> None: if not isinstance(node, ast.AstSymbolNode): return + # Adding the path of the file related to the py import path: str = "" if isinstance(node, ast.ModulePath): if node.path: path = ".".join([i.value for i in node.path]) node.abs_path = self.ir.py_mod_dep_map.get(path) + if node.abs_path and os.path.isfile(node.abs_path.replace(".pyi", ".py")): + node.abs_path = node.abs_path.replace(".pyi", ".py") + elif isinstance(node, ast.ModuleItem): imp = node.parent_of_type(ast.Import) mod_path_node = imp.get_all_sub_nodes(ast.ModulePath)[0] @@ -40,6 +46,8 @@ def enter_node(self, node: ast.AstNode) -> None: path = ".".join([i.value for i in mod_path_node.path]) path += f".{node.name.value}" node.abs_path = self.ir.py_mod_dep_map.get(path) + if node.abs_path and os.path.isfile(node.abs_path.replace(".pyi", ".py")): + node.abs_path = node.abs_path.replace(".pyi", ".py") if len(node.gen.mypy_ast) == 0: return diff --git a/jaclang/compiler/passes/main/tests/fixtures/pygame_mock/__init__.pyi b/jaclang/compiler/passes/main/tests/fixtures/pygame_mock/__init__.pyi new file mode 100644 index 000000000..671e25b64 --- /dev/null +++ b/jaclang/compiler/passes/main/tests/fixtures/pygame_mock/__init__.pyi @@ -0,0 +1,3 @@ +from .display import * +from .color import * +from .constants import * diff --git a/jaclang/compiler/passes/main/tests/test_import_pass.py b/jaclang/compiler/passes/main/tests/test_import_pass.py index 1dc590188..916103fc8 100644 --- a/jaclang/compiler/passes/main/tests/test_import_pass.py +++ b/jaclang/compiler/passes/main/tests/test_import_pass.py @@ -70,19 +70,19 @@ def test_py_raise_map(self) -> None: ) assert isinstance(build.ir, ast.Module) p = { - "math": "jaclang/jaclang/vendor/mypy/typeshed/stdlib/math.pyi", - "pygame_mock": "pygame_mock/__init__.py", - "pygame_mock.color": "pygame_mock/color.py", - "pygame_mock.constants": "pygame_mock/constants.py", - "argparse": "jaclang/vendor/mypy/typeshed/stdlib/argparse.pyi", - "builtins": "jaclang/vendor/mypy/typeshed/stdlib/builtins.pyi", - "pygame_mock.display": "pygame_mock/display.py", - "os": "jaclang/vendor/mypy/typeshed/stdlib/os/__init__.pyi", - "genericpath": "jaclang/vendor/mypy/typeshed/stdlib/genericpath.pyi", + "math": r"jaclang/jaclang/vendor/mypy/typeshed/stdlib/math.pyi$", + "pygame_mock": r"pygame_mock/__init__.pyi$", + "pygame_mock.color": r"pygame_mock/color.py$", + "pygame_mock.constants": r"pygame_mock/constants.py$", + "argparse": r"jaclang/vendor/mypy/typeshed/stdlib/argparse.pyi$", + "builtins": r"jaclang/vendor/mypy/typeshed/stdlib/builtins.pyi$", + "pygame_mock.display": r"pygame_mock/display.py$", + "os": r"jaclang/vendor/mypy/typeshed/stdlib/os/__init__.pyi$", + "genericpath": r"jaclang/vendor/mypy/typeshed/stdlib/genericpath.pyi$", } for i in p: self.assertIn(i, build.ir.py_raise_map) - self.assertIn(p[i], re.sub(r".*fixtures/", "", build.ir.py_raise_map[i])) + self.assertRegex(re.sub(r".*fixtures/", "", build.ir.py_raise_map[i]), p[i]) def test_py_raised_mods(self) -> None: """Basic test for pass.""" From 630576cfce2ba8b8d2412cf2b7dbbc5d54fa5bbd Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Wed, 4 Sep 2024 17:59:21 +0530 Subject: [PATCH 2/3] use abs_path of ModuleItem in go_to_definition --- jaclang/langserve/engine.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jaclang/langserve/engine.py b/jaclang/langserve/engine.py index 8f6041e54..7d02cc94f 100644 --- a/jaclang/langserve/engine.py +++ b/jaclang/langserve/engine.py @@ -349,7 +349,10 @@ def get_definition( elif node_selected.parent and isinstance( node_selected.parent, ast.ModuleItem ): - path = node_selected.parent.from_mod_path.abs_path + path = ( + node_selected.parent.abs_path + or node_selected.parent.from_mod_path.abs_path + ) try: # TODO: Get rid of this when 'from' import is fixed loc_range = tuple( loc - 1 if loc > 0 else loc From 7128bc2ed4b344ef3f6f843a68531f50a7c667b9 Mon Sep 17 00:00:00 2001 From: kugesan1105 Date: Wed, 4 Sep 2024 19:15:53 +0530 Subject: [PATCH 3/3] test updated : first check for stub file ('pyi') --- jaclang/langserve/tests/test_server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jaclang/langserve/tests/test_server.py b/jaclang/langserve/tests/test_server.py index a6809cdd4..b6a4e1595 100644 --- a/jaclang/langserve/tests/test_server.py +++ b/jaclang/langserve/tests/test_server.py @@ -218,9 +218,9 @@ def test_go_to_definition_foolme(self) -> None: ) lsp.deep_check(import_file) positions = [ - (6, 39, "/pygame_mock/__init__.py:2:0-2:0"), + (6, 39, "/pygame_mock/__init__.pyi:2:0-2:0"), (6, 45, "/pygame_mock/constants.py:3:0-4:1"), - (7, 31, "/pygame_mock/__init__.py:2:0-2:0"), + (7, 31, "/pygame_mock/__init__.pyi:2:0-2:0"), (7, 35, "/pygame_mock/constants.py:3:0-4:1"), (20, 51, "/py_imp_test.jac:6:4-6:11"), (20, 64, "/pygame_mock/constants.py:4:3-4:15"),