def _convert_member_type(member_type, value): if member_type == dict: return DictValueComponent.create(value).val try: return member_type(value) except ValueError as error: raise ParseError(str(error))
def to_value_type(self, val_str, type_arg, member_type, dest): """Convert a string to a value of the option's type.""" if val_str is None: return None if type_arg == bool: return self._ensure_bool(val_str) try: if type_arg == list: return ListValueComponent.create(val_str, member_type=member_type) if type_arg == dict: return DictValueComponent.create(val_str) return type_arg(val_str) except (TypeError, ValueError) as e: raise ParseError( f"Error applying type '{type_arg.__name__}' to option value '{val_str}', " f"for option '--{dest}' in {self._scope_str()}: {e}" )
def to_value_type(self, val_str, type_arg, member_type, dest): """Convert a string to a value of the option's type.""" if val_str is None: return None if type_arg == bool: return self.ensure_bool(val_str) try: if type_arg == list: return ListValueComponent.create(val_str, member_type=member_type) if type_arg == dict: return DictValueComponent.create(val_str) return type_arg(val_str) except (TypeError, ValueError) as e: if issubclass(type_arg, Enum): choices = ", ".join(f"{choice.value}" for choice in type_arg) raise ParseError(f"Invalid choice '{val_str}'. Choose from: {choices}") raise ParseError( f"Error applying type '{type_arg.__name__}' to option value '{val_str}': {e}" )
def assert_dict_parsed(s: str, *, expected: ParsedDict) -> None: assert expected == DictValueComponent.create(s).val
def _convert_member_type(t, x): if t == dict: return DictValueComponent.create(x).val return t(x)
def _convert_member_type(member_type, value): if member_type == dict: return DictValueComponent.create(value).val return member_type(value)