Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions codeflash/code_utils/static_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from pydantic import BaseModel, ConfigDict, field_validator

from codeflash.models.models import FunctionParent

if TYPE_CHECKING:
from codeflash.models.models import FunctionParent

Expand Down Expand Up @@ -139,14 +141,20 @@ def get_first_top_level_function_or_method_ast(


def function_kind(node: ast.FunctionDef | ast.AsyncFunctionDef, parents: list[FunctionParent]) -> FunctionKind | None:
if not parents or parents[0].type in ["FunctionDef", "AsyncFunctionDef"]:
# cache parents[0].type to avoid repeated lookup
if not parents:
return FunctionKind.FUNCTION
parent_type = parents[0].type
if parent_type == "FunctionDef" or parent_type == "AsyncFunctionDef":
return FunctionKind.FUNCTION
if parents[0].type == "ClassDef":
if parent_type == "ClassDef":
# Use set for decorator comparison and break early
for decorator in node.decorator_list:
if isinstance(decorator, ast.Name):
if decorator.id == "classmethod":
name = decorator.id
if name == "classmethod":
return FunctionKind.CLASS_METHOD
if decorator.id == "staticmethod":
if name == "staticmethod":
return FunctionKind.STATIC_METHOD
return FunctionKind.INSTANCE_METHOD
return None
Expand Down
Loading