Skip to content

Commit

Permalink
Filter to avoid manually selecting packages/distributions multiple times
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaochao Dong (@damnever) <[email protected]>
  • Loading branch information
damnever committed Dec 16, 2024
1 parent 755aed7 commit c67ec1a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions pigar/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import importlib
import importlib.util
import importlib.machinery
from typing import NamedTuple, List, Dict, Any, Optional, Tuple
from typing import NamedTuple, List, Dict, Any, Optional, Tuple, Set, AnyStr
import asyncio

from .db import database
Expand Down Expand Up @@ -168,8 +168,12 @@ def analyze_requirements(
importables = dict()
tryimports = set()
importlib.invalidate_caches()
visited_import_names = set()

def _resolve(module: Module, from_annotation: bool):
def _resolve(
module: Module, from_annotation: bool,
visited_import_names: Set[AnyStr]
):
name = module.name
if is_user_module(module, self._project_root):
logger.debug(
Expand Down Expand Up @@ -199,6 +203,10 @@ def _resolve(module: Module, from_annotation: bool):
names.append(name)

for name in names:
if name in visited_import_names:
continue
visited_import_names.add(name)

if name in self._installed_dists_by_imports:
reqs = self._installed_dists_by_imports[name]
locs = _Locations.build_from(module.file, module.lineno)
Expand All @@ -218,7 +226,7 @@ def _resolve(module: Module, from_annotation: bool):
] = from_annotation

for module in imported_modules:
_resolve(module, False)
_resolve(module, False, visited_import_names)
for annotation in annotations:
if annotation.top_level_import_name is not None:
module = Module(
Expand All @@ -227,7 +235,7 @@ def _resolve(module: Module, from_annotation: bool):
lineno=annotation.lineno,
try_=False
)
_resolve(module, True)
_resolve(module, True, visited_import_names)
elif annotation.distribution_name is not None:
req = self._installed_dists.get(
canonicalize_name(annotation.distribution_name), None
Expand Down Expand Up @@ -394,15 +402,15 @@ def write_requirements(

if self._uncertain_requirements:
stream.write(
'\nWARNING(pigar): some manual fixes are required since pigar has found duplicate requirements for the same import name.\n'
'\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'
)
uncertain_requirements = sorted(
self._uncertain_requirements.items(),
key=lambda item: item[0].lower()
)
for import_name, reqs in uncertain_requirements:
stream.write(
f'# WARNING(pigar): the following duplicate requirements are for import name: {import_name}\n'
f'# WARNING(pigar): the following duplicate requirements are for the import name: {import_name}\n'
)
with_ref_comments_once = with_ref_comments
for _, req in reqs.sorted_items():
Expand Down

0 comments on commit c67ec1a

Please sign in to comment.