예제 #1
0
def consistency_check(obj, obj_name):
    patt = re.compile(r"(?<=>`)(.*?)(?=[\(|`:])")
    doc = format_doc(obj)
    try:
        arg_list_index = doc.index("**Args**:")
        end = doc[arg_list_index:].find("</ul")
        arg_doc = doc[arg_list_index:(arg_list_index + end)]
        doc_args = {arg.strip() for arg in patt.findall(arg_doc)}
    except ValueError:
        doc_args = set()

    items = get_call_signature(obj)
    actual_args = {(a if isinstance(a, str) else a[0]) for a in items}

    undocumented = actual_args.difference(doc_args)
    # If the sig contains **kwargs, any keyword is valid
    if any(k.startswith("**") for k in actual_args):
        non_existent = {}
    else:
        non_existent = doc_args.difference(actual_args)

    if undocumented:
        undoc_args = ", ".join(undocumented)
        raise ValueError(
            f"{obj_name} has arguments without documentation: {undoc_args}")
    elif non_existent:
        undoc_args = ", ".join(non_existent)
        raise ValueError(
            f"{obj_name} has documentation for arguments that aren't real: {undoc_args}"
        )
예제 #2
0
def consistency_check(obj, obj_name):
    patt = re.compile(r"(?<=>`)(.*?)(?=[\(|`:])")
    doc = format_doc(obj)
    try:
        arg_list_index = doc.index("**Args**:")
        end = doc[arg_list_index:].find("</ul")
        arg_doc = doc[arg_list_index : (arg_list_index + end)]
        doc_args = {arg.strip() for arg in patt.findall(arg_doc)}
    except ValueError:
        doc_args = set()

    standalone, varargs, kwonly, kwargs, varkwargs = get_call_signature(obj)
    actual_args = (
        set()
        .union(standalone)
        .union(varargs)
        .union([k for k, v in kwonly])
        .union([k for k, v in kwargs])
        .union(varkwargs)
    )

    if actual_args.intersection(doc_args) < actual_args:
        undoc_args = ", ".join(actual_args.difference(doc_args))
        raise ValueError(
            f"{obj_name} has arguments without documentation: {undoc_args}"
        )
    elif doc_args.intersection(actual_args) < doc_args:
        undoc_args = ", ".join(doc_args.difference(actual_args))
        raise ValueError(
            f"{obj_name} has documentation for arguments that aren't real: {undoc_args}"
        )