dbrattli / dbrattli/Expression
Any data type / structure for error accumulation?
- Dominant language
- Python
- Stars
- 775
- Forks
- 38
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 10
Description
Is there any structure with which to accumulate errors from multiple `Result`s? Say I have a dataclass with three fields and each needs validation, and I'd like to take input and either emit a `Result.Ok` with an instance of my dataclass, or a `Result.Error` with a _collection_ of the (one or more) errors from the (failed) validation of the input.
Something from Scala might looks like...
```scala
case class Mine(a: A, b: B, c: C)
def safeA: String => Either[String, A]
def safeB: String => Either[String, B]
def safeC: String => Either[String, C]
def safeBuild(s1: String, s2: String, s2: String): cats.data.ValidatedNel[String, Mine] =
val aNel = safeA(s1).toValidatedNel
val bNel = safeB(s2).toValidatedNel
val cNel = safeC(s3).toValidatedNel
(aNel, bNel, cNel).mapN(Mine.apply)
```
So I guess I'm looking for something like...
```python
from dataclasses import dataclass
@dataclass
class Mine:
a: int
b: float
c: str
def maybeA(s: str) -> Result[int, str]: ...
def maybeB(s: str) -> Result[float, str]: ...
def maybeC(s: str) -> Result[str, str]: ...
def maybeMine(s1: str, s2: str, s3: str) -> Result[Mine, list[str]]:
??? # how to combine the application of each component's safe parser?
```
Contributor guide
Research direction
Start by reviewing the existing Result type and how it combines or transforms multiple results; the issue provides no file or test entry point. A complete change would need a decided API for accumulating validation errors across several Result values, plus tests showing both collected errors and successful dataclass construction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100