Skip to content

Fix #640: Jupyter integration conflicts with variable type converters from other integrations #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,8 @@ import org.jetbrains.kotlinx.dataframe.impl.renderType
import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
import org.jetbrains.kotlinx.dataframe.io.SupportedCodeGenerationFormat
import org.jetbrains.kotlinx.dataframe.io.supportedFormats
import org.jetbrains.kotlinx.jupyter.api.HTML
import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost
import org.jetbrains.kotlinx.jupyter.api.Notebook
import org.jetbrains.kotlinx.jupyter.api.VariableName
import org.jetbrains.kotlinx.jupyter.api.declare
import org.jetbrains.kotlinx.jupyter.api.libraries.ColorScheme
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
import org.jetbrains.kotlinx.jupyter.api.libraries.resources
import org.jetbrains.kotlinx.jupyter.api.*
import org.jetbrains.kotlinx.jupyter.api.libraries.*
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KType
Expand Down Expand Up @@ -281,16 +274,25 @@ internal class Integration(
import("org.jetbrains.kotlinx.dataframe.dataTypes.*")
import("org.jetbrains.kotlinx.dataframe.impl.codeGen.urlCodeGenReader")

updateVariable<Any> { instance, property ->
when (instance) {
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
else -> null
addTypeConverter(object : FieldHandler {
override val execution = FieldHandlerFactory.createUpdateExecution<Any> { instance, property ->
when (instance) {
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
else -> error("${instance::class} should not be handled by Dataframe field handler")
}
}
}
override fun accepts(value: Any?, property: KProperty<*>): Boolean {
return value is AnyCol ||
value is ColumnGroup<*> ||
value is AnyRow ||
value is AnyFrame ||
value is ImportDataSchema
}
})

fun KotlinKernelHost.addDataSchemas(classes: List<KClass<*>>) {
val code = classes.joinToString("\n") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ class JupyterCodegenTests : JupyterReplTestCase() {
res2.shouldBeInstanceOf<Unit>()
}

@Test
fun `type converter does not conflict with other type converters`() {
@Language("kts")
val anotherTypeConverter = """
notebook.fieldsHandlersProcessor.register(
FieldHandlerFactory.createUpdateHandler<ByteArray>(TypeDetection.RUNTIME) { _, prop ->
execute(prop.name + ".toList()").name
},
ProcessingPriority.LOW
)
""".trimIndent()
execEx(anotherTypeConverter)
execEx("val x = ByteArray(1)")
val res1 = execRaw("x")
res1.shouldBeInstanceOf<List<*>>()
}

@Test
fun `generate a new marker when dataframe marker is not a data schema so that columns are accessible with extensions`() {
@Language("kts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,8 @@ import org.jetbrains.kotlinx.dataframe.impl.renderType
import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
import org.jetbrains.kotlinx.dataframe.io.SupportedCodeGenerationFormat
import org.jetbrains.kotlinx.dataframe.io.supportedFormats
import org.jetbrains.kotlinx.jupyter.api.HTML
import org.jetbrains.kotlinx.jupyter.api.JupyterClientType
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost
import org.jetbrains.kotlinx.jupyter.api.Notebook
import org.jetbrains.kotlinx.jupyter.api.VariableName
import org.jetbrains.kotlinx.jupyter.api.declare
import org.jetbrains.kotlinx.jupyter.api.libraries.ColorScheme
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
import org.jetbrains.kotlinx.jupyter.api.libraries.resources
import org.jetbrains.kotlinx.jupyter.api.*
import org.jetbrains.kotlinx.jupyter.api.libraries.*
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KType
Expand Down Expand Up @@ -281,16 +274,25 @@ internal class Integration(
import("org.jetbrains.kotlinx.dataframe.dataTypes.*")
import("org.jetbrains.kotlinx.dataframe.impl.codeGen.urlCodeGenReader")

updateVariable<Any> { instance, property ->
when (instance) {
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
else -> null
addTypeConverter(object : FieldHandler {
override val execution = FieldHandlerFactory.createUpdateExecution<Any> { instance, property ->
when (instance) {
is AnyCol -> updateAnyColVariable(instance, property, codeGen)
is ColumnGroup<*> -> updateColumnGroupVariable(instance, property, codeGen)
is AnyRow -> updateAnyRowVariable(instance, property, codeGen)
is AnyFrame -> updateAnyFrameVariable(instance, property, codeGen)
is ImportDataSchema -> updateImportDataSchemaVariable(instance, property)
else -> error("${instance::class} should not be handled by Dataframe field handler")
}
}
}
override fun accepts(value: Any?, property: KProperty<*>): Boolean {
return value is AnyCol ||
value is ColumnGroup<*> ||
value is AnyRow ||
value is AnyFrame ||
value is ImportDataSchema
}
})

fun KotlinKernelHost.addDataSchemas(classes: List<KClass<*>>) {
val code = classes.joinToString("\n") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ class JupyterCodegenTests : JupyterReplTestCase() {
res2.shouldBeInstanceOf<Unit>()
}

@Test
fun `type converter does not conflict with other type converters`() {
@Language("kts")
val anotherTypeConverter = """
notebook.fieldsHandlersProcessor.register(
FieldHandlerFactory.createUpdateHandler<ByteArray>(TypeDetection.RUNTIME) { _, prop ->
execute(prop.name + ".toList()").name
},
ProcessingPriority.LOW
)
""".trimIndent()
execEx(anotherTypeConverter)
execEx("val x = ByteArray(1)")
val res1 = execRaw("x")
res1.shouldBeInstanceOf<List<*>>()
}

@Test
fun `generate a new marker when dataframe marker is not a data schema so that columns are accessible with extensions`() {
@Language("kts")
Expand Down