Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve kwargs typing in callbacks protocols #720

Merged
merged 4 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kopf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
Meta,
Body,
BodyEssence,
Labels,
Annotations,
OwnerReference,
ObjectReference,
build_object_reference,
Expand Down Expand Up @@ -186,6 +188,8 @@
'Meta',
'Body',
'BodyEssence',
'Labels',
'Annotations',
'ObjectReference',
'OwnerReference',
'Memo', 'Index', 'Store',
Expand Down
94 changes: 47 additions & 47 deletions kopf/on.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def creation_handler(**kwargs):
from kopf.reactor import handling, registries
from kopf.structs import callbacks, dicts, filters, handlers, references

ActivityDecorator = Callable[["callbacks.ActivityFn"], "callbacks.ActivityFn"]
ResourceIndexingDecorator = Callable[["callbacks.ResourceIndexingFn"], "callbacks.ResourceIndexingFn"]
ResourceWatchingDecorator = Callable[["callbacks.ResourceWatchingFn"], "callbacks.ResourceWatchingFn"]
ResourceChangingDecorator = Callable[["callbacks.ResourceChangingFn"], "callbacks.ResourceChangingFn"]
ResourceDaemonDecorator = Callable[["callbacks.ResourceDaemonFn"], "callbacks.ResourceDaemonFn"]
ResourceTimerDecorator = Callable[["callbacks.ResourceTimerFn"], "callbacks.ResourceTimerFn"]
ActivityDecorator = Callable[[callbacks.ActivityFn], callbacks.ActivityFn]
ResourceIndexingDecorator = Callable[[callbacks.ResourceIndexingFn], callbacks.ResourceIndexingFn]
ResourceWatchingDecorator = Callable[[callbacks.ResourceWatchingFn], callbacks.ResourceWatchingFn]
ResourceChangingDecorator = Callable[[callbacks.ResourceChangingFn], callbacks.ResourceChangingFn]
ResourceDaemonDecorator = Callable[[callbacks.ResourceDaemonFn], callbacks.ResourceDaemonFn]
ResourceTimerDecorator = Callable[[callbacks.ResourceTimerFn], callbacks.ResourceTimerFn]


