Skip to content

SecretMonad

secret_type.SecretMonad

Provides a monad-like interface for Secret objects.

These methods are all also available on the Secret class.

Source code in secret_type/monad.py
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
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
86
87
88
89
90
class SecretMonad:
    """Provides a monad-like interface for [`Secret`][secret_type.Secret] objects.

    These methods are all also available on the [`Secret`][secret_type.Secret] class.
    """

    @overload
    @classmethod
    def wrap(cls, o: "Secret[T]") -> "Secret[T]":
        ...

    @overload
    @classmethod
    def wrap(cls, o: T) -> "Secret[T]":
        ...

    @classmethod
    def wrap(cls, o: Union[T, "Secret[T]"]) -> "Secret[T]":
        """Wraps a value in the appropriate [`Secret`][secret_type.Secret] container.

        If the value is already a [`Secret`][secret_type.Secret], it is returned as-is.

        Attributes:
            o (Union[str, bytes, int, float, bool]): The value to wrap.

        Examples: Example:
            ```python
            secret = SecretMonad.wrap(42)
            ```

        Raises:
            TypeError: If `o` is not a primitive value.
        """
        from secret_type.containers.bool import SecretBool
        from secret_type.containers.number import SecretNumber
        from secret_type.containers.secret import Secret
        from secret_type.containers.sequence import SecretStr

        if isinstance(o, Secret):
            return o
        elif isinstance(o, (str, bytes)):
            return SecretStr(o)
        elif isinstance(o, bool):
            return SecretBool(o)
        elif isinstance(o, Rational):
            return SecretNumber(o)
        elif isinstance(o, Number):
            return Secret(o)
        else:
            raise TypeError("Cannot wrap type '{}'".format(type(o).__name__))

    @overload
    @classmethod
    def unwrap(cls, o: "Secret[T]") -> T:
        ...

    @overload
    @classmethod
    def unwrap(cls, o: R) -> R:
        ...

    @classmethod
    def unwrap(cls, o: Union["Secret[T]", R]) -> Union[T, R]:
        """Unwraps a [`Secret`][secret_type.Secret] into it's underlying value.

        If the value is not a [`Secret`][secret_type.Secret], it is returned as-is.

        Attributes:
            o (Secret[T]): The value to unwrap.

        Examples: Example:
            ```python
            assert 42 == SecretMonad.unwrap(secret)
            ```
        """
        from secret_type.containers.secret import Secret

        if isinstance(o, Secret):
            return o._dangerous_extract()
        else:
            return o

wrap(o: Union[T, Secret[T]]) -> Secret[T] classmethod

Wraps a value in the appropriate Secret container.

If the value is already a Secret, it is returned as-is.

ATTRIBUTE DESCRIPTION
o

The value to wrap.

TYPE: Union[str, bytes, int, float, bool]

Example:

secret = SecretMonad.wrap(42)
RAISES DESCRIPTION
TypeError

If o is not a primitive value.

Source code in secret_type/monad.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@classmethod
def wrap(cls, o: Union[T, "Secret[T]"]) -> "Secret[T]":
    """Wraps a value in the appropriate [`Secret`][secret_type.Secret] container.

    If the value is already a [`Secret`][secret_type.Secret], it is returned as-is.

    Attributes:
        o (Union[str, bytes, int, float, bool]): The value to wrap.

    Examples: Example:
        ```python
        secret = SecretMonad.wrap(42)
        ```

    Raises:
        TypeError: If `o` is not a primitive value.
    """
    from secret_type.containers.bool import SecretBool
    from secret_type.containers.number import SecretNumber
    from secret_type.containers.secret import Secret
    from secret_type.containers.sequence import SecretStr

    if isinstance(o, Secret):
        return o
    elif isinstance(o, (str, bytes)):
        return SecretStr(o)
    elif isinstance(o, bool):
        return SecretBool(o)
    elif isinstance(o, Rational):
        return SecretNumber(o)
    elif isinstance(o, Number):
        return Secret(o)
    else:
        raise TypeError("Cannot wrap type '{}'".format(type(o).__name__))

unwrap(o: Union[Secret[T], R]) -> Union[T, R] classmethod

Unwraps a Secret into it's underlying value.

If the value is not a Secret, it is returned as-is.

ATTRIBUTE DESCRIPTION
o

The value to unwrap.

TYPE: Secret[T]

Example:

assert 42 == SecretMonad.unwrap(secret)
Source code in secret_type/monad.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
@classmethod
def unwrap(cls, o: Union["Secret[T]", R]) -> Union[T, R]:
    """Unwraps a [`Secret`][secret_type.Secret] into it's underlying value.

    If the value is not a [`Secret`][secret_type.Secret], it is returned as-is.

    Attributes:
        o (Secret[T]): The value to unwrap.

    Examples: Example:
        ```python
        assert 42 == SecretMonad.unwrap(secret)
        ```
    """
    from secret_type.containers.secret import Secret

    if isinstance(o, Secret):
        return o._dangerous_extract()
    else:
        return o