How does codehound find the enclosing function / detect awaited calls? #1
Answered
by
kratos0718
kratos0718
asked this question in
Q&A
|
Question: Why does codehound build a child-to-parent map of the AST instead of just walking the tree top-down? I see most of the checks call |
Answered by
kratos0718
Jun 7, 2026
Replies: 1 comment
|
Python's
Building the map once is O(n) and keeps each check simple and precise. |
0 replies
Answer selected by
kratos0718
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python's
astnodes know their children but not their parent. So codehound does one pass per file building a{id(child): parent}map (build_parents). Every check then walks upward through that map:enclosing_function(node)climbs parents until it hits aFunctionDef/AsyncFunctionDef— that's how CH001 knows a call is inside anasync def.is_awaited(node)simply checks whether the node's parent is anast.Await— that's how CH001 skipsawait client.post(...)and avoids false positives.Building the map once is O(n) and keeps each check simple and precise.