class Analyzer::Python::PythonEngine

Direct Known Subclasses

Defined in:

analyzer/engines/python_engine.cr

Constant Summary

DOT_NATION = /[a-zA-Z_][a-zA-Z0-9_.]*/

Regex for valid Python module names

HTTP_METHODS = ["get", "post", "put", "patch", "delete", "head", "options", "trace"]

HTTP method names commonly used in REST APIs

INDENTATION_SIZE = 4

Indentation size in spaces; different sizes can cause analysis issues

PYTHON_VAR_NAME_REGEX = /[a-zA-Z_][a-zA-Z0-9_]*/

Regex for valid Python variable names

Class Method Summary

Instance Method Summary

Instance methods inherited from class Analyzer

analyze analyze, base_path : String base_path, base_paths : Array(String) base_paths, callees_needed? : Bool callees_needed?, logger : NoirLogger logger, parallel_analyze(channel : Channel(String), &block : String -> Nil) parallel_analyze, read_file_content(path : String) : String read_file_content, result : Array(Endpoint) result, url : String url

Constructor methods inherited from class Analyzer

new(options : Hash(String, YAML::Any)) new

Macros inherited from class Analyzer

define_getter_methods(names) define_getter_methods

Instance methods inherited from module FileHelper

all_files : Array(String) all_files, get_files_by_extension(extension : String) : Array(String) get_files_by_extension, get_files_by_prefix(prefix : String) : Array(String) get_files_by_prefix, get_files_by_prefix_and_extension(prefix : String, extension : String) : Array(String) get_files_by_prefix_and_extension, get_public_dir_files(base_path : String, folder : String) : Array(String) get_public_dir_files, get_public_files(base_path : String, anchors : Array(String) = ["shard.yml", "Gemfile"]) : Array(String) get_public_files, populate_channel_with_files(channel : Channel(String)) populate_channel_with_files, populate_channel_with_filtered_files(channel : Channel(String), extension : String)
populate_channel_with_filtered_files(channel : Channel(String), extensions : Array(String))
populate_channel_with_filtered_files

Class Method Detail

def self.python_test_path?(path : String) : Bool #

Standard Python/pytest/unittest test-file conventions. A file under any of these patterns ships with python -m pytest or python -m unittest and never serves real traffic in production. Centralized so every analyzer can opt in via next if PythonEngine.python_test_path?(path).

  • /tests/ — pytest discovery default (Django, Litestar, FastAPI all use it)
  • tests.py — the legacy Django per-app test module
  • test_*.py — unittest / pytest default discovery
  • *_test.py — pytest-go style suffix (rare in Python, but cheap to include)

[View source]

Instance Method Detail

def build_callees_from(body : String, body_start_line : Int32, path : String, *, definition_base_path : String | Nil = nil, source : String | Nil = nil) : Array(Callee) #

Build 1-hop callees observed in body (a handler's Python source). body_start_line is the 0-based file line at which body's first character sits, so tree-sitter rows can be translated into absolute call-site lines (1-indexed). Use when one body maps to multiple endpoints (e.g. Sanic's multi-method routes) so the tree-sitter parse happens once and the same Callee list gets pushed onto each endpoint.

Note: callers using parse_code_block(lines[def_idx..]) should pass def_idx because that helper keeps the def line. Callers using extract_function_body(lines, def_idx) should pass def_idx + 1 because that helper skips the def line. When definition_base_path is provided, callees with reachable same-file or imported Python definitions are rewritten to that definition location; unresolved callees keep their call-site path/line.


[View source]
def find_def_line(lines : Array(String), decorator_line : Int32) : Int32 | Nil #

Walk forward from decorator_line past any stacked decorators, blank lines, and comments to the actual def / async def that they apply to. Returns the 0-based line of the def, or nil if none is found before a non-decorator/non-blank statement.

This exists because real-world Python decorator stacks (@app.post(...) + @auth_required, blank-line spacers, or a # comment between the route decorator and the def) make the "def is at decorator_line + 1" assumption silently wrong — both for parameter extraction and for handler-body parsing.


[View source]
def find_imported_modules(app_base_path : String, file_path : String, content : String | Nil = nil) : Hash(String, Tuple(String, Int32)) #

Resolve every import and from … import … in the file to {name => {filepath, package_type}}. Thin delegator over Noir::ImportGraph::Python.find_imported_modules so future Python analyzers (or new tagger logic) can call the resolver directly without going through PythonEngine.


[View source]
def find_imported_package(package_path : String, dotted_as_names : String) : Array(Tuple(String, String, Int32)) #

See #find_imported_modules — same delegator.


[View source]
def find_json_params(codeblock_lines : Array(String), json_var_names : Array(String)) : Array(Param) #

Finds all parameters in JSON objects within a given code block


[View source]
def join_until_python_call_closes(lines : Array(String), index : Int32, line : String) : String #

Given a 0-based index into lines whose content line is known to open a Python call whose ( is unbalanced, join continuation lines until the running paren delta drops to ≤ 0. Returns the joined string with newlines collapsed to single spaces so analyzer-side regexes don't need a multi-line flag. The caller is expected to short-circuit (return line) when the call already balances on the same line — this method always walks forward at least once.


[View source]
def parse_code_block(data : String | Array(String), after : Regex | Nil = nil) : String | Nil #

Parses a function or class definition from a string or an array of strings


[View source]
def parse_function_def(source_lines : Array(String), start_index : Int32) : FunctionDefinition | Nil #

Parses the definition of a function from the source lines starting at a given index


[View source]
def push_callees_from(endpoint : Endpoint, body : String, body_start_line : Int32, path : String, *, definition_base_path : String | Nil = nil, source : String | Nil = nil) : Nil #

Convenience wrapper around #build_callees_from: parse + push in one call when one body maps to exactly one endpoint. Endpoint#push_callee enforces dedup and the per-endpoint cap.


[View source]
def python_paren_delta(line : String) : Int32 #

Net () count on a single Python source line, ignoring parens that fall inside single- or double-quoted strings on the same line. Sufficient for decorator / function-call headers, which never carry triple-quoted strings on the call line.

Used by analyzers that walk source line-by-line and need to join continuation lines into one logical call (e.g. multi-line decorators in FastAPI / Litestar / Sanic / Bottle, multi-line Route(...) entries in Starlette).


[View source]
def return_literal_value(data : String) : String #

Returns the literal value from a string if it represents a number or a quoted string


[View source]