Skip to main content

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 or FunctionDefinition): the current function we are in (null if we are not in a function)
  • currentTryBlock (type TryBlock): the current try/block element we are in (null if we are not in a try/block)
  • imports (type ImportStatement or ImportFrom): 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;

Examples of rules