|
7 | 7 |
|
8 | 8 | from pydantic import BaseModel, ConfigDict, field_validator |
9 | 9 |
|
| 10 | +from codeflash.models.models import FunctionParent |
| 11 | + |
10 | 12 | if TYPE_CHECKING: |
11 | 13 | from codeflash.models.models import FunctionParent |
12 | 14 |
|
@@ -139,16 +141,19 @@ def get_first_top_level_function_or_method_ast( |
139 | 141 |
|
140 | 142 |
|
141 | 143 | def function_kind(node: ast.FunctionDef | ast.AsyncFunctionDef, parents: list[FunctionParent]) -> FunctionKind | None: |
142 | | - if not parents or parents[0].type in ["FunctionDef", "AsyncFunctionDef"]: |
| 144 | + if not parents: |
| 145 | + return FunctionKind.FUNCTION |
| 146 | + parent_type = parents[0].type |
| 147 | + if parent_type in {"FunctionDef", "AsyncFunctionDef"}: |
143 | 148 | return FunctionKind.FUNCTION |
144 | | - for _i in range(len(parents) - 1, -1, -1): |
145 | | - continue |
146 | | - if parents[0].type == "ClassDef": |
| 149 | + if parent_type == "ClassDef": |
| 150 | + # Fast path: decorator_list is typically small; scan for 'classmethod' and 'staticmethod' |
147 | 151 | for decorator in node.decorator_list: |
148 | 152 | if isinstance(decorator, ast.Name): |
149 | | - if decorator.id == "classmethod": |
| 153 | + did = decorator.id |
| 154 | + if did == "classmethod": |
150 | 155 | return FunctionKind.CLASS_METHOD |
151 | | - if decorator.id == "staticmethod": |
| 156 | + if did == "staticmethod": |
152 | 157 | return FunctionKind.STATIC_METHOD |
153 | 158 | return FunctionKind.INSTANCE_METHOD |
154 | 159 | return None |
|
0 commit comments