fix: Framework detection now works by including import-only files (fixes #239)
## Problem
Framework detection was broken because files with only imports (no
classes/functions) were excluded from analysis. The architectural pattern
detector received empty file lists, resulting in 0 frameworks detected.
## Root Cause
In codebase_scraper.py:873-881, the has_content check filtered out files
that didn't have classes, functions, or other structural elements. This
excluded simple __init__.py files that only contained import statements,
which are critical for framework detection.
## Solution (3 parts)
1. **Extract imports from Python files** (code_analyzer.py:140-178)
- Added import extraction using AST (ast.Import, ast.ImportFrom)
- Returns imports list in analysis results
- Now captures: "from flask import Flask" → ["flask"]
2. **Include import-only files** (codebase_scraper.py:873-881)
- Updated has_content check to include files with imports
- Files with imports are now included in analysis results
- Comment added: "IMPORTANT: Include files with imports for framework
detection (fixes #239)"
3. **Enhance framework detection** (architectural_pattern_detector.py:195-240)
- Extract imports from all Python files in analysis
- Check imports in addition to file paths and directory structure
- Prioritize import-based detection (high confidence)
- Require 2+ matches for path-based detection (avoid false positives)
- Added debug logging: "Collected N imports for framework detection"
## Results
**Before fix:**
- Test Flask project: 0 files analyzed, 0 frameworks detected
- Files with imports: excluded from analysis
- Framework detection: completely broken
**After fix:**
- Test Flask project: 3 files analyzed, Flask detected ✅
- Files with imports: included in analysis
- Framework detection: working correctly
- No false positives (ASP.NET, Rails, etc.)
## Testing
Added comprehensive test suite (tests/test_framework_detection.py):
- ✅ test_flask_framework_detection_from_imports
- ✅ test_files_with_imports_are_included
- ✅ test_no_false_positive_frameworks
All existing tests pass:
- ✅ 38 tests in test_codebase_scraper.py
- ✅ 54 tests in test_code_analyzer.py
- ✅ 3 new tests in test_framework_detection.py
## Impact
- Fixes issue #239 completely
- Framework detection now works for Python projects
- Import-only files (common in Python packages) are properly analyzed
- No performance impact (import extraction is fast)
- No breaking changes to existing functionality
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -147,6 +147,7 @@ class CodeAnalyzer:
|
||||
|
||||
classes = []
|
||||
functions = []
|
||||
imports = []
|
||||
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.ClassDef):
|
||||
@@ -171,11 +172,24 @@ class CodeAnalyzer:
|
||||
if not is_method:
|
||||
func_sig = self._extract_python_function(node)
|
||||
functions.append(asdict(func_sig))
|
||||
elif isinstance(node, ast.Import):
|
||||
# Extract: import foo, bar
|
||||
for alias in node.names:
|
||||
imports.append(alias.name)
|
||||
elif isinstance(node, ast.ImportFrom):
|
||||
# Extract: from foo import bar
|
||||
module = node.module or ""
|
||||
imports.append(module)
|
||||
|
||||
# Extract comments
|
||||
comments = self._extract_python_comments(content)
|
||||
|
||||
return {"classes": classes, "functions": functions, "comments": comments}
|
||||
return {
|
||||
"classes": classes,
|
||||
"functions": functions,
|
||||
"comments": comments,
|
||||
"imports": imports, # Include imports for framework detection
|
||||
}
|
||||
|
||||
def _extract_python_class(self, node: ast.ClassDef) -> ClassSignature:
|
||||
"""Extract class signature from AST node."""
|
||||
|
||||
Reference in New Issue
Block a user