converters.py 531 B

123456789101112131415161718192021222324
  1. """
  2. Commonly useful converters.
  3. """
  4. from __future__ import absolute_import, division, print_function
  5. def optional(converter):
  6. """
  7. A converter that allows an attribute to be optional. An optional attribute
  8. is one which can be set to ``None``.
  9. :param callable converter: the converter that is used for non-``None``
  10. values.
  11. .. versionadded:: 17.1.0
  12. """
  13. def optional_converter(val):
  14. if val is None:
  15. return None
  16. return converter(val)
  17. return optional_converter