detect-shadowed-modules 0.1.0__tar.gz → 0.2.1__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.2.1/PKG-INFO +180 -0
- detect_shadowed_modules-0.1.0/PKG-INFO → detect_shadowed_modules-0.2.1/README.md +57 -20
- {detect_shadowed_modules-0.1.0 → detect_shadowed_modules-0.2.1}/detect_shadowed_modules/__init__.py +83 -10
- detect_shadowed_modules-0.2.1/detect_shadowed_modules/editable.py +67 -0
- detect_shadowed_modules-0.2.1/detect_shadowed_modules/version.py +34 -0
- detect_shadowed_modules-0.2.1/pyproject.toml +68 -0
- detect_shadowed_modules-0.2.1/pyproject.toml.orig +57 -0
- detect_shadowed_modules-0.1.0/README.md +0 -114
- detect_shadowed_modules-0.1.0/pyproject.toml +0 -31
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: detect-shadowed-modules
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Detect conflicting directory names across Python's sys.path that shadow installed packages
|
|
5
|
+
Keywords: import,shadowing,diagnostics
|
|
6
|
+
Author: Michael Bianco
|
|
7
|
+
Author-email: Michael Bianco <mike@mikebian.co>
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Project-URL: Repository, https://github.com/iloveitaly/detect-shadowed-modules
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# Detect Python Shadowed Modules
|
|
20
|
+
|
|
21
|
+
Detect conflicting directory names across Python's `sys.path` that may be shadowing your installed packages.
|
|
22
|
+
|
|
23
|
+
Common cases where this happened:
|
|
24
|
+
|
|
25
|
+
* Using an editable package. All folders within the editable package dir (including `tests`) are
|
|
26
|
+
added to `sys.path` and there is no way to define precedence (at least, as of 2026-01-24 when using `uv`)
|
|
27
|
+
* A package that has been improperly packaged with something like a `tests` directory.
|
|
28
|
+
* There are two different types of Python package distributions: a source distribution and a built distribution.
|
|
29
|
+
A source distribution can be downloaded on PyPy using the tar.gzip, where a built distribution is the wheel with the extension `whl`.
|
|
30
|
+
These distributions may not use the same distribution script and can contain different contents.
|
|
31
|
+
|
|
32
|
+
If you like this project, you'll probably enjoy [uv-development-toggle](https://github.com/iloveitaly/uv-development-toggle)
|
|
33
|
+
## Problem
|
|
34
|
+
|
|
35
|
+
When Python imports a module, it searches directories in `sys.path` order. If the same directory name exists in multiple `sys.path` entries, only the first one is used—potentially shadowing packages you intended to import. This can lead to confusing import errors and unexpected behavior.
|
|
36
|
+
|
|
37
|
+
### Example Conflict Scenario
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
sys.path = ["/project", "/usr/lib/python3/site-packages"]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If both paths contain a `requests/` directory:
|
|
44
|
+
- `/project/requests/` ← This shadows the real package
|
|
45
|
+
- `/usr/lib/python3/site-packages/requests/`
|
|
46
|
+
|
|
47
|
+
When you `import requests`, Python will use `/project/requests/` instead of the installed package, which is likely not what you want!
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
uv add detect-shadowed-modules
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Or, you can just use via uvx:
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
uvx detect-shadowed-modules@latest --help
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
### Command Line
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# Print conflicts to stdout with scanning progress
|
|
67
|
+
detect-shadowed-modules
|
|
68
|
+
|
|
69
|
+
detect-shadowed-modules --json
|
|
70
|
+
|
|
71
|
+
# Quiet mode (conflicts only, no progress messages)
|
|
72
|
+
detect-shadowed-modules -q
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### As a Python Module
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
import detect_shadowed_modules
|
|
79
|
+
|
|
80
|
+
# Find conflicts
|
|
81
|
+
conflicts = detect_shadowed_modules.find_conflicts()
|
|
82
|
+
|
|
83
|
+
# Generate a human-readable report
|
|
84
|
+
report = detect_shadowed_modules.format_report(conflicts)
|
|
85
|
+
print(report)
|
|
86
|
+
|
|
87
|
+
# Or get JSON output
|
|
88
|
+
json_output = detect_shadowed_modules.format_json(conflicts)
|
|
89
|
+
print(json_output)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Output Examples
|
|
93
|
+
|
|
94
|
+
### Human-Readable Format
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
Conflicting directory names detected:
|
|
98
|
+
|
|
99
|
+
These directories exist in multiple sys.path locations. The first
|
|
100
|
+
location listed will shadow the others during import.
|
|
101
|
+
|
|
102
|
+
requests/
|
|
103
|
+
→ /home/user/myproject/requests (shadows others)
|
|
104
|
+
/usr/lib/python3/site-packages/requests [requests] (shadowed)
|
|
105
|
+
|
|
106
|
+
Searched directories (50 total):
|
|
107
|
+
/home/user/myproject [editable]
|
|
108
|
+
/home/user/.venv/lib/python3.13/site-packages
|
|
109
|
+
/usr/lib/python3/site-packages
|
|
110
|
+
...
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### JSON Format
|
|
114
|
+
|
|
115
|
+
```json
|
|
116
|
+
{
|
|
117
|
+
"shadowed": [
|
|
118
|
+
{
|
|
119
|
+
"path": "/usr/lib/python3/site-packages/requests",
|
|
120
|
+
"shadowed_by": "/home/user/myproject/requests",
|
|
121
|
+
"owner": "requests"
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
"searched_paths": [
|
|
125
|
+
{
|
|
126
|
+
"path": "/home/user/myproject",
|
|
127
|
+
"editable": true
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"path": "/home/user/.venv/lib/python3.13/site-packages",
|
|
131
|
+
"editable": false
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
"path": "/usr/lib/python3/site-packages",
|
|
135
|
+
"editable": false
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## How It Works
|
|
142
|
+
|
|
143
|
+
The tool scans all directories in `sys.path` and:
|
|
144
|
+
1. Automatically detects editable packages installed via `uv` (using `uv pip list -e`)
|
|
145
|
+
2. Combines editable package paths with `sys.path` (editable paths take precedence)
|
|
146
|
+
3. Identifies subdirectories in each path
|
|
147
|
+
4. Detects when the same directory name appears in multiple locations
|
|
148
|
+
5. Reports which packages are being shadowed and by what
|
|
149
|
+
6. Attempts to identify the installed package that owns each directory
|
|
150
|
+
|
|
151
|
+
Excluded from scanning:
|
|
152
|
+
|
|
153
|
+
- Hidden directories (starting with `.`)
|
|
154
|
+
- `__pycache__` directories
|
|
155
|
+
- Package metadata directories (`.dist-info`, `.egg-info`)
|
|
156
|
+
|
|
157
|
+
## Debugging Import Issues
|
|
158
|
+
|
|
159
|
+
If you suspect a module is being imported from the wrong location, you can check where Python is actually loading it from:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
python -c 'import <module>; print(<module>.__file__)'
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
For example:
|
|
166
|
+
```bash
|
|
167
|
+
python -c 'import requests; print(requests.__file__)'
|
|
168
|
+
# Output: /home/user/myproject/requests/__init__.py
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This will show you the exact file path Python is using for the import, helping you confirm if shadowing is occurring.
|
|
172
|
+
|
|
173
|
+
## Exit Codes
|
|
174
|
+
|
|
175
|
+
- `0`: No conflicts found
|
|
176
|
+
- `1`: Conflicts detected
|
|
177
|
+
|
|
178
|
+
This makes it easy to use in CI/CD pipelines or pre-commit hooks.
|
|
179
|
+
|
|
180
|
+
## [MIT License](LICENSE.md)
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
Name: detect-shadowed-modules
|
|
3
|
-
Version: 0.1.0
|
|
4
|
-
Summary: Detect conflicting directory names across Python's sys.path that shadow installed packages
|
|
5
|
-
Keywords: import,shadowing,diagnostics
|
|
6
|
-
Author: Michael Bianco
|
|
7
|
-
Author-email: Michael Bianco <mike@mikebian.co>
|
|
8
|
-
Requires-Python: >=3.11
|
|
9
|
-
Project-URL: Repository, https://github.com/iloveitaly/detect-shadowed-modules
|
|
10
|
-
Description-Content-Type: text/markdown
|
|
11
|
-
|
|
12
|
-
# detect-shadowed-modules
|
|
1
|
+
# Detect Python Shadowed Modules
|
|
13
2
|
|
|
14
3
|
Detect conflicting directory names across Python's `sys.path` that may be shadowing your installed packages.
|
|
15
4
|
|
|
5
|
+
Common cases where this happened:
|
|
6
|
+
|
|
7
|
+
* Using an editable package. All folders within the editable package dir (including `tests`) are
|
|
8
|
+
added to `sys.path` and there is no way to define precedence (at least, as of 2026-01-24 when using `uv`)
|
|
9
|
+
* A package that has been improperly packaged with something like a `tests` directory.
|
|
10
|
+
* There are two different types of Python package distributions: a source distribution and a built distribution.
|
|
11
|
+
A source distribution can be downloaded on PyPy using the tar.gzip, where a built distribution is the wheel with the extension `whl`.
|
|
12
|
+
These distributions may not use the same distribution script and can contain different contents.
|
|
13
|
+
|
|
14
|
+
If you like this project, you'll probably enjoy [uv-development-toggle](https://github.com/iloveitaly/uv-development-toggle)
|
|
16
15
|
## Problem
|
|
17
16
|
|
|
18
17
|
When Python imports a module, it searches directories in `sys.path` order. If the same directory name exists in multiple `sys.path` entries, only the first one is used—potentially shadowing packages you intended to import. This can lead to confusing import errors and unexpected behavior.
|
|
@@ -20,7 +19,7 @@ When Python imports a module, it searches directories in `sys.path` order. If th
|
|
|
20
19
|
### Example Conflict Scenario
|
|
21
20
|
|
|
22
21
|
```python
|
|
23
|
-
sys.path = [
|
|
22
|
+
sys.path = ["/project", "/usr/lib/python3/site-packages"]
|
|
24
23
|
```
|
|
25
24
|
|
|
26
25
|
If both paths contain a `requests/` directory:
|
|
@@ -49,11 +48,10 @@ uvx detect-shadowed-modules@latest --help
|
|
|
49
48
|
# Print conflicts to stdout with scanning progress
|
|
50
49
|
detect-shadowed-modules
|
|
51
50
|
|
|
51
|
+
detect-shadowed-modules --json
|
|
52
|
+
|
|
52
53
|
# Quiet mode (conflicts only, no progress messages)
|
|
53
54
|
detect-shadowed-modules -q
|
|
54
|
-
|
|
55
|
-
# Output as JSON
|
|
56
|
-
detect-shadowed-modules --json
|
|
57
55
|
```
|
|
58
56
|
|
|
59
57
|
### As a Python Module
|
|
@@ -86,6 +84,12 @@ location listed will shadow the others during import.
|
|
|
86
84
|
requests/
|
|
87
85
|
→ /home/user/myproject/requests (shadows others)
|
|
88
86
|
/usr/lib/python3/site-packages/requests [requests] (shadowed)
|
|
87
|
+
|
|
88
|
+
Searched directories (50 total):
|
|
89
|
+
/home/user/myproject [editable]
|
|
90
|
+
/home/user/.venv/lib/python3.13/site-packages
|
|
91
|
+
/usr/lib/python3/site-packages
|
|
92
|
+
...
|
|
89
93
|
```
|
|
90
94
|
|
|
91
95
|
### JSON Format
|
|
@@ -98,6 +102,20 @@ location listed will shadow the others during import.
|
|
|
98
102
|
"shadowed_by": "/home/user/myproject/requests",
|
|
99
103
|
"owner": "requests"
|
|
100
104
|
}
|
|
105
|
+
],
|
|
106
|
+
"searched_paths": [
|
|
107
|
+
{
|
|
108
|
+
"path": "/home/user/myproject",
|
|
109
|
+
"editable": true
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"path": "/home/user/.venv/lib/python3.13/site-packages",
|
|
113
|
+
"editable": false
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"path": "/usr/lib/python3/site-packages",
|
|
117
|
+
"editable": false
|
|
118
|
+
}
|
|
101
119
|
]
|
|
102
120
|
}
|
|
103
121
|
```
|
|
@@ -105,16 +123,35 @@ location listed will shadow the others during import.
|
|
|
105
123
|
## How It Works
|
|
106
124
|
|
|
107
125
|
The tool scans all directories in `sys.path` and:
|
|
108
|
-
1.
|
|
109
|
-
2.
|
|
110
|
-
3.
|
|
111
|
-
4.
|
|
126
|
+
1. Automatically detects editable packages installed via `uv` (using `uv pip list -e`)
|
|
127
|
+
2. Combines editable package paths with `sys.path` (editable paths take precedence)
|
|
128
|
+
3. Identifies subdirectories in each path
|
|
129
|
+
4. Detects when the same directory name appears in multiple locations
|
|
130
|
+
5. Reports which packages are being shadowed and by what
|
|
131
|
+
6. Attempts to identify the installed package that owns each directory
|
|
112
132
|
|
|
113
133
|
Excluded from scanning:
|
|
134
|
+
|
|
114
135
|
- Hidden directories (starting with `.`)
|
|
115
136
|
- `__pycache__` directories
|
|
116
137
|
- Package metadata directories (`.dist-info`, `.egg-info`)
|
|
117
138
|
|
|
139
|
+
## Debugging Import Issues
|
|
140
|
+
|
|
141
|
+
If you suspect a module is being imported from the wrong location, you can check where Python is actually loading it from:
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
python -c 'import <module>; print(<module>.__file__)'
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For example:
|
|
148
|
+
```bash
|
|
149
|
+
python -c 'import requests; print(requests.__file__)'
|
|
150
|
+
# Output: /home/user/myproject/requests/__init__.py
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
This will show you the exact file path Python is using for the import, helping you confirm if shadowing is occurring.
|
|
154
|
+
|
|
118
155
|
## Exit Codes
|
|
119
156
|
|
|
120
157
|
- `0`: No conflicts found
|
{detect_shadowed_modules-0.1.0 → detect_shadowed_modules-0.2.1}/detect_shadowed_modules/__init__.py
RENAMED
|
@@ -31,6 +31,10 @@ 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
|
+
|
|
36
|
+
from .version import __version__
|
|
37
|
+
|
|
34
38
|
logger = logging.getLogger(__name__)
|
|
35
39
|
|
|
36
40
|
|
|
@@ -68,8 +72,7 @@ def get_subdirectories(path: Path) -> list[str]:
|
|
|
68
72
|
if (
|
|
69
73
|
name.startswith(".")
|
|
70
74
|
or name == "__pycache__"
|
|
71
|
-
or name.endswith(".dist-info")
|
|
72
|
-
or name.endswith(".egg-info")
|
|
75
|
+
or name.endswith((".dist-info", ".egg-info"))
|
|
73
76
|
):
|
|
74
77
|
logger.debug(f" Skipping: {name}/")
|
|
75
78
|
continue
|
|
@@ -169,11 +172,24 @@ def find_conflicts(paths: list[str] | None = None) -> dict[str, list[Path]]:
|
|
|
169
172
|
return {name: locs for name, locs in dir_locations.items() if len(locs) > 1}
|
|
170
173
|
|
|
171
174
|
|
|
172
|
-
def format_report(
|
|
173
|
-
|
|
175
|
+
def format_report(
|
|
176
|
+
conflicts: dict[str, list[Path]],
|
|
177
|
+
searched_paths: list[str] | None = None,
|
|
178
|
+
editable_paths: set[str] | None = None,
|
|
179
|
+
) -> str:
|
|
180
|
+
"""
|
|
181
|
+
Format conflicts as a human-readable report.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
conflicts: Dict mapping conflicting directory names to their locations.
|
|
185
|
+
searched_paths: List of paths that were searched. Shown when conflicts exist.
|
|
186
|
+
editable_paths: Set of paths that are editable packages.
|
|
187
|
+
"""
|
|
174
188
|
# ANSI color codes
|
|
175
189
|
green = "\033[92m"
|
|
176
190
|
red = "\033[91m"
|
|
191
|
+
yellow = "\033[93m"
|
|
192
|
+
cyan = "\033[96m"
|
|
177
193
|
reset = "\033[0m"
|
|
178
194
|
|
|
179
195
|
if not conflicts:
|
|
@@ -199,11 +215,33 @@ def format_report(conflicts: dict[str, list[Path]]) -> str:
|
|
|
199
215
|
lines.append(f"{prefix}{full_path}{owner_info}{suffix}")
|
|
200
216
|
lines.append("")
|
|
201
217
|
|
|
218
|
+
# Add searched directories section when conflicts exist
|
|
219
|
+
if searched_paths and editable_paths is not None:
|
|
220
|
+
lines.append(
|
|
221
|
+
f"{yellow}Searched directories ({len(searched_paths)} total):{reset}"
|
|
222
|
+
)
|
|
223
|
+
for path_str in searched_paths:
|
|
224
|
+
is_editable = path_str in editable_paths
|
|
225
|
+
marker = f" {cyan}[editable]{reset}" if is_editable else ""
|
|
226
|
+
lines.append(f" {path_str}{marker}")
|
|
227
|
+
lines.append("")
|
|
228
|
+
|
|
202
229
|
return "\n".join(lines)
|
|
203
230
|
|
|
204
231
|
|
|
205
|
-
def format_json(
|
|
206
|
-
|
|
232
|
+
def format_json(
|
|
233
|
+
conflicts: dict[str, list[Path]],
|
|
234
|
+
searched_paths: list[str] | None = None,
|
|
235
|
+
editable_paths: set[str] | None = None,
|
|
236
|
+
) -> str:
|
|
237
|
+
"""
|
|
238
|
+
Format conflicts as JSON, including shadowed directories and what shadows them.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
conflicts: Dict mapping conflicting directory names to their locations.
|
|
242
|
+
searched_paths: List of paths that were searched.
|
|
243
|
+
editable_paths: Set of paths that are editable packages.
|
|
244
|
+
"""
|
|
207
245
|
shadowed = []
|
|
208
246
|
|
|
209
247
|
for name, locations in conflicts.items():
|
|
@@ -224,7 +262,19 @@ def format_json(conflicts: dict[str, list[Path]]) -> str:
|
|
|
224
262
|
}
|
|
225
263
|
)
|
|
226
264
|
|
|
227
|
-
|
|
265
|
+
result = {"shadowed": shadowed}
|
|
266
|
+
|
|
267
|
+
# Add searched paths information
|
|
268
|
+
if searched_paths is not None and editable_paths is not None:
|
|
269
|
+
result["searched_paths"] = [
|
|
270
|
+
{
|
|
271
|
+
"path": path_str,
|
|
272
|
+
"editable": path_str in editable_paths,
|
|
273
|
+
}
|
|
274
|
+
for path_str in searched_paths
|
|
275
|
+
]
|
|
276
|
+
|
|
277
|
+
return json.dumps(result, indent=2)
|
|
228
278
|
|
|
229
279
|
|
|
230
280
|
def main() -> int:
|
|
@@ -246,17 +296,40 @@ def main() -> int:
|
|
|
246
296
|
dest="json_output",
|
|
247
297
|
help="Output as JSON",
|
|
248
298
|
)
|
|
299
|
+
parser.add_argument(
|
|
300
|
+
"-V",
|
|
301
|
+
"--version",
|
|
302
|
+
action="version",
|
|
303
|
+
version=f"detect-shadowed-modules version {__version__}",
|
|
304
|
+
)
|
|
249
305
|
args = parser.parse_args()
|
|
250
306
|
|
|
251
307
|
setup_logging(quiet=args.quiet or args.json_output)
|
|
252
308
|
|
|
309
|
+
# Get editable package paths
|
|
310
|
+
editable_paths_list = get_editable_paths()
|
|
311
|
+
logger.debug(f"Found {len(editable_paths_list)} editable package(s)")
|
|
312
|
+
|
|
313
|
+
# Track which paths are editable (as strings for easy comparison)
|
|
314
|
+
editable_paths_set = {str(p) for p in editable_paths_list}
|
|
315
|
+
|
|
316
|
+
# Combine editable paths with sys.path (editable paths first, then deduplicate)
|
|
317
|
+
combined_paths = [str(p) for p in editable_paths_list] + sys.path
|
|
318
|
+
# Deduplicate while preserving order
|
|
319
|
+
seen = set()
|
|
320
|
+
unique_paths = []
|
|
321
|
+
for path in combined_paths:
|
|
322
|
+
if path not in seen:
|
|
323
|
+
seen.add(path)
|
|
324
|
+
unique_paths.append(path)
|
|
325
|
+
|
|
253
326
|
logger.debug("Scanning sys.path for directory conflicts...")
|
|
254
|
-
conflicts = find_conflicts()
|
|
327
|
+
conflicts = find_conflicts(unique_paths)
|
|
255
328
|
|
|
256
329
|
if args.json_output:
|
|
257
|
-
print(format_json(conflicts))
|
|
330
|
+
print(format_json(conflicts, unique_paths, editable_paths_set))
|
|
258
331
|
else:
|
|
259
|
-
print(format_report(conflicts))
|
|
332
|
+
print(format_report(conflicts, unique_paths, editable_paths_set))
|
|
260
333
|
|
|
261
334
|
return 1 if conflicts else 0
|
|
262
335
|
|
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
check=False,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
if result.returncode != 0:
|
|
39
|
+
logger.debug(f"uv pip list failed with code {result.returncode}")
|
|
40
|
+
return []
|
|
41
|
+
|
|
42
|
+
# Parse JSON output
|
|
43
|
+
packages = json.loads(result.stdout)
|
|
44
|
+
|
|
45
|
+
# Extract editable_project_location from each package
|
|
46
|
+
paths = []
|
|
47
|
+
for pkg in packages:
|
|
48
|
+
if "editable_project_location" in pkg:
|
|
49
|
+
path = Path(pkg["editable_project_location"])
|
|
50
|
+
paths.append(path)
|
|
51
|
+
logger.debug(f"Found editable package: {pkg.get('name')} at {path}")
|
|
52
|
+
|
|
53
|
+
return paths
|
|
54
|
+
|
|
55
|
+
except FileNotFoundError:
|
|
56
|
+
# uv is not installed or not in PATH
|
|
57
|
+
logger.debug("uv not found in PATH")
|
|
58
|
+
return []
|
|
59
|
+
except json.JSONDecodeError as e:
|
|
60
|
+
logger.debug(f"Failed to parse uv output as JSON: {e}")
|
|
61
|
+
return []
|
|
62
|
+
except subprocess.TimeoutExpired:
|
|
63
|
+
logger.debug("uv pip list timed out")
|
|
64
|
+
return []
|
|
65
|
+
except Exception as e: # noqa: BLE001
|
|
66
|
+
logger.debug(f"Unexpected error getting editable paths: {e}")
|
|
67
|
+
return []
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Version handling for detect-shadowed-modules."""
|
|
2
|
+
|
|
3
|
+
import importlib.metadata
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def is_local_source_checkout() -> bool:
|
|
8
|
+
"""Check if the code is running from a local source checkout."""
|
|
9
|
+
package_dir = Path(__file__).resolve().parent
|
|
10
|
+
# Since this is a flat layout (module-root = ""), repo root is the parent of the package dir
|
|
11
|
+
repo_root = package_dir.parent
|
|
12
|
+
|
|
13
|
+
return (repo_root / ".git").exists() and (repo_root / "pyproject.toml").exists()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_version() -> str:
|
|
17
|
+
"""Get the version string, appending .dev if running from source."""
|
|
18
|
+
try:
|
|
19
|
+
# Try to get the version of the installed package
|
|
20
|
+
version = importlib.metadata.version("detect-shadowed-modules")
|
|
21
|
+
except importlib.metadata.PackageNotFoundError:
|
|
22
|
+
# Fallback for local development if not installed
|
|
23
|
+
version = "0.1.0"
|
|
24
|
+
|
|
25
|
+
if not is_local_source_checkout():
|
|
26
|
+
return version
|
|
27
|
+
|
|
28
|
+
if version.endswith(".dev"):
|
|
29
|
+
return version
|
|
30
|
+
|
|
31
|
+
return f"{version}.dev"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
__version__ = get_version()
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "detect-shadowed-modules"
|
|
3
|
+
version = "0.2.1"
|
|
4
|
+
description = "Detect conflicting directory names across Python's sys.path that shadow installed packages"
|
|
5
|
+
keywords = [
|
|
6
|
+
"import",
|
|
7
|
+
"shadowing",
|
|
8
|
+
"diagnostics",
|
|
9
|
+
]
|
|
10
|
+
readme = "README.md"
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Programming Language :: Python :: 3.14",
|
|
19
|
+
"Programming Language :: Python :: 3.15",
|
|
20
|
+
]
|
|
21
|
+
dependencies = []
|
|
22
|
+
|
|
23
|
+
[[project.authors]]
|
|
24
|
+
name = "Michael Bianco"
|
|
25
|
+
email = "mike@mikebian.co"
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Repository = "https://github.com/iloveitaly/detect-shadowed-modules"
|
|
29
|
+
|
|
30
|
+
[project.scripts]
|
|
31
|
+
detect-shadowed-modules = "detect_shadowed_modules:main"
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["uv_build>=0.11.0,<0.14"]
|
|
35
|
+
build-backend = "uv_build"
|
|
36
|
+
|
|
37
|
+
[tool.uv.build-backend]
|
|
38
|
+
module-root = ""
|
|
39
|
+
|
|
40
|
+
[tool.pyright]
|
|
41
|
+
exclude = [
|
|
42
|
+
"examples/",
|
|
43
|
+
"playground/",
|
|
44
|
+
"tmp/",
|
|
45
|
+
".venv/",
|
|
46
|
+
"tests/",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
addopts = "--cov --cov-report=term-missing --cov-report=html:tmp/htmlcov"
|
|
51
|
+
|
|
52
|
+
[tool.coverage.run]
|
|
53
|
+
plugins = ["covdefaults"]
|
|
54
|
+
source = ["detect_shadowed_modules"]
|
|
55
|
+
|
|
56
|
+
[tool.coverage.report]
|
|
57
|
+
fail_under = 50
|
|
58
|
+
|
|
59
|
+
[dependency-groups]
|
|
60
|
+
dev = [
|
|
61
|
+
"pytest>=8.3.4",
|
|
62
|
+
"pyright[nodejs]>=1.1.408",
|
|
63
|
+
"ruff>=0.15.0",
|
|
64
|
+
"coverage>=7.13.4",
|
|
65
|
+
"pytest-cov>=7.0.0",
|
|
66
|
+
"covdefaults>=2.3.0",
|
|
67
|
+
"beautiful-traceback>=1.0.2; python_version >= '3.12'",
|
|
68
|
+
]
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "detect-shadowed-modules"
|
|
3
|
+
version = "0.2.1"
|
|
4
|
+
description = "Detect conflicting directory names across Python's sys.path that shadow installed packages"
|
|
5
|
+
keywords = ["import", "shadowing", "diagnostics"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Programming Language :: Python :: 3",
|
|
10
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
11
|
+
"Programming Language :: Python :: 3.11",
|
|
12
|
+
"Programming Language :: Python :: 3.12",
|
|
13
|
+
"Programming Language :: Python :: 3.13",
|
|
14
|
+
"Programming Language :: Python :: 3.14",
|
|
15
|
+
"Programming Language :: Python :: 3.15",
|
|
16
|
+
]
|
|
17
|
+
dependencies = []
|
|
18
|
+
authors = [{ name = "Michael Bianco", email = "mike@mikebian.co" }]
|
|
19
|
+
urls = { "Repository" = "https://github.com/iloveitaly/detect-shadowed-modules" }
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# additional packaging information: https://packaging.python.org/en/latest/specifications/core-metadata/#license
|
|
23
|
+
[project.scripts]
|
|
24
|
+
detect-shadowed-modules = "detect_shadowed_modules:main"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ["uv_build>=0.11.0,<0.14"]
|
|
29
|
+
build-backend = "uv_build"
|
|
30
|
+
|
|
31
|
+
[tool.uv.build-backend]
|
|
32
|
+
# avoids the src/ directory structure
|
|
33
|
+
module-root = ""
|
|
34
|
+
|
|
35
|
+
[dependency-groups]
|
|
36
|
+
dev = [
|
|
37
|
+
"pytest>=8.3.4",
|
|
38
|
+
"pyright[nodejs]>=1.1.408",
|
|
39
|
+
"ruff>=0.15.0",
|
|
40
|
+
"coverage>=7.13.4",
|
|
41
|
+
"pytest-cov>=7.0.0",
|
|
42
|
+
"covdefaults>=2.3.0",
|
|
43
|
+
"beautiful-traceback>=1.0.2; python_version >= '3.12'",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[tool.pyright]
|
|
47
|
+
exclude = ["examples/", "playground/", "tmp/", ".venv/", "tests/"]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
addopts = "--cov --cov-report=term-missing --cov-report=html:tmp/htmlcov"
|
|
51
|
+
|
|
52
|
+
[tool.coverage.run]
|
|
53
|
+
plugins = ["covdefaults"]
|
|
54
|
+
source = ["detect_shadowed_modules"]
|
|
55
|
+
|
|
56
|
+
[tool.coverage.report]
|
|
57
|
+
fail_under = 50
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
# detect-shadowed-modules
|
|
2
|
-
|
|
3
|
-
Detect conflicting directory names across Python's `sys.path` that may be shadowing your installed packages.
|
|
4
|
-
|
|
5
|
-
## Problem
|
|
6
|
-
|
|
7
|
-
When Python imports a module, it searches directories in `sys.path` order. If the same directory name exists in multiple `sys.path` entries, only the first one is used—potentially shadowing packages you intended to import. This can lead to confusing import errors and unexpected behavior.
|
|
8
|
-
|
|
9
|
-
### Example Conflict Scenario
|
|
10
|
-
|
|
11
|
-
```python
|
|
12
|
-
sys.path = ['/project', '/usr/lib/python3/site-packages']
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
If both paths contain a `requests/` directory:
|
|
16
|
-
- `/project/requests/` ← This shadows the real package
|
|
17
|
-
- `/usr/lib/python3/site-packages/requests/`
|
|
18
|
-
|
|
19
|
-
When you `import requests`, Python will use `/project/requests/` instead of the installed package, which is likely not what you want!
|
|
20
|
-
|
|
21
|
-
## Installation
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
uv add detect-shadowed-modules
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
Or, you can just use via uvx:
|
|
28
|
-
|
|
29
|
-
```sh
|
|
30
|
-
uvx detect-shadowed-modules@latest --help
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## Usage
|
|
34
|
-
|
|
35
|
-
### Command Line
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
# Print conflicts to stdout with scanning progress
|
|
39
|
-
detect-shadowed-modules
|
|
40
|
-
|
|
41
|
-
# Quiet mode (conflicts only, no progress messages)
|
|
42
|
-
detect-shadowed-modules -q
|
|
43
|
-
|
|
44
|
-
# Output as JSON
|
|
45
|
-
detect-shadowed-modules --json
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
### As a Python Module
|
|
49
|
-
|
|
50
|
-
```python
|
|
51
|
-
import detect_shadowed_modules
|
|
52
|
-
|
|
53
|
-
# Find conflicts
|
|
54
|
-
conflicts = detect_shadowed_modules.find_conflicts()
|
|
55
|
-
|
|
56
|
-
# Generate a human-readable report
|
|
57
|
-
report = detect_shadowed_modules.format_report(conflicts)
|
|
58
|
-
print(report)
|
|
59
|
-
|
|
60
|
-
# Or get JSON output
|
|
61
|
-
json_output = detect_shadowed_modules.format_json(conflicts)
|
|
62
|
-
print(json_output)
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Output Examples
|
|
66
|
-
|
|
67
|
-
### Human-Readable Format
|
|
68
|
-
|
|
69
|
-
```
|
|
70
|
-
Conflicting directory names detected:
|
|
71
|
-
|
|
72
|
-
These directories exist in multiple sys.path locations. The first
|
|
73
|
-
location listed will shadow the others during import.
|
|
74
|
-
|
|
75
|
-
requests/
|
|
76
|
-
→ /home/user/myproject/requests (shadows others)
|
|
77
|
-
/usr/lib/python3/site-packages/requests [requests] (shadowed)
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### JSON Format
|
|
81
|
-
|
|
82
|
-
```json
|
|
83
|
-
{
|
|
84
|
-
"shadowed": [
|
|
85
|
-
{
|
|
86
|
-
"path": "/usr/lib/python3/site-packages/requests",
|
|
87
|
-
"shadowed_by": "/home/user/myproject/requests",
|
|
88
|
-
"owner": "requests"
|
|
89
|
-
}
|
|
90
|
-
]
|
|
91
|
-
}
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
## How It Works
|
|
95
|
-
|
|
96
|
-
The tool scans all directories in `sys.path` and:
|
|
97
|
-
1. Identifies subdirectories in each path
|
|
98
|
-
2. Detects when the same directory name appears in multiple locations
|
|
99
|
-
3. Reports which packages are being shadowed and by what
|
|
100
|
-
4. Attempts to identify the installed package that owns each directory
|
|
101
|
-
|
|
102
|
-
Excluded from scanning:
|
|
103
|
-
- Hidden directories (starting with `.`)
|
|
104
|
-
- `__pycache__` directories
|
|
105
|
-
- Package metadata directories (`.dist-info`, `.egg-info`)
|
|
106
|
-
|
|
107
|
-
## Exit Codes
|
|
108
|
-
|
|
109
|
-
- `0`: No conflicts found
|
|
110
|
-
- `1`: Conflicts detected
|
|
111
|
-
|
|
112
|
-
This makes it easy to use in CI/CD pipelines or pre-commit hooks.
|
|
113
|
-
|
|
114
|
-
## [MIT License](LICENSE.md)
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
[project]
|
|
2
|
-
name = "detect-shadowed-modules"
|
|
3
|
-
version = "0.1.0"
|
|
4
|
-
description = "Detect conflicting directory names across Python's sys.path that shadow installed packages"
|
|
5
|
-
keywords = ["import", "shadowing", "diagnostics"]
|
|
6
|
-
readme = "README.md"
|
|
7
|
-
requires-python = ">=3.11"
|
|
8
|
-
dependencies = []
|
|
9
|
-
authors = [{ name = "Michael Bianco", email = "mike@mikebian.co" }]
|
|
10
|
-
urls = { "Repository" = "https://github.com/iloveitaly/detect-shadowed-modules" }
|
|
11
|
-
|
|
12
|
-
# additional packaging information: https://packaging.python.org/en/latest/specifications/core-metadata/#license
|
|
13
|
-
[project.scripts]
|
|
14
|
-
detect-shadowed-modules = "detect_shadowed_modules:main"
|
|
15
|
-
|
|
16
|
-
[build-system]
|
|
17
|
-
requires = ["uv_build>=0.9.0,<0.10.0"]
|
|
18
|
-
build-backend = "uv_build"
|
|
19
|
-
|
|
20
|
-
[tool.uv.build-backend]
|
|
21
|
-
# avoids the src/ directory structure
|
|
22
|
-
module-root = ""
|
|
23
|
-
|
|
24
|
-
[dependency-groups]
|
|
25
|
-
dev = ["pytest>=8.3.3","pyright[nodejs]>=1.1.380","ruff>=0.8.0"]
|
|
26
|
-
|
|
27
|
-
[tool.pyright]
|
|
28
|
-
exclude = ["examples/", "playground/", "tmp/", ".venv/", "tests/"]
|
|
29
|
-
|
|
30
|
-
[tool.ruff]
|
|
31
|
-
extend-exclude = ["playground.py", "playground/"]
|