Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #601 from Jaseci-Labs/real_python_raise
Browse files Browse the repository at this point in the history
Make abs_path point to the real python file if possible
  • Loading branch information
marsninja authored Sep 5, 2024
2 parents 98017c4 + 7128bc2 commit 5882d8d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
8 changes: 8 additions & 0 deletions jaclang/compiler/passes/main/py_collect_dep_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,18 +30,24 @@ 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]
if mod_path_node.path:
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .display import *
from .color import *
from .constants import *
20 changes: 10 additions & 10 deletions jaclang/compiler/passes/main/tests/test_import_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
5 changes: 4 additions & 1 deletion jaclang/langserve/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions jaclang/langserve/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 5882d8d

Please sign in to comment.