Fixes the docstring of an object by replacing its arguments documentaiton by the one matched with the signature. Args: obj (`Any`): The object whose dostring we are fixing. old_doc_args (`str`): The current documentation of the parameters of `obj` in
(obj: Any, old_doc_args: str, new_doc_args: str)
| 891 | |
| 892 | |
| 893 | def fix_docstring(obj: Any, old_doc_args: str, new_doc_args: str): |
| 894 | """ |
| 895 | Fixes the docstring of an object by replacing its arguments documentaiton by the one matched with the signature. |
| 896 | |
| 897 | Args: |
| 898 | obj (`Any`): |
| 899 | The object whose dostring we are fixing. |
| 900 | old_doc_args (`str`): |
| 901 | The current documentation of the parameters of `obj` in the docstring (as returned by |
| 902 | `match_docstring_with_signature`). |
| 903 | new_doc_args (`str`): |
| 904 | The documentation of the parameters of `obj` matched with its signature (as returned by |
| 905 | `match_docstring_with_signature`). |
| 906 | """ |
| 907 | # Read the docstring in the source code and make sure we have the right part of the docstring |
| 908 | source, line_number = inspect.getsourcelines(obj) |
| 909 | |
| 910 | # Get to the line where we start documenting arguments |
| 911 | idx = 0 |
| 912 | while idx < len(source) and _re_args.search(source[idx]) is None: |
| 913 | idx += 1 |
| 914 | |
| 915 | if idx == len(source): |
| 916 | # Args are not defined in the docstring of this object |
| 917 | return |
| 918 | |
| 919 | # Get to the line where we stop documenting arguments |
| 920 | indent = find_indent(source[idx]) |
| 921 | idx += 1 |
| 922 | start_idx = idx |
| 923 | while idx < len(source) and (len(source[idx].strip()) == 0 or find_indent(source[idx]) > indent): |
| 924 | idx += 1 |
| 925 | |
| 926 | idx -= 1 |
| 927 | while len(source[idx].strip()) == 0: |
| 928 | idx -= 1 |
| 929 | idx += 1 |
| 930 | |
| 931 | if "".join(source[start_idx:idx])[:-1] != old_doc_args: |
| 932 | # Args are not fully defined in the docstring of this object |
| 933 | return |
| 934 | |
| 935 | obj_file = find_source_file(obj) |
| 936 | with open(obj_file, "r", encoding="utf-8") as f: |
| 937 | content = f.read() |
| 938 | |
| 939 | # Replace content |
| 940 | lines = content.split("\n") |
| 941 | lines = lines[: line_number + start_idx - 1] + [new_doc_args] + lines[line_number + idx - 1 :] |
| 942 | |
| 943 | print(f"Fixing the docstring of {obj.__name__} in {obj_file}.") |
| 944 | with open(obj_file, "w", encoding="utf-8") as f: |
| 945 | f.write("\n".join(lines)) |
| 946 | |
| 947 | |
| 948 | def check_docstrings(overwrite: bool = False, check_all: bool = False): |
no test coverage detected