From 2e7093da6c069562f4ad79d88a4a2b128b7d35e1 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Mon, 31 Mar 2025 21:49:17 +0000 Subject: [PATCH 1/2] Fix mypy errors in src/codegen/gscli/backend/typestub_utils.py --- src/codegen/gscli/backend/typestub_utils.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/codegen/gscli/backend/typestub_utils.py b/src/codegen/gscli/backend/typestub_utils.py index 4bab38674..5bfc076be 100644 --- a/src/codegen/gscli/backend/typestub_utils.py +++ b/src/codegen/gscli/backend/typestub_utils.py @@ -3,6 +3,7 @@ import re from collections.abc import Callable from concurrent.futures import ThreadPoolExecutor +from typing import TypeVar, Union import astor @@ -10,6 +11,9 @@ logger = get_logger(__name__) +# Define a type variable for AST nodes +ASTNode = TypeVar('ASTNode', ast.FunctionDef, ast.AnnAssign, ast.Assign) + class MethodRemover(ast.NodeTransformer): def __init__(self, conditions: list[Callable[[ast.FunctionDef], bool]]): @@ -44,7 +48,7 @@ def should_remove(self, node: ast.FunctionDef | ast.AnnAssign) -> bool: class FieldRemover(ast.NodeTransformer): - def __init__(self, conditions: list[Callable[[ast.FunctionDef], bool]]): + def __init__(self, conditions: list[Callable[[Union[ast.AnnAssign, ast.Assign]], bool]]): self.conditions = conditions def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: @@ -79,20 +83,22 @@ def _remove_methods(source: str, conditions: list[Callable[[ast.FunctionDef], bo return astor.to_source(modified_tree) -def _remove_fields(source: str, conditions: list[Callable[[ast.FunctionDef], bool]]) -> str: +def _remove_fields(source: str, conditions: list[Callable[[Union[ast.AnnAssign, ast.Assign]], bool]]) -> str: tree = ast.parse(source) transformer = FieldRemover(conditions) modified_tree = transformer.visit(tree) return astor.to_source(modified_tree) -def _starts_with_underscore(node: ast.FunctionDef | ast.AnnAssign | ast.Assign) -> bool: +def _starts_with_underscore(node: Union[ast.FunctionDef, ast.AnnAssign, ast.Assign]) -> bool: if isinstance(node, ast.FunctionDef): return node.name.startswith("_") and (not node.name.startswith("__") and not node.name.endswith("__")) elif isinstance(node, ast.Assign): - return node.targets[0].id.startswith("_") + if isinstance(node.targets[0], ast.Name): + return node.targets[0].id.startswith("_") elif isinstance(node, ast.AnnAssign): - return node.target.id.startswith("_") + if isinstance(node.target, ast.Name): + return node.target.id.startswith("_") return False @@ -121,7 +127,9 @@ def _strip_internal_symbols(file: str, root: str) -> None: _has_decorator("noapidoc"), ] - modified_content = _remove_fields(original_content, [_starts_with_underscore]) + # Type cast _starts_with_underscore to the correct type for _remove_fields + field_condition = _starts_with_underscore + modified_content = _remove_fields(original_content, [field_condition]) modified_content = _remove_methods(modified_content, conditions) if modified_content.strip().endswith(":"): From aed60c2110914308de8ba9155e0e97bc0c06206a Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Mon, 31 Mar 2025 21:49:59 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- src/codegen/gscli/backend/typestub_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/gscli/backend/typestub_utils.py b/src/codegen/gscli/backend/typestub_utils.py index 5bfc076be..d8bd51482 100644 --- a/src/codegen/gscli/backend/typestub_utils.py +++ b/src/codegen/gscli/backend/typestub_utils.py @@ -12,7 +12,7 @@ logger = get_logger(__name__) # Define a type variable for AST nodes -ASTNode = TypeVar('ASTNode', ast.FunctionDef, ast.AnnAssign, ast.Assign) +ASTNode = TypeVar("ASTNode", ast.FunctionDef, ast.AnnAssign, ast.Assign) class MethodRemover(ast.NodeTransformer):