Skip to content

Commit 57dd962

Browse files
committed
raise an exception when a function is called with unexpected keyword parameters that
don't have corresponding keyword argyment (however, the trigger parameter names are excluded from this check, since trigger functions are allowed to have any subset of keyword arguments)
1 parent c4a269d commit 57dd962

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

custom_components/pyscript/eval.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@
6060
"pyscript_executor",
6161
}
6262

63+
TRIGGER_KWARGS = {
64+
"context",
65+
"event_type",
66+
"old_value",
67+
"payload",
68+
"qos",
69+
"topic",
70+
"trigger_type",
71+
"trigger_time",
72+
"var_name",
73+
"value",
74+
}
75+
6376

6477
def ast_eval_exec_factory(ast_ctx, mode):
6578
"""Generate a function that executes eval() or exec() with given ast_ctx."""
@@ -633,6 +646,13 @@ async def call(self, ast_ctx, *args, **kwargs):
633646
sym_table[var_name] = val
634647
if self.func_def.args.kwarg:
635648
sym_table[self.func_def.args.kwarg.arg] = kwargs
649+
elif not set(kwargs.keys()).issubset(TRIGGER_KWARGS):
650+
# don't raise an exception for extra trigger keyword parameters;
651+
# it's difficult to apply this exception to just trigger functions
652+
# since they could have non-trigger decorators too
653+
raise TypeError(
654+
f"{self.name}() called with unexpected keyword arguments: {', '.join(sorted(kwargs.keys()))}"
655+
)
636656
if self.func_def.args.vararg:
637657
if len(args) > len(self.func_def.args.args):
638658
sym_table[self.func_def.args.vararg.arg] = tuple(args[len(self.func_def.args.args) :])

custom_components/pyscript/trigger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ def stop(self):
884884
if self.run_on_shutdown:
885885
notify_type = "shutdown"
886886
notify_info = {"trigger_type": "time", "trigger_time": "shutdown"}
887+
notify_info.update(self.time_trigger_kwargs.get("kwargs", {}))
887888
action_future = self.call_action(notify_type, notify_info, run_task=False)
888889
Function.waiter_await(action_future)
889890

docs/new_features.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Planned new features post 1.2.1 include:
2626
share pyscripts. The ``print`` function only logs a message, rather than implements the real ``print`` features,
2727
such as specifying an output file handle. Support might be added in the future using an executor job, perhaps
2828
enabled when ``allow_all_imports`` is set.
29-
- Consider adding an option argument to ``@pyscript_compile`` that wraps the function with ``task.executor``.
3029

3130
The new features since 1.2.1 in master include:
3231

@@ -51,3 +50,6 @@ Bug fixes since 1.2.1 include:
5150

5251
- Fixed subscripts when running python 3.9.x.
5352
- When exception text is created, ensure lineno is inside code_list[]; with lambda function or eval it might not be.
53+
- An exception is raised when a function is called with unexpected keyword parameters that don't have corresponding
54+
keyword arguments (however, the trigger parameter names are excluded from this check, since trigger functions
55+
are allowed to have any subset of keyword arguments).

0 commit comments

Comments
 (0)