Skip to content

Commit 195a305

Browse files
committed
Fix #640: Jupyter integration conflicts with variable type converters from other integrations.
1 parent 9de12a3 commit 195a305

File tree

4 files changed

+74
-36
lines changed

4 files changed

+74
-36
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,8 @@ import org.jetbrains.kotlinx.dataframe.impl.renderType
4545
import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
4646
import org.jetbrains.kotlinx.dataframe.io.SupportedCodeGenerationFormat
4747
import org.jetbrains.kotlinx.dataframe.io.supportedFormats
48-
import org.jetbrains.kotlinx.jupyter.api.HTML
49-
import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
50-
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost
51-
import org.jetbrains.kotlinx.jupyter.api.Notebook
52-
import org.jetbrains.kotlinx.jupyter.api.VariableName
53-
import org.jetbrains.kotlinx.jupyter.api.declare
54-
import org.jetbrains.kotlinx.jupyter.api.libraries.ColorScheme
55-
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
56-
import org.jetbrains.kotlinx.jupyter.api.libraries.resources
48+
import org.jetbrains.kotlinx.jupyter.api.*
49+
import org.jetbrains.kotlinx.jupyter.api.libraries.*
5750
import kotlin.reflect.KClass
5851
import kotlin.reflect.KProperty
5952
import kotlin.reflect.KType
@@ -281,16 +274,25 @@ internal class Integration(
281274
import("org.jetbrains.kotlinx.dataframe.dataTypes.*")
282275
import("org.jetbrains.kotlinx.dataframe.impl.codeGen.urlCodeGenReader")
283276

284-
updateVariable<Any> { instance, property ->
285-
when (instance) {
286-
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
287-
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
288-
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
289-
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
290-
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
291-
else -> null
277+
addTypeConverter(object : FieldHandler {
278+
override val execution = FieldHandlerFactory.createUpdateExecution<Any> { instance, property ->
279+
when (instance) {
280+
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
281+
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
282+
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
283+
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
284+
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
285+
else -> error("${instance::class} should not be handled by Dataframe field handler")
286+
}
292287
}
293-
}
288+
override fun accepts(value: Any?, property: KProperty<*>): Boolean {
289+
return value is AnyCol ||
290+
value is ColumnGroup<*> ||
291+
value is AnyRow ||
292+
value is AnyFrame ||
293+
value is ImportDataSchema
294+
}
295+
})
294296

295297
fun KotlinKernelHost.addDataSchemas(classes: List<KClass<*>>) {
296298
val code = classes.joinToString("\n") {

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/JupyterCodegenTests.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,23 @@ class JupyterCodegenTests : JupyterReplTestCase() {
301301
res2.shouldBeInstanceOf<Unit>()
302302
}
303303

304+
@Test
305+
fun `type converter does not conflict with other type converters`() {
306+
@Language("kts")
307+
val anotherTypeConverter = """
308+
notebook.fieldsHandlersProcessor.register(
309+
FieldHandlerFactory.createUpdateHandler<ByteArray>(TypeDetection.RUNTIME) { _, prop ->
310+
execute(prop.name + ".toList()").name
311+
},
312+
ProcessingPriority.LOW
313+
)
314+
""".trimIndent()
315+
execEx(anotherTypeConverter)
316+
execEx("val x = ByteArray(1)")
317+
val res1 = execRaw("x")
318+
res1.shouldBeInstanceOf<List<*>>()
319+
}
320+
304321
@Test
305322
fun `generate a new marker when dataframe marker is not a data schema so that columns are accessible with extensions`() {
306323
@Language("kts")

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,8 @@ import org.jetbrains.kotlinx.dataframe.impl.renderType
4545
import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
4646
import org.jetbrains.kotlinx.dataframe.io.SupportedCodeGenerationFormat
4747
import org.jetbrains.kotlinx.dataframe.io.supportedFormats
48-
import org.jetbrains.kotlinx.jupyter.api.HTML
49-
import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
50-
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost
51-
import org.jetbrains.kotlinx.jupyter.api.Notebook
52-
import org.jetbrains.kotlinx.jupyter.api.VariableName
53-
import org.jetbrains.kotlinx.jupyter.api.declare
54-
import org.jetbrains.kotlinx.jupyter.api.libraries.ColorScheme
55-
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
56-
import org.jetbrains.kotlinx.jupyter.api.libraries.resources
48+
import org.jetbrains.kotlinx.jupyter.api.*
49+
import org.jetbrains.kotlinx.jupyter.api.libraries.*
5750
import kotlin.reflect.KClass
5851
import kotlin.reflect.KProperty
5952
import kotlin.reflect.KType
@@ -281,16 +274,25 @@ internal class Integration(
281274
import("org.jetbrains.kotlinx.dataframe.dataTypes.*")
282275
import("org.jetbrains.kotlinx.dataframe.impl.codeGen.urlCodeGenReader")
283276

284-
updateVariable<Any> { instance, property ->
285-
when (instance) {
286-
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
287-
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
288-
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
289-
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
290-
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
291-
else -> null
277+
addTypeConverter(object : FieldHandler {
278+
override val execution = FieldHandlerFactory.createUpdateExecution<Any> { instance, property ->
279+
when (instance) {
280+
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
281+
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
282+
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
283+
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
284+
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
285+
else -> error("${instance::class} should not be handled by Dataframe field handler")
286+
}
292287
}
293-
}
288+
override fun accepts(value: Any?, property: KProperty<*>): Boolean {
289+
return value is AnyCol ||
290+
value is ColumnGroup<*> ||
291+
value is AnyRow ||
292+
value is AnyFrame ||
293+
value is ImportDataSchema
294+
}
295+
})
294296

295297
fun KotlinKernelHost.addDataSchemas(classes: List<KClass<*>>) {
296298
val code = classes.joinToString("\n") {

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/JupyterCodegenTests.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,23 @@ class JupyterCodegenTests : JupyterReplTestCase() {
301301
res2.shouldBeInstanceOf<Unit>()
302302
}
303303

304+
@Test
305+
fun `type converter does not conflict with other type converters`() {
306+
@Language("kts")
307+
val anotherTypeConverter = """
308+
notebook.fieldsHandlersProcessor.register(
309+
FieldHandlerFactory.createUpdateHandler<ByteArray>(TypeDetection.RUNTIME) { _, prop ->
310+
execute(prop.name + ".toList()").name
311+
},
312+
ProcessingPriority.LOW
313+
)
314+
""".trimIndent()
315+
execEx(anotherTypeConverter)
316+
execEx("val x = ByteArray(1)")
317+
val res1 = execRaw("x")
318+
res1.shouldBeInstanceOf<List<*>>()
319+
}
320+
304321
@Test
305322
fun `generate a new marker when dataframe marker is not a data schema so that columns are accessible with extensions`() {
306323
@Language("kts")

0 commit comments

Comments
 (0)