Python: refactor PyError hierarchy, migrate to PyResult.

DO:
For upper-level (public) API use `PyResult`.
(Optionally) for low-level APIs inside your modules use python `Result<S, E>`.
Represent errors as `PyError` whenever possible.
Report `PyError` to `ErrorSink` at the top of your code.

DON'T:
Use `kotlin.Result`
Use `PyExecutionException`
Use any exception to represent user errors.

GitOrigin-RevId: 4ecf69e1fae8be9192cd33b90e0147c725a98964
This commit is contained in:
Ilya.Kazakevich
2025-04-28 17:52:06 +02:00
committed by intellij-monorepo-bot
parent d366245171
commit 803e270d45
74 changed files with 693 additions and 713 deletions

View File

@@ -9,10 +9,10 @@ import com.jetbrains.python.mapResult
import org.apache.tuweni.toml.TomlArray
import org.apache.tuweni.toml.TomlTable
import org.jetbrains.annotations.ApiStatus.Internal
import org.toml.lang.psi.TomlKeyValue as PsiTomlKeyValue
import org.toml.lang.psi.TomlTable as PsiTomlTable
import org.toml.lang.psi.TomlLiteral as PsiTomlLiteral
import kotlin.reflect.KClass
import org.toml.lang.psi.TomlKeyValue as PsiTomlKeyValue
import org.toml.lang.psi.TomlLiteral as PsiTomlLiteral
import org.toml.lang.psi.TomlTable as PsiTomlTable
/**
* The error union used by [TomlTable.safeGet], [TomlTable.safeGetRequired] and [TomlTable.safeGetArr].

View File

@@ -1,22 +1,21 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.python.pyproject
import com.jetbrains.python.Result.Companion.failure
import com.jetbrains.python.Result.Companion.success
import com.jetbrains.python.Result
import org.apache.tuweni.toml.Toml
import org.apache.tuweni.toml.TomlParseError
import org.apache.tuweni.toml.TomlTable
import java.io.InputStream
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.toNioPathOrNull
import com.jetbrains.python.Result
import com.jetbrains.python.Result.Companion.success
import com.jetbrains.python.sdk.basePath
import com.jetbrains.python.sdk.findAmongRoots
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
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
/**
@@ -112,7 +111,7 @@ data class PyProjectToml(
val toml = Toml.parse(inputStream)
if (toml.hasErrors()) {
return failure(toml.errors())
return Result.failure(toml.errors())
}
val projectTable = toml.safeGet<TomlTable>("project").getOrIssue(issues)

View File

@@ -1,12 +1,9 @@
package com.intellij.python.pyproject
import com.intellij.python.pyproject.PyProjectIssue.InvalidContact
import com.intellij.python.pyproject.PyProjectIssue.MissingName
import com.intellij.python.pyproject.PyProjectIssue.MissingVersion
import com.intellij.python.pyproject.PyProjectIssue.SafeGetError
import com.intellij.python.pyproject.PyProjectIssue.*
import com.intellij.python.pyproject.TomlTableSafeGetError.RequiredValueMissing
import com.intellij.python.pyproject.TomlTableSafeGetError.UnexpectedType
import com.jetbrains.python.Result.Failure
import com.jetbrains.python.Result
import com.jetbrains.python.getOrThrow
import com.jetbrains.python.isFailure
import org.apache.tuweni.toml.TomlArray
@@ -29,7 +26,7 @@ class PyProjectTomlTest {
// THEN
assert(result.isFailure)
assert((result as Failure).error.isNotEmpty())
assert((result as Result.Failure).error.isNotEmpty())
}
@Test