Skip to content

Fixed local classes being inferred as Any by changing visibility check #929

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 2 commits into from
Oct 22, 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 @@ -228,15 +228,14 @@ internal fun commonParents(classes: Iterable<KClass<*>>): List<KClass<*>> =
.let {
when {
// if there is only one class - return it
it.size == 1 && it[0].visibility == KVisibility.PUBLIC -> listOf(it[0])
it.size == 1 && it[0].visibility.let { it == null || it == KVisibility.PUBLIC } -> listOf(it[0])

else ->
it.fold(null as (Set<KClass<*>>?)) { set, clazz ->
// collect a set of all common superclasses from original classes
val superclasses =
(clazz.allSuperclasses + clazz)
.filter { it.visibility == KVisibility.PUBLIC }
.toSet()
val superclasses = (clazz.allSuperclasses + clazz)
.filter { it.visibility == null || it.visibility == KVisibility.PUBLIC }
.toSet()
set?.intersect(superclasses) ?: superclasses
}!!.let {
// leave only 'leaf' classes, that are not super to some other class in a set
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.jetbrains.kotlinx.dataframe.api

import io.kotest.matchers.shouldBe
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.impl.nothingType
import org.jetbrains.kotlinx.dataframe.type
import org.junit.Test
import kotlin.reflect.typeOf

class ConstructorsTests {

Expand Down Expand Up @@ -32,4 +34,30 @@ class ConstructorsTests {
dataFrameOf("a" to emptyList())["a"].type shouldBe nothingType(false)
dataFrameOf("a" to listOf(null))["a"].type shouldBe nothingType(true)
}

@Suppress("ktlint:standard:argument-list-wrapping")
@Test
fun `dataFrameOf with local class`() {
// issue #928
data class Car(val type: String, val model: String)

val cars: DataFrame<*> = dataFrameOf("owner", "car")(
"Max", Car("audi", "a8"),
"Tom", Car("toyota", "corolla"),
)

cars["car"].type shouldBe typeOf<Car>()

val unfolded = cars.unfold("car")
unfolded["car"]["type"].type shouldBe typeOf<String>()
unfolded["car"]["model"].type shouldBe typeOf<String>()

val cars2 = listOf(
Car("audi", "a8"),
Car("toyota", "corolla"),
).toDataFrame()

cars2["type"].type shouldBe typeOf<String>()
cars2["model"].type shouldBe typeOf<String>()
}
}
Loading