PythonNodeContext AST Python Object
The PythonNodeContext
is attached to each AST object for Python via the context
attribute. It inherits the Context
type.
Attributes
currentFunction
(array orFunctionDefinition
): the current function we are in (null
if we are not in a function)currentTryBlock
(typeTryBlock
): the current try/block element we are in (null
if we are not in a try/block)imports
(typeImportStatement
orImportFrom
): the list of arguments
Example of use
// Getting the context
const ctx = node.context;
// Getting the imports
const allPackages = ctx.imports
.filter((i) => i.packages)
.flatMap((i) => i.packages.map((p) => p.name.str));
// Check if the ssl package is being used
const useSslPackage = allPackages.filter((i) => i === "ssl").length > 0;