Skip to content

Commit 843cbbb

Browse files
authored
Make CodeInput naming related to the function object more consistent (#84)
Rename `get_function_object` to `wrapped_function` and make it property to be more consistent with the `function`. In `__call__` the wrapped function is now called, this way `__call__` replaces `run` function that is removed as a consequence.
1 parent 202d993 commit 843cbbb

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/scwidgets/code/_widget_code_input.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import types
66
import warnings
77
from functools import wraps
8-
from typing import Any, List, Optional
8+
from typing import List, Optional
99

1010
from widget_code_input import WidgetCodeInput
1111
from widget_code_input.utils import (
@@ -68,12 +68,21 @@ def __init__(
6868
@property
6969
def function(self) -> types.FunctionType:
7070
"""
71-
Returns the unwrapped function object
71+
Return the compiled function object.
72+
73+
This can be assigned to a variable and then called, for instance::
74+
75+
func = widget.wrapped_function # This can raise a SyntaxError
76+
retval = func(parameters)
77+
78+
:raise SyntaxError: if the function code has syntax errors (or if
79+
the function name is not a valid identifier)
7280
"""
73-
return inspect.unwrap(self.get_function_object())
81+
return inspect.unwrap(self.wrapped_function)
7482

75-
def __call__(self, *args, **kwargs) -> Any:
76-
return self.function(*args, **kwargs)
83+
def __call__(self, *args, **kwargs) -> Check.FunOutParamsT:
84+
"""Calls the wrapped function"""
85+
return self.wrapped_function(*args, **kwargs)
7786

7887
def compatible_with_signature(self, parameters: List[str]) -> str:
7988
"""
@@ -95,9 +104,6 @@ def compatible_with_signature(self, parameters: List[str]) -> str:
95104
def function_parameters_name(self) -> List[str]:
96105
return self.function_parameters.replace(",", "").split(" ")
97106

98-
def run(self, *args, **kwargs) -> Check.FunOutParamsT:
99-
return self.get_function_object()(*args, **kwargs)
100-
101107
@staticmethod
102108
def get_code(func: types.FunctionType) -> str:
103109
source_lines, _ = inspect.getsourcelines(func)
@@ -140,13 +146,15 @@ def get_code(func: types.FunctionType) -> str:
140146

141147
return source
142148

143-
def get_function_object(self):
149+
@property
150+
def wrapped_function(self) -> types.FunctionType:
144151
"""
145-
Return the compiled function object.
152+
Return the compiled function object wrapped by an try-catch block
153+
raising a `CodeValidationError`.
146154
147155
This can be assigned to a variable and then called, for instance::
148156
149-
func = widget.get_function_object() # This can raise a SyntaxError
157+
func = widget.wrapped_function # This can raise a SyntaxError
150158
retval = func(parameters)
151159
152160
:raise SyntaxError: if the function code has syntax errors (or if

src/scwidgets/exercise/_widget_code_exercise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def run_code(self, *args, **kwargs) -> Check.FunOutParamsT:
756756
raise ValueError(
757757
"run_code was invoked, but no code was given on initializaion"
758758
)
759-
return self._code.run(*args, **kwargs)
759+
return self._code(*args, **kwargs)
760760
except CodeValidationError as e:
761761
raise e
762762
except Exception as e:

0 commit comments

Comments
 (0)