PY-74236: Get rid of nullable Sdk?.

IO errors must be reported as `Result`, other should simply be thrown.

GitOrigin-RevId: d9a8ce7f38ada3e3f59412bc8425e9fc5556c880
This commit is contained in:
Ilya.Kazakevich
2024-07-30 15:25:55 +02:00
committed by intellij-monorepo-bot
parent aeaf8e921e
commit 4e297d445d
8 changed files with 40 additions and 47 deletions

View File

@@ -51,7 +51,7 @@ class CondaNewEnvironmentCreator(model: PythonMutableTargetAddInterpreterModel)
model.state.newCondaEnvName.set(model.projectPath.get().substringAfterLast(File.separator))
}
override fun getOrCreateSdk(): Sdk? {
override fun getOrCreateSdk(): Sdk {
return model.createCondaEnvironment(NewCondaEnvRequest.EmptyNamedEnv(pythonVersion.get(), model.state.newCondaEnvName.get()))
}

View File

@@ -57,7 +57,7 @@ class PipEnvNewEnvironmentCreator(model: PythonMutableTargetAddInterpreterModel)
//}
}
override fun getOrCreateSdk(): Sdk? {
override fun getOrCreateSdk(): Sdk {
if (model is PythonLocalAddInterpreterModel) {
PropertiesComponent.getInstance().pipEnvPath = model.state.pipenvExecutable.get().nullize()
}

View File

@@ -120,7 +120,7 @@ class PythonAddCustomInterpreter(val model: PythonMutableTargetAddInterpreterMod
existingInterpreterSelectors.values.forEach(PythonAddEnvironment::onShown)
}
fun getSdk(): Sdk? = currentSdkManager.getOrCreateSdk()
fun getSdk(): Sdk = currentSdkManager.getOrCreateSdk()
fun createStatisticsInfo(): InterpreterStatisticsInfo {
return currentSdkManager.createStatisticsInfo(PythonInterpreterCreationTargets.LOCAL_MACHINE)

View File

@@ -128,13 +128,13 @@ class PythonAddNewEnvironmentPanel(val projectPath: ObservableProperty<String>,
}
}
fun getSdk(): Sdk? {
fun getSdk(): Sdk {
model.navigator.saveLastState()
return when (selectedMode.get()) {
PROJECT_VENV -> model.setupVirtualenv(Path.of(projectPath.get(), ".venv"), // todo just keep venv path, all the rest is in the model
projectPath.get(),
//pythonBaseVersion.get()!!)
model.state.baseInterpreter.get()!!)
model.state.baseInterpreter.get()!!).getOrThrow()
BASE_CONDA -> model.selectCondaEnvironment(model.state.baseCondaEnv.get()!!.envIdentity)
CUSTOM -> custom.getSdk()
}

View File

@@ -184,9 +184,9 @@ class PythonNewVirtualenvCreator(model: PythonMutableTargetAddInterpreterModel)
return currentName.removeSuffix(digitSuffix) + newSuffix
}
override fun getOrCreateSdk(): Sdk? {
override fun getOrCreateSdk(): Sdk {
// todo remove project path, or move to controller
return model.setupVirtualenv((Path.of(model.state.venvPath.get())), model.projectPath.get(), model.state.baseInterpreter.get()!!)
return model.setupVirtualenv((Path.of(model.state.venvPath.get())), model.projectPath.get(), model.state.baseInterpreter.get()!!).getOrThrow()
}
companion object {

View File

@@ -46,7 +46,7 @@ abstract class PythonAddEnvironment(open val model: PythonAddInterpreterModel) {
abstract fun buildOptions(panel: Panel, validationRequestor: DialogValidationRequestor)
open fun onShown() {}
abstract fun getOrCreateSdk(): Sdk?
abstract fun getOrCreateSdk(): Sdk
abstract fun createStatisticsInfo(target: PythonInterpreterCreationTargets): InterpreterStatisticsInfo
}

View File

@@ -31,7 +31,7 @@ internal fun PythonAddInterpreterModel.createCondaCommand(): PyCondaCommand =
targetConfig = targetEnvironmentConfiguration)
@RequiresEdt
internal fun PythonAddInterpreterModel.createCondaEnvironment(request: NewCondaEnvRequest): Sdk? {
internal fun PythonAddInterpreterModel.createCondaEnvironment(request: NewCondaEnvRequest): Sdk {
val project = ProjectManager.getInstance().defaultProject
val existingSdks = this@createCondaEnvironment.existingSdks
val sdk = runWithModalProgressBlocking(ModalTaskOwner.guess(),
@@ -41,8 +41,8 @@ internal fun PythonAddInterpreterModel.createCondaEnvironment(request: NewCondaE
.createCondaSdkAlongWithNewEnv(request,
Dispatchers.EDT,
existingSdks,
project).getOrNull()
} ?: return null
project).getOrThrow()
}
(sdk.sdkType as PythonSdkType).setupSdkPaths(sdk)
SdkConfigurationUtil.addSdk(sdk)

View File

@@ -3,7 +3,6 @@ package com.jetbrains.python.sdk.add.v2
import com.intellij.execution.ExecutionException
import com.intellij.openapi.module.ModuleUtil
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.projectRoots.Sdk
@@ -13,9 +12,10 @@ import com.intellij.platform.ide.progress.ModalTaskOwner
import com.intellij.platform.ide.progress.TaskCancellation
import com.intellij.platform.ide.progress.runWithModalProgressBlocking
import com.jetbrains.python.PyBundle
import com.jetbrains.python.sdk.*
import com.jetbrains.python.sdk.PythonSdkType
import com.jetbrains.python.sdk.PythonSdkUtil
import com.jetbrains.python.sdk.add.target.conda.createCondaSdkFromExistingEnv
import com.jetbrains.python.sdk.configuration.createVirtualEnvSynchronously
import com.jetbrains.python.sdk.excludeInnerVirtualEnv
import com.jetbrains.python.sdk.flavors.conda.PyCondaCommand
import com.jetbrains.python.sdk.flavors.conda.PyCondaEnvIdentity
import com.jetbrains.python.sdk.suggestAssociatedSdkName
@@ -23,7 +23,7 @@ import java.nio.file.Path
// todo should it be overriden for targets?
internal fun PythonMutableTargetAddInterpreterModel.setupVirtualenv(venvPath: Path, projectPath: String, baseSdk: PythonSelectableInterpreter): Sdk? {
internal fun PythonMutableTargetAddInterpreterModel.setupVirtualenv(venvPath: Path, projectPath: String, baseSdk: PythonSelectableInterpreter): Result<Sdk> {
val venvPathOnTarget = venvPath.convertToPathOnTarget(targetEnvironmentConfiguration)
@@ -35,7 +35,7 @@ internal fun PythonMutableTargetAddInterpreterModel.setupVirtualenv(venvPath: Pa
}
createVirtualenv(baseSdkPath!!, // todo handle null
createVirtualenv(baseSdkPath!!,
venvPathOnTarget,
projectPath,
targetEnvironmentConfiguration,
@@ -45,40 +45,32 @@ internal fun PythonMutableTargetAddInterpreterModel.setupVirtualenv(venvPath: Pa
inheritSitePackages = state.inheritSitePackages.get(),
makeShared = state.makeAvailable.get())
if (targetEnvironmentConfiguration == null) {
val venvPython = PythonSdkUtil.getPythonExecutable(venvPathOnTarget)
val homeFile = try {
StandardFileSystems.local().refreshAndFindFileByPath(venvPython!!)!!
}
catch (e: ExecutionException) {
showSdkExecutionException(null, e, PyBundle.message("python.sdk.failed.to.create.interpreter.title"))
return null
}
val suggestedName = /*suggestedSdkName ?:*/ suggestAssociatedSdkName(homeFile.path, projectPath)
val newSdk = SdkConfigurationUtil.setupSdk(existingSdks.toTypedArray(), homeFile,
PythonSdkType.getInstance(),
false, null, suggestedName)
SdkConfigurationUtil.addSdk(newSdk!!)
// todo check exclude
ProjectManager.getInstance().openProjects
.firstNotNullOfOrNull { ModuleUtil.findModuleForFile(homeFile, it) }
?.excludeInnerVirtualEnv(newSdk)
return newSdk
if (targetEnvironmentConfiguration != null) error("Remote targets aren't supported")
val venvPython = PythonSdkUtil.getPythonExecutable(venvPathOnTarget)
val homeFile = try {
StandardFileSystems.local().refreshAndFindFileByPath(venvPython!!)!!
}
catch (e: ExecutionException) {
return Result.failure(e)
}
// todo find venv path on target
return null
val suggestedName = /*suggestedSdkName ?:*/ suggestAssociatedSdkName(homeFile.path, projectPath)
val newSdk = SdkConfigurationUtil.setupSdk(existingSdks.toTypedArray(), homeFile,
PythonSdkType.getInstance(),
false, null, suggestedName)
SdkConfigurationUtil.addSdk(newSdk!!)
// todo check exclude
ProjectManager.getInstance().openProjects
.firstNotNullOfOrNull { ModuleUtil.findModuleForFile(homeFile, it) }
?.excludeInnerVirtualEnv(newSdk)
return Result.success(newSdk)
}
// todo rewrite this
internal fun PythonAddInterpreterModel.selectCondaEnvironment(identity: PyCondaEnvIdentity): Sdk {
val existingSdk = ProjectJdkTable.getInstance().findJdk(identity.userReadableName)
@@ -89,8 +81,8 @@ internal fun PythonAddInterpreterModel.selectCondaEnvironment(identity: PyCondaE
TaskCancellation.nonCancellable()) {
//PyCondaCommand(condaExecutableOnTarget, targetConfig = targetEnvironmentConfiguration)
PyCondaCommand(state.condaExecutable.get(), targetConfig = targetEnvironmentConfiguration).createCondaSdkFromExistingEnv(identity,
this@selectCondaEnvironment.existingSdks,
ProjectManager.getInstance().defaultProject)
this@selectCondaEnvironment.existingSdks,
ProjectManager.getInstance().defaultProject)
}
(sdk.sdkType as PythonSdkType).setupSdkPaths(sdk)
@@ -103,5 +95,6 @@ internal fun PythonAddInterpreterModel.installPythonIfNeeded(interpreter: Python
// todo use target config
return if (interpreter is InstallableSelectableInterpreter) {
installBaseSdk(interpreter.sdk, existingSdks)?.homePath ?: return null
} else interpreter.homePath
}
else interpreter.homePath
}