[docs]defvalidate_not_nan(self:Any,attribute:Attribute,value:Any)->None:"""Attrs-compatible validator to forbid 'nan' values."""ifisinstance(value,float)andmath.isnan(value):raiseValueError(f"The value passed to attribute '{attribute.name}' of class "f"'{self.__class__.__name__}' cannot be 'nan'.")
def_make_restricted_float_validator(allow_nan:bool,allow_inf:bool)->Callable[[Any,Attribute,Any],None]:"""Make an attrs-compatible validator for restricted floats. Args: allow_nan: If False, validated values cannot be 'nan'. allow_inf: If False, validated values cannot be 'inf' or '-inf'. Raises: ValueError: If no float range restriction is in place. Returns: The validator. """ifallow_nanandallow_inf:raiseValueError("The requested validator would not restrict the float range. ""Hence, you can use `attrs.validators.instance_of(float)` instead.")defvalidator(self:Any,attribute:Attribute,value:Any)->None:ifnotisinstance(value,float):raiseValueError(f"Values assigned to attribute '{attribute.name}' of class "f"'{self.__class__.__name__}' must be of type 'float'. "f"Given: {value} (type: {type(value)})")ifnotallow_infandmath.isinf(value):raiseValueError(f"Values assigned to attribute '{attribute.name}' of class "f"'{self.__class__.__name__}' cannot be 'inf' or '-inf'.")ifnotallow_nanandmath.isnan(value):raiseValueError(f"Values assigned to attribute '{attribute.name}' of class "f"'{self.__class__.__name__}' cannot be 'nan'.")returnvalidatorfinite_float=_make_restricted_float_validator(allow_nan=False,allow_inf=False)"""Validator for finite (i.e., non-nan and non-infinite) floats."""non_nan_float=_make_restricted_float_validator(allow_nan=False,allow_inf=True)"""Validator for non-nan floats."""non_inf_float=_make_restricted_float_validator(allow_nan=True,allow_inf=False)"""Validator for non-infinite floats."""