|
4 | 4 | import os |
5 | 5 | from collections import defaultdict |
6 | 6 | from itertools import chain |
7 | | -from typing import TYPE_CHECKING |
| 7 | +from typing import TYPE_CHECKING, cast |
8 | 8 |
|
9 | 9 | import libcst as cst |
10 | 10 |
|
@@ -625,20 +625,24 @@ def prune_cst_for_code_hashing( # noqa: PLR0911 |
625 | 625 | if not isinstance(node.body, cst.IndentedBlock): |
626 | 626 | raise ValueError("ClassDef body is not an IndentedBlock") # noqa: TRY004 |
627 | 627 | class_prefix = f"{prefix}.{node.name.value}" if prefix else node.name.value |
628 | | - new_body = [] |
| 628 | + new_class_body: list[cst.CSTNode] = [] |
629 | 629 | found_target = False |
630 | 630 |
|
631 | 631 | for stmt in node.body.body: |
632 | 632 | if isinstance(stmt, cst.FunctionDef): |
633 | 633 | qualified_name = f"{class_prefix}.{stmt.name.value}" |
634 | 634 | if qualified_name in target_functions: |
635 | | - stmt_with_changes = stmt.with_changes(body=remove_docstring_from_body(stmt.body)) |
636 | | - new_body.append(stmt_with_changes) |
| 635 | + stmt_with_changes = stmt.with_changes( |
| 636 | + body=remove_docstring_from_body(cast("cst.IndentedBlock", stmt.body)) |
| 637 | + ) |
| 638 | + new_class_body.append(stmt_with_changes) |
637 | 639 | found_target = True |
638 | 640 | # If no target functions found, remove the class entirely |
639 | | - if not new_body or not found_target: |
| 641 | + if not new_class_body or not found_target: |
640 | 642 | return None, False |
641 | | - return node.with_changes(body=cst.IndentedBlock(new_body)) if new_body else None, found_target |
| 643 | + return node.with_changes( |
| 644 | + body=cst.IndentedBlock(cast("list[cst.BaseStatement]", new_class_body)) |
| 645 | + ) if new_class_body else None, found_target |
642 | 646 |
|
643 | 647 | # For other nodes, we preserve them only if they contain target functions in their children. |
644 | 648 | section_names = get_section_names(node) |
|
0 commit comments