Skip to content

Commit 5cf3b75

Browse files
committed
rewrite capturing param errors during assignment
1 parent a6b78d9 commit 5cf3b75

File tree

1 file changed

+51
-63
lines changed

1 file changed

+51
-63
lines changed

waveform_editor/tendencies/base.py

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,57 @@ class BaseTendency(param.Parameterized):
7777
def __init__(self, **kwargs):
7878
self.annotations = Annotations()
7979
self.line_number = kwargs.pop("user_line_number", 0)
80-
self._check_for_unknown_kwargs(kwargs)
81-
self._setup_param(kwargs)
80+
unknown_kwargs = []
81+
super().__init__()
82+
83+
with param.parameterized.batch_call_watchers(self):
84+
for param_name, value in kwargs.items():
85+
param_name_no_user = param_name.replace("user_", "")
86+
if param_name not in self.param:
87+
unknown_kwargs.append(param_name_no_user)
88+
continue
89+
90+
try:
91+
setattr(self, param_name, value)
92+
except Exception as error:
93+
self._handle_error(error)
94+
95+
self._handle_unknown_kwargs(unknown_kwargs)
96+
97+
def _handle_error(self, error):
98+
"""Handle exceptions raised by param assignment and add them as annotations.
99+
100+
Args:
101+
param_name: The name of the assigned param
102+
error_msg: The error message raised by param
103+
"""
104+
error_msg = str(error)
105+
# Remove the class name and user_ part of the error message
106+
replace_str = f"{type(self).__name__}.user_"
107+
cleaned_msg = error_msg.replace(replace_str, "")
108+
self.annotations.add(
109+
self.line_number,
110+
f"{cleaned_msg}\nThis keyword is ignored.\n",
111+
is_warning=True,
112+
)
113+
114+
def _handle_unknown_kwargs(self, unknown_kwargs):
115+
"""Suggest alternative keyword arguments if the keyword argument is unknown.
116+
117+
Args:
118+
unknown_kwargs: List of unknown keyword arguments.
119+
"""
120+
if unknown_kwargs:
121+
params_list = [
122+
word.replace("user_", "") for word in self.param if "user_" in word
123+
]
124+
for unknown_kwarg in unknown_kwargs:
125+
suggestion = self.annotations.suggest(unknown_kwarg, params_list)
126+
error_msg = (
127+
f"Unknown keyword passed: {unknown_kwarg!r}. {suggestion}"
128+
"This keyword will be ignored.\n"
129+
)
130+
self.annotations.add(self.line_number, error_msg, is_warning=True)
82131

83132
def __repr__(self):
84133
# Override __repr__ from parametrized to avoid showing way too many details
@@ -194,64 +243,3 @@ def _calc_times(self):
194243
if self.duration <= 0:
195244
error_msg = "Tendency end time must be greater than its start time."
196245
self.annotations.add(self.line_number, error_msg)
197-
198-
def _check_for_unknown_kwargs(self, kwargs):
199-
"""Identifies and removes unrecognized keyword arguments, suggesting
200-
alternatives.
201-
202-
Args:
203-
kwargs: The passed keyword arguments.
204-
"""
205-
unknown_kwargs = []
206-
for key in list(kwargs.keys()):
207-
if key not in self.param:
208-
unknown_kwargs.append(key.replace("user_", ""))
209-
del kwargs[key]
210-
if unknown_kwargs:
211-
# Remove the user_ part of the param names to match suggestion
212-
params_list = [
213-
word.replace("user_", "") for word in self.param if "user_" in word
214-
]
215-
for unknown_kwarg in unknown_kwargs:
216-
suggestion = self.annotations.suggest(unknown_kwarg, params_list)
217-
error_msg = (
218-
f"Unknown keyword passed: {unknown_kwarg!r}. {suggestion}"
219-
"This keyword will be ignored.\n"
220-
)
221-
222-
self.annotations.add(self.line_number, error_msg, is_warning=True)
223-
224-
def _setup_param(self, kwargs):
225-
"""Call the constructor of param.Parameterized. If this results in a ValueError,
226-
the ValueError is added to the annotations, and the problematic keyword
227-
argument is removed. Then, the method retries the initialization process with
228-
the modified keyword arguments.
229-
230-
Args:
231-
kwargs: The passed keyword arguments.
232-
"""
233-
try:
234-
super().__init__(**kwargs)
235-
except ValueError as error:
236-
# Fetch error and add to annotations
237-
error_msg = str(error)
238-
match = re.search(r"'(\w+\.\w+)'", error_msg)
239-
param_to_remove = match.group(1)
240-
param_to_remove_no_class = param_to_remove.split(".")[1]
241-
cleaned_error_msg = error_msg.replace(
242-
param_to_remove, param_to_remove_no_class.replace("user_", "")
243-
)
244-
245-
self.annotations.add(
246-
self.line_number,
247-
f"{cleaned_error_msg}\nThis keyword is ignored.\n",
248-
is_warning=True,
249-
)
250-
# Ignore keyword argument with ValueError
251-
if param_to_remove_no_class in kwargs:
252-
kwargs.pop(param_to_remove_no_class)
253-
else:
254-
return
255-
256-
# Recursively retry with new kwargs
257-
self._setup_param(kwargs)

0 commit comments

Comments
 (0)