Skip to content

Commit a6579c9

Browse files
committed
fix: fall back to python pagerank when scipy is missing
1 parent bdb4d9f commit a6579c9

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

aider/repomap.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ def get_ranked_tags(
523523

524524
try:
525525
ranked = nx.pagerank(G, weight="weight", **pers_args)
526+
except ModuleNotFoundError:
527+
from networkx.algorithms.link_analysis.pagerank_alg import _pagerank_python
528+
529+
ranked = _pagerank_python(G, weight="weight", **pers_args)
526530
except ZeroDivisionError:
527531
# Issue #1536
528532
try:

tests/basic/test_repomap.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
import time
55
import unittest
66
from pathlib import Path
7+
from unittest import mock
78

89
import git
910

1011
from aider.dump import dump # noqa: F401
1112
from aider.io import InputOutput
1213
from aider.models import Model
13-
from aider.repomap import RepoMap
14+
from aider.repomap import RepoMap, Tag
1415
from aider.utils import GitTemporaryDirectory, IgnorantTemporaryDirectory
1516

1617

@@ -273,6 +274,47 @@ def test_get_repo_map_excludes_added_files(self):
273274
# close the open cache files, so Windows won't error
274275
del repo_map
275276

277+
def test_get_ranked_tags_falls_back_without_scipy(self):
278+
with IgnorantTemporaryDirectory() as temp_dir:
279+
file1 = os.path.join(temp_dir, "file1.py")
280+
file2 = os.path.join(temp_dir, "file2.py")
281+
Path(file1).write_text("def alpha():\n return 1\n", encoding="utf-8")
282+
Path(file2).write_text("def beta():\n return alpha()\n", encoding="utf-8")
283+
284+
io = InputOutput()
285+
repo_map = RepoMap(main_model=self.GPT35, root=temp_dir, io=io)
286+
287+
rel1 = repo_map.get_rel_fname(file1)
288+
rel2 = repo_map.get_rel_fname(file2)
289+
290+
def fake_get_tags(fname, rel_fname):
291+
if rel_fname == rel1:
292+
return [Tag(rel1, file1, 1, "alpha", "def")]
293+
if rel_fname == rel2:
294+
return [
295+
Tag(rel2, file2, 1, "beta", "def"),
296+
Tag(rel2, file2, 2, "alpha", "ref"),
297+
]
298+
return []
299+
300+
fallback_ranks = {rel1: 0.4, rel2: 0.6}
301+
302+
with (
303+
mock.patch.object(repo_map, "get_tags", side_effect=fake_get_tags),
304+
mock.patch("networkx.pagerank", side_effect=ModuleNotFoundError("No module named 'scipy'")),
305+
mock.patch(
306+
"networkx.algorithms.link_analysis.pagerank_alg._pagerank_python",
307+
return_value=fallback_ranks,
308+
) as mock_fallback,
309+
):
310+
ranked_tags = repo_map.get_ranked_tags([], [file1, file2], set(), set())
311+
312+
self.assertTrue(ranked_tags)
313+
mock_fallback.assert_called_once()
314+
315+
# close the open cache files, so Windows won't error
316+
del repo_map
317+
276318

277319
class TestRepoMapTypescript(unittest.TestCase):
278320
def setUp(self):

0 commit comments

Comments
 (0)