def _keys_match(attribute):
    """
    Create an invariant for a ``field`` holding a ``pmap``.

    The invariant enforced is that the keys of the ``pmap`` equal the value of
    a particular attribute of the corresponding values.

    :param str attribute: The name of the attribute of the ``pmap`` values
        which must equal the corresponding key.
    :return: A function suitable for use as a pyrsistent invariant.
    """
    def key_match_invariant(pmap):
        # Either the field allows None, in which case this is necessary,
        # or it doesn't in which case this won't do any harm since
        # invalidity of None will be enforced elsewhere:
        if pmap is None:
            return (True, "")

        for (key, value) in pmap.items():
            if key != getattr(value, attribute):
                return (
                    False, "{} is not correct key for {}".format(key, value)
                )
        return (True, "")
    return key_match_invariant