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
19 changes: 15 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"]:
# Fast exit if no parents or parent is function
if not parents:
return FunctionKind.FUNCTION
parent_type = parents[0].type
if parent_type in _FUNCTION_OR_ASYNC:
return FunctionKind.FUNCTION
if parents[0].type == "ClassDef":
if parent_type == "ClassDef":
# Use a set for quick id matching, short-circuit as soon as possible
for decorator in node.decorator_list:
if isinstance(decorator, ast.Name):
if decorator.id == "classmethod":
did = decorator.id
if did == "classmethod":
return FunctionKind.CLASS_METHOD
if decorator.id == "staticmethod":
if did == "staticmethod":
return FunctionKind.STATIC_METHOD
return FunctionKind.INSTANCE_METHOD
return None
Expand All @@ -159,3 +167,6 @@ def has_typed_parameters(node: ast.FunctionDef | ast.AsyncFunctionDef, parents:
if kind in [FunctionKind.CLASS_METHOD, FunctionKind.INSTANCE_METHOD]:
return all(arg.annotation for arg in node.args.args[1:])
return False


_FUNCTION_OR_ASYNC = {"FunctionDef", "AsyncFunctionDef"}
Loading