From 86502978668556f4776ea3599c92c8ab2d7188ca Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Thu, 2 Oct 2025 00:51:23 +0530 Subject: [PATCH 1/5] make type checks viable --- .pre-commit-config.yaml | 17 ++++++++--------- pythonbpf/codegen.py | 2 +- pythonbpf/functions_pass.py | 4 ++-- pythonbpf/maps/maps_utils.py | 6 +++++- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3972f3e..89c6a80 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -41,16 +41,15 @@ repos: - id: ruff args: ["--fix", "--show-fixes"] - id: ruff-format - exclude: ^(docs) + exclude: ^(docs)|^(tests)|^(examples) -## Checking static types -#- repo: https://github.com/pre-commit/mirrors-mypy -# rev: "v1.10.0" -# hooks: -# - id: mypy -# files: "setup.py" -# args: [] -# additional_dependencies: [types-setuptools] +# Checking static types +- repo: https://github.com/pre-commit/mirrors-mypy + rev: "v1.10.0" + hooks: + - id: mypy + exclude: ^(tests)|^(examples) + additional_dependencies: [types-setuptools] # Changes tabs to spaces - repo: https://github.com/Lucas-C/pre-commit-hooks diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index ff20422..f7dfaa7 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -141,7 +141,7 @@ def compile() -> bool: success = True success = compile_to_ir(str(caller_file), str(ll_file)) and success - success = ( + success = bool( subprocess.run( [ "llc", diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 2aef65f..0533431 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -1,13 +1,13 @@ from llvmlite import ir import ast - +from typing import Any from .bpf_helper_handler import helper_func_list, handle_helper_call from .type_deducer import ctypes_to_ir from .binary_ops import handle_binary_op from .expr_pass import eval_expr, handle_expr -local_var_metadata = {} +local_var_metadata: dict[str | Any, Any] = {} def get_probe_string(func_node): diff --git a/pythonbpf/maps/maps_utils.py b/pythonbpf/maps/maps_utils.py index c7f8203..ee3ad08 100644 --- a/pythonbpf/maps/maps_utils.py +++ b/pythonbpf/maps/maps_utils.py @@ -1,7 +1,11 @@ +from collections.abc import Callable +from typing import Any + + class MapProcessorRegistry: """Registry for map processor functions""" - _processors = {} + _processors: dict[str, Callable[..., Any]] = {} @classmethod def register(cls, map_type_name): From 3c956e671a36e402f027f858ec61a976f30b0875 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Thu, 2 Oct 2025 01:11:54 +0530 Subject: [PATCH 2/5] add static type checking Signed-off-by: varun-r-mallya --- pythonbpf/functions_pass.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index 0533431..e1a5b1c 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -608,7 +608,7 @@ def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab): ) -def infer_return_type(func_node: ast.FunctionDef): +def infer_return_type(func_node: ast.FunctionDef) -> str | None | Any: if not isinstance(func_node, (ast.FunctionDef, ast.AsyncFunctionDef)): raise TypeError("Expected ast.FunctionDef") if func_node.returns is not None: @@ -656,9 +656,9 @@ def _expr_type(e): except Exception: return type(e).__name__ - for node in ast.walk(func_node): - if isinstance(node, ast.Return): - t = _expr_type(node.value) + for walked_node in ast.walk(func_node): + if isinstance(walked_node, ast.Return): + t = _expr_type(walked_node.value) if found_type is None: found_type = t elif found_type != t: From cf5faaad7fc10d8abbcd17eaeafc7105409ebb84 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Thu, 2 Oct 2025 01:27:03 +0530 Subject: [PATCH 3/5] remove pointless type annotation Signed-off-by: varun-r-mallya --- pythonbpf/functions_pass.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonbpf/functions_pass.py b/pythonbpf/functions_pass.py index e1a5b1c..aaebff0 100644 --- a/pythonbpf/functions_pass.py +++ b/pythonbpf/functions_pass.py @@ -608,7 +608,7 @@ def func_proc(tree, module, chunks, map_sym_tab, structs_sym_tab): ) -def infer_return_type(func_node: ast.FunctionDef) -> str | None | Any: +def infer_return_type(func_node: ast.FunctionDef): if not isinstance(func_node, (ast.FunctionDef, ast.AsyncFunctionDef)): raise TypeError("Expected ast.FunctionDef") if func_node.returns is not None: From 8792740eb022b0dc2b105c705d0f25bc0745cf50 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Thu, 2 Oct 2025 01:36:14 +0530 Subject: [PATCH 4/5] workflow update --- .github/workflows/format.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 687854b..4c8893e 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -7,8 +7,6 @@ on: workflow_dispatch: pull_request: push: - branches: - - master jobs: pre-commit: From c92272dd35feb15d3edad8fb1e2ddbfc23f4dd27 Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Thu, 2 Oct 2025 01:36:14 +0530 Subject: [PATCH 5/5] workflow update --- .github/workflows/format.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 4c8893e..dee65eb 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -5,7 +5,6 @@ name: Format on: workflow_dispatch: - pull_request: push: jobs: