@@ -68,11 +68,30 @@ public void enterImport_stmt(Import_stmtContext ctx) {
6868 }
6969 List <String > fullNames = sureImportedModulesParsed (0 ,moduleName ,null );
7070
71+ // Find the main entity for this import (the package or main module)
72+ Entity mainEntity = null ;
73+ String mainFullName = null ;
7174 for (String fullName :fullNames ) {
7275 if (FileUtil .existFile (fullName ) && !(FileUtil .isDirectory (fullName ))) {
7376 context .foundNewImport (new FileImport (fullName ));
7477 }
75- context .foundNewImport (new NameAliasImport (fullName , entityRepo .getEntity (fullName ), aliasName ));
78+ Entity importedEntity = entityRepo .getEntity (fullName );
79+ // If this is the __init__.py file, use the package entity instead
80+ if (fullName .endsWith (PythonBuiltInType .PACKAGE_PY_NAME ) && importedEntity != null ) {
81+ Entity parent = importedEntity .getParent ();
82+ if (parent != null ) {
83+ mainEntity = parent ;
84+ mainFullName = parent .getRawName ().uniqName ();
85+ }
86+ } else if (mainEntity == null && importedEntity != null ) {
87+ // For non-package imports, use the first file as main entity
88+ mainEntity = importedEntity ;
89+ mainFullName = fullName ;
90+ }
91+ }
92+ // Create only ONE NameAliasImport for the main entity
93+ if (mainEntity != null ) {
94+ context .foundNewImport (new NameAliasImport (mainFullName , mainEntity , aliasName ));
7695 }
7796 }
7897 super .enterImport_stmt (ctx );
@@ -220,7 +239,9 @@ private List<String> visitIncludedFile(String fullName) {
220239 }
221240 }
222241 if (FileUtil .existFile (fullName +File .separator + PythonBuiltInType .PACKAGE_PY_NAME )) {
223- visitedFiles .add ( fullName +File .separator +PythonBuiltInType .PACKAGE_PY_NAME );
242+ String initFile = fullName +File .separator +PythonBuiltInType .PACKAGE_PY_NAME ;
243+ invokeParser (initFile );
244+ visitedFiles .add (initFile );
224245 }
225246 }else {
226247 invokeParser (fullName );
0 commit comments