55import types
66import warnings
77from functools import wraps
8- from typing import Any , List , Optional
8+ from typing import List , Optional
99
1010from widget_code_input import WidgetCodeInput
1111from 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
0 commit comments