Skip to content

Commit a9996b1

Browse files
committed
Fix bug in code generation for nullable variables.
1 parent 3282657 commit a9996b1

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/Integration.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ internal class Integration : JupyterIntegration() {
119119
} else null
120120
}
121121

122-
fun KotlinKernelHost.execute(codeWithConverter: CodeWithConverter, property: KProperty<*>): VariableName? = execute(codeWithConverter, property.name)
122+
fun KotlinKernelHost.execute(codeWithConverter: CodeWithConverter, property: KProperty<*>): VariableName? {
123+
val variableName = property.name + if (property.returnType.isMarkedNullable) "!!" else ""
124+
return execute(codeWithConverter, variableName)
125+
}
123126

124127
updateVariable<AnyFrame> { df, property ->
125128
execute(codeGen.process(df, property), property)
@@ -135,8 +138,10 @@ internal class Integration : JupyterIntegration() {
135138

136139
updateVariable<AnyCol> { col, property ->
137140
if (col.isColumnGroup()) {
138-
val codeWithConverter = codeGen.process(col.asColumnGroup().asDataFrame(), property)
139-
execute(codeWithConverter, "${property.name}.asColumnGroup()")
141+
val codeWithConverter = codeGen.process(col.asColumnGroup().asDataFrame(), property).let { c ->
142+
CodeWithConverter(c.declarations) { c.converter(it + ".asColumnGroup()") }
143+
}
144+
execute(codeWithConverter, property)
140145
} else null
141146
}
142147

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.jetbrains.kotlinx.dataframe.jupyter
2+
3+
import org.jetbrains.kotlinx.jupyter.api.Code
4+
import org.junit.Test
5+
6+
class CodeGenerationTests : DataFrameJupyterTest() {
7+
8+
private fun Code.checkCompilation() {
9+
lines().forEach {
10+
exec(it)
11+
}
12+
}
13+
14+
@Test
15+
fun `nullable dataframe`() {
16+
"""
17+
fun create(): AnyFrame? = dataFrameOf("a")(1)
18+
val df = create()
19+
df.a
20+
""".checkCompilation()
21+
}
22+
23+
@Test
24+
fun `nullable columnGroup`() {
25+
"""
26+
fun create(): AnyCol? = dataFrameOf("a")(1).asColumnGroup().asDataColumn()
27+
val col = create()
28+
col.a
29+
""".checkCompilation()
30+
}
31+
32+
@Test
33+
fun `nullable dataRow`() {
34+
"""
35+
fun create(): AnyRow? = dataFrameOf("a")(1).single()
36+
val row = create()
37+
row.a
38+
""".checkCompilation()
39+
}
40+
}

0 commit comments

Comments
 (0)