Skip to content

converters

Commonly useful converters.

default_if_none(default=NOTHING, factory=None)

default_if_none(default: _T) -> _ConverterType
default_if_none(*, factory: Callable[[], _T]) -> _ConverterType

A converter that allows to replace None values by default or the result of factory.

:param default: Value to be used if None is passed. Passing an instance of attr.Factory is supported, however the takes_self option is not. :param callable factory: A callable that takes no parameters whose result is used if None is passed.

:raises TypeError: If neither default or factory is passed. :raises TypeError: If both default and factory are passed. :raises ValueError: If an instance of attr.Factory is passed with takes_self=True.

.. versionadded:: 18.2.0

Source code in client/ayon_fusion/vendor/attr/converters.py
 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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def default_if_none(default=NOTHING, factory=None):
    """
    A converter that allows to replace ``None`` values by *default* or the
    result of *factory*.

    :param default: Value to be used if ``None`` is passed. Passing an instance
       of `attr.Factory` is supported, however the ``takes_self`` option
       is *not*.
    :param callable factory: A callable that takes no parameters whose result
       is used if ``None`` is passed.

    :raises TypeError: If **neither** *default* or *factory* is passed.
    :raises TypeError: If **both** *default* and *factory* are passed.
    :raises ValueError: If an instance of `attr.Factory` is passed with
       ``takes_self=True``.

    .. versionadded:: 18.2.0
    """
    if default is NOTHING and factory is None:
        raise TypeError("Must pass either `default` or `factory`.")

    if default is not NOTHING and factory is not None:
        raise TypeError(
            "Must pass either `default` or `factory` but not both."
        )

    if factory is not None:
        default = Factory(factory)

    if isinstance(default, Factory):
        if default.takes_self:
            raise ValueError(
                "`takes_self` is not supported by default_if_none."
            )

        def default_if_none_converter(val):
            if val is not None:
                return val

            return default.factory()

    else:

        def default_if_none_converter(val):
            if val is not None:
                return val

            return default

    return default_if_none_converter

optional(converter)

A converter that allows an attribute to be optional. An optional attribute is one which can be set to None.

Type annotations will be inferred from the wrapped converter's, if it has any.

:param callable converter: the converter that is used for non-None values.

.. versionadded:: 17.1.0

Source code in client/ayon_fusion/vendor/attr/converters.py
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
def optional(converter):
    """
    A converter that allows an attribute to be optional. An optional attribute
    is one which can be set to ``None``.

    Type annotations will be inferred from the wrapped converter's, if it
    has any.

    :param callable converter: the converter that is used for non-``None``
        values.

    .. versionadded:: 17.1.0
    """

    def optional_converter(val):
        if val is None:
            return None
        return converter(val)

    if not PY2:
        sig = None
        try:
            sig = inspect.signature(converter)
        except (ValueError, TypeError):  # inspect failed
            pass
        if sig:
            params = list(sig.parameters.values())
            if params and params[0].annotation is not inspect.Parameter.empty:
                optional_converter.__annotations__["val"] = typing.Optional[
                    params[0].annotation
                ]
            if sig.return_annotation is not inspect.Signature.empty:
                optional_converter.__annotations__["return"] = typing.Optional[
                    sig.return_annotation
                ]

    return optional_converter

pipe(*converters)

A converter that composes multiple converters into one.

When called on a value, it runs all wrapped converters, returning the last value.

Type annotations will be inferred from the wrapped converters', if they have any.

:param callables converters: Arbitrary number of converters.

.. versionadded:: 20.1.0

Source code in client/ayon_fusion/vendor/attr/_make.py
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
def pipe(*converters):
    """
    A converter that composes multiple converters into one.

    When called on a value, it runs all wrapped converters, returning the
    *last* value.

    Type annotations will be inferred from the wrapped converters', if
    they have any.

    :param callables converters: Arbitrary number of converters.

    .. versionadded:: 20.1.0
    """

    def pipe_converter(val):
        for converter in converters:
            val = converter(val)

        return val

    if not PY2:
        if not converters:
            # If the converter list is empty, pipe_converter is the identity.
            A = typing.TypeVar("A")
            pipe_converter.__annotations__ = {"val": A, "return": A}
        else:
            # Get parameter type.
            sig = None
            try:
                sig = inspect.signature(converters[0])
            except (ValueError, TypeError):  # inspect failed
                pass
            if sig:
                params = list(sig.parameters.values())
                if (
                    params
                    and params[0].annotation is not inspect.Parameter.empty
                ):
                    pipe_converter.__annotations__["val"] = params[
                        0
                    ].annotation
            # Get return type.
            sig = None
            try:
                sig = inspect.signature(converters[-1])
            except (ValueError, TypeError):  # inspect failed
                pass
            if sig and sig.return_annotation is not inspect.Signature().empty:
                pipe_converter.__annotations__[
                    "return"
                ] = sig.return_annotation

    return pipe_converter