Skip to content

Commit

Permalink
In CodeExercise rename update_func to update (#85)
Browse files Browse the repository at this point in the history
This way the meaning of the `run_update` function should be clearer and
is less to type.
  • Loading branch information
agoscinski authored Nov 30, 2024
1 parent 843cbbb commit 2e42bff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/scwidgets/exercise/_widget_code_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CodeExercise(VBox, CheckableWidget, ExerciseWidget):
:param outputs:
List of CueOuputs that are drawn and refreshed
:param update_func:
:param update:
A function that is run during the update process. The function takes as argument
the CodeExercise, so it can update all cue_ouputs
Expand All @@ -71,7 +71,7 @@ def __init__(
] = None,
update_mode: str = "manual",
outputs: Union[None, Figure, CueOutput, List[CueOutput]] = None,
update_func: Optional[
update: Optional[
Union[
Callable[[CodeExercise], Union[Any, Check.FunOutParamsT]],
Callable[[], Union[Any, Check.FunOutParamsT]],
Expand All @@ -95,20 +95,22 @@ def __init__(
Callable[[CodeExercise], Union[Any, Check.FunOutParamsT]],
Callable[[], Union[Any, Check.FunOutParamsT]],
]
] = update_func
] = update

# We test update instead of self._update_func because self._update_func
# has one additional argument because of self
self._update_func_nb_nondefault_args: Optional[int]
if update_func is not None:
if update is not None:
self._update_func_nb_nondefault_args = len(
[
value
for value in inspect.signature(update_func).parameters.values()
for value in inspect.signature(update).parameters.values()
if not isinstance(value.default, inspect._empty)
]
)
if self._update_func_nb_nondefault_args > 1:
raise ValueError(
f"The given update_func has "
f"The given update function has "
f"{self._update_func_nb_nondefault_args} parameters without "
"defaults, but only zero or one are supported."
)
Expand Down Expand Up @@ -160,7 +162,7 @@ def __init__(

# check compability between code and params, can only be checked if
# update_func is not used because we cannot know how the code input is used
if update_func is None and code is not None and params is not None:
if update is None and code is not None and params is not None:
if isinstance(params, dict):
compatibility_result = code.compatible_with_signature(
list(params.keys())
Expand Down
8 changes: 4 additions & 4 deletions tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def update_print(code_ex: CodeExercise):
check_registry=CheckRegistry() if include_checks is True else None,
params=parameters if include_params is True else None,
outputs=[CueObject("Not initialized")],
update_func=update_print,
update=update_print,
update_mode=update_mode,
)

Expand Down Expand Up @@ -291,7 +291,7 @@ def print_success(code_ex: CodeExercise | None):
exercise_registry=exercise_registry,
exercise_key="test_save_registry_ex",
outputs=[cue_output],
update_func=print_success,
update=print_success,
)

exercise_registry._student_name_text.value = "test_save_registry-student_name"
Expand Down Expand Up @@ -341,11 +341,11 @@ def failing_update(a, b):
pass

with pytest.raises(
ValueError, match=r".*The given update_func has 2 parameters .*"
ValueError, match=r".*The given update function has 2 parameters .*"
):
CodeExercise(
code=TestCodeInput.mock_function_0,
update_func=failing_update,
update=failing_update,
)

def test_figure(self):
Expand Down

0 comments on commit 2e42bff

Please sign in to comment.