@@ -67,29 +67,34 @@ class PythonTypeStorage(
67
67
}
68
68
}
69
69
70
- sealed class PythonTypeDescription (name : Name ): TypeMetaDataWithName(name) {
70
+ sealed class PythonTypeDescription (name : Name ) : TypeMetaDataWithName(name) {
71
71
open fun castToCompatibleTypeApi (type : Type ): Type = type
72
72
open fun getNamedMembers (type : Type ): List <PythonAttribute > = emptyList() // direct members (without inheritance)
73
73
open fun getAnnotationParameters (type : Type ): List <Type > = emptyList()
74
- open fun getMemberByName (storage : PythonTypeStorage , type : Type , name : String ): PythonAttribute ? = // overridden for some types
74
+ open fun getMemberByName (storage : PythonTypeStorage , type : Type , name : String ): PythonAttribute ? =
75
+ // overridden for some types
75
76
getNamedMembers(type).find { it.name == name }
76
- open fun createTypeWithNewAnnotationParameters (origin : Type , newParams : List <Type >): Type = // overriden for Callable
77
+
78
+ open fun createTypeWithNewAnnotationParameters (origin : Type , newParams : List <Type >): Type =
79
+ // overriden for Callable
77
80
DefaultSubstitutionProvider .substituteAll(origin, newParams)
78
81
}
79
82
80
83
sealed class PythonCompositeTypeDescription (
81
84
name : Name ,
82
85
private val memberNames : List <String >
83
- ): PythonTypeDescription(name) {
86
+ ) : PythonTypeDescription(name) {
84
87
override fun castToCompatibleTypeApi (type : Type ): CompositeType {
85
88
return type as ? CompositeType
86
89
? : error(" Got unexpected type PythonCompositeTypeDescription: $type " )
87
90
}
91
+
88
92
override fun getNamedMembers (type : Type ): List <PythonAttribute > {
89
93
val compositeType = castToCompatibleTypeApi(type)
90
94
assert (compositeType.members.size == memberNames.size)
91
95
return (memberNames zip compositeType.members).map { PythonAttribute (it.first, it.second) }
92
96
}
97
+
93
98
override fun getAnnotationParameters (type : Type ): List <Type > = type.parameters
94
99
fun mro (storage : PythonTypeStorage , type : Type ): List <Type > {
95
100
val compositeType = castToCompatibleTypeApi(type)
@@ -125,6 +130,7 @@ sealed class PythonCompositeTypeDescription(
125
130
}
126
131
return result
127
132
}
133
+
128
134
override fun getMemberByName (storage : PythonTypeStorage , type : Type , name : String ): PythonAttribute ? {
129
135
for (parent in mro(storage, type)) {
130
136
val cur = parent.getPythonAttributes().find { it.name == name }
@@ -135,22 +141,24 @@ sealed class PythonCompositeTypeDescription(
135
141
}
136
142
}
137
143
138
- sealed class PythonSpecialAnnotation (name : Name ): PythonTypeDescription(name)
144
+ sealed class PythonSpecialAnnotation (name : Name ) : PythonTypeDescription(name)
139
145
140
146
class PythonTypeVarDescription (
141
147
name : Name ,
142
148
val variance : Variance ,
143
149
val parameterKind : ParameterKind
144
- ): PythonTypeDescription(name) {
150
+ ) : PythonTypeDescription(name) {
145
151
override fun castToCompatibleTypeApi (type : Type ): TypeParameter {
146
152
return type as ? TypeParameter
147
153
? : error(" Got unexpected type PythonTypeVarDescription: $type " )
148
154
}
155
+
149
156
enum class Variance {
150
157
INVARIANT ,
151
158
COVARIANT ,
152
159
CONTRAVARIANT
153
160
}
161
+
154
162
enum class ParameterKind {
155
163
WithUpperBound ,
156
164
WithConcreteValues
@@ -160,35 +168,41 @@ class PythonTypeVarDescription(
160
168
// Composite types
161
169
class PythonConcreteCompositeTypeDescription (
162
170
name : Name ,
163
- memberNames : List <String >
164
- ): PythonCompositeTypeDescription(name, memberNames)
171
+ memberNames : List <String >,
172
+ val isAbstract : Boolean
173
+ ) : PythonCompositeTypeDescription(name, memberNames)
174
+
165
175
class PythonProtocolDescription (
166
176
name : Name ,
167
177
memberNames : List <String >,
168
178
val protocolMemberNames : List <String >
169
- ): PythonCompositeTypeDescription(name, memberNames)
179
+ ) : PythonCompositeTypeDescription(name, memberNames)
170
180
171
181
class PythonCallableTypeDescription (
172
182
val argumentKinds : List <ArgKind >,
173
183
val argumentNames : List <String >,
174
184
val isClassMethod : Boolean ,
175
185
val isStaticMethod : Boolean
176
- ): PythonTypeDescription(pythonCallableName) {
186
+ ) : PythonTypeDescription(pythonCallableName) {
177
187
override fun castToCompatibleTypeApi (type : Type ): FunctionType {
178
188
return type as ? FunctionType
179
189
? : error(" Got unexpected type PythonCallableTypeDescription: $type " )
180
190
}
191
+
181
192
override fun getNamedMembers (type : Type ): List <PythonAttribute > {
182
193
val functionType = castToCompatibleTypeApi(type)
183
194
return listOf (PythonAttribute (" __call__" , functionType))
184
195
}
196
+
185
197
override fun getAnnotationParameters (type : Type ): List <Type > {
186
198
val functionType = castToCompatibleTypeApi(type)
187
199
return functionType.arguments + listOf (functionType.returnValue)
188
200
}
201
+
189
202
enum class ArgKind {
190
203
Positional
191
204
}
205
+
192
206
override fun createTypeWithNewAnnotationParameters (origin : Type , newParams : List <Type >): Type {
193
207
val args = newParams.dropLast(1 )
194
208
val returnValue = newParams.last()
@@ -215,17 +229,17 @@ class PythonCallableTypeDescription(
215
229
}
216
230
217
231
// Special Python annotations
218
- object PythonAnyTypeDescription: PythonSpecialAnnotation(pythonAnyName) {
232
+ object PythonAnyTypeDescription : PythonSpecialAnnotation(pythonAnyName) {
219
233
override fun getMemberByName (storage : PythonTypeStorage , type : Type , name : String ): PythonAttribute {
220
234
return PythonAttribute (name, pythonAnyType)
221
235
}
222
236
}
223
237
224
- object PythonNoneTypeDescription: PythonSpecialAnnotation(pythonNoneName) {
238
+ object PythonNoneTypeDescription : PythonSpecialAnnotation(pythonNoneName) {
225
239
// TODO: override getNamedMembers and/or getMemberByName
226
240
}
227
241
228
- object PythonUnionTypeDescription: PythonSpecialAnnotation(pythonUnionName) {
242
+ object PythonUnionTypeDescription : PythonSpecialAnnotation(pythonUnionName) {
229
243
override fun getMemberByName (storage : PythonTypeStorage , type : Type , name : String ): PythonAttribute ? {
230
244
val children = type.parameters.mapNotNull {
231
245
it.getPythonAttributeByName(storage, name)?.type
@@ -238,15 +252,16 @@ object PythonUnionTypeDescription: PythonSpecialAnnotation(pythonUnionName) {
238
252
type = createPythonUnionType(children)
239
253
)
240
254
}
255
+
241
256
override fun getAnnotationParameters (type : Type ): List <Type > = type.parameters
242
257
}
243
258
244
- object PythonOverloadTypeDescription: PythonSpecialAnnotation(overloadName) {
259
+ object PythonOverloadTypeDescription : PythonSpecialAnnotation(overloadName) {
245
260
override fun getAnnotationParameters (type : Type ): List <Type > = type.parameters
246
261
// TODO: override getMemberByName
247
262
}
248
263
249
- object PythonTupleTypeDescription: PythonSpecialAnnotation(pythonTupleName) {
264
+ object PythonTupleTypeDescription : PythonSpecialAnnotation(pythonTupleName) {
250
265
override fun getAnnotationParameters (type : Type ): List <Type > = castToCompatibleTypeApi(type).parameters
251
266
// TODO: getMemberByName and/or getNamedMembers
252
267
}
@@ -274,9 +289,14 @@ fun createPythonConcreteCompositeType(
274
289
name : Name ,
275
290
numberOfParameters : Int ,
276
291
memberNames : List <String >,
292
+ isAbstract : Boolean ,
277
293
initialization : (CompositeTypeCreator .Original ) -> CompositeTypeCreator .InitializationData
278
294
): CompositeType =
279
- CompositeTypeCreator .create(numberOfParameters, PythonConcreteCompositeTypeDescription (name, memberNames), initialization)
295
+ CompositeTypeCreator .create(
296
+ numberOfParameters,
297
+ PythonConcreteCompositeTypeDescription (name, memberNames, isAbstract),
298
+ initialization
299
+ )
280
300
281
301
fun createPythonProtocol (
282
302
name : Name ,
@@ -285,7 +305,11 @@ fun createPythonProtocol(
285
305
protocolMemberNames : List <String >,
286
306
initialization : (CompositeTypeCreator .Original ) -> CompositeTypeCreator .InitializationData
287
307
): CompositeType =
288
- CompositeTypeCreator .create(numberOfParameters, PythonProtocolDescription (name, memberNames, protocolMemberNames), initialization)
308
+ CompositeTypeCreator .create(
309
+ numberOfParameters,
310
+ PythonProtocolDescription (name, memberNames, protocolMemberNames),
311
+ initialization
312
+ )
289
313
290
314
fun createPythonCallableType (
291
315
numberOfParameters : Int ,
0 commit comments