Skip to content

Commit 4b2a774

Browse files
committed
Leave a TODO for nested edge case, move fns around
Signed-off-by: Fabrice Normandin <[email protected]>
1 parent d57572e commit 4b2a774

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

simple_parsing/parsing.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ def _resolve_subgroups(
648648
if subgroup_field.subgroup_default is dataclasses.MISSING:
649649
assert argument_options["required"]
650650
elif isinstance(argument_options["default"], dict):
651+
# TODO: In this case here, the value of a nested subgroup in this default dict
652+
# should also be used!
651653
# BUG #276: The default here is a dict because it came from a config file.
652654
# Here we want the subgroup field to have a 'str' default, because we just want
653655
# to be able to choose between the subgroup names.
@@ -1196,29 +1198,6 @@ def _create_dataclass_instance(
11961198
return constructor(**constructor_args)
11971199

11981200

1199-
def _has_values_of_type(
1200-
mapping: Mapping[K, Any], value_type: type[V] | tuple[type[V], ...]
1201-
) -> TypeGuard[Mapping[K, V]]:
1202-
# Utility functions used to narrow the type of dictionaries.
1203-
return all(isinstance(v, value_type) for v in mapping.values())
1204-
1205-
1206-
def _has_keys_of_type(
1207-
mapping: Mapping[Any, V], key_type: type[K] | tuple[type[K], ...]
1208-
) -> TypeGuard[Mapping[K, V]]:
1209-
# Utility functions used to narrow the type of dictionaries.
1210-
return all(isinstance(k, key_type) for k in mapping.keys())
1211-
1212-
1213-
def _has_items_of_type(
1214-
mapping: Mapping[Any, Any],
1215-
item_type: tuple[type[K] | tuple[type[K], ...], type[V] | tuple[type[V], ...]],
1216-
) -> TypeGuard[Mapping[K, V]]:
1217-
# Utility functions used to narrow the type of a dictionary or mapping.
1218-
key_type, value_type = item_type
1219-
return _has_keys_of_type(mapping, key_type) and _has_values_of_type(mapping, value_type)
1220-
1221-
12221201
def _infer_subgroup_key_to_use_from_config(
12231202
default_in_config: dict[str, Any],
12241203
# subgroup_default: Hashable,
@@ -1296,6 +1275,29 @@ def _infer_subgroup_key_to_use_from_config(
12961275
# }
12971276

12981277

1278+
def _has_values_of_type(
1279+
mapping: Mapping[K, Any], value_type: type[V] | tuple[type[V], ...]
1280+
) -> TypeGuard[Mapping[K, V]]:
1281+
# Utility functions used to narrow the type of dictionaries.
1282+
return all(isinstance(v, value_type) for v in mapping.values())
1283+
1284+
1285+
def _has_keys_of_type(
1286+
mapping: Mapping[Any, V], key_type: type[K] | tuple[type[K], ...]
1287+
) -> TypeGuard[Mapping[K, V]]:
1288+
# Utility functions used to narrow the type of dictionaries.
1289+
return all(isinstance(k, key_type) for k in mapping.keys())
1290+
1291+
1292+
def _has_items_of_type(
1293+
mapping: Mapping[Any, Any],
1294+
item_type: tuple[type[K] | tuple[type[K], ...], type[V] | tuple[type[V], ...]],
1295+
) -> TypeGuard[Mapping[K, V]]:
1296+
# Utility functions used to narrow the type of a dictionary or mapping.
1297+
key_type, value_type = item_type
1298+
return _has_keys_of_type(mapping, key_type) and _has_values_of_type(mapping, value_type)
1299+
1300+
12991301
def _default_constructor_argument_values(
13001302
some_dataclass_type: type[Dataclass] | functools.partial[Dataclass],
13011303
) -> PossiblyNestedDict[str, Any]:

0 commit comments

Comments
 (0)