def startup( # lgtm[py/similar-function]
Expand All @@ -37,8 +37,8 @@ def startup( # lgtm[py/similar-function]
registry: Optional[registries.OperatorRegistry] = None,
) -> ActivityDecorator:
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ActivityFn",
) -> "callbacks.ActivityFn":
fn: callbacks.ActivityFn,
) -> callbacks.ActivityFn:
real_registry = registry if registry is not None else registries.get_default_registry()
real_id = registries.generate_id(fn=fn, id=id)
handler = handlers.ActivityHandler(
Expand All @@ -64,8 +64,8 @@ def cleanup( # lgtm[py/similar-function]
registry: Optional[registries.OperatorRegistry] = None,
) -> ActivityDecorator:
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ActivityFn",
) -> "callbacks.ActivityFn":
fn: callbacks.ActivityFn,
) -> callbacks.ActivityFn:
real_registry = registry if registry is not None else registries.get_default_registry()
real_id = registries.generate_id(fn=fn, id=id)
handler = handlers.ActivityHandler(
Expand All @@ -92,8 +92,8 @@ def login( # lgtm[py/similar-function]
) -> ActivityDecorator:
""" ``@kopf.on.login()`` handler for custom (re-)authentication. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ActivityFn",
) -> "callbacks.ActivityFn":
fn: callbacks.ActivityFn,
) -> callbacks.ActivityFn:
real_registry = registry if registry is not None else registries.get_default_registry()
real_id = registries.generate_id(fn=fn, id=id)
handler = handlers.ActivityHandler(
Expand All @@ -120,8 +120,8 @@ def probe( # lgtm[py/similar-function]
) -> ActivityDecorator:
""" ``@kopf.on.probe()`` handler for arbitrary liveness metrics. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ActivityFn",
) -> "callbacks.ActivityFn":
fn: callbacks.ActivityFn,
) -> callbacks.ActivityFn:
real_registry = registry if registry is not None else registries.get_default_registry()
real_id = registries.generate_id(fn=fn, id=id)
handler = handlers.ActivityHandler(
Expand Down Expand Up @@ -158,16 +158,16 @@ def resume( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
# Operator specification:
registry: Optional[registries.OperatorRegistry] = None,
) -> ResourceChangingDecorator:
""" ``@kopf.on.resume()`` handler for the object resuming on operator (re)start. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceChangingFn",
) -> "callbacks.ResourceChangingFn":
fn: callbacks.ResourceChangingFn,
) -> callbacks.ResourceChangingFn:
_warn_conflicting_values(field, value)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -214,16 +214,16 @@ def create( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
# Operator specification:
registry: Optional[registries.OperatorRegistry] = None,
) -> ResourceChangingDecorator:
""" ``@kopf.on.create()`` handler for the object creation. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceChangingFn",
) -> "callbacks.ResourceChangingFn":
fn: callbacks.ResourceChangingFn,
) -> callbacks.ResourceChangingFn:
_warn_conflicting_values(field, value)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -270,7 +270,7 @@ def update( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
old: Optional[filters.ValueFilter] = None,
Expand All @@ -280,8 +280,8 @@ def update( # lgtm[py/similar-function]
) -> ResourceChangingDecorator:
""" ``@kopf.on.update()`` handler for the object update or change. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceChangingFn",
) -> "callbacks.ResourceChangingFn":
fn: callbacks.ResourceChangingFn,
) -> callbacks.ResourceChangingFn:
_warn_conflicting_values(field, value, old, new)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -329,16 +329,16 @@ def delete( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
# Operator specification:
registry: Optional[registries.OperatorRegistry] = None,
) -> ResourceChangingDecorator:
""" ``@kopf.on.delete()`` handler for the object deletion. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceChangingFn",
) -> "callbacks.ResourceChangingFn":
fn: callbacks.ResourceChangingFn,
) -> callbacks.ResourceChangingFn:
_warn_conflicting_values(field, value)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -385,7 +385,7 @@ def field( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: dicts.FieldSpec,
value: Optional[filters.ValueFilter] = None,
old: Optional[filters.ValueFilter] = None,
Expand All @@ -395,8 +395,8 @@ def field( # lgtm[py/similar-function]
) -> ResourceChangingDecorator:
""" ``@kopf.on.field()`` handler for the individual field changes. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceChangingFn",
) -> "callbacks.ResourceChangingFn":
fn: callbacks.ResourceChangingFn,
) -> callbacks.ResourceChangingFn:
_warn_conflicting_values(field, value, old, new)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -443,16 +443,16 @@ def index( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
# Operator specification:
registry: Optional[registries.OperatorRegistry] = None,
) -> ResourceIndexingDecorator:
""" ``@kopf.index()`` handler for the indexing callbacks. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceIndexingFn",
) -> "callbacks.ResourceIndexingFn":
fn: callbacks.ResourceIndexingFn,
) -> callbacks.ResourceIndexingFn:
_warn_conflicting_values(field, value)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -493,16 +493,16 @@ def event( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
# Operator specification:
registry: Optional[registries.OperatorRegistry] = None,
) -> ResourceWatchingDecorator:
""" ``@kopf.on.event()`` handler for the silent spies on the events. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceWatchingFn",
) -> "callbacks.ResourceWatchingFn":
fn: callbacks.ResourceWatchingFn,
) -> callbacks.ResourceWatchingFn:
_warn_conflicting_values(field, value)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -551,16 +551,16 @@ def daemon( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
# Operator specification:
registry: Optional[registries.OperatorRegistry] = None,
) -> ResourceDaemonDecorator:
""" ``@kopf.daemon()`` decorator for the background threads/tasks. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceDaemonFn",
) -> "callbacks.ResourceDaemonFn":
fn: callbacks.ResourceDaemonFn,
) -> callbacks.ResourceDaemonFn:
_warn_conflicting_values(field, value)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -613,16 +613,16 @@ def timer( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
# Operator specification:
registry: Optional[registries.OperatorRegistry] = None,
) -> ResourceTimerDecorator:
""" ``@kopf.timer()`` handler for the regular events. """
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceTimerFn",
) -> "callbacks.ResourceTimerFn":
fn: callbacks.ResourceTimerFn,
) -> callbacks.ResourceTimerFn:
_warn_conflicting_values(field, value)
_verify_filters(labels, annotations)
real_registry = registry if registry is not None else registries.get_default_registry()
Expand Down Expand Up @@ -658,7 +658,7 @@ def subhandler( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
when: Optional[callbacks.WhenFilterFn] = None,
field: Optional[dicts.FieldSpec] = None,
value: Optional[filters.ValueFilter] = None,
old: Optional[filters.ValueFilter] = None, # only for on.update's subhandlers
Expand Down Expand Up @@ -693,8 +693,8 @@ def create_task(*, spec, task=task, **kwargs):
create function will have its own value, not the latest in the for-cycle.
"""
def decorator( # lgtm[py/similar-function]
fn: "callbacks.ResourceChangingFn",
) -> "callbacks.ResourceChangingFn":
fn: callbacks.ResourceChangingFn,
) -> callbacks.ResourceChangingFn:
parent_handler = handling.handler_var.get()
if not isinstance(parent_handler, handlers.ResourceChangingHandler):
raise TypeError("Sub-handlers are only supported for resource-changing handlers.")
Expand All @@ -720,7 +720,7 @@ def decorator( # lgtm[py/similar-function]


def register( # lgtm[py/similar-function]
fn: "callbacks.ResourceChangingFn",
fn: callbacks.ResourceChangingFn,
*,
# Handler's behaviour specification:
id: Optional[str] = None,
Expand All @@ -732,8 +732,8 @@ def register( # lgtm[py/similar-function]
# Resource object specification:
labels: Optional[filters.MetaFilter] = None,
annotations: Optional[filters.MetaFilter] = None,
when: Optional["callbacks.WhenFilterFn"] = None,
) -> "callbacks.ResourceChangingFn":
when: Optional[callbacks.WhenFilterFn] = None,
) -> callbacks.ResourceChangingFn:
"""
Register a function as a sub-handler of the currently executed handler.

Expand Down
2 changes: 1 addition & 1 deletion kopf/reactor/handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class HandlerChildrenRetry(TemporaryError):

async def execute(
*,
fns: Optional[Iterable["callbacks.ResourceChangingFn"]] = None,
fns: Optional[Iterable[callbacks.ResourceChangingFn]] = None,
handlers: Optional[Iterable[handlers_.ResourceChangingHandler]] = None,
registry: Optional[registries.ResourceChangingRegistry] = None,
lifecycle: Optional[lifecycles.LifeCycleFn] = None,
Expand Down
Loading