# Buggy code
def match(
    handler: handlers.ResourceHandler,
    cause: causation.ResourceCause,
) -> bool:
    # Kwargs are lazily evaluated on the first _actual_ use, and shared for all filters since then.
    kwargs: MutableMapping[str, Any] = {}
    return all(
        [
            _matches_resource(handler, cause.resource),
            _matches_labels(handler, cause, kwargs),
            _matches_annotations(handler, cause, kwargs),
            _matches_field_values(handler, cause, kwargs),
            _matches_field_changes(handler, cause, kwargs),
            _matches_filter_callback(handler, cause, kwargs),
        ]
    )


# Traceback (KeyError)
2021-01-19 16:43:29,972] kopf.reactor.activit [INFO    ] Initial authentication has been initiated.
[2021-01-19 16:43:30,129] kopf.activities.auth [INFO    ] Activity 'login_via_client' succeeded.
[2021-01-19 16:43:30,129] kopf.reactor.activit [INFO    ] Initial authentication has finished.
[2021-01-19 16:43:30,359] kopf.objects         [ERROR   ] [kube-system/coredns] Throttling for 1 seconds due to an unexpected error:
Traceback (most recent call last):
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/effects.py", line 200, in throttled
yield should_run
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/processing.py", line 79, in process_resource_event
delays, matched = await process_resource_causes(
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/processing.py", line 167, in process_resource_causes
not registry._resource_changing.prematch(cause=resource_changing_cause)):
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/registries.py", line 211, in prematch
if prematch(handler=handler, cause=cause):
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/registries.py", line 348, in prematch
_matches_filter_callback(handler, cause, kwargs),
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/registries.py", line 501, in _matches_filter_callback
return handler.when(**kwargs)
File "/Users/etilley/Documents/kubernetes/kopf/handler-testing/handler.py", line 11, in is_good_enough
return spec['field'] in spec['items']
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/structs/dicts.py", line 249, in __getitem__
return resolve(self._src, self._path + (item,))
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/structs/dicts.py", line 83, in resolve
result = result[key]
KeyError: 'field'
[2021-01-19 16:43:30,368] kopf.objects         [ERROR   ] [local-path-storage/local-path-provisioner] Throttling for 1 seconds due to an unexpected error:
Traceback (most recent call last):
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/effects.py", line 200, in throttled
yield should_run
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/processing.py", line 79, in process_resource_event
delays, matched = await process_resource_causes(
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/processing.py", line 167, in process_resource_causes
not registry._resource_changing.prematch(cause=resource_changing_cause)):
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/registries.py", line 211, in prematch
if prematch(handler=handler, cause=cause):
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/registries.py", line 348, in prematch
_matches_filter_callback(handler, cause, kwargs),
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/reactor/registries.py", line 501, in _matches_filter_callback
return handler.when(**kwargs)
File "/Users/etilley/Documents/kubernetes/kopf/handler-testing/handler.py", line 11, in is_good_enough
return spec['field'] in spec['items']
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/structs/dicts.py", line 249, in __getitem__
return resolve(self._src, self._path + (item,))
File "/Users/etilley/.pyenv/versions/3.8.0/lib/python3.8/site-packages/kopf/structs/dicts.py", line 83, in resolve
result = result[key]
KeyError: 'field'
[2021-01-19 16:43:31,361] kopf.objects         [INFO    ] [kube-system/coredns] Throttling is over. Switching back to normal operations.
[2021-01-19 16:43:31,369] kopf.objects         [INFO    ] [local-path-storage/local-path-provisioner] Throttling is over. Switching back to normal operations.

# Fixed code
def match(
    handler: handlers.ResourceHandler,
    cause: causation.ResourceCause,
) -> bool:
    # Kwargs are lazily evaluated on the first _actual_ use, and shared for all filters since then.
    kwargs: MutableMapping[str, Any] = {}
    return (
        _matches_resource(handler, cause.resource)
        and _matches_labels(handler, cause, kwargs)
        and _matches_annotations(handler, cause, kwargs)
        and _matches_field_values(handler, cause, kwargs)
        and _matches_field_changes(handler, cause, kwargs)
        and _matches_filter_callback(
            handler, cause, kwargs
        )  # the callback comes in the end!
    )