import attr


def optional_field(field_type):
    """Define an optional field of the specified `field_type`.

    Parameters
    ----------
    field_type : type
        The expected type of the field when not ``None``.

    Returns
    -------
    Any
        An ``attrs`` attribute, with a default value set to ``None``, and a validator checking
        that this field is optional and, if set, of the correct type.

    """
    return attr.ib(
        default=None,
        validator=attr.validators.optional(attr.validators.instance_of(field_type)),
    )