Skip to content

Commit b51c9d5

Browse files
committed
fix ast deprecations for python 3.14: repalce node.n by node.value and use ast.Constant
More verbose exceptions in `fix_docstring` on docstring formatting issues.
1 parent 89b35be commit b51c9d5

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

utils/check_docstrings.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ def eval_math_expression(expression: str) -> Optional[Union[float, int]]:
670670

671671

672672
def eval_node(node):
673-
if isinstance(node, ast.Num): # <number>
674-
return node.n
673+
if isinstance(node, ast.Constant): # <number>
674+
return node.value
675675
elif isinstance(node, ast.BinOp): # <left> <operator> <right>
676676
return MATH_OPERATORS[type(node.op)](eval_node(node.left), eval_node(node.right))
677677
elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
@@ -931,7 +931,12 @@ def fix_docstring(obj: Any, old_doc_args: str, new_doc_args: str):
931931

932932
if idx == len(source):
933933
# Args are not defined in the docstring of this object
934-
return
934+
obj_file = find_source_file(obj)
935+
raise ValueError(
936+
f"Cannot fix docstring of {obj.__name__} in {obj_file} because no argument section was found in the docstring. "
937+
f"The docstring should contain a section starting with 'Args:', 'Arguments:', 'Parameters:', or similar. "
938+
f"Current docstring:\n{obj.__doc__[:200]}{'...' if len(obj.__doc__) > 200 else ''}"
939+
)
935940

936941
# Get to the line where we stop documenting arguments
937942
indent = find_indent(source[idx])
@@ -947,7 +952,17 @@ def fix_docstring(obj: Any, old_doc_args: str, new_doc_args: str):
947952

948953
if "".join(source[start_idx:idx])[:-1] != old_doc_args:
949954
# Args are not fully defined in the docstring of this object
950-
return
955+
obj_file = find_source_file(obj)
956+
actual_args_section = "".join(source[start_idx:idx])[:-1]
957+
raise ValueError(
958+
f"Cannot fix docstring of {obj.__name__} in {obj_file} because the argument section in the source code "
959+
f"does not match the expected format. This usually happens when:\n"
960+
f"1. The argument section is not properly indented\n"
961+
f"2. The argument section contains unexpected formatting\n"
962+
f"3. The docstring parsing failed to correctly identify the argument boundaries\n\n"
963+
f"Expected argument section:\n{repr(old_doc_args)}\n\n"
964+
f"Actual argument section found:\n{repr(actual_args_section)}\n\n"
965+
)
951966

952967
obj_file = find_source_file(obj)
953968
with open(obj_file, "r", encoding="utf-8") as f:

0 commit comments

Comments
 (0)