11package org.utbot.python.newtyping.mypy
22
3- import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
43import org.utbot.python.newtyping.*
54import org.utbot.python.newtyping.general.*
5+ import org.utbot.python.utils.CustomPolymorphicJsonAdapterFactory
66
77class MypyAnnotation (
88 val nodeId : String ,
@@ -11,14 +11,24 @@ class MypyAnnotation(
1111 var initialized = false
1212 @Transient lateinit var storage: MypyInfoBuild
1313 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+ }
1521 val asUtBotType: UtType
1622 get() {
17- assert (initialized)
23+ require (initialized)
1824 val origin = storage.getUtBotTypeOfNode(node)
25+ if (origin.pythonDescription() is PythonAnyTypeDescription )
26+ return origin
1927 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 })
2232 val argTypes = args.map { it.asUtBotType }
2333 return DefaultSubstitutionProvider .substitute(
2434 origin,
@@ -47,7 +57,10 @@ sealed class CompositeAnnotationNode(
4757 fun getInitData (self : CompositeTypeCreator .Original ): CompositeTypeCreator .InitializationData {
4858 storage.nodeToUtBotType[this ] = self
4959 (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+ }
5164 storage.nodeToUtBotType[typeVar] = typeParam
5265 typeParam.meta = PythonTypeVarDescription (Name (emptyList(), typeVar.varName), typeVar.variance, typeVar.kind)
5366 typeParam.constraints = typeVar.constraints
@@ -70,7 +83,7 @@ class ConcreteAnnotation(
7083 val isAbstract : Boolean
7184): CompositeAnnotationNode(module, simpleName, members, typeVars, bases) {
7285 override fun initializeType (): UtType {
73- assert (storage.nodeToUtBotType[this ] == null )
86+ require (storage.nodeToUtBotType[this ] == null )
7487 return createPythonConcreteCompositeType(
7588 Name (module.split(' .' ), simpleName),
7689 typeVars.size,
@@ -117,7 +130,10 @@ class FunctionNode(
117130 ) { self ->
118131 storage.nodeToUtBotType[this ] = self
119132 (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+ }
121137 storage.nodeToUtBotType[typeVar] = typeParam
122138 typeParam.meta = PythonTypeVarDescription (Name (emptyList(), typeVar.varName), typeVar.variance, typeVar.kind)
123139 typeParam.constraints = typeVar.constraints
@@ -140,9 +156,16 @@ class TypeVarNode(
140156): MypyAnnotationNode() {
141157 override val children: List <MypyAnnotation >
142158 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+ }
146169 val constraints: Set <TypeParameterConstraint > by lazy {
147170 val upperBoundConstraint: Set <TypeParameterConstraint > =
148171 upperBound?.let { setOf (TypeParameterConstraint (upperBoundRelation, it.asUtBotType)) } ? : emptySet()
@@ -236,28 +259,22 @@ enum class AnnotationType {
236259 Unknown
237260}
238261
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
261279 )
262-
263- }
280+ )
0 commit comments