Skip to content

Commit 69796a4

Browse files
authored
Mypy fixes (#2302)
* Change mypy log output * Fix problem with tested function from __init__ file
1 parent 4eee309 commit 69796a4

File tree

6 files changed

+20
-4
lines changed

6 files changed

+20
-4
lines changed

utbot-cli-python/src/main/kotlin/org/utbot/cli/language/python/PythonGenerateTestsCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.utbot.python.framework.codegen.model.Pytest
1717
import org.utbot.python.framework.codegen.model.Unittest
1818
import org.utbot.python.newtyping.ast.parseClassDefinition
1919
import org.utbot.python.newtyping.ast.parseFunctionDefinition
20+
import org.utbot.python.newtyping.mypy.dropInitFile
2021
import org.utbot.python.utils.*
2122
import org.utbot.python.utils.RequirementsUtils.installRequirements
2223
import org.utbot.python.utils.RequirementsUtils.requirements
@@ -234,7 +235,7 @@ class PythonGenerateTestsCommand : CliktCommand(
234235
pythonFilePath = sourceFile.toAbsolutePath(),
235236
pythonFileContent = sourceFileContent,
236237
directoriesForSysPath = directoriesForSysPath.map { it.toAbsolutePath() }.toSet(),
237-
currentPythonModule = currentPythonModule,
238+
currentPythonModule = currentPythonModule.dropInitFile(),
238239
pythonMethods = pythonMethods,
239240
containingClassName = pythonClass,
240241
timeout = timeout,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import org.utbot.python.PythonTestGenerationProcessor
4242
import org.utbot.python.PythonTestGenerationProcessor.processTestGeneration
4343
import org.utbot.python.framework.api.python.PythonClassId
4444
import org.utbot.python.framework.codegen.PythonCgLanguageAssistant
45+
import org.utbot.python.newtyping.mypy.dropInitFile
4546
import org.utbot.python.utils.RequirementsUtils.installRequirements
4647
import org.utbot.python.utils.RequirementsUtils.requirements
4748
import org.utbot.python.utils.camelToSnakeCase
@@ -174,7 +175,7 @@ object PythonDialogProcessor {
174175
realElements.toSet(),
175176
model.runtimeExceptionTestsBehaviour,
176177
directoriesForSysPath,
177-
moduleToImport,
178+
moduleToImport.dropInitFile(),
178179
file,
179180
realElements.first().getContainingClass() as PyClass?
180181
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def init_function(x: int):
2+
return x ** 2

utbot-python/src/main/kotlin/org/utbot/python/newtyping/inference/TypeInferenceProcessor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class TypeInferenceProcessor(
6060
val (mypyStorage, report) = readMypyAnnotationStorageAndInitialErrors(
6161
pythonPath,
6262
path.toString(),
63-
moduleOfSourceFile,
63+
moduleOfSourceFile.dropInitFile(),
6464
configFile
6565
)
6666

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ fun readMypyAnnotationStorageAndInitialErrors(
4545
)
4646
)
4747
val stderr = if (fileForMypyStderr.exists()) fileForMypyStderr.readText() else null
48+
val stdout = if (fileForMypyStdout.exists()) fileForMypyStdout.readText() else null
4849
val mypyExitStatus = if (fileForMypyExitStatus.exists()) fileForMypyExitStatus.readText() else null
4950
if (result.exitValue != 0 || mypyExitStatus != "0")
5051
error("Something went wrong in initial mypy run. " +
5152
"\nPython stderr ${result.stderr}" +
52-
"\nMypy stderr: $stderr")
53+
"\nMypy stderr: $stderr" +
54+
"\nMypy stdout: $stdout")
5355
return Pair(
5456
readMypyAnnotationStorage(fileForAnnotationStorage.readText()),
5557
getErrorsAndNotes(fileForMypyStdout.readText())

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,14 @@ fun String.modifyWindowsPath(): String {
55
val (disk, path) = this.split(":", limit = 2)
66
"\\\\localhost\\$disk$${path.replace("/", "\\")}"
77
} else this
8+
}
9+
10+
/**
11+
* Remove .__init__ suffix if it exists.
12+
It resolves problem with duplication module names in mypy, e.g. mod.__init__ and mod
13+
*/
14+
fun String.dropInitFile(): String {
15+
return if (this.endsWith(".__init__")) {
16+
this.dropLast(9)
17+
} else this
818
}

0 commit comments

Comments
 (0)