def METHOD_NAME(self, key, value):
    """Set a setting and save it to disk.
    Check type before and raise an exception if necessary."""
    extension_name = (self.extension().display_name()
        or self.extension().name())
    if not key in self._defaults.keys():
        raise KeyError(
            _("Trying to store unknown setting '{setting}' "
              "in extension '{extension}'").format(
                setting=key,
                extension=extension_name))
    try:
        old_value = self._data[key]
        value = type(self._defaults[key])(value)
        if value == old_value:
            return
        self._data[key] = value
        QSettings(self.settings_file(), QSettings.IniFormat).setValue(key, value)
        self.extension().settings_changed(key, old_value, value)
    except ValueError:
        # type conversion failed
        raise ValueError(
            _("Trying to store setting '{setting}' in extension "
              "'{extension}' with type '{type}' instead of '{type_is}'").format(
              setting=key,
              extension=extension_name,
              type = type(self._defaults[key]),
              type_is = type(value)))