Skip to content

Simple

SimpleParseError

Bases: ValueError, Generic[T]

Represents simple parse errors.

Source code in src/wraps/parse/simple.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class SimpleParseError(ValueError, Generic[T]):
    """Represents simple parse errors."""

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

        super().__init__(simple_parse_failed(string, get_name(type)))  # 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

string: str property

The string that could not be parsed.

type: Type[T] property

The type parsing into which failed.

SimpleFromString

Bases: Protocol

Represents types that can be parsed from strings.

Source code in src/wraps/parse/simple.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@runtime_checkable
class SimpleFromString(Protocol):
    """Represents types that can be parsed from strings."""

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

        Arguments:
            string: The string to parse.

        Returns:
            The parse result, [`Some[Self]`][wraps.option.Some] if parsing was successful,
                and [`Null`][wraps.option.Null] otherwise.
        """
        ...

    @classmethod
    def parse(cls, string: str) -> Self:
        """Calls [`from_string`][wraps.parse.simple.SimpleFromString.from_string] and raises
        [`SimpleParseError[Self]`][wraps.parse.simple.SimpleParseError] if parsing fails.

        Arguments:
            string: The string to parse.

        Returns:
            The parsed value.

        Raises:
            SimpleParseError[Self]: In case parsing fails.
        """
        return cls.from_string(string).or_raise(SimpleParseError(string, cls))

from_string(string: str) -> Option[Self] 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
Option[Self]

The parse result, Some[Self] if parsing was successful, and Null otherwise.

Source code in src/wraps/parse/simple.py
43
44
45
46
47
48
49
50
51
52
53
54
55
@classmethod
@required
def from_string(cls, string: str) -> Option[Self]:
    """Parses the given string to return some value of type `Self`.

    Arguments:
        string: The string to parse.

    Returns:
        The parse result, [`Some[Self]`][wraps.option.Some] if parsing was successful,
            and [`Null`][wraps.option.Null] otherwise.
    """
    ...

parse(string: str) -> Self classmethod

Calls from_string and raises SimpleParseError[Self] 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
SimpleParseError[Self]

In case parsing fails.

Source code in src/wraps/parse/simple.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@classmethod
def parse(cls, string: str) -> Self:
    """Calls [`from_string`][wraps.parse.simple.SimpleFromString.from_string] and raises
    [`SimpleParseError[Self]`][wraps.parse.simple.SimpleParseError] if parsing fails.

    Arguments:
        string: The string to parse.

    Returns:
        The parsed value.

    Raises:
        SimpleParseError[Self]: In case parsing fails.
    """
    return cls.from_string(string).or_raise(SimpleParseError(string, cls))