Skip to content

Commit e21928a

Browse files
committed
Fixed sorting logic for lists and dicts; added option to restrict allowed type inputs
1 parent b551dfd commit e21928a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/murfey/cli/generate_config.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def construct_dict(
175175
allow_empty_value: bool = True,
176176
allow_eval: bool = True,
177177
sort_keys: bool = True,
178-
restrict_values_to_types: Optional[Type[Any] | tuple[Type[Any], ...]] = None,
178+
restrict_to_types: Optional[Type[Any] | tuple[Type[Any], ...]] = None,
179179
debug: bool = False,
180180
) -> dict[str, Any]:
181181
"""
@@ -218,11 +218,23 @@ def is_type(value: str, instance: Type[Any] | tuple[Type[Any], ...]) -> bool:
218218
console.print("No value provided", style="red")
219219
add_entry = ask_for_input(dict_name, True)
220220
continue
221-
# Convert values to numericals if set
221+
# Convert values if set
222222
try:
223223
eval_value = literal_eval(value) if allow_eval else value
224224
except Exception:
225225
eval_value = value
226+
# Reject incorrect value types if set
227+
if restrict_to_types is not None:
228+
allowed_types = (
229+
(restrict_to_types,)
230+
if not isinstance(restrict_to_types, (tuple, list))
231+
else restrict_to_types
232+
)
233+
if not isinstance(eval_value, allowed_types):
234+
console.print("The value is not of an allowed type.", style="red")
235+
add_entry = ask_for_input(dict_name, True)
236+
continue
237+
# Assign value to key
226238
dct[key] = eval_value
227239
add_entry = ask_for_input(dict_name, True)
228240
continue

0 commit comments

Comments
 (0)