def test_no_attributes_set_in_init(estimator, preprocessor): """Check setting during init. Adapted from scikit-learn.""" estimator = clone(estimator) estimator.set_params(preprocessor=preprocessor) if hasattr(type(estimator).__init__, "deprecated_original"): return init_params = _get_args(type(estimator).__init__) parents_init_params = [ param for params_parent in (_get_args(parent) for parent in type(estimator).__mro__) for param in params_parent ] # Test for no setting apart from parameters during init invalid_attr = (set(vars(estimator)) - set(init_params) - set(parents_init_params)) assert not invalid_attr, \ ("Estimator %s should not set any attribute apart" " from parameters during init. Found attributes %s." % (type(estimator).__name__, sorted(invalid_attr))) # Ensure that each parameter is set in init invalid_attr = (set(init_params) - set(vars(estimator)) - set(["self"])) assert not invalid_attr, \ ("Estimator %s should store all parameters" " as an attribute during init. Did not find " "attributes %s." % (type(estimator).__name__, sorted(invalid_attr)))
def test_no_attributes_set_in_init(estimator, preprocessor): """Check setting during init. Adapted from scikit-learn.""" estimator = clone(estimator) estimator.set_params(preprocessor=preprocessor) if hasattr(type(estimator).__init__, "deprecated_original"): return init_params = _get_args(type(estimator).__init__) parents_init_params = [param for params_parent in (_get_args(parent) for parent in type(estimator).__mro__) for param in params_parent] # Test for no setting apart from parameters during init invalid_attr = (set(vars(estimator)) - set(init_params) - set(parents_init_params)) assert not invalid_attr, \ ("Estimator %s should not set any attribute apart" " from parameters during init. Found attributes %s." % (type(estimator).__name__, sorted(invalid_attr))) # Ensure that each parameter is set in init invalid_attr = (set(init_params) - set(vars(estimator)) - set(["self"])) assert not invalid_attr, \ ("Estimator %s should store all parameters" " as an attribute during init. Did not find " "attributes %s." % (type(estimator).__name__, sorted(invalid_attr)))
def check_docstring_parameters(func, doc=None, ignore=None, class_name=None): """Helper to check docstring Parameters ---------- func : callable The function object to test. doc : str, optional (default: None) Docstring if it is passed manually to the test. ignore : None | list Parameters to ignore. class_name : string, optional (default: None) If ``func`` is a class method and the class name is known specify class_name for the error message. Returns ------- incorrect : list A list of string describing the incorrect results. """ from numpydoc import docscrape incorrect = [] ignore = [] if ignore is None else ignore func_name = _get_func_name(func, class_name=class_name) if not func_name.startswith('sklearn_lvq.'): return incorrect # Don't check docstring for property-functions if inspect.isdatadescriptor(func): return incorrect args = list(filter(lambda x: x not in ignore, _get_args(func))) # drop self if len(args) > 0 and args[0] == 'self': args.remove('self') if doc is None: with warnings.catch_warnings(record=True) as w: try: doc = docscrape.FunctionDoc(func) except Exception as exp: incorrect += [func_name + ' parsing error: ' + str(exp)] return incorrect if len(w): raise RuntimeError('Error for %s:\n%s' % (func_name, w[0])) param_names = [] for name, type_definition, param_doc in doc['Parameters']: if (type_definition.strip() == "" or type_definition.strip().startswith(':')): param_name = name.lstrip() # If there was no space between name and the colon # "verbose:" -> len(["verbose", ""][0]) -> 7 # If "verbose:"[7] == ":", then there was no space if param_name[len(param_name.split(':')[0].strip())] == ':': incorrect += [func_name + ' There was no space between the param name and ' 'colon ("%s")' % name] else: incorrect += [func_name + ' Incorrect type definition for ' 'param: "%s" (type definition was "%s")' % (name.split(':')[0], type_definition)] if '*' not in name: param_names.append(name.split(':')[0].strip('` ')) param_names = list(filter(lambda x: x not in ignore, param_names)) if len(param_names) != len(args): bad = str(sorted(list(set(param_names) ^ set(args)))) incorrect += [func_name + ' arg mismatch: ' + bad] else: for n1, n2 in zip(param_names, args): if n1 != n2: incorrect += [func_name + ' ' + n1 + ' != ' + n2] return incorrect