Skip to content

Commit c67ec1a

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

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Diff for: pigar/core.py

+14-6
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
@@ -168,8 +168,12 @@ def analyze_requirements(
168168
importables = dict()
169169
tryimports = set()
170170
importlib.invalidate_caches()
171+
visited_import_names = set()
171172

172-
def _resolve(module: Module, from_annotation: bool):
173+
def _resolve(
174+
module: Module, from_annotation: bool,
175+
visited_import_names: Set[AnyStr]
176+
):
173177
name = module.name
174178
if is_user_module(module, self._project_root):
175179
logger.debug(
@@ -199,6 +203,10 @@ def _resolve(module: Module, from_annotation: bool):
199203
names.append(name)
200204

201205
for name in names:
206+
if name in visited_import_names:
207+
continue
208+
visited_import_names.add(name)
209+
202210
if name in self._installed_dists_by_imports:
203211
reqs = self._installed_dists_by_imports[name]
204212
locs = _Locations.build_from(module.file, module.lineno)
@@ -218,7 +226,7 @@ def _resolve(module: Module, from_annotation: bool):
218226
] = from_annotation
219227

220228
for module in imported_modules:
221-
_resolve(module, False)
229+
_resolve(module, False, visited_import_names)
222230
for annotation in annotations:
223231
if annotation.top_level_import_name is not None:
224232
module = Module(
@@ -227,7 +235,7 @@ def _resolve(module: Module, from_annotation: bool):
227235
lineno=annotation.lineno,
228236
try_=False
229237
)
230-
_resolve(module, True)
238+
_resolve(module, True, visited_import_names)
231239
elif annotation.distribution_name is not None:
232240
req = self._installed_dists.get(
233241
canonicalize_name(annotation.distribution_name), None
@@ -394,15 +402,15 @@ def write_requirements(
394402

395403
if self._uncertain_requirements:
396404
stream.write(
397-
'\nWARNING(pigar): some manual fixes are required since pigar has found duplicate requirements for the same import name.\n'
405+
'\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'
398406
)
399407
uncertain_requirements = sorted(
400408
self._uncertain_requirements.items(),
401409
key=lambda item: item[0].lower()
402410
)
403411
for import_name, reqs in uncertain_requirements:
404412
stream.write(
405-
f'# WARNING(pigar): the following duplicate requirements are for import name: {import_name}\n'
413+
f'# WARNING(pigar): the following duplicate requirements are for the import name: {import_name}\n'
406414
)
407415
with_ref_comments_once = with_ref_comments
408416
for _, req in reqs.sorted_items():

0 commit comments

Comments
 (0)