Skip to content

Unexpected keyword arguments are silently ignored when their names are in TRIGGER_KWARGS #905

Open

Description

@dmamelin

EvalFunc.call raises TypeError for unexpected keyword arguments, except for names in the hard-coded TRIGGER_KWARGS set (context, event_type, old_value, payload, payload_obj, qos, request, retain, topic, trigger_type, trigger_time, var_name, value, webhook_id). These are silently dropped in every call of a pyscript function, not only in calls made by triggers:

def notify(message, title="pyscript"):
    return f"{title}: {message}"

notify("door open", topic="alerts")    # -> "pyscript: door open", topic is silently dropped
notify("door open", topics="alerts")   # -> TypeError: notify() called with unexpected keyword arguments: topics

In Python both calls raise TypeError.

Trigger functions no longer need the list: the decorator manager passes them only the kwargs they declare (#870, and #899 for @service). Now only decorator wrappers rely on it, because they pass **kwargs on to the wrapped function:

def once(func):
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

@state_trigger("pyscript.door == 'open'")
@once
def door_opened():  # receives context, old_value, trigger_type, value, var_name via wrapper
    log.info("door opened")

Plan

  1. An upcoming PR keeps the current behaviour, but logs a warning once per function when keyword arguments are ignored because of TRIGGER_KWARGS. The warning links to this issue:
    door_opened() was called with keyword arguments it doesn't declare: context, old_value, trigger_type, value, var_name; ...
    
  2. After a few releases, TRIGGER_KWARGS will be removed and such calls will raise TypeError as in Python.

How to fix your code

  • Ordinary call: remove the argument, or add the parameter to the function.
  • Function under a decorator wrapper: declare the kwargs it uses, or add **kwargs:
    @state_trigger("pyscript.door == 'open'")
    @once
    def door_opened(**kwargs):
        log.info("door opened")
  • Or don't pass trigger kwargs on in the wrapper, if the wrapped function doesn't need them:
    def once(func):
        def wrapper(*args, **kwargs):
            return func(*args)
        return wrapper

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions