Skip to content

Commit a032e82

Browse files
committed
removed the requirement of dataframe properties to be mutable for isOpen to be true in DataSchemas in Jupyter
1 parent 65687b4 commit a032e82

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/codeGen/ReplCodeGenerator.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ import org.jetbrains.kotlinx.dataframe.codeGen.CodeWithConverter
66
import org.jetbrains.kotlinx.dataframe.impl.codeGen.ReplCodeGeneratorImpl
77
import org.jetbrains.kotlinx.jupyter.api.Code
88
import kotlin.reflect.KClass
9-
import kotlin.reflect.KMutableProperty
109
import kotlin.reflect.KProperty
1110

1211
internal interface ReplCodeGenerator {
1312

1413
fun process(
1514
df: AnyFrame,
1615
property: KProperty<*>? = null,
17-
isMutable: Boolean = property is KMutableProperty?,
1816
): CodeWithConverter
1917

2018
fun process(
2119
row: AnyRow,
2220
property: KProperty<*>? = null,
23-
isMutable: Boolean = property is KMutableProperty?,
2421
): CodeWithConverter
2522

2623
fun process(markerClass: KClass<*>): Code

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/codeGen/ReplCodeGeneratorImpl.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import org.jetbrains.kotlinx.dataframe.codeGen.MarkersExtractor
1616
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
1717
import org.jetbrains.kotlinx.jupyter.api.Code
1818
import kotlin.reflect.KClass
19-
import kotlin.reflect.KMutableProperty
2019
import kotlin.reflect.KProperty
2120
import kotlin.reflect.KType
2221
import kotlin.reflect.KVisibility
@@ -45,25 +44,22 @@ internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
4544
else -> null
4645
}
4746

48-
override fun process(row: AnyRow, property: KProperty<*>?, isMutable: Boolean) =
49-
process(row.df(), property, isMutable)
47+
override fun process(row: AnyRow, property: KProperty<*>?): CodeWithConverter = process(row.df(), property)
5048

51-
override fun process(df: AnyFrame, property: KProperty<*>?, isMutable: Boolean): CodeWithConverter {
49+
override fun process(df: AnyFrame, property: KProperty<*>?): CodeWithConverter {
5250
var targetSchema = df.schema()
53-
var isMutable = isMutable
5451

5552
if (property != null) {
5653
val wasProcessedBefore = property in registeredProperties
5754
registeredProperties.add(property)
58-
isMutable = property is KMutableProperty
5955

6056
// maybe property is already properly typed, let's do some checks
6157
val currentMarker = getMarkerClass(property.returnType)
6258
?.takeIf { it.findAnnotation<DataSchema>() != null }
6359
?.let { registeredMarkers[it] ?: MarkersExtractor.get(it) }
6460
if (currentMarker != null) {
65-
// if property is mutable, we need to make sure that its marker type is open in order to let derived data frames be assignable to it
66-
if (!isMutable || currentMarker.isOpen) {
61+
// we need to make sure that the property's marker type is open in order to let derived data frames be assignable to it
62+
if (currentMarker.isOpen) {
6763
val columnSchema = currentMarker.schema
6864
// for mutable properties we do strong typing only at the first processing, after that we allow its type to be more general than actual data frame type
6965
if (wasProcessedBefore || columnSchema == targetSchema) {
@@ -79,7 +75,7 @@ internal class ReplCodeGeneratorImpl : ReplCodeGenerator {
7975
}
8076
}
8177

82-
return generate(schema = targetSchema, name = markerInterfacePrefix, isOpen = isMutable)
78+
return generate(schema = targetSchema, name = markerInterfacePrefix, isOpen = true)
8379
}
8480

8581
fun generate(

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/codeGen/ReplCodeGenTests.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ReplCodeGenTests : BaseTest() {
6868
@Test
6969
fun `process derived markers`() {
7070
val repl = ReplCodeGenerator.create()
71-
val code = repl.process(df, isMutable = true).declarations
71+
val code = repl.process(df).declarations
7272

7373
val marker = ReplCodeGeneratorImpl.markerInterfacePrefix
7474
val markerFull = Test1._DataFrameType::class.qualifiedName!!
@@ -100,10 +100,10 @@ class ReplCodeGenTests : BaseTest() {
100100
code2 shouldBe ""
101101

102102
val df3 = typed.filter { city != null }
103-
val code3 = repl.process(df3, isMutable = false).declarations
103+
val code3 = repl.process(df3).declarations
104104
val marker3 = marker + "1"
105105
val expected3 = """
106-
@DataSchema(isOpen = false)
106+
@DataSchema
107107
interface $marker3 : $markerFull
108108
109109
val $dfName<$marker3>.city: $dataCol<$stringName> @JvmName("${marker3}_city") get() = this["city"] as $dataCol<$stringName>
@@ -118,10 +118,10 @@ class ReplCodeGenTests : BaseTest() {
118118
code4 shouldBe ""
119119

120120
val df5 = typed.filter { weight != null }
121-
val code5 = repl.process(df5, isMutable = false).declarations
121+
val code5 = repl.process(df5).declarations
122122
val marker5 = marker + "2"
123123
val expected5 = """
124-
@DataSchema(isOpen = false)
124+
@DataSchema
125125
interface $marker5 : $markerFull
126126
127127
val $dfName<$marker5>.weight: $dataCol<$intName> @JvmName("${marker5}_weight") get() = this["weight"] as $dataCol<$intName>
@@ -144,12 +144,12 @@ class ReplCodeGenTests : BaseTest() {
144144
repl.process<Test2._DataFrameType1>() shouldBe ""
145145

146146
val expected = """
147-
@DataSchema(isOpen = false)
147+
@DataSchema
148148
interface ${Test2._DataFrameType2::class.simpleName!!} : ${Test2._DataFrameType::class.qualifiedName}, ${Test2._DataFrameType1::class.qualifiedName}
149149
150150
""".trimIndent()
151151

152-
val code = repl.process(typed, isMutable = false).declarations.trimIndent()
152+
val code = repl.process(typed).declarations.trimIndent()
153153
code shouldBe expected
154154
}
155155

@@ -163,7 +163,7 @@ class ReplCodeGenTests : BaseTest() {
163163

164164
val marker = Test2._DataFrameType2::class.simpleName!!
165165
val expected = """
166-
@DataSchema(isOpen = false)
166+
@DataSchema
167167
interface $marker : ${Test2._DataFrameType::class.qualifiedName}
168168
169169
val $dfName<$marker>.city: $dataCol<$stringName?> @JvmName("${marker}_city") get() = this["city"] as $dataCol<$stringName?>
@@ -176,17 +176,17 @@ class ReplCodeGenTests : BaseTest() {
176176
val $dfRowName<$marker?>.weight: $intName? @JvmName("Nullable${marker}_weight") get() = this["weight"] as $intName?
177177
""".trimIndent()
178178

179-
val code = repl.process(typed, isMutable = false).declarations.trimIndent()
179+
val code = repl.process(typed).declarations.trimIndent()
180180
code shouldBe expected
181181
}
182182

183183
@Test
184-
fun `process overriden property`() {
184+
fun `process overridden property`() {
185185
val repl = ReplCodeGenerator.create()
186186
repl.process<Test3.A>()
187187
repl.process<Test3.B>()
188188
repl.process<Test3.C>()
189-
val c = repl.process(Test3.df, Test3::df)
189+
val c = repl.process(Test3.df, Test3::df) // TODO this now generates stuff
190190
c.declarations.shouldBeEmpty()
191191
}
192192

0 commit comments

Comments
 (0)