Skip to content

Commit f1521d7

Browse files
fix: resolve pre-existing mypy errors on files touched by this PR
The prek mypy hook runs on changed files and bypasses the pyproject.toml tests/ exclude, surfacing pre-existing errors in both context.py and test_context.py that block CI for this PR. Fixes applied: - Import Language from language_enum instead of base (base re-exports are not explicit; strict mypy flags attr-defined) - Annotate _extract_class_declaration, _import_to_statement, get_java_imported_type_skeletons, and resolved_imports - Guard None start/end_line in _extract_function_source_by_lines and find_helper_functions; guard None file_path in the import skeleton loop - Drop unreachable `if not node: continue` in _extract_public_method_signatures (JavaMethodNode.node is non-nullable) - Add -> None to every test method and fix an `int | None` comparison in test_context.py All 880 Java tests pass after the change.
1 parent e95f701 commit f1521d7

2 files changed

Lines changed: 116 additions & 105 deletions

File tree

codeflash/languages/java/context.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111
from typing import TYPE_CHECKING
1212

1313
from codeflash.code_utils.code_utils import encoded_tokens_len
14-
from codeflash.languages.base import CodeContext, HelperFunction, Language
14+
from codeflash.languages.base import CodeContext, HelperFunction
1515
from codeflash.languages.java.discovery import discover_functions_from_source
1616
from codeflash.languages.java.import_resolver import JavaImportResolver, find_helper_files
1717
from codeflash.languages.java.parser import get_java_analyzer
18+
from codeflash.languages.language_enum import Language
1819

1920
if TYPE_CHECKING:
2021
from pathlib import Path
2122

2223
from tree_sitter import Node
2324

2425
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
25-
from codeflash.languages.java.parser import JavaAnalyzer, JavaMethodNode
26+
from codeflash.languages.java.import_resolver import ResolvedImport
27+
from codeflash.languages.java.parser import JavaAnalyzer, JavaImportInfo, JavaMethodNode
2628

2729
logger = logging.getLogger(__name__)
2830

@@ -360,7 +362,7 @@ def _extract_type_declaration(type_node: Node, source_bytes: bytes, type_kind: s
360362

361363

362364
# Keep old function name for backwards compatibility
363-
def _extract_class_declaration(node, source_bytes):
365+
def _extract_class_declaration(node: Node, source_bytes: bytes) -> str:
364366
return _extract_type_declaration(node, source_bytes, "class")
365367

366368

@@ -629,6 +631,8 @@ def _extract_function_source_by_lines(source: str, function: FunctionToOptimize)
629631

630632
start_line = function.doc_start_line or function.starting_line
631633
end_line = function.ending_line
634+
if start_line is None or end_line is None:
635+
return ""
632636

633637
# Convert from 1-indexed to 0-indexed
634638
start_idx = start_line - 1
@@ -672,6 +676,8 @@ def find_helper_functions(
672676
func_id = f"{file_path}:{func.qualified_name}"
673677
if func_id not in visited_functions:
674678
visited_functions.add(func_id)
679+
if func.starting_line is None or func.ending_line is None:
680+
continue
675681

676682
# Extract the function source using tree-sitter for resilient lookup
677683
func_source = extract_function_source(source, func, analyzer=analyzer)
@@ -795,7 +801,7 @@ def extract_read_only_context(source: str, function: FunctionToOptimize, analyze
795801
return "\n".join(context_parts)
796802

797803

798-
def _import_to_statement(import_info) -> str:
804+
def _import_to_statement(import_info: JavaImportInfo) -> str:
799805
"""Convert a JavaImportInfo to an import statement string.
800806
801807
Args:
@@ -898,7 +904,11 @@ def _extract_type_names_from_code(code: str, analyzer: JavaAnalyzer) -> set[str]
898904

899905

900906
def get_java_imported_type_skeletons(
901-
imports: list, project_root: Path, module_root: Path | None, analyzer: JavaAnalyzer, target_code: str = ""
907+
imports: list[JavaImportInfo],
908+
project_root: Path,
909+
module_root: Path | None,
910+
analyzer: JavaAnalyzer,
911+
target_code: str = "",
902912
) -> str:
903913
"""Extract type skeletons for project-internal imported types.
904914
@@ -933,7 +943,7 @@ def get_java_imported_type_skeletons(
933943
priority_types = _extract_type_names_from_code(target_code, analyzer)
934944

935945
# Pre-resolve all imports, expanding wildcards into individual types
936-
resolved_imports: list = []
946+
resolved_imports: list[ResolvedImport] = []
937947
for imp in imports:
938948
if imp.is_wildcard:
939949
# First try unfiltered expansion with a cap. If the package is small enough, take all types.
@@ -978,7 +988,7 @@ def get_java_imported_type_skeletons(
978988

979989
for resolved in resolved_imports:
980990
class_name = resolved.class_name
981-
if not class_name:
991+
if not class_name or resolved.file_path is None:
982992
continue
983993

984994
dedup_key = (str(resolved.file_path), class_name)
@@ -1100,8 +1110,6 @@ def _extract_public_method_signatures(source: str, class_name: str, analyzer: Ja
11001110
continue
11011111

11021112
node = method.node
1103-
if not node:
1104-
continue
11051113

11061114
# Check if the method is public
11071115
is_public = False

0 commit comments

Comments
 (0)