Exemplo n.º 1
0
def validate_object(import_path: str) -> list:
    """
    Check docstrings of an entity that can be imported.

    Parameters
    ----------
    import_path : str
        Python-like import path.

    Returns
    -------
    errors : list
        List with string representations of errors.
    """
    from numpydoc.validate import validate

    errors = []
    doc = Docstring(import_path)
    results = validate(import_path)
    results = validate_modin_error(doc, results)
    noqa_checks = get_noqa_checks(doc)
    for err_code, err_desc in results["errors"]:
        if (err_code not in NUMPYDOC_BASE_ERROR_CODES
                and err_code not in MODIN_ERROR_CODES) or skip_check_if_noqa(
                    doc, err_code, noqa_checks):
            continue
        errors.append(":".join(
            [import_path,
             str(results["file_line"]), err_code, err_desc]))
    return errors
Exemplo n.º 2
0
    def validate(self, from_package='', allow_rules=None, disallow_rules=None):
        results = []

        def callback(obj):
            try:
                result = validate(obj)
            except OSError as e:
                symbol = f"{obj.__module__}.{obj.__name__}"
                logger.warning(f"Unable to validate `{symbol}` due to `{e}`")
                return

            errors = []
            for errcode, errmsg in result.get('errors', []):
                if allow_rules and errcode not in allow_rules:
                    continue
                if disallow_rules and errcode in disallow_rules:
                    continue
                errors.append((errcode, errmsg))

            if len(errors):
                result['errors'] = errors
                results.append((obj, result))

        with self._apply_patches():
            for symbol in self.symbols:
                try:
                    obj = Docstring._load_obj(symbol)
                except (ImportError, AttributeError):
                    print('{} is not available for import'.format(symbol))
                else:
                    self.traverse(callback, obj, from_package=from_package)

        return results
Exemplo n.º 3
0
    def validate(self,
                 from_package='',
                 rules_blacklist=None,
                 rules_whitelist=None):
        results = []

        def callback(obj):
            result = validate(obj)

            errors = []
            for errcode, errmsg in result.get('errors', []):
                if rules_whitelist and errcode not in rules_whitelist:
                    continue
                if rules_blacklist and errcode in rules_blacklist:
                    continue
                errors.append((errcode, errmsg))

            if len(errors):
                result['errors'] = errors
                results.append((obj, result))

        with self._apply_patches():
            for symbol in self.symbols:
                try:
                    obj = Docstring._load_obj(symbol)
                except (ImportError, AttributeError):
                    print('{} is not available for import'.format(symbol))
                else:
                    self.traverse(callback, obj, from_package=from_package)

        return results
Exemplo n.º 4
0
def get_noqa_checks(doc: Docstring) -> list:
    """
    Get codes after `# noqa`.

    Parameters
    ----------
    doc : numpydoc.validate.Docstring
        Docstring handler.

    Returns
    -------
    list
        List with codes.

    Notes
    -----
    If noqa doesn't have any codes - returns ["all"].
    """
    source = doc.method_source
    # case when property decorator is used; it hides sources
    if not source and isinstance(doc.obj, property):
        new_doc = Docstring(doc.name)
        new_doc.obj = doc.obj.fget
        source = new_doc.method_source
        if not source:
            return []

    noqa_str = None
    if not inspect.ismodule(doc.obj):
        # find last line of obj definition
        for line in source.split("\n"):
            if ")" in line and ":" in line.split(")", 1)[1]:
                noqa_str = line
                break
    else:
        # pydocstyle only check first line; aling with it
        noqa_str = source.split("\n", 1)[0]

    if "# noqa:" in noqa_str:
        noqa_checks = noqa_str.split("# noqa:", 1)[1].split(",")
    elif "# noqa" in noqa_str:
        noqa_checks = ["all"]
    else:
        noqa_checks = []
    return [check.strip() for check in noqa_checks]
Exemplo n.º 5
0
def test_check_spelling_words(import_path, result):
    result_errors = []
    for code, line, word, reference in result:
        result_errors.append((
            code,
            MODIN_ERROR_CODES[code].format(line=line,
                                           word=word,
                                           reference=reference),
        ))
    errors = check_spelling_words(Docstring(import_path))
    # the order of incorrect words found on the same line is not guaranteed
    for error in errors:
        assert error in result_errors
Exemplo n.º 6
0
def test_check_optional_args(import_path, result):
    errors = check_optional_args(Docstring(import_path))
    assert errors == result
Exemplo n.º 7
0
def test_get_optional_args(import_path, result):
    optional_args = get_optional_args(Docstring(import_path))
    assert optional_args == result
Exemplo n.º 8
0
def test_get_noqa_checks(import_path, result):
    noqa_checks = get_noqa_checks(Docstring(import_path))
    assert noqa_checks == result
Exemplo n.º 9
0
def render_object(import_path, config=None):
    """Test numpydoc docstring generation for a given object"""
    # TODO: Move Docstring._load_obj to a better place than validate
    print(get_doc_object(Docstring(import_path).obj, config=dict(config or [])))
    return 0