Skip to content

Commit 2e42bff

Browse files
authored
In CodeExercise rename update_func to update (#85)
This way the meaning of the `run_update` function should be clearer and is less to type.
1 parent 843cbbb commit 2e42bff

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/scwidgets/exercise/_widget_code_exercise.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CodeExercise(VBox, CheckableWidget, ExerciseWidget):
5454
:param outputs:
5555
List of CueOuputs that are drawn and refreshed
5656
57-
:param update_func:
57+
:param update:
5858
A function that is run during the update process. The function takes as argument
5959
the CodeExercise, so it can update all cue_ouputs
6060
@@ -71,7 +71,7 @@ def __init__(
7171
] = None,
7272
update_mode: str = "manual",
7373
outputs: Union[None, Figure, CueOutput, List[CueOutput]] = None,
74-
update_func: Optional[
74+
update: Optional[
7575
Union[
7676
Callable[[CodeExercise], Union[Any, Check.FunOutParamsT]],
7777
Callable[[], Union[Any, Check.FunOutParamsT]],
@@ -95,20 +95,22 @@ def __init__(
9595
Callable[[CodeExercise], Union[Any, Check.FunOutParamsT]],
9696
Callable[[], Union[Any, Check.FunOutParamsT]],
9797
]
98-
] = update_func
98+
] = update
9999

100+
# We test update instead of self._update_func because self._update_func
101+
# has one additional argument because of self
100102
self._update_func_nb_nondefault_args: Optional[int]
101-
if update_func is not None:
103+
if update is not None:
102104
self._update_func_nb_nondefault_args = len(
103105
[
104106
value
105-
for value in inspect.signature(update_func).parameters.values()
107+
for value in inspect.signature(update).parameters.values()
106108
if not isinstance(value.default, inspect._empty)
107109
]
108110
)
109111
if self._update_func_nb_nondefault_args > 1:
110112
raise ValueError(
111-
f"The given update_func has "
113+
f"The given update function has "
112114
f"{self._update_func_nb_nondefault_args} parameters without "
113115
"defaults, but only zero or one are supported."
114116
)
@@ -160,7 +162,7 @@ def __init__(
160162

161163
# check compability between code and params, can only be checked if
162164
# update_func is not used because we cannot know how the code input is used
163-
if update_func is None and code is not None and params is not None:
165+
if update is None and code is not None and params is not None:
164166
if isinstance(params, dict):
165167
compatibility_result = code.compatible_with_signature(
166168
list(params.keys())

tests/test_code.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def update_print(code_ex: CodeExercise):
156156
check_registry=CheckRegistry() if include_checks is True else None,
157157
params=parameters if include_params is True else None,
158158
outputs=[CueObject("Not initialized")],
159-
update_func=update_print,
159+
update=update_print,
160160
update_mode=update_mode,
161161
)
162162

@@ -291,7 +291,7 @@ def print_success(code_ex: CodeExercise | None):
291291
exercise_registry=exercise_registry,
292292
exercise_key="test_save_registry_ex",
293293
outputs=[cue_output],
294-
update_func=print_success,
294+
update=print_success,
295295
)
296296

297297
exercise_registry._student_name_text.value = "test_save_registry-student_name"
@@ -341,11 +341,11 @@ def failing_update(a, b):
341341
pass
342342

343343
with pytest.raises(
344-
ValueError, match=r".*The given update_func has 2 parameters .*"
344+
ValueError, match=r".*The given update function has 2 parameters .*"
345345
):
346346
CodeExercise(
347347
code=TestCodeInput.mock_function_0,
348-
update_func=failing_update,
348+
update=failing_update,
349349
)
350350

351351
def test_figure(self):

0 commit comments

Comments
 (0)