File: exceptions.py

package info (click to toggle)
python-returns 0.26.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (47 lines) | stat: -rw-r--r-- 1,386 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
46
47
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from returns.interfaces.unwrappable import Unwrappable  # noqa: WPS433


class UnwrapFailedError(Exception):
    """Raised when a container can not be unwrapped into a meaningful value."""

    __slots__ = ('halted_container',)

    def __init__(self, container: Unwrappable) -> None:
        """
        Saves halted container in the inner state.

        So, this container can later be unpacked from this exception
        and used as a regular value.
        """
        super().__init__()
        self.halted_container = container

    def __reduce__(self):  # noqa: WPS603
        """Custom reduce method for pickle protocol.

        This helps properly reconstruct the exception during unpickling.
        """
        return (
            self.__class__,  # callable
            (self.halted_container,),  # args to callable
        )


class ImmutableStateError(AttributeError):
    """
    Raised when a container is forced to be mutated.

    It is a sublclass of ``AttributeError`` for two reasons:

    1. It seems kinda reasonable to expect ``AttributeError``
       on attribute modification
    2. It is used inside ``typing.py`` this way,
       we do have several typing features that requires that behaviour

    See: https://github.com/dry-python/returns/issues/394
    """