Skip to content

Commit c3feb6a

Browse files
authored
Refactor name and module management in python code generation and fuzzing (#1957)
1 parent 32d5784 commit c3feb6a

30 files changed

+591
-791
lines changed

utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ object PythonDialogProcessor {
137137
.mapNotNull {
138138
val functionName = it.name ?: return@mapNotNull null
139139
val moduleFilename = it.containingFile.virtualFile?.canonicalPath ?: ""
140-
val containingClassId = it.containingClass?.name?.let{ PythonClassId(it) }
140+
val containingClassId = it.containingClass?.name?.let{ cls -> PythonClassId(cls) }
141141
return@mapNotNull PythonMethodHeader(
142142
functionName,
143143
moduleFilename,

utbot-python/src/main/kotlin/org/utbot/python/PythonTestGenerationProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ object PythonTestGenerationProcessor {
124124

125125
val classId =
126126
if (containingClassName == null)
127-
PythonClassId("$currentPythonModule.TopLevelFunctions")
127+
PythonClassId(currentPythonModule, "TopLevelFunctions")
128128
else
129-
PythonClassId("$currentPythonModule.$containingClassName")
129+
PythonClassId(currentPythonModule, containingClassName)
130130

131131
val methodIds = notEmptyTests.associate {
132132
it.method to PythonMethodId(

utbot-python/src/main/kotlin/org/utbot/python/evaluation/serialiation/PythonObjectParser.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,68 +45,75 @@ class MemoryDump(
4545
sealed class MemoryObject(
4646
val id: String,
4747
val kind: String,
48+
val module: String,
4849
val comparable: Boolean,
49-
)
50+
) {
51+
val qualname: String = if (module.isEmpty()) kind else "$module.$kind"
52+
}
5053

5154
class ReprMemoryObject(
5255
id: String,
5356
kind: String,
57+
module: String,
5458
comparable: Boolean,
5559
val value: String,
56-
): MemoryObject(id, kind, comparable)
60+
): MemoryObject(id, kind, module, comparable)
5761

5862
class ListMemoryObject(
5963
id: String,
6064
kind: String,
65+
module: String,
6166
comparable: Boolean,
6267
val items: List<String>,
63-
): MemoryObject(id, kind, comparable)
68+
): MemoryObject(id, kind, module, comparable)
6469

6570
class DictMemoryObject(
6671
id: String,
6772
kind: String,
73+
module: String,
6874
comparable: Boolean,
6975
val items: Map<String, String>,
70-
): MemoryObject(id, kind, comparable)
76+
): MemoryObject(id, kind, module, comparable)
7177

7278
class ReduceMemoryObject(
7379
id: String,
7480
kind: String,
81+
module: String,
7582
comparable: Boolean,
7683
val constructor: String,
7784
val args: String,
7885
val state: String,
7986
val listitems: String,
8087
val dictitems: String
81-
): MemoryObject(id, kind, comparable)
88+
): MemoryObject(id, kind, module, comparable)
8289

8390
fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump): String {
8491
val obj = when(this) {
8592
is PythonTree.PrimitiveNode -> {
86-
ReprMemoryObject(this.id.toString(), this.type.name, this.comparable, this.repr)
93+
ReprMemoryObject(this.id.toString(), this.type.name, this.type.moduleName, this.comparable, this.repr)
8794
}
8895
is PythonTree.ListNode -> {
8996
val items = this.items.entries
9097
.sortedBy { it.key }
9198
.map { it.value.toMemoryObject(memoryDump) }
92-
ListMemoryObject(this.id.toString(), this.type.name, this.comparable, items)
99+
ListMemoryObject(this.id.toString(), this.type.name, this.type.moduleName, this.comparable, items)
93100
}
94101
is PythonTree.TupleNode -> {
95102
val items = this.items.entries
96103
.sortedBy { it.key }
97104
.map { it.value.toMemoryObject(memoryDump) }
98-
ListMemoryObject(this.id.toString(), this.type.name, this.comparable, items)
105+
ListMemoryObject(this.id.toString(), this.type.name, this.type.moduleName, this.comparable, items)
99106
}
100107
is PythonTree.SetNode -> {
101108
val items = this.items.map { it.toMemoryObject(memoryDump) }
102-
ListMemoryObject(this.id.toString(), this.type.name, this.comparable, items)
109+
ListMemoryObject(this.id.toString(), this.type.name, this.type.moduleName, this.comparable, items)
103110
}
104111
is PythonTree.DictNode -> {
105112
val items = this.items.entries
106113
.associate {
107114
it.key.toMemoryObject(memoryDump) to it.value.toMemoryObject(memoryDump)
108115
}
109-
DictMemoryObject(this.id.toString(), this.type.name, this.comparable, items)
116+
DictMemoryObject(this.id.toString(), this.type.name, this.type.moduleName, this.comparable, items)
110117
}
111118
is PythonTree.ReduceNode -> {
112119
val stateObjId = PythonTree.DictNode(this.state.entries.associate { PythonTree.PrimitiveNode(pythonStrClassId, it.key) to it.value }.toMutableMap())
@@ -116,6 +123,7 @@ fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump): String {
116123
ReduceMemoryObject(
117124
this.id.toString(),
118125
this.type.name,
126+
this.type.moduleName,
119127
this.comparable,
120128
this.constructor.name,
121129
argsIds.toMemoryObject(memoryDump),
@@ -137,7 +145,7 @@ fun MemoryObject.toPythonTree(memoryDump: MemoryDump): PythonTree.PythonTreeNode
137145
is ReprMemoryObject -> {
138146
PythonTree.PrimitiveNode(
139147
this.id.toLong(),
140-
PythonClassId(this.kind),
148+
PythonClassId(this.module, this.kind),
141149
this.value
142150
)
143151
}
@@ -153,7 +161,7 @@ fun MemoryObject.toPythonTree(memoryDump: MemoryDump): PythonTree.PythonTreeNode
153161
val elementsMap = items.withIndex().associate {
154162
it.index to memoryDump.getById(it.value).toPythonTree(memoryDump)
155163
}.toMutableMap()
156-
when (this.kind) {
164+
when (this.qualname) {
157165
"builtins.tuple" -> {
158166
PythonTree.TupleNode(this.id.toLong(), elementsMap)
159167
}
@@ -172,7 +180,7 @@ fun MemoryObject.toPythonTree(memoryDump: MemoryDump): PythonTree.PythonTreeNode
172180
val dictitemsObjs = memoryDump.getById(dictitems) as DictMemoryObject
173181
PythonTree.ReduceNode(
174182
this.id.toLong(),
175-
PythonClassId(this.kind),
183+
PythonClassId(this.module, this.kind),
176184
PythonClassId(this.constructor),
177185
arguments.items.map { memoryDump.getById(it).toPythonTree(memoryDump) },
178186
stateObjs.items.entries.associate {
Lines changed: 15 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.utbot.python.framework.api.python
22

3-
import org.utbot.common.withToStringThreadLocalReentrancyGuard
43
import org.utbot.framework.plugin.api.ClassId
54
import org.utbot.framework.plugin.api.MethodId
65
import org.utbot.framework.plugin.api.UtModel
6+
import org.utbot.python.framework.api.python.util.comparePythonTree
77
import org.utbot.python.framework.api.python.util.moduleOfType
88

99
/**
@@ -17,17 +17,18 @@ import org.utbot.python.framework.api.python.util.moduleOfType
1717
const val pythonBuiltinsModuleName = "builtins"
1818

1919
class PythonClassId(
20-
name: String // includes module (like "_ast.Assign")
21-
) : ClassId(name) {
22-
override fun toString(): String = name
23-
val rootModuleName: String = this.toString().split(".")[0]
24-
override val simpleName: String = name.split(".").last()
25-
val moduleName: String
26-
get() {
27-
return moduleOfType(name) ?: pythonBuiltinsModuleName
28-
}
29-
override val packageName = moduleName
20+
val moduleName: String,
21+
val typeName: String,
22+
) : ClassId("$moduleName.$typeName") {
23+
constructor(fullName: String) : this(
24+
moduleOfType(fullName) ?: pythonBuiltinsModuleName,
25+
fullName.removePrefix(moduleOfType(fullName) ?: pythonBuiltinsModuleName).removePrefix(".")
26+
)
27+
override fun toString(): String = canonicalName
28+
val rootModuleName: String = moduleName.split(".").first()
29+
override val simpleName: String = typeName
3030
override val canonicalName = name
31+
override val packageName = moduleName
3132
}
3233

3334
open class RawPythonAnnotation(
@@ -73,114 +74,13 @@ class PythonTreeModel(
7374
}
7475

7576
override fun equals(other: Any?): Boolean {
76-
if (other is PythonTreeModel) {
77-
return tree == other.tree
77+
if (other !is PythonTreeModel) {
78+
return false
7879
}
79-
return false
80+
return comparePythonTree(tree, other.tree)
8081
}
8182

8283
override fun hashCode(): Int {
8384
return tree.hashCode()
8485
}
8586
}
86-
87-
class PythonDefaultModel(
88-
val repr: String,
89-
classId: PythonClassId
90-
): PythonModel(classId) {
91-
override fun toString() = repr
92-
}
93-
94-
class PythonPrimitiveModel(
95-
val value: Any,
96-
classId: PythonClassId
97-
): PythonModel(classId) {
98-
override fun toString() = "$value"
99-
}
100-
101-
class PythonBoolModel(val value: Boolean): PythonModel(classId) {
102-
override fun toString() =
103-
if (value) "True" else "False"
104-
companion object {
105-
val classId = PythonClassId("builtins.bool")
106-
}
107-
}
108-
109-
class PythonInitObjectModel(
110-
val type: String,
111-
val initValues: List<PythonModel>
112-
): PythonModel(PythonClassId(type)) {
113-
override fun toString(): String {
114-
val params = initValues.joinToString(separator = ", ") { it.toString() }
115-
return "$type($params)"
116-
}
117-
118-
override val allContainingClassIds: Set<PythonClassId>
119-
get() = super.allContainingClassIds + initValues.flatMap { it.allContainingClassIds }
120-
}
121-
122-
class PythonListModel(
123-
val length: Int = 0,
124-
val stores: List<PythonModel>
125-
) : PythonModel(classId) {
126-
override fun toString() =
127-
(0 until length).joinToString(", ", "[", "]") { stores[it].toString() }
128-
129-
override val allContainingClassIds: Set<PythonClassId>
130-
get() = super.allContainingClassIds + stores.flatMap { it.allContainingClassIds }
131-
132-
companion object {
133-
val classId = PythonClassId("builtins.list")
134-
}
135-
}
136-
137-
class PythonTupleModel(
138-
val length: Int = 0,
139-
val stores: List<PythonModel>
140-
) : PythonModel(classId) {
141-
override fun toString() =
142-
(0 until length).joinToString(", ", "(", ")") { stores[it].toString() }
143-
144-
override val allContainingClassIds: Set<PythonClassId>
145-
get() = super.allContainingClassIds + stores.flatMap { it.allContainingClassIds }
146-
147-
companion object {
148-
val classId = PythonClassId("builtins.tuple")
149-
}
150-
}
151-
152-
class PythonDictModel(
153-
val length: Int = 0,
154-
val stores: Map<PythonModel, PythonModel>
155-
) : PythonModel(classId) {
156-
override fun toString() = withToStringThreadLocalReentrancyGuard {
157-
stores.entries.joinToString(", ", "{", "}") { "${it.key}: ${it.value}" }
158-
}
159-
160-
override val allContainingClassIds: Set<PythonClassId>
161-
get() = super.allContainingClassIds +
162-
stores.entries.flatMap { it.key.allContainingClassIds + it.value.allContainingClassIds }
163-
164-
companion object {
165-
val classId = PythonClassId("builtins.dict")
166-
}
167-
}
168-
169-
class PythonSetModel(
170-
val length: Int = 0,
171-
val stores: Set<PythonModel>
172-
) : PythonModel(classId) {
173-
override fun toString() = withToStringThreadLocalReentrancyGuard {
174-
if (stores.isEmpty())
175-
"set()"
176-
else
177-
stores.joinToString(", ", "{", "}") { it.toString() }
178-
}
179-
180-
override val allContainingClassIds: Set<PythonClassId>
181-
get() = super.allContainingClassIds + stores.flatMap { it.allContainingClassIds }
182-
183-
companion object {
184-
val classId = PythonClassId("builtins.set")
185-
}
186-
}

utbot-python/src/main/kotlin/org/utbot/python/framework/api/python/PythonTree.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.utbot.python.framework.api.python
22

33
import org.utbot.python.framework.api.python.util.pythonBoolClassId
4+
import org.utbot.python.framework.api.python.util.pythonDictClassId
45
import org.utbot.python.framework.api.python.util.pythonFloatClassId
56
import org.utbot.python.framework.api.python.util.pythonIntClassId
7+
import org.utbot.python.framework.api.python.util.pythonListClassId
68
import org.utbot.python.framework.api.python.util.pythonNoneClassId
9+
import org.utbot.python.framework.api.python.util.pythonObjectClassId
10+
import org.utbot.python.framework.api.python.util.pythonSetClassId
711
import org.utbot.python.framework.api.python.util.pythonStrClassId
12+
import org.utbot.python.framework.api.python.util.pythonTupleClassId
813
import org.utbot.python.framework.api.python.util.toPythonRepr
914
import org.utbot.python.newtyping.general.Type
1015
import org.utbot.python.newtyping.pythonTypeName
@@ -121,7 +126,7 @@ object PythonTree {
121126
class ListNode(
122127
id: Long,
123128
val items: MutableMap<Int, PythonTreeNode>
124-
) : PythonTreeNode(id, PythonClassId("builtins.list")) {
129+
) : PythonTreeNode(id, pythonListClassId) {
125130
constructor(items: MutableMap<Int, PythonTreeNode>) : this(PythonIdGenerator.createId(), items)
126131

127132
override val children: List<PythonTreeNode>
@@ -139,7 +144,7 @@ object PythonTree {
139144
class DictNode(
140145
id: Long,
141146
val items: MutableMap<PythonTreeNode, PythonTreeNode>
142-
) : PythonTreeNode(id, PythonClassId("builtins.dict")) {
147+
) : PythonTreeNode(id, pythonDictClassId) {
143148
constructor(items: MutableMap<PythonTreeNode, PythonTreeNode>) : this(PythonIdGenerator.createId(), items)
144149

145150
override val children: List<PythonTreeNode>
@@ -159,7 +164,7 @@ object PythonTree {
159164
class SetNode(
160165
id: Long,
161166
val items: MutableSet<PythonTreeNode>
162-
) : PythonTreeNode(id, PythonClassId("builtins.set")) {
167+
) : PythonTreeNode(id, pythonSetClassId) {
163168
constructor(items: MutableSet<PythonTreeNode>) : this(PythonIdGenerator.createId(), items)
164169

165170
override val children: List<PythonTreeNode>
@@ -182,7 +187,7 @@ object PythonTree {
182187
class TupleNode(
183188
id: Long,
184189
val items: MutableMap<Int, PythonTreeNode>
185-
) : PythonTreeNode(id, PythonClassId("builtins.tuple")) {
190+
) : PythonTreeNode(id, pythonTupleClassId) {
186191
constructor(items: MutableMap<Int, PythonTreeNode>) : this(PythonIdGenerator.createId(), items)
187192

188193
override val children: List<PythonTreeNode>
@@ -259,7 +264,7 @@ object PythonTree {
259264

260265
fun fromObject(): PrimitiveNode {
261266
return PrimitiveNode(
262-
PythonClassId("builtins.object"),
267+
pythonObjectClassId,
263268
"object()"
264269
)
265270
}

utbot-python/src/main/kotlin/org/utbot/python/framework/api/python/util/PythonIdUtils.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ package org.utbot.python.framework.api.python.util
22

33
import org.utbot.python.framework.api.python.PythonClassId
44
import org.utbot.python.framework.api.python.NormalizedPythonAnnotation
5-
import org.utbot.python.framework.api.python.PythonBoolModel
6-
import org.utbot.python.framework.api.python.PythonListModel
7-
import org.utbot.python.framework.api.python.PythonTupleModel
8-
import org.utbot.python.framework.api.python.PythonDictModel
9-
import org.utbot.python.framework.api.python.PythonSetModel
105

116
// none annotation can be used in code only since Python 3.10
127
val pythonNoneClassId = PythonClassId("types.NoneType")

0 commit comments

Comments
 (0)