# Buggy code
def _extract_nc4_encoding(
    variable, raise_on_invalid=False, lsd_okay=True, backend="netCDF4"
):
    encoding = variable.encoding.copy()

    safe_to_drop = set(["source", "original_shape"])
    valid_encodings = set(
        ["zlib", "complevel", "fletcher32", "contiguous", "chunksizes"]
    )
    if lsd_okay:
        valid_encodings.add("least_significant_digit")

    if (
        encoding.get("chunksizes") is not None
        and (encoding.get("original_shape", variable.shape) != variable.shape)
        and not raise_on_invalid
    ):
        del encoding["chunksizes"]

    for k in safe_to_drop:
        if k in encoding:
            del encoding[k]

    if raise_on_invalid:
        invalid = [k for enc in encoding for k in enc if k not in valid_encodings]
        if invalid:
            raise ValueError(
                "unexpected encoding parameters for %r backend: "
                " %r" % (backend, invalid)
            )
    else:
        for k in list(encoding):
            if k not in valid_encodings:
                del encoding[k]

    return encoding


# Traceback (ValueError)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-ec637fd612fd> in <module>()
----> 1 ds.to_netcdf('demo.nc', mode='w', encoding={'data': {'zlib': True}})

/Users/takeshi/Developer/xray/xray/core/dataset.py in to_netcdf(self, path, mode, format, group, engine, encoding)
782         from ..backends.api import to_netcdf
783         return to_netcdf(self, path, mode, format=format, group=group,
--> 784                          engine=engine, encoding=encoding)
785
786     dump = utils.function_alias(to_netcdf, 'dump')

/Users/takeshi/Developer/xray/xray/backends/api.py in to_netcdf(dataset, path, mode, format, group, engine, writer, encoding)
352     store = store_cls(path, mode, format, group, writer)
353     try:
--> 354         dataset.dump_to_store(store, sync=sync, encoding=encoding)
355         if isinstance(path, BytesIO):
356             return path.getvalue()

/Users/takeshi/Developer/xray/xray/core/dataset.py in dump_to_store(self, store, encoder, sync, encoding)
728             variables, attrs = encoder(variables, attrs)
729
--> 730         store.store(variables, attrs, check_encoding)
731         if sync:
732             store.sync()

/Users/takeshi/Developer/xray/xray/backends/common.py in store(self, variables, attributes, check_encoding_set)
226         cf_variables, cf_attrs = cf_encoder(variables, attributes)
227         AbstractWritableDataStore.store(self, cf_variables, cf_attrs,
--> 228                                         check_encoding_set)

/Users/takeshi/Developer/xray/xray/backends/common.py in store(self, variables, attributes, check_encoding_set)
201                                 if not (k in neccesary_dims and
202                                         is_trivial_index(v)))
--> 203         self.set_variables(variables, check_encoding_set)
204
205     def set_attributes(self, attributes):

/Users/takeshi/Developer/xray/xray/backends/common.py in set_variables(self, variables, check_encoding_set)
211             name = _encode_variable_name(vn)
212             check = vn in check_encoding_set
--> 213             target, source = self.prepare_variable(name, v, check)
214             self.writer.add(source, target)
215

/Users/takeshi/Developer/xray/xray/backends/netCDF4_.py in prepare_variable(self, name, variable, check_encoding)
259
260         encoding = _extract_nc4_encoding(variable,
--> 261                                          raise_on_invalid=check_encoding)
262         nc4_var = self.ds.createVariable(
263             varname=name,

/Users/takeshi/Developer/xray/xray/backends/netCDF4_.py in _extract_nc4_encoding(variable, raise_on_invalid, lsd_okay, backend)
160         if invalid:
161             raise ValueError('unexpected encoding parameters for %r backend: '
--> 162                              ' %r' % (backend, invalid))
163     else:
164         for k in list(encoding):

ValueError: unexpected encoding parameters for 'netCDF4' backend:  ['z', 'l', 'i', 'b']

# Fixed code
def _extract_nc4_encoding(
    variable, raise_on_invalid=False, lsd_okay=True, backend="netCDF4"
):
    encoding = variable.encoding.copy()

    safe_to_drop = set(["source", "original_shape"])
    valid_encodings = set(
        ["zlib", "complevel", "fletcher32", "contiguous", "chunksizes"]
    )
    if lsd_okay:
        valid_encodings.add("least_significant_digit")

    if (
        encoding.get("chunksizes") is not None
        and (encoding.get("original_shape", variable.shape) != variable.shape)
        and not raise_on_invalid
    ):
        del encoding["chunksizes"]

    for k in safe_to_drop:
        if k in encoding:
            del encoding[k]

    if raise_on_invalid:
        invalid = [k for k in encoding if k not in valid_encodings]
        if invalid:
            raise ValueError(
                "unexpected encoding parameters for %r backend: "
                " %r" % (backend, invalid)
            )
    else:
        for k in list(encoding):
            if k not in valid_encodings:
                del encoding[k]

    return encoding