1
1
package org.utbot.python.newtyping.mypy
2
2
3
- import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
4
3
import org.utbot.python.newtyping.*
5
4
import org.utbot.python.newtyping.general.*
5
+ import org.utbot.python.utils.CustomPolymorphicJsonAdapterFactory
6
6
7
7
class MypyAnnotation (
8
8
val nodeId : String ,
@@ -11,14 +11,24 @@ class MypyAnnotation(
11
11
var initialized = false
12
12
@Transient lateinit var storage: MypyInfoBuild
13
13
val node: MypyAnnotationNode
14
- get() = storage.nodeStorage[nodeId]!!
14
+ get() {
15
+ val result = storage.nodeStorage[nodeId]
16
+ require(result != null ) {
17
+ " Required node is absent in storage: $nodeId "
18
+ }
19
+ return result
20
+ }
15
21
val asUtBotType: UtType
16
22
get() {
17
- assert (initialized)
23
+ require (initialized)
18
24
val origin = storage.getUtBotTypeOfNode(node)
25
+ if (origin.pythonDescription() is PythonAnyTypeDescription )
26
+ return origin
19
27
if (args != null ) {
20
- assert (origin.parameters.size == args.size)
21
- assert (origin.parameters.all { it is TypeParameter })
28
+ require(origin.parameters.size == args.size) {
29
+ " Bad arguments on ${origin.pythonTypeRepresentation()} . Expected ${origin.parameters.size} parameters but got ${args.size} "
30
+ }
31
+ require(origin.parameters.all { it is TypeParameter })
22
32
val argTypes = args.map { it.asUtBotType }
23
33
return DefaultSubstitutionProvider .substitute(
24
34
origin,
@@ -47,7 +57,10 @@ sealed class CompositeAnnotationNode(
47
57
fun getInitData (self : CompositeTypeCreator .Original ): CompositeTypeCreator .InitializationData {
48
58
storage.nodeToUtBotType[this ] = self
49
59
(typeVars zip self.parameters).forEach { (node, typeParam) ->
50
- val typeVar = node.node as TypeVarNode
60
+ val typeVar = node.node as ? TypeVarNode
61
+ require(typeVar != null ) {
62
+ " Did not construct type variable"
63
+ }
51
64
storage.nodeToUtBotType[typeVar] = typeParam
52
65
typeParam.meta = PythonTypeVarDescription (Name (emptyList(), typeVar.varName), typeVar.variance, typeVar.kind)
53
66
typeParam.constraints = typeVar.constraints
@@ -70,7 +83,7 @@ class ConcreteAnnotation(
70
83
val isAbstract : Boolean
71
84
): CompositeAnnotationNode(module, simpleName, members, typeVars, bases) {
72
85
override fun initializeType (): UtType {
73
- assert (storage.nodeToUtBotType[this ] == null )
86
+ require (storage.nodeToUtBotType[this ] == null )
74
87
return createPythonConcreteCompositeType(
75
88
Name (module.split(' .' ), simpleName),
76
89
typeVars.size,
@@ -117,7 +130,10 @@ class FunctionNode(
117
130
) { self ->
118
131
storage.nodeToUtBotType[this ] = self
119
132
(typeVars zip self.parameters).forEach { (nodeId, typeParam) ->
120
- val typeVar = storage.nodeStorage[nodeId] as TypeVarNode
133
+ val typeVar = storage.nodeStorage[nodeId] as ? TypeVarNode
134
+ require(typeVar != null ) {
135
+ " Did not construct type variable"
136
+ }
121
137
storage.nodeToUtBotType[typeVar] = typeParam
122
138
typeParam.meta = PythonTypeVarDescription (Name (emptyList(), typeVar.varName), typeVar.variance, typeVar.kind)
123
139
typeParam.constraints = typeVar.constraints
@@ -140,9 +156,16 @@ class TypeVarNode(
140
156
): MypyAnnotationNode() {
141
157
override val children: List <MypyAnnotation >
142
158
get() = super .children + values + (upperBound?.let { listOf (it) } ? : emptyList())
143
- override fun initializeType () =
144
- error(" Initialization of TypeVar must be done in defining class or function." +
145
- " TypeVar name: $varName , def_id: $def " )
159
+ override fun initializeType (): UtType {
160
+ /* error(
161
+ "Initialization of TypeVar must be done in defining class or function." +
162
+ " TypeVar name: $varName, def_id: $def"
163
+ )*/
164
+ // this a rare and bad case:
165
+ // https://github.com/sqlalchemy/sqlalchemy/blob/rel_2_0_20/lib/sqlalchemy/sql/sqltypes.py#L2091C5-L2091C23
166
+ storage.nodeStorage[def]!! .initializeType()
167
+ return storage.nodeToUtBotType[this ] ? : error(" Error while initializing TypeVar name: $varName , def_id: $def " )
168
+ }
146
169
val constraints: Set <TypeParameterConstraint > by lazy {
147
170
val upperBoundConstraint: Set <TypeParameterConstraint > =
148
171
upperBound?.let { setOf (TypeParameterConstraint (upperBoundRelation, it.asUtBotType)) } ? : emptySet()
@@ -236,28 +259,22 @@ enum class AnnotationType {
236
259
Unknown
237
260
}
238
261
239
- val annotationAdapter: PolymorphicJsonAdapterFactory <MypyAnnotationNode > =
240
- PolymorphicJsonAdapterFactory .of(MypyAnnotationNode ::class .java, " type" )
241
- .withSubtype(ConcreteAnnotation ::class .java, AnnotationType .Concrete .name)
242
- .withSubtype(Protocol ::class .java, AnnotationType .Protocol .name)
243
- .withSubtype(TypeVarNode ::class .java, AnnotationType .TypeVar .name)
244
- .withSubtype(OverloadedFunction ::class .java, AnnotationType .Overloaded .name)
245
- .withSubtype(FunctionNode ::class .java, AnnotationType .Function .name)
246
- .withSubtype(PythonAny ::class .java, AnnotationType .Any .name)
247
- // .withSubtype(PythonLiteral::class.java, AnnotationType.Literal.name)
248
- .withSubtype(PythonUnion ::class .java, AnnotationType .Union .name)
249
- .withSubtype(PythonTuple ::class .java, AnnotationType .Tuple .name)
250
- .withSubtype(PythonNoneType ::class .java, AnnotationType .NoneType .name)
251
- .withSubtype(TypeAliasNode ::class .java, AnnotationType .TypeAlias .name)
252
- .withSubtype(UnknownAnnotationNode ::class .java, AnnotationType .Unknown .name)
253
-
254
- object MypyAnnotations {
255
-
256
- data class MypyReportLine (
257
- val line : Int ,
258
- val type : String ,
259
- val message : String ,
260
- val file : String
262
+ val annotationAdapter = CustomPolymorphicJsonAdapterFactory (
263
+ MypyAnnotationNode ::class .java,
264
+ contentLabel = " content" ,
265
+ keyLabel = " type" ,
266
+ mapOf (
267
+ AnnotationType .Concrete .name to ConcreteAnnotation ::class .java,
268
+ AnnotationType .Protocol .name to Protocol ::class .java,
269
+ AnnotationType .TypeVar .name to TypeVarNode ::class .java,
270
+ AnnotationType .Overloaded .name to OverloadedFunction ::class .java,
271
+ AnnotationType .Function .name to FunctionNode ::class .java,
272
+ AnnotationType .Any .name to PythonAny ::class .java,
273
+ // .withSubtype(PythonLiteral::class.java, AnnotationType.Literal.name)
274
+ AnnotationType .Union .name to PythonUnion ::class .java,
275
+ AnnotationType .Tuple .name to PythonTuple ::class .java,
276
+ AnnotationType .NoneType .name to PythonNoneType ::class .java,
277
+ AnnotationType .TypeAlias .name to TypeAliasNode ::class .java,
278
+ AnnotationType .Unknown .name to UnknownAnnotationNode ::class .java
261
279
)
262
-
263
- }
280
+ )
0 commit comments