Poetry module cleanup

GitOrigin-RevId: ea243ddcba46e17cf628f033e832575850324a96
This commit is contained in:
Ilya.Kazakevich
2025-09-23 19:41:58 +02:00
committed by intellij-monorepo-bot
parent 917ca34985
commit ae1c23b793
7 changed files with 40 additions and 20 deletions

View File

@@ -3,36 +3,52 @@ package com.intellij.pycharm.community.ide.impl.configuration
import com.intellij.codeInspection.util.IntentionName
import com.intellij.openapi.application.EDT
import com.intellij.openapi.diagnostic.fileLogger
import com.intellij.openapi.module.Module
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.readText
import com.intellij.pycharm.community.ide.impl.PyCharmCommunityCustomizationBundle
import com.intellij.python.pyproject.PyProjectToml
import com.jetbrains.python.errorProcessing.MessageError
import com.jetbrains.python.errorProcessing.PyResult
import com.jetbrains.python.getOrNull
import com.jetbrains.python.getOrLogException
import com.jetbrains.python.onSuccess
import com.jetbrains.python.projectModel.enablePyProjectToml
import com.jetbrains.python.projectModel.uv.UvProjectModelService
import com.jetbrains.python.sdk.*
import com.jetbrains.python.sdk.PythonSdkUtil
import com.jetbrains.python.sdk.basePath
import com.jetbrains.python.sdk.configuration.PyProjectSdkConfigurationExtension
import com.jetbrains.python.sdk.persist
import com.jetbrains.python.sdk.setAssociationToModule
import com.jetbrains.python.sdk.uv.impl.getUvExecutable
import com.jetbrains.python.sdk.uv.setupNewUvSdkAndEnv
import com.jetbrains.python.venvReader.tryResolvePath
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.annotations.ApiStatus
import java.io.IOException
import java.nio.file.Path
private val logger = fileLogger()
@ApiStatus.Internal
class PyUvSdkConfiguration : PyProjectSdkConfigurationExtension {
override suspend fun getIntention(module: Module): @IntentionName String? {
val tomlFile = PyProjectToml.findFile(module) ?: return null
getUvExecutable() ?: return null
val tomlContentResult = PyProjectToml.parse(tomlFile.inputStream)
val tomlContent = tomlContentResult.getOrNull() ?: return null
val project = tomlContent.project ?: return null
val tomlFileContent = withContext(Dispatchers.IO) {
try {
tomlFile.readText()
}
catch (e: IOException) {
logger.debug("Can't read ${tomlFile}", e)
null
}
} ?: return null
val tomlContentResult = withContext(Dispatchers.Default) { PyProjectToml.parse(tomlFileContent) }
val tomlContent = tomlContentResult.getOrLogException(logger) ?: return null
val project = tomlContent.project ?: return null
return PyCharmCommunityCustomizationBundle.message("sdk.set.up.uv.environment", project.name ?: tomlFile.inputStream)

View File

@@ -85,7 +85,7 @@ inline fun <reified T> TomlTable.safeGet(key: String): Result<T?, TomlTableSafeG
* ```
*/
@Internal
inline fun <reified T> TomlTable.safeGetRequired(key: String): Result<T, TomlTableSafeGetError> =
internal inline fun <reified T> TomlTable.safeGetRequired(key: String): Result<T, TomlTableSafeGetError> =
safeGet<T>(key).mapResult {
if (it == null) {
failure(TomlTableSafeGetError.RequiredValueMissing(key))

View File

@@ -12,7 +12,6 @@ import org.apache.tuweni.toml.Toml
import org.apache.tuweni.toml.TomlParseError
import org.apache.tuweni.toml.TomlTable
import org.jetbrains.annotations.ApiStatus.Internal
import java.io.InputStream
import java.nio.file.Path
import kotlin.io.path.isRegularFile
@@ -113,9 +112,9 @@ data class PyProjectToml(
* val hatch = pyProject.getTool(HatchPyProject)
* ```
*/
fun parse(inputStream: InputStream): Result<PyProjectToml, List<TomlParseError>> {
fun parse(tomlFileContent: String): Result<PyProjectToml, List<TomlParseError>> {
val issues = mutableListOf<PyProjectIssue>()
val toml = Toml.parse(inputStream)
val toml = Toml.parse(tomlFileContent)
if (toml.hasErrors()) {
return Result.failure(toml.errors())

View File

@@ -2,5 +2,8 @@ package com.intellij.python.pyproject.psi
import com.intellij.psi.PsiFile
import com.intellij.python.pyproject.PY_PROJECT_TOML
import org.jetbrains.annotations.ApiStatus
@ApiStatus.Internal
fun PsiFile.isPyProjectToml(): Boolean = this.name == PY_PROJECT_TOML

View File

@@ -1,5 +1,6 @@
package com.intellij.python.pyproject
package com.intellij.python.junit5Tests.unit.pyproject
import com.intellij.python.pyproject.*
import com.intellij.python.pyproject.PyProjectIssue.*
import com.intellij.python.pyproject.TomlTableSafeGetError.RequiredValueMissing
import com.intellij.python.pyproject.TomlTableSafeGetError.UnexpectedType
@@ -22,7 +23,7 @@ class PyProjectTomlTest {
val configContents = "[proj"
// WHEN
val result = PyProjectToml.parse(configContents.byteInputStream())
val result = PyProjectToml.Companion.parse(configContents)
// THEN
assert(result.isFailure)
@@ -44,7 +45,7 @@ class PyProjectTomlTest {
bar="test bar"
baz="test baz"
""".trimIndent()
val pyproject = PyProjectToml.parse(configContents.byteInputStream()).orThrow()
val pyproject = PyProjectToml.Companion.parse(configContents).orThrow()
// WHEN
val testTool = pyproject.getTool(TestPyProject)
@@ -68,7 +69,7 @@ class PyProjectTomlTest {
""".trimIndent()
// WHEN
val pyproject = PyProjectToml.parse(configContents.byteInputStream()).orThrow()
val pyproject = PyProjectToml.Companion.parse(configContents).orThrow()
val testTool = pyproject.getTool(TestPyProject)
// THEN
@@ -94,7 +95,7 @@ class PyProjectTomlTest {
""".trimIndent()
// WHEN
val pyproject = PyProjectToml.parse(configContents.byteInputStream()).orThrow()
val pyproject = PyProjectToml.Companion.parse(configContents).orThrow()
val testTool = pyproject.getTool(TestPyProject)
// THEN
@@ -113,7 +114,7 @@ class PyProjectTomlTest {
name="Some project"
version="1.2.3"
""".trimIndent()
val pyproject = PyProjectToml.parse(configContents.byteInputStream()).orThrow()
val pyproject = PyProjectToml.Companion.parse(configContents).orThrow()
// WHEN
val testTool = pyproject.getTool(TestPyProject)
@@ -126,7 +127,7 @@ class PyProjectTomlTest {
@ParameterizedTest(name = "{0}")
@MethodSource("parseTestCases")
fun parseTests(name: String, pyprojectToml: String, expectedProjectTable: PyProjectTable?, expectedIssues: List<PyProjectIssue>) {
val result = PyProjectToml.parse(pyprojectToml.byteInputStream())
val result = PyProjectToml.Companion.parse(pyprojectToml)
val unwrapped = result.getOrThrow()
assertEquals(expectedProjectTable, unwrapped.project)

View File

@@ -5,6 +5,7 @@ import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.ex.ActionUtil
import com.intellij.openapi.ui.popup.ListSeparator
import com.intellij.openapi.vfs.readText
import com.intellij.python.community.services.systemPython.SystemPythonService
import com.intellij.python.sdk.ui.evolution.sdk.EvoModuleSdk
import com.intellij.python.sdk.ui.evolution.sdk.resolvePythonExecutable
@@ -46,7 +47,7 @@ private class PoetrySelectSdkProvider() : EvoSelectSdkProvider {
val (projectName, requiresPython) = withContext(Dispatchers.IO) {
val toml = PyProjectToml.parse(pyProjectTomlFile.inputStream).getOrNull()
val toml = PyProjectToml.parse(pyProjectTomlFile.readText()).getOrNull()
(toml?.project?.name) to (toml?.project?.requiresPython)
}
val poetryVirtualenvsPath = runPoetry(pyProjectTomlFile.parent.toNioPath(), "config", "virtualenvs.path")

View File

@@ -46,7 +46,7 @@ import kotlinx.coroutines.withContext
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.io.path.exists
import kotlin.io.path.inputStream
import kotlin.io.path.readText
internal class EnvironmentCreatorUv(
@@ -132,7 +132,7 @@ internal class EnvironmentCreatorUv(
val pythonVersions = withContext(Dispatchers.IO) {
val versionRequest = if (pyProjectTomlPath.exists()) {
PyProjectToml.parse(pyProjectTomlPath.inputStream()).getOrNull()?.project?.requiresPython
PyProjectToml.parse(pyProjectTomlPath.readText()).getOrNull()?.project?.requiresPython
} else {
null
}