| 11 | |
| 12 | |
| 13 | def _is_main_guard_test(node: ast.AST) -> bool: |
| 14 | if not isinstance(node, ast.Compare): |
| 15 | return False |
| 16 | if len(node.ops) != 1 or not isinstance(node.ops[0], ast.Eq): |
| 17 | return False |
| 18 | if len(node.comparators) != 1: |
| 19 | return False |
| 20 | |
| 21 | left = node.left |
| 22 | right = node.comparators[0] |
| 23 | |
| 24 | left_is_name = isinstance(left, ast.Name) and left.id == "__name__" |
| 25 | right_is_main = ( |
| 26 | isinstance(right, ast.Constant) |
| 27 | and isinstance(right.value, str) |
| 28 | and right.value == "__main__" |
| 29 | ) |
| 30 | if left_is_name and right_is_main: |
| 31 | return True |
| 32 | |
| 33 | right_is_name = isinstance(right, ast.Name) and right.id == "__name__" |
| 34 | left_is_main = ( |
| 35 | isinstance(left, ast.Constant) |
| 36 | and isinstance(left.value, str) |
| 37 | and left.value == "__main__" |
| 38 | ) |
| 39 | return right_is_name and left_is_main |
| 40 | |
| 41 | |
| 42 | def has_main_guard(entrypoint_abs: str | None) -> bool: |