|
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,25 @@ 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 | + # Fast check if no parents or parent is function type |
| 145 | + if not parents: |
| 146 | + return FunctionKind.FUNCTION |
| 147 | + |
| 148 | + parent_type = parents[0].type |
| 149 | + if parent_type in {"FunctionDef", "AsyncFunctionDef"}: |
143 | 150 | return FunctionKind.FUNCTION |
144 | | - if parents[0].type == "ClassDef": |
| 151 | + |
| 152 | + if parent_type == "ClassDef": |
| 153 | + # Store allowed decorator names in a set for O(1) comparisons. |
145 | 154 | for decorator in node.decorator_list: |
146 | 155 | if isinstance(decorator, ast.Name): |
147 | | - if decorator.id == "classmethod": |
| 156 | + dec_id = decorator.id |
| 157 | + if dec_id == "classmethod": |
148 | 158 | return FunctionKind.CLASS_METHOD |
149 | | - if decorator.id == "staticmethod": |
| 159 | + if dec_id == "staticmethod": |
150 | 160 | return FunctionKind.STATIC_METHOD |
151 | 161 | return FunctionKind.INSTANCE_METHOD |
| 162 | + |
152 | 163 | return None |
153 | 164 |
|
154 | 165 |
|
|
0 commit comments