Skip to content

Commit 5ebdad4

Browse files
committed
Filter to avoid manually selecting packages/distributions multiple times
Signed-off-by: Xiaochao Dong (@damnever) <[email protected]>
1 parent 755aed7 commit 5ebdad4

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Diff for: pigar/core.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import importlib
1111
import importlib.util
1212
import importlib.machinery
13-
from typing import NamedTuple, List, Dict, Any, Optional, Tuple
13+
from typing import NamedTuple, List, Dict, Any, Optional, Tuple, Set, AnyStr
1414
import asyncio
1515

1616
from .db import database
@@ -140,6 +140,7 @@ def __init__(self, project_root):
140140
distributions=self._installed_dists.values()
141141
)
142142
self._requirements = _LocatableRequirements()
143+
self._choosed_manually = dict()
143144
self._uncertain_requirements = collections.defaultdict(
144145
_LocatableRequirements
145146
) # Multiple requirements for same import name.
@@ -394,15 +395,15 @@ def write_requirements(
394395

395396
if self._uncertain_requirements:
396397
stream.write(
397-
'\nWARNING(pigar): some manual fixes are required since pigar has found duplicate requirements for the same import name.\n'
398+
'\n# WARNING(pigar): some manual fixes might be required as pigar has detected duplicate requirements for the same import name (possibly for different submodules).\n' # noqa: E501
398399
)
399400
uncertain_requirements = sorted(
400401
self._uncertain_requirements.items(),
401402
key=lambda item: item[0].lower()
402403
)
403404
for import_name, reqs in uncertain_requirements:
404405
stream.write(
405-
f'# WARNING(pigar): the following duplicate requirements are for import name: {import_name}\n'
406+
f'# WARNING(pigar): the following duplicate requirements are for the import name: {import_name}\n' # noqa: E501
406407
)
407408
with_ref_comments_once = with_ref_comments
408409
for _, req in reqs.sorted_items():
@@ -461,6 +462,10 @@ def _maybe_filter_distributions_with_same_import_name(
461462
):
462463
if dists_filter is None or len(distributions) <= 1:
463464
return distributions
465+
# We can use `functools.cache` in later versions of Python.
466+
existing = self._choosed_manually.get(import_name, None)
467+
if existing is not None:
468+
return existing
464469

465470
assert (hasattr(distributions[0], 'name'))
466471

@@ -481,7 +486,11 @@ def _maybe_filter_distributions_with_same_import_name(
481486
best_match = casefold_match
482487
if best_match is None and len(contains) == 1:
483488
best_match = contains[0]
484-
return dists_filter(import_name, locations, distributions, best_match)
489+
choosed = dists_filter(
490+
import_name, locations, distributions, best_match
491+
)
492+
self._choosed_manually[import_name] = choosed
493+
return choosed
485494

486495

487496
class LocalRequirementWithLatestVersion(NamedTuple):

Diff for: pigar/tests/test_cli.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ def test_generate(self):
3535
cli, [
3636
'gen', '--with-referenced-comments',
3737
'--dont-show-differences', '--exclude-glob',
38-
'**/tests/data/*', '--exclude-glob',
39-
'**/_vendor/pip/*', '-f',
40-
generated_requirement_file, project_path
38+
'**/tests/data/*', '--exclude-glob', '**/_vendor/pip/*',
39+
'-f', generated_requirement_file, project_path
4140
]
4241
)
4342
self.assertEqual(result.exit_code, 0, result.output)
4443
expected = self._read_filelines(expected_requirements)
4544
actual = self._read_filelines(generated_requirement_file)
46-
self.assertEqual(len(expected), len(actual))
45+
self.assertEqual(len(expected), len(actual), actual)
4746
for idx, line in enumerate(expected):
4847
line2 = actual[idx]
4948
if not line or line.startswith('#') or line.startswith('\n'):

0 commit comments

Comments
 (0)