diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-07-01 10:16:04 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-07-01 10:16:04 +0300 |
| commit | 7c6285c2ac4740652d31c0c68adfff8671574602 (patch) | |
| tree | 434e644a91958720d3d058f19dd4099898a9a9fd /scripts | |
| parent | c7868a49b8d444a4a43b3fd1ad1cee82eb59c2ab (diff) | |
| download | fungus-launcher-7c6285c2ac4740652d31c0c68adfff8671574602.tar.gz fungus-launcher-7c6285c2ac4740652d31c0c68adfff8671574602.tar.xz | |
add support for mixins, run signal check in a separate job
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/check_handlers.py | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/scripts/check_handlers.py b/scripts/check_handlers.py index 0bc2b8f..3cc655e 100644 --- a/scripts/check_handlers.py +++ b/scripts/check_handlers.py @@ -19,11 +19,23 @@ from pathlib import Path # --------------------------------------------------------------------------- -# AST-based class / method extraction +# AST-based class / method extraction (with inheritance resolution) # --------------------------------------------------------------------------- +def _get_base_names(node: ast.ClassDef) -> list[str]: + """Extract simple identifier base class names from a class definition.""" + bases: list[str] = [] + for base in node.bases: + if isinstance(base, ast.Name): + bases.append(base.id) + elif isinstance(base, ast.Attribute): + # e.g. Gtk.Dialog — grab the final attribute name + bases.append(base.attr) + return bases + + def _extract_classes(tree: ast.Module): - """Yield (class_name, list of method names) for all top-level classes.""" + """Yield (class_name, set of method names) for all top-level classes.""" for node in ast.iter_child_nodes(tree): if isinstance(node, ast.ClassDef): methods = { @@ -32,6 +44,30 @@ def _extract_classes(tree: ast.Module): yield node.name, methods +def _collect_inherited_methods( + class_name: str, + all_classes: dict[str, set[str]], + file_classes: dict[str, set[str]], + class_bases: dict[str, list[str]], + visited: set[str] | None = None, +) -> set[str]: + """Collect methods from *class_name* and every ancestor.""" + if visited is None: + visited = set() + if class_name in visited: + return set() + visited.add(class_name) + + cls_methods = all_classes.get(class_name) or file_classes.get(class_name) + if cls_methods is None: + return set() + + result = set(cls_methods) + for base_name in class_bases.get(class_name, []): + result |= _collect_inherited_methods(base_name, all_classes, file_classes, class_bases, visited) + return result + + # --------------------------------------------------------------------------- # Regex-based connect() extraction (works across modules without gi imported) # --------------------------------------------------------------------------- @@ -89,19 +125,29 @@ def validate_file(filepath: Path, all_classes: dict[str, set[str]], assigned_widgets = file_widgets.get(filepath, set()) - # Build class -> methods map for this file + # Build class -> methods map for this file, and collect bases try: tree = ast.parse(source, filename=str(filepath)) except SyntaxError as exc: return [f"{filepath}: syntax error — {exc}"] - file_classes = {cls_name: methods for cls_name, methods in _extract_classes(tree)} + file_classes: dict[str, set[str]] = {} + class_bases: dict[str, list[str]] = {} + for node in ast.iter_child_nodes(tree): + if isinstance(node, ast.ClassDef): + methods = { + n.name for n in node.body if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef)) + } + file_classes[node.name] = methods + class_bases[node.name] = _get_base_names(node) for lineno, widget_attr, signal, handler_method in connect_calls: - # Check: does the handler method exist on any class in this file? + # Check: does the handler method exist on any class in this file, + # including methods inherited from mixins / base classes? found_handler = False - for cls_methods in file_classes.values(): - if handler_method in cls_methods: + for cls_name in file_classes: + inherited = _collect_inherited_methods(cls_name, all_classes, file_classes, class_bases) + if handler_method in inherited: found_handler = True break |
