detect-shadowed-modules 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {detect_shadowed_modules-0.1.0 → detect_shadowed_modules-0.2.0}/PKG-INFO +43 -5
- {detect_shadowed_modules-0.1.0 → detect_shadowed_modules-0.2.0}/README.md +42 -4
- {detect_shadowed_modules-0.1.0 → detect_shadowed_modules-0.2.0}/detect_shadowed_modules/__init__.py +74 -8
- detect_shadowed_modules-0.2.0/detect_shadowed_modules/editable.py +66 -0
- {detect_shadowed_modules-0.1.0 → detect_shadowed_modules-0.2.0}/pyproject.toml +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: detect-shadowed-modules
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Detect conflicting directory names across Python's sys.path that shadow installed packages
|
|
5
5
|
Keywords: import,shadowing,diagnostics
|
|
6
6
|
Author: Michael Bianco
|
|
@@ -86,6 +86,12 @@ location listed will shadow the others during import.
|
|
|
86
86
|
requests/
|
|
87
87
|
→ /home/user/myproject/requests (shadows others)
|
|
88
88
|
/usr/lib/python3/site-packages/requests [requests] (shadowed)
|
|
89
|
+
|
|
90
|
+
Searched directories (50 total):
|
|
91
|
+
/home/user/myproject [editable]
|
|
92
|
+
/home/user/.venv/lib/python3.13/site-packages
|
|
93
|
+
/usr/lib/python3/site-packages
|
|
94
|
+
...
|
|
89
95
|
```
|
|
90
96
|
|
|
91
97
|
### JSON Format
|
|
@@ -98,6 +104,20 @@ location listed will shadow the others during import.
|
|
|
98
104
|
"shadowed_by": "/home/user/myproject/requests",
|
|
99
105
|
"owner": "requests"
|
|
100
106
|
}
|
|
107
|
+
],
|
|
108
|
+
"searched_paths": [
|
|
109
|
+
{
|
|
110
|
+
"path": "/home/user/myproject",
|
|
111
|
+
"editable": true
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"path": "/home/user/.venv/lib/python3.13/site-packages",
|
|
115
|
+
"editable": false
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"path": "/usr/lib/python3/site-packages",
|
|
119
|
+
"editable": false
|
|
120
|
+
}
|
|
101
121
|
]
|
|
102
122
|
}
|
|
103
123
|
```
|
|
@@ -105,16 +125,34 @@ location listed will shadow the others during import.
|
|
|
105
125
|
## How It Works
|
|
106
126
|
|
|
107
127
|
The tool scans all directories in `sys.path` and:
|
|
108
|
-
1.
|
|
109
|
-
2.
|
|
110
|
-
3.
|
|
111
|
-
4.
|
|
128
|
+
1. Automatically detects editable packages installed via `uv` (using `uv pip list -e`)
|
|
129
|
+
2. Combines editable package paths with `sys.path` (editable paths take precedence)
|
|
130
|
+
3. Identifies subdirectories in each path
|
|
131
|
+
4. Detects when the same directory name appears in multiple locations
|
|
132
|
+
5. Reports which packages are being shadowed and by what
|
|
133
|
+
6. Attempts to identify the installed package that owns each directory
|
|
112
134
|
|
|
113
135
|
Excluded from scanning:
|
|
114
136
|
- Hidden directories (starting with `.`)
|
|
115
137
|
- `__pycache__` directories
|
|
116
138
|
- Package metadata directories (`.dist-info`, `.egg-info`)
|
|
117
139
|
|
|
140
|
+
## Debugging Import Issues
|
|
141
|
+
|
|
142
|
+
If you suspect a module is being imported from the wrong location, you can check where Python is actually loading it from:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
python -c 'import <module>; print(<module>.__file__)'
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
For example:
|
|
149
|
+
```bash
|
|
150
|
+
python -c 'import requests; print(requests.__file__)'
|
|
151
|
+
# Output: /home/user/myproject/requests/__init__.py
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
This will show you the exact file path Python is using for the import, helping you confirm if shadowing is occurring.
|
|
155
|
+
|
|
118
156
|
## Exit Codes
|
|
119
157
|
|
|
120
158
|
- `0`: No conflicts found
|
|
@@ -75,6 +75,12 @@ location listed will shadow the others during import.
|
|
|
75
75
|
requests/
|
|
76
76
|
→ /home/user/myproject/requests (shadows others)
|
|
77
77
|
/usr/lib/python3/site-packages/requests [requests] (shadowed)
|
|
78
|
+
|
|
79
|
+
Searched directories (50 total):
|
|
80
|
+
/home/user/myproject [editable]
|
|
81
|
+
/home/user/.venv/lib/python3.13/site-packages
|
|
82
|
+
/usr/lib/python3/site-packages
|
|
83
|
+
...
|
|
78
84
|
```
|
|
79
85
|
|
|
80
86
|
### JSON Format
|
|
@@ -87,6 +93,20 @@ location listed will shadow the others during import.
|
|
|
87
93
|
"shadowed_by": "/home/user/myproject/requests",
|
|
88
94
|
"owner": "requests"
|
|
89
95
|
}
|
|
96
|
+
],
|
|
97
|
+
"searched_paths": [
|
|
98
|
+
{
|
|
99
|
+
"path": "/home/user/myproject",
|
|
100
|
+
"editable": true
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"path": "/home/user/.venv/lib/python3.13/site-packages",
|
|
104
|
+
"editable": false
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
"path": "/usr/lib/python3/site-packages",
|
|
108
|
+
"editable": false
|
|
109
|
+
}
|
|
90
110
|
]
|
|
91
111
|
}
|
|
92
112
|
```
|
|
@@ -94,16 +114,34 @@ location listed will shadow the others during import.
|
|
|
94
114
|
## How It Works
|
|
95
115
|
|
|
96
116
|
The tool scans all directories in `sys.path` and:
|
|
97
|
-
1.
|
|
98
|
-
2.
|
|
99
|
-
3.
|
|
100
|
-
4.
|
|
117
|
+
1. Automatically detects editable packages installed via `uv` (using `uv pip list -e`)
|
|
118
|
+
2. Combines editable package paths with `sys.path` (editable paths take precedence)
|
|
119
|
+
3. Identifies subdirectories in each path
|
|
120
|
+
4. Detects when the same directory name appears in multiple locations
|
|
121
|
+
5. Reports which packages are being shadowed and by what
|
|
122
|
+
6. Attempts to identify the installed package that owns each directory
|
|
101
123
|
|
|
102
124
|
Excluded from scanning:
|
|
103
125
|
- Hidden directories (starting with `.`)
|
|
104
126
|
- `__pycache__` directories
|
|
105
127
|
- Package metadata directories (`.dist-info`, `.egg-info`)
|
|
106
128
|
|
|
129
|
+
## Debugging Import Issues
|
|
130
|
+
|
|
131
|
+
If you suspect a module is being imported from the wrong location, you can check where Python is actually loading it from:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
python -c 'import <module>; print(<module>.__file__)'
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
For example:
|
|
138
|
+
```bash
|
|
139
|
+
python -c 'import requests; print(requests.__file__)'
|
|
140
|
+
# Output: /home/user/myproject/requests/__init__.py
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
This will show you the exact file path Python is using for the import, helping you confirm if shadowing is occurring.
|
|
144
|
+
|
|
107
145
|
## Exit Codes
|
|
108
146
|
|
|
109
147
|
- `0`: No conflicts found
|
{detect_shadowed_modules-0.1.0 → detect_shadowed_modules-0.2.0}/detect_shadowed_modules/__init__.py
RENAMED
|
@@ -31,6 +31,8 @@ import sys
|
|
|
31
31
|
from collections import defaultdict
|
|
32
32
|
from pathlib import Path
|
|
33
33
|
|
|
34
|
+
from detect_shadowed_modules.editable import get_editable_paths
|
|
35
|
+
|
|
34
36
|
logger = logging.getLogger(__name__)
|
|
35
37
|
|
|
36
38
|
|
|
@@ -169,11 +171,24 @@ def find_conflicts(paths: list[str] | None = None) -> dict[str, list[Path]]:
|
|
|
169
171
|
return {name: locs for name, locs in dir_locations.items() if len(locs) > 1}
|
|
170
172
|
|
|
171
173
|
|
|
172
|
-
def format_report(
|
|
173
|
-
|
|
174
|
+
def format_report(
|
|
175
|
+
conflicts: dict[str, list[Path]],
|
|
176
|
+
searched_paths: list[str] | None = None,
|
|
177
|
+
editable_paths: set[str] | None = None,
|
|
178
|
+
) -> str:
|
|
179
|
+
"""
|
|
180
|
+
Format conflicts as a human-readable report.
|
|
181
|
+
|
|
182
|
+
Args:
|
|
183
|
+
conflicts: Dict mapping conflicting directory names to their locations.
|
|
184
|
+
searched_paths: List of paths that were searched. Shown when conflicts exist.
|
|
185
|
+
editable_paths: Set of paths that are editable packages.
|
|
186
|
+
"""
|
|
174
187
|
# ANSI color codes
|
|
175
188
|
green = "\033[92m"
|
|
176
189
|
red = "\033[91m"
|
|
190
|
+
yellow = "\033[93m"
|
|
191
|
+
cyan = "\033[96m"
|
|
177
192
|
reset = "\033[0m"
|
|
178
193
|
|
|
179
194
|
if not conflicts:
|
|
@@ -199,11 +214,33 @@ def format_report(conflicts: dict[str, list[Path]]) -> str:
|
|
|
199
214
|
lines.append(f"{prefix}{full_path}{owner_info}{suffix}")
|
|
200
215
|
lines.append("")
|
|
201
216
|
|
|
217
|
+
# Add searched directories section when conflicts exist
|
|
218
|
+
if searched_paths and editable_paths is not None:
|
|
219
|
+
lines.append(
|
|
220
|
+
f"{yellow}Searched directories ({len(searched_paths)} total):{reset}"
|
|
221
|
+
)
|
|
222
|
+
for path_str in searched_paths:
|
|
223
|
+
is_editable = path_str in editable_paths
|
|
224
|
+
marker = f" {cyan}[editable]{reset}" if is_editable else ""
|
|
225
|
+
lines.append(f" {path_str}{marker}")
|
|
226
|
+
lines.append("")
|
|
227
|
+
|
|
202
228
|
return "\n".join(lines)
|
|
203
229
|
|
|
204
230
|
|
|
205
|
-
def format_json(
|
|
206
|
-
|
|
231
|
+
def format_json(
|
|
232
|
+
conflicts: dict[str, list[Path]],
|
|
233
|
+
searched_paths: list[str] | None = None,
|
|
234
|
+
editable_paths: set[str] | None = None,
|
|
235
|
+
) -> str:
|
|
236
|
+
"""
|
|
237
|
+
Format conflicts as JSON, including shadowed directories and what shadows them.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
conflicts: Dict mapping conflicting directory names to their locations.
|
|
241
|
+
searched_paths: List of paths that were searched.
|
|
242
|
+
editable_paths: Set of paths that are editable packages.
|
|
243
|
+
"""
|
|
207
244
|
shadowed = []
|
|
208
245
|
|
|
209
246
|
for name, locations in conflicts.items():
|
|
@@ -224,7 +261,19 @@ def format_json(conflicts: dict[str, list[Path]]) -> str:
|
|
|
224
261
|
}
|
|
225
262
|
)
|
|
226
263
|
|
|
227
|
-
|
|
264
|
+
result = {"shadowed": shadowed}
|
|
265
|
+
|
|
266
|
+
# Add searched paths information
|
|
267
|
+
if searched_paths is not None and editable_paths is not None:
|
|
268
|
+
result["searched_paths"] = [
|
|
269
|
+
{
|
|
270
|
+
"path": path_str,
|
|
271
|
+
"editable": path_str in editable_paths,
|
|
272
|
+
}
|
|
273
|
+
for path_str in searched_paths
|
|
274
|
+
]
|
|
275
|
+
|
|
276
|
+
return json.dumps(result, indent=2)
|
|
228
277
|
|
|
229
278
|
|
|
230
279
|
def main() -> int:
|
|
@@ -250,13 +299,30 @@ def main() -> int:
|
|
|
250
299
|
|
|
251
300
|
setup_logging(quiet=args.quiet or args.json_output)
|
|
252
301
|
|
|
302
|
+
# Get editable package paths
|
|
303
|
+
editable_paths_list = get_editable_paths()
|
|
304
|
+
logger.debug(f"Found {len(editable_paths_list)} editable package(s)")
|
|
305
|
+
|
|
306
|
+
# Track which paths are editable (as strings for easy comparison)
|
|
307
|
+
editable_paths_set = {str(p) for p in editable_paths_list}
|
|
308
|
+
|
|
309
|
+
# Combine editable paths with sys.path (editable paths first, then deduplicate)
|
|
310
|
+
combined_paths = [str(p) for p in editable_paths_list] + sys.path
|
|
311
|
+
# Deduplicate while preserving order
|
|
312
|
+
seen = set()
|
|
313
|
+
unique_paths = []
|
|
314
|
+
for path in combined_paths:
|
|
315
|
+
if path not in seen:
|
|
316
|
+
seen.add(path)
|
|
317
|
+
unique_paths.append(path)
|
|
318
|
+
|
|
253
319
|
logger.debug("Scanning sys.path for directory conflicts...")
|
|
254
|
-
conflicts = find_conflicts()
|
|
320
|
+
conflicts = find_conflicts(unique_paths)
|
|
255
321
|
|
|
256
322
|
if args.json_output:
|
|
257
|
-
print(format_json(conflicts))
|
|
323
|
+
print(format_json(conflicts, unique_paths, editable_paths_set))
|
|
258
324
|
else:
|
|
259
|
-
print(format_report(conflicts))
|
|
325
|
+
print(format_report(conflicts, unique_paths, editable_paths_set))
|
|
260
326
|
|
|
261
327
|
return 1 if conflicts else 0
|
|
262
328
|
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Get paths to editable packages installed via uv.
|
|
3
|
+
|
|
4
|
+
This module provides functionality to discover editable package locations
|
|
5
|
+
by querying uv's package list.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import logging
|
|
10
|
+
import subprocess
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_editable_paths() -> list[Path]:
|
|
17
|
+
"""
|
|
18
|
+
Get paths to editable packages by running uv pip list.
|
|
19
|
+
|
|
20
|
+
Runs: uv pip list -e --format json
|
|
21
|
+
|
|
22
|
+
The JSON output from uv looks like:
|
|
23
|
+
[{"name":"pkg","version":"0.1.0","editable_project_location":"/path/to/pkg"}]
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
List of paths from editable_project_location field.
|
|
27
|
+
Returns empty list if uv is unavailable or fails.
|
|
28
|
+
"""
|
|
29
|
+
try:
|
|
30
|
+
result = subprocess.run(
|
|
31
|
+
["uv", "pip", "list", "-e", "--format", "json"],
|
|
32
|
+
capture_output=True,
|
|
33
|
+
text=True,
|
|
34
|
+
timeout=10,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
if result.returncode != 0:
|
|
38
|
+
logger.debug(f"uv pip list failed with code {result.returncode}")
|
|
39
|
+
return []
|
|
40
|
+
|
|
41
|
+
# Parse JSON output
|
|
42
|
+
packages = json.loads(result.stdout)
|
|
43
|
+
|
|
44
|
+
# Extract editable_project_location from each package
|
|
45
|
+
paths = []
|
|
46
|
+
for pkg in packages:
|
|
47
|
+
if "editable_project_location" in pkg:
|
|
48
|
+
path = Path(pkg["editable_project_location"])
|
|
49
|
+
paths.append(path)
|
|
50
|
+
logger.debug(f"Found editable package: {pkg.get('name')} at {path}")
|
|
51
|
+
|
|
52
|
+
return paths
|
|
53
|
+
|
|
54
|
+
except FileNotFoundError:
|
|
55
|
+
# uv is not installed or not in PATH
|
|
56
|
+
logger.debug("uv not found in PATH")
|
|
57
|
+
return []
|
|
58
|
+
except json.JSONDecodeError as e:
|
|
59
|
+
logger.debug(f"Failed to parse uv output as JSON: {e}")
|
|
60
|
+
return []
|
|
61
|
+
except subprocess.TimeoutExpired:
|
|
62
|
+
logger.debug("uv pip list timed out")
|
|
63
|
+
return []
|
|
64
|
+
except Exception as e:
|
|
65
|
+
logger.debug(f"Unexpected error getting editable paths: {e}")
|
|
66
|
+
return []
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "detect-shadowed-modules"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
description = "Detect conflicting directory names across Python's sys.path that shadow installed packages"
|
|
5
5
|
keywords = ["import", "shadowing", "diagnostics"]
|
|
6
6
|
readme = "README.md"
|