Skip to content

Commit 3efc505

Browse files
authored
improve the logging of import locations when running in verbose mode (#627)
1 parent e331e3d commit 3efc505

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

python/deptry/imports/extract.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

3+
import json
34
import logging
4-
from collections import defaultdict
5+
from collections import OrderedDict, defaultdict
56
from typing import TYPE_CHECKING
67

78
from deptry.rust import get_imports_from_ipynb_files, get_imports_from_py_files
@@ -25,21 +26,29 @@ def get_imported_modules_from_list_of_files(list_of_files: list[Path]) -> dict[s
2526
# Process all .py files in parallel using Rust
2627
if py_files:
2728
rust_result = get_imports_from_py_files(py_files)
28-
for module, locations in convert_rust_locations_to_python_locations(rust_result).items():
29+
for module, locations in _convert_rust_locations_to_python_locations(rust_result).items():
2930
modules[module].extend(locations)
3031

3132
# Process all .ipynb files in parallel using Rust
3233
if ipynb_files:
3334
rust_result = get_imports_from_ipynb_files(ipynb_files)
34-
for module, locations in convert_rust_locations_to_python_locations(rust_result).items():
35+
for module, locations in _convert_rust_locations_to_python_locations(rust_result).items():
3536
modules[module].extend(locations)
3637

37-
logging.debug("All imported modules: %s\n", modules)
38+
sorted_modules = OrderedDict(sorted(modules.items()))
39+
_log_modules_with_locations(sorted_modules)
40+
return sorted_modules
3841

39-
return modules
4042

43+
def _log_modules_with_locations(modules: dict[str, list[Location]]) -> None:
44+
modules_dict = {
45+
module_name: [str(location) for location in locations] for module_name, locations in modules.items()
46+
}
47+
modules_json = json.dumps(modules_dict, indent=2)
48+
logging.debug("All imported modules and their locations:\n%s", modules_json)
4149

42-
def convert_rust_locations_to_python_locations(
50+
51+
def _convert_rust_locations_to_python_locations(
4352
imported_modules: dict[str, list[RustLocation]],
4453
) -> dict[str, list[Location]]:
4554
converted_modules: dict[str, list[Location]] = {}

0 commit comments

Comments
 (0)