@@ -84,7 +84,7 @@ private static ModuleType createEmptyModule(String moduleName, ModuleType parent
84
84
private ModuleType createModuleFromSymbols (String name , ModuleType parent , Collection <Symbol > symbols ) {
85
85
var members = new HashMap <String , PythonType >();
86
86
symbols .forEach (symbol -> {
87
- var type = convertToType (symbol );
87
+ var type = convertToType (symbol , new HashMap <>() );
88
88
members .put (symbol .name (), type );
89
89
});
90
90
var module = new ModuleType (name , parent );
@@ -99,26 +99,49 @@ private static PythonType convertToObjectType(Symbol symbol) {
99
99
return PythonType .UNKNOWN ;
100
100
}
101
101
102
- private static PythonType convertToFunctionType (FunctionSymbol symbol ) {
103
- return new FunctionType (symbol .name (), List .of (), List .of (), PythonType .UNKNOWN , false , false , false , false , null );
102
+ private static PythonType convertToFunctionType (FunctionSymbol symbol , Map <Symbol , PythonType > createdTypesBySymbol ) {
103
+ // TODO: ensure we don't build twice
104
+ if (createdTypesBySymbol .containsKey (symbol )) {
105
+ return createdTypesBySymbol .get (symbol );
106
+ }
107
+ FunctionTypeBuilder functionTypeBuilder =
108
+ new FunctionTypeBuilder (symbol .name ())
109
+ .withAttributes (List .of ())
110
+ .withParameters (List .of ())
111
+ .withReturnType (PythonType .UNKNOWN )
112
+ .withAsynchronous (false )
113
+ .withHasDecorators (false )
114
+ .withInstanceMethod (false )
115
+ .withHasVariadicParameter (false )
116
+ .withOwner (null );
117
+ FunctionType functionType = functionTypeBuilder .build ();
118
+ createdTypesBySymbol .put (symbol , functionType );
119
+ return functionType ;
104
120
}
105
121
106
- private PythonType convertToClassType (ClassSymbol symbol ) {
107
- Set <Member > members = symbol .declaredMembers ().stream ().map (m -> new Member (m .name (), convertToType (m ))).collect (Collectors .toSet ());
108
- List <PythonType > superClasses = symbol .superClasses ().stream ().map (this ::convertToType ).toList ();
109
- return new ClassType (symbol .name (), members , List .of (), superClasses );
122
+ private PythonType convertToClassType (ClassSymbol symbol , Map <Symbol , PythonType > createdTypesBySymbol ) {
123
+ if (createdTypesBySymbol .containsKey (symbol )) {
124
+ return createdTypesBySymbol .get (symbol );
125
+ }
126
+ ClassType classType = new ClassType (symbol .name ());
127
+ createdTypesBySymbol .put (symbol , classType );
128
+ Set <Member > members = symbol .declaredMembers ().stream ().map (m -> new Member (m .name (), convertToType (m , createdTypesBySymbol ))).collect (Collectors .toSet ());
129
+ classType .members ().addAll (members );
130
+ List <PythonType > superClasses = symbol .superClasses ().stream ().map (s -> convertToType (s , createdTypesBySymbol )).toList ();
131
+ classType .superClasses ().addAll (superClasses );
132
+ return classType ;
110
133
}
111
134
112
- private PythonType convertToUnionType (AmbiguousSymbol ambiguousSymbol ) {
113
- List <PythonType > pythonTypes = ambiguousSymbol .alternatives ().stream ().map (this :: convertToType ).toList ();
135
+ private PythonType convertToUnionType (AmbiguousSymbol ambiguousSymbol , Map < Symbol , PythonType > createdTypesBySymbol ) {
136
+ List <PythonType > pythonTypes = ambiguousSymbol .alternatives ().stream ().map (a -> convertToType ( a , createdTypesBySymbol ) ).toList ();
114
137
return new UnionType (pythonTypes );
115
138
}
116
139
117
- private PythonType convertToType (Symbol symbol ) {
140
+ private PythonType convertToType (Symbol symbol , Map < Symbol , PythonType > createdTypesBySymbol ) {
118
141
return switch (symbol .kind ()) {
119
- case CLASS -> convertToClassType ((ClassSymbol ) symbol );
120
- case FUNCTION -> convertToFunctionType ((FunctionSymbol ) symbol );
121
- case AMBIGUOUS -> convertToUnionType ((AmbiguousSymbol ) symbol );
142
+ case CLASS -> convertToClassType ((ClassSymbol ) symbol , createdTypesBySymbol );
143
+ case FUNCTION -> convertToFunctionType ((FunctionSymbol ) symbol , createdTypesBySymbol );
144
+ case AMBIGUOUS -> convertToUnionType ((AmbiguousSymbol ) symbol , createdTypesBySymbol );
122
145
case OTHER -> convertToObjectType (symbol );
123
146
};
124
147
}
0 commit comments