def validate(cls, raw_config_vars: Optional[RawConfigVarsType] = None) -> Any: """ Validate the value obtained from the ``YAML`` file and coerce into the appropriate return type. :param raw_config_vars: Dictionary to obtain the value from. :rtype: See the :attr:`~.ConfigVar.rtype` attribute. """ if raw_config_vars is None: raw_config_vars = {} if cls.rtype is None: cls.rtype = cls.dtype if raw_config_vars is None: raw_config_vars = {} obj = optional_getter(raw_config_vars, cls, cls.required) if isinstance(obj, Dict): output = {} for k, v in obj.items(): if v is None: v = {} if not isinstance(v, dict): raise ValueError( f"'{cls.__name__}' must be a dictionary mapping strings to dictionaries" ) output[str(k)] = {str(kk): vv for kk, vv in v.items()} return output elif isinstance(obj, List): data = optional_getter(raw_config_vars, cls, cls.required) if isinstance(data, str) or not isinstance(data, Iterable): raise ValueError( f"'{cls.__name__}' must be a list of strings or floats") buf: Dict[str, Dict[str, Any]] = {} for ver in data: if not isinstance(ver, (str, float)): raise ValueError( f"Values in '{cls.__name__}' must be strings or floats" ) buf[str(ver)] = {} return buf else: raise ValueError( f"'{cls.__name__}' must be a dictionary mapping strings to dictionaries, or a list of strings" )
def validate(cls, raw_config_vars: Optional[Dict[str, Any]] = None): # noqa: D102 if raw_config_vars is None: raw_config_vars = {} if cls.rtype is None: cls.rtype = cls.dtype classifier_list = set() data = optional_getter(raw_config_vars, cls, cls.required) if isinstance(data, str) or not isinstance(data, Iterable): raise ValueError( f"'classifiers' must be a List of {cls.dtype.__args__[0]}" ) from None # type: ignore for classifier in data: if not isinstance(classifier, str): raise ValueError( f"'classifiers' must be a List of {cls.dtype.__args__[0]}" # type: ignore ) from None for classifier in data: classifier_list.add(classifier) validate_classifiers(classifier_list) return natsorted(classifier_list)
def visit_list(self, raw_config_vars: RawConfigVarsType) -> List: """ Used to validate and convert :class:`list` values. :param raw_config_vars: """ # Lists of strings, numbers, Unions and Literals buf = [] data = optional_getter(raw_config_vars, self.config_var, self.config_var.required) if isinstance(data, str) or not isinstance(data, Iterable): raise ValueError( f"'{self.config_var.__name__}' must be a List of {self.config_var.dtype.__args__[0]}" ) from None if get_origin(self.config_var.dtype.__args__[0]) is Union: for obj in data: if not check_union(obj, self.config_var.dtype.__args__[0]): raise ValueError( f"'{self.config_var.__name__}' must be a " f"List of {self.config_var.dtype.__args__[0]}" ) from None elif is_literal_type(self.config_var.dtype.__args__[0]): for obj in data: # if isinstance(obj, str): # obj = obj.lower() if obj not in get_literal_values( self.config_var.dtype.__args__[0]): raise ValueError( f"Elements of '{self.config_var.__name__}' must be " f"one of {get_literal_values(self.config_var.dtype.__args__[0])}" ) from None else: for obj in data: if not check_union(obj, self.config_var.dtype): raise ValueError( f"'{self.config_var.__name__}' must be a List of {self.config_var.dtype.__args__[0]}" ) from None try: for obj in data: if self.config_var.rtype.__args__[0] in { int, str, float, bool }: buf.append(self.config_var.rtype.__args__[0](obj)) else: buf.append(obj) return buf except ValueError: raise ValueError( f"Values in '{self.config_var.__name__}' must be {self.config_var.rtype.__args__[0]}" ) from None
def validate(cls, raw_config_vars: Optional[Dict[str, Any]] = None): if raw_config_vars is None: raw_config_vars = {} if cls.rtype is None: cls.rtype = cls.dtype obj = optional_getter(raw_config_vars, cls, cls.required) if stubs_package.get(raw_config_vars): return cls.validator(obj) else: obj = optional_getter(raw_config_vars, cls, cls.required) if not isinstance(obj, cls.dtype): raise ValueError(f"'{cls.__name__}' must be a {cls.dtype}") from None return cls.rtype(obj)
def visit_dict(self, raw_config_vars: RawConfigVarsType) -> Dict: """ Used to validate and convert :class:`dict` values. :param raw_config_vars: """ # Dict[str, str] if self.config_var.dtype == Dict[str, str]: obj = optional_getter(raw_config_vars, self.config_var, self.config_var.required) if not isinstance(obj, dict): raise ValueError( f"'{self.config_var.__name__}' must be a dictionary" ) from None return {str(k): str(v) for k, v in obj.items()} # Dict[str, Any] elif self.config_var.dtype == Dict[str, Any]: obj = optional_getter(raw_config_vars, self.config_var, self.config_var.required) if not isinstance(obj, dict): raise ValueError( f"'{self.config_var.__name__}' must be a dictionary" ) from None return obj # Dict[str, List[str] elif self.config_var.dtype == Dict[str, List[str]]: obj = optional_getter(raw_config_vars, self.config_var, self.config_var.required) if not isinstance(obj, dict): raise ValueError( f"'{self.config_var.__name__}' must be a dictionary" ) from None return {str(k): [str(i) for i in v] for k, v in obj.items()} else: self.unknown_type()
def _visit_str_number( self, raw_config_vars: RawConfigVarsType) -> Union[str, int, float]: obj = optional_getter(raw_config_vars, self.config_var, self.config_var.required) if not isinstance(obj, self.config_var.dtype): raise ValueError( f"'{self.config_var.__name__}' must be a {self.config_var.dtype}" ) from None return obj
def visit_bool(self, raw_config_vars: RawConfigVarsType) -> bool: """ Used to validate and convert :class:`bool` values. :param raw_config_vars: """ obj = optional_getter(raw_config_vars, self.config_var, self.config_var.required) if not isinstance(obj, (int, bool, str)): raise ValueError( f"'{self.config_var.__name__}' must be one of {(int, bool, str)}, " f"not {type(obj)}") from None return self.config_var.rtype(strtobool(obj))
def visit_literal(self, raw_config_vars: RawConfigVarsType) -> Any: """ Used to validate and convert :class:`typing.Literal` values. :param raw_config_vars: """ obj = optional_getter(raw_config_vars, self.config_var, self.config_var.required) # if isinstance(obj, str): # obj = obj.lower() if obj not in get_literal_values(self.config_var.dtype): raise ValueError( f"'{self.config_var.__name__}' must be one of {get_literal_values(self.config_var.dtype)}" ) from None return obj
def visit_union(self, raw_config_vars: RawConfigVarsType) -> Any: """ Used to validate and convert :class:`typing.Union` values. :param raw_config_vars: """ obj = optional_getter(raw_config_vars, self.config_var, self.config_var.required) if not check_union(obj, self.config_var.dtype): raise ValueError( f"'{self.config_var.__name__}' must be one of {self.config_var.dtype.__args__}, " f"not {type(obj)}") from None try: return self.config_var.rtype(obj) except ValueError: raise ValueError( f"'{self.config_var.__name__}' must be {self.config_var.rtype.__args__}, " f"not {type(obj)}") from None
def get(cls, raw_config_vars: Optional[Dict[str, Any]] = None) -> Any: """ Returns the value of this :class:`~configconfig.configvar.ConfigVar` :param raw_config_vars: Dictionary to obtain the value from. :return: :rtype: See the ``rtype`` attribute. """ if raw_config_vars is None: raw_config_vars = {} if cls.rtype is None: cls.rtype = cls.dtype if stubs_package.get(raw_config_vars): name = cls.dtype(optional_getter(raw_config_vars, cls, cls.required)) if name.endswith("-stubs"): return name[:-6] return name else: return cls.validator(cls.validate(raw_config_vars))