Skip to content

Normal

ParseError

Bases: ValueError, Generic[T, E]

Represents parse errors.

Source code in src/wraps/parse/normal.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class ParseError(ValueError, Generic[T, E]):
    """Represents parse errors."""

    def __init__(self, string: str, type: Type[T], error: E) -> None:
        self._string = string
        self._type = type
        self._error = error

        super().__init__(parse_failed(string, get_name(type), error))  # type: ignore[arg-type]

    @property
    def string(self) -> str:
        """The string that could not be parsed."""
        return self._string

    @property
    def type(self) -> Type[T]:
        """The type parsing into which failed."""
        return self._type

    @property
    def error(self) -> E:
        """The error returned by the
        [`from_string`][wraps.parse.normal.FromString.from_string] function.
        """
        return self._error

string: str property

The string that could not be parsed.

type: Type[T] property

The type parsing into which failed.

error: E property

The error returned by the from_string function.

FromString

Bases: Protocol[E]

Represents types that can be parsed from strings.

Source code in src/wraps/parse/normal.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@runtime_checkable
class FromString(Protocol[E]):
    """Represents types that can be parsed from strings."""

    @classmethod
    @required
    def from_string(cls, string: str) -> Result[Self, E]:
        """Parses the given string to return some value of type `Self`.

        Arguments:
            string: The string to parse.

        Returns:
            The parse result, [`Ok[Self]`][wraps.result.Ok] if parsing was successful,
                and [`Error[E]`][wraps.result.Error] otherwise.
        """
        ...

    @classmethod
    def parse(cls, string: str) -> Self:
        """Calls [`from_string`][wraps.parse.normal.FromString.from_string] and raises
        [`ParseError[Self, E]`][wraps.parse.normal.ParseError] if parsing fails.

        Arguments:
            string: The string to parse.

        Returns:
            The parsed value.

        Raises:
            ParseError[Self, E]: In case parsing fails.
        """
        result = cls.from_string(string)

        if result.is_ok():
            return result.unwrap()

        raise ParseError(string, cls, result.unwrap_err())

from_string(string: str) -> Result[Self, E] classmethod

Parses the given string to return some value of type Self.

Parameters:

Name Type Description Default
string str

The string to parse.

required

Returns:

Type Description
Result[Self, E]

The parse result, Ok[Self] if parsing was successful, and [Error[E]][wraps.result.Error] otherwise.

Source code in src/wraps/parse/normal.py
52
53
54
55
56
57
58
59
60
61
62
63
64
@classmethod
@required
def from_string(cls, string: str) -> Result[Self, E]:
    """Parses the given string to return some value of type `Self`.

    Arguments:
        string: The string to parse.

    Returns:
        The parse result, [`Ok[Self]`][wraps.result.Ok] if parsing was successful,
            and [`Error[E]`][wraps.result.Error] otherwise.
    """
    ...

parse(string: str) -> Self classmethod

Calls from_string and raises ParseError[Self, E] if parsing fails.

Parameters:

Name Type Description Default
string str

The string to parse.

required

Returns:

Type Description
Self

The parsed value.

Raises:

Type Description
ParseError[Self, E]

In case parsing fails.

Source code in src/wraps/parse/normal.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@classmethod
def parse(cls, string: str) -> Self:
    """Calls [`from_string`][wraps.parse.normal.FromString.from_string] and raises
    [`ParseError[Self, E]`][wraps.parse.normal.ParseError] if parsing fails.

    Arguments:
        string: The string to parse.

    Returns:
        The parsed value.

    Raises:
        ParseError[Self, E]: In case parsing fails.
    """
    result = cls.from_string(string)

    if result.is_ok():
        return result.unwrap()

    raise ParseError(string, cls, result.unwrap_err())