[python] migration from kotlin.Result to python.PyResult in package managers (PY-81238)

* introduce PyResult.localizedError() (instead of failure()
* get rid of obsolete kotlin.Result.failure(..) extension
* add errorOrNull to python.Result, to access it from Java classes

Merge-request: IJ-MR-163973
Merged-by: Vitaly Legchilkin <Vitaly.Legchilkin@jetbrains.com>

GitOrigin-RevId: 2bd8336cf4c53170eea5d050f609fd64541ebd99
This commit is contained in:
Vitaly Legchilkin
2025-05-27 07:18:45 +00:00
committed by intellij-monorepo-bot
parent f419dbbb08
commit 50995c2812
83 changed files with 607 additions and 754 deletions
@@ -9,6 +9,7 @@ import com.intellij.python.community.services.internal.impl.PythonWithLanguageLe
import com.intellij.python.community.services.shared.PythonWithLanguageLevel
import com.jetbrains.python.PythonBinary
import com.jetbrains.python.Result
import com.jetbrains.python.errorProcessing.PyResult
import com.jetbrains.python.psi.LanguageLevel
import com.jetbrains.python.validatePythonAndGetVersion
import kotlinx.coroutines.async
@@ -34,7 +35,7 @@ class PythonWithLanguageLevelImpl internal constructor(
* Like [createByPythonBinary] but runs in parallel up to [concurrentLimit]
* @return python path -> python with language level sorted from highest to lowest.
*/
suspend fun createByPythonBinaries(pythonBinaries: Collection<PythonBinary>): Collection<Pair<PythonBinary, Result<PythonWithLanguageLevel, @Nls String>>> =
suspend fun createByPythonBinaries(pythonBinaries: Collection<PythonBinary>): Collection<Pair<PythonBinary, PyResult<PythonWithLanguageLevel>>> =
coroutineScope {
pythonBinaries.map {
async {
@@ -45,7 +46,7 @@ class PythonWithLanguageLevelImpl internal constructor(
}.awaitAll()
}.sortedBy { it.first }
suspend fun createByPythonBinary(pythonBinary: PythonBinary): Result<PythonWithLanguageLevelImpl, @Nls String> {
suspend fun createByPythonBinary(pythonBinary: PythonBinary): PyResult<PythonWithLanguageLevelImpl> {
val languageLevel = pythonBinary.validatePythonAndGetVersion().getOr { return it }
return Result.success(PythonWithLanguageLevelImpl(pythonBinary, languageLevel))
}
@@ -8,6 +8,7 @@ import com.intellij.platform.eel.provider.localEel
import com.intellij.python.community.services.shared.PythonWithLanguageLevel
import com.jetbrains.python.PythonBinary
import com.jetbrains.python.Result
import com.jetbrains.python.errorProcessing.PyResult
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.Nls
import javax.swing.Icon
@@ -27,7 +28,7 @@ sealed interface SystemPythonService {
* When user provides a path to the python binary, use this method to the [SystemPython].
* @return either [SystemPython] or an error if python is broken.
*/
suspend fun registerSystemPython(pythonPath: PythonBinary): Result<SystemPython, @Nls String>
suspend fun registerSystemPython(pythonPath: PythonBinary): PyResult<SystemPython>
/**
* @return tool to install python on OS If [eelApi] supports python installation
@@ -17,12 +17,12 @@ import com.intellij.python.community.services.systemPython.impl.Cache
import com.intellij.python.community.services.systemPython.impl.CoreSystemPythonProvider
import com.jetbrains.python.PythonBinary
import com.jetbrains.python.Result
import com.jetbrains.python.errorProcessing.PyResult
import com.jetbrains.python.sdk.installer.installBinary
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.jetbrains.annotations.ApiStatus.Internal
import org.jetbrains.annotations.Nls
import java.nio.file.InvalidPathException
import java.nio.file.Path
import kotlin.io.path.pathString
@@ -59,7 +59,7 @@ internal class SystemPythonServiceImpl(scope: CoroutineScope) : SystemPythonServ
}
}
override suspend fun registerSystemPython(pythonPath: PythonBinary): Result<SystemPython, @Nls String> {
override suspend fun registerSystemPython(pythonPath: PythonBinary): PyResult<SystemPython> {
val pythonWithLangLevel = PythonWithLanguageLevelImpl.createByPythonBinary(pythonPath).getOr { return it }
val systemPython = SystemPython(pythonWithLangLevel, null)
state.userProvidedPythons.add(pythonPath.pathString)