Skip to content

Commit 59e41af

Browse files
Different fixes relatated to mypy (#1817)
Co-authored-by: Vyacheslav Tamarin <slavabarsuk@ya.ru>
1 parent 5561be1 commit 59e41af

File tree

9 files changed

+454
-517
lines changed

9 files changed

+454
-517
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlinx.coroutines.runBlocking
2525
import org.jetbrains.kotlin.idea.util.application.runWriteAction
2626
import org.jetbrains.kotlin.idea.util.module
2727
import org.jetbrains.kotlin.idea.util.projectStructure.sdk
28+
import org.jetbrains.kotlin.konan.file.File
2829
import org.utbot.common.PathUtil.toPath
2930
import org.utbot.common.appendHtmlLine
3031
import org.utbot.framework.UtSettings
@@ -334,9 +335,9 @@ fun getDirectoriesForSysPath(
334335
}
335336
}
336337

337-
// Select modules only from this project
338+
// Select modules only from this project but not from installation directory
338339
importedPaths.forEach {
339-
if (it.isProjectSubmodule(ancestor)) {
340+
if (it.isProjectSubmodule(ancestor) && !it.path.split(File.separator).contains("site-packages")) {
340341
sources.add(it)
341342
}
342343
}

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -174,43 +174,25 @@ class PythonEngine(
174174

175175
fun fuzzing(parameters: List<Type>, isCancelled: () -> Boolean, until: Long): Flow<FuzzingExecutionFeedback> = flow {
176176
val additionalModules = parameters.flatMap { it.pythonModules() }
177-
178-
val pmd = PythonMethodDescription(
179-
methodUnderTest.name,
180-
parameters,
181-
fuzzedConcreteValues,
182-
pythonTypeStorage,
183-
Trie(Instruction::id)
184-
)
185-
186177
val coveredLines = initialCoveredLines.toMutableSet()
187178

188-
PythonFuzzing(pmd.pythonTypeStorage) { description, arguments ->
189-
if (isCancelled()) {
190-
logger.info { "Fuzzing process was interrupted" }
191-
return@PythonFuzzing PythonFeedback(control = Control.STOP)
192-
}
193-
if (System.currentTimeMillis() >= until) {
194-
logger.info { "Fuzzing process was interrupted by timeout" }
195-
return@PythonFuzzing PythonFeedback(control = Control.STOP)
196-
}
197-
179+
suspend fun fuzzingResultHandler(description: PythonMethodDescription, arguments: List<PythonFuzzedValue>): PythonFeedback {
198180
val codeExecutor = constructEvaluationInput(arguments, additionalModules)
199-
when (val evaluationResult = codeExecutor.run()) {
181+
return when (val evaluationResult = codeExecutor.run()) {
200182
is PythonEvaluationError -> {
201183
val utError = UtError(
202184
"Error evaluation: ${evaluationResult.status}, ${evaluationResult.message}",
203185
Throwable(evaluationResult.stackTrace.joinToString("\n"))
204186
)
205187
logger.debug(evaluationResult.stackTrace.joinToString("\n"))
206188
emit(InvalidExecution(utError))
207-
return@PythonFuzzing PythonFeedback(control = Control.PASS)
189+
PythonFeedback(control = Control.PASS)
208190
}
209191

210192
is PythonEvaluationTimeout -> {
211193
val utError = UtError(evaluationResult.message, Throwable())
212194
emit(InvalidExecution(utError))
213-
return@PythonFuzzing PythonFeedback(control = Control.PASS)
195+
PythonFeedback(control = Control.PASS)
214196
}
215197

216198
is PythonEvaluationSuccess -> {
@@ -228,19 +210,44 @@ class PythonEngine(
228210
logger.debug { arguments }
229211
val trieNode: Trie.Node<Instruction> = description.tracer.add(coveredInstructions)
230212
emit(result)
231-
return@PythonFuzzing PythonFeedback(control = Control.CONTINUE, result = trieNode)
213+
PythonFeedback(control = Control.CONTINUE, result = trieNode)
232214
}
233215
is ArgumentsTypeErrorFeedback, is TypeErrorFeedback -> {
234216
emit(result)
235-
return@PythonFuzzing PythonFeedback(control = Control.PASS)
217+
PythonFeedback(control = Control.PASS)
236218
}
237219
is InvalidExecution -> {
238220
emit(result)
239-
return@PythonFuzzing PythonFeedback(control = Control.CONTINUE)
221+
PythonFeedback(control = Control.CONTINUE)
240222
}
241223
}
242224
}
243225
}
244-
}.fuzz(pmd)
226+
}
227+
228+
val pmd = PythonMethodDescription(
229+
methodUnderTest.name,
230+
parameters,
231+
fuzzedConcreteValues,
232+
pythonTypeStorage,
233+
Trie(Instruction::id)
234+
)
235+
236+
if (parameters.isEmpty()) {
237+
fuzzingResultHandler(pmd, emptyList())
238+
} else {
239+
PythonFuzzing(pmd.pythonTypeStorage) { description, arguments ->
240+
if (isCancelled()) {
241+
logger.info { "Fuzzing process was interrupted" }
242+
return@PythonFuzzing PythonFeedback(control = Control.STOP)
243+
}
244+
if (System.currentTimeMillis() >= until) {
245+
logger.info { "Fuzzing process was interrupted by timeout" }
246+
return@PythonFuzzing PythonFeedback(control = Control.STOP)
247+
}
248+
249+
return@PythonFuzzing fuzzingResultHandler(description, arguments)
250+
}.fuzz(pmd)
251+
}
245252
}
246253
}

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/PythonCodeGenerator.kt

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,32 +149,35 @@ class PythonCodeGenerator(
149149
CgVariable(argument.name, argument.annotation?.let { PythonClassId(it) } ?: pythonAnyClassId)
150150
}
151151

152-
var argumentsTryCatch = tryBlock {
153-
methodArguments.zip(arguments).map { (model, argument) ->
154-
if (model is PythonTreeModel) {
155-
val obj =
156-
(context.cgLanguageAssistant.getVariableConstructorBy(context) as PythonCgVariableConstructor)
157-
.getOrCreateVariable(model)
158-
+CgAssignment(
159-
argument,
160-
(obj as CgPythonTree).value
161-
)
162-
} else {
163-
+CgAssignment(argument, CgLiteral(model.classId, model.toString()))
152+
if (method.arguments.isNotEmpty()) {
153+
var argumentsTryCatch = tryBlock {
154+
methodArguments.zip(arguments).map { (model, argument) ->
155+
if (model is PythonTreeModel) {
156+
val obj =
157+
(context.cgLanguageAssistant.getVariableConstructorBy(context) as PythonCgVariableConstructor)
158+
.getOrCreateVariable(model)
159+
+CgAssignment(
160+
argument,
161+
(obj as CgPythonTree).value
162+
)
163+
} else {
164+
+CgAssignment(argument, CgLiteral(model.classId, model.toString()))
165+
}
164166
}
165167
}
166-
}
167-
argumentsTryCatch = argumentsTryCatch.catch(PythonClassId("builtins.Exception")) { exception ->
168-
+CgPythonFunctionCall(
169-
pythonNoneClassId,
170-
failArgumentsFunctionName,
171-
listOf(
172-
outputPath,
173-
exception,
168+
argumentsTryCatch = argumentsTryCatch.catch(PythonClassId("builtins.Exception")) { exception ->
169+
+CgPythonFunctionCall(
170+
pythonNoneClassId,
171+
failArgumentsFunctionName,
172+
listOf(
173+
outputPath,
174+
exception,
175+
)
174176
)
175-
)
176-
emptyLine()
177-
+CgPythonRepr(pythonAnyClassId, "sys.exit()")
177+
emptyLine()
178+
+CgPythonRepr(pythonAnyClassId, "sys.exit()")
179+
}
180+
argumentsTryCatch.accept(renderer)
178181
}
179182

180183
val args = CgPythonList(emptyList())
@@ -195,7 +198,6 @@ class PythonCodeGenerator(
195198
)
196199
)
197200

198-
argumentsTryCatch.accept(renderer)
199201
executorCall.accept(renderer)
200202

201203
renderer.toString()

utbot-python/src/main/kotlin/org/utbot/python/newtyping/mypy/RunMypy.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fun readMypyAnnotationStorageAndInitialErrors(
2222
val result = runCommand(
2323
listOf(
2424
pythonPath,
25+
"-X",
26+
"utf8",
2527
"-m",
2628
"utbot_mypy_runner",
2729
"--config",
@@ -72,17 +74,18 @@ fun checkWithDMypy(pythonPath: String, fileWithCodePath: String, configFile: Fil
7274

7375
fun setConfigFile(directoriesForSysPath: Set<String>): File {
7476
val file = TemporaryFileManager.assignTemporaryFile(configFilename)
77+
val dirForCache = TemporaryFileManager.assignTemporaryFile(tag = "mypy_cache")
7578
val configContent = """
7679
[mypy]
7780
mypy_path = ${directoriesForSysPath.joinToString(separator = ":") { it.modifyWindowsPath() } }
7881
namespace_packages = True
79-
explicit_package_bases = True
82+
cache_dir = ${dirForCache.absolutePath}
8083
show_absolute_path = True
8184
cache_fine_grained = True
8285
check_untyped_defs = True
83-
implicit_optional = True
8486
strict_optional = False
8587
disable_error_code = assignment,union-attr
88+
implicit_optional = True
8689
""".trimIndent()
8790
TemporaryFileManager.writeToAssignedFile(file, configContent)
8891
return file

0 commit comments

Comments
 (0)