mercurial/thirdparty/attr/converters.py
author Martin von Zweigbergk <martinvonz@google.com>
Wed, 18 Oct 2017 22:07:53 -0700
changeset 34895 7b857c5947ec
parent 34397 765eb17a7eb8
child 49643 e1c586b9a43c
permissions -rw-r--r--
registrar: move "constant" possiblecmdtypes to class level While at it, switch to set literal syntax. Differential Revision: https://phab.mercurial-scm.org/D1187

"""
Commonly useful converters.
"""

from __future__ import absolute_import, division, print_function


def optional(converter):
    """
    A converter that allows an attribute to be optional. An optional attribute
    is one which can be set to ``None``.

    :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)

    return optional_converter