knit_script.knit_script_interpreter.statements.Import_Statement module

Used to import python and other knitscript code into program.

This module provides the Import_Statement class, which handles importing functionality from Python modules, knit script standard library modules, and local knit script files. It supports both direct imports and aliased imports with comprehensive fallback logic for module resolution.

class Import_Statement(parser_node, src, alias=None)[source]

Bases: Scoped_Statement

Statement that imports a Python or knit script module.

Supports importing Python modules, knit script standard library modules, and local knit script files. Handles both direct imports and aliased imports with comprehensive module resolution that searches multiple locations for the requested module.

The import system follows a priority order: Python modules first, then knit script standard library, then local files, and finally standard library knit script files.

This allows knit script programs to seamlessly integrate with Python libraries while providing access to knit script-specific functionality.

src

Expression representing the module name or path to import.

Type:

Expression

alias

Optional alias name for the imported module.

Type:

Expression | None

__init__(parser_node, src, alias=None)[source]

Initialize an import statement.

Parameters:
  • parser_node (LRStackNode) – The parser node from the abstract syntax tree.

  • src (Attribute_Accessor_Expression | Variable_Expression) – A Variable or Attribute_Accessor expression representing the module name or path to import.

  • alias (Variable_Expression, optional) – Optional variable expression that declares the alias name for the imported module. Defaults to no alias.

property source_string: str
Returns:

The string representing the path of values in the source expression.

Return type:

str

Raises:

AttributeError | ImportError – If the source attribute cannot be parsed into a path of variables names.

property source_ks_file: str
Returns:

The string representing the file path to a ks file for the source expression.

Return type:

str

Raises:

AttributeError | ImportError – If the source attribute cannot be parsed into a path of variables names.

property alias_name: str

Returns: str: The alias of this import statement derived from the alias expression or the variable name of the source expression.

execute(context)[source]

Execute the import by loading the module and adding it to scope.

Attempts to import in the following order: 1. Python module with the exact name 2. Knit script standard library module 3. Local knit script file 4. Standard library knit script file. Once found, adds the module to the current variable scope with the appropriate name or alias.

Parameters:

context (Knit_Script_Context) – The current execution context to import into.

Raises:

ImportError – If src is not a module name or path expression, or if alias is not a valid variable expression. If the module cannot be found in any location after trying all resolution methods.

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

classmethod __init_subclass__(**kwargs)

Automatically wrap execute methods in subclasses with appropriate error handling and debugging decorators.

This method is called whenever a class inherits from Statement. It checks if the subclass defines its own execute method and wraps it with the appropriate decorators

Parameters:

**kwargs (Any) – Additional keyword arguments passed to super().__init_subclass__

property file_name: str | None

Returns: str | None: The file name of the knitscript program this was parsed from or None if the program was passed as a string.

property line_number: int

Get the line number of the symbol that generated this statement.

Returns:

The line number where this element appears in the source file.

Return type:

int

property local_path: str | None

Returns: str | None: The path to the directory containing the file from which this element was parsed or None if the value was parsed from a python string.

property location: Location

Get the location of this symbol in KnitScript file.

Returns:

The location of this symbol in the source file, including file name, line number, and position information.

Return type:

Location

property location_str: str

Returns: str: The string referencing the line number and possible file name information about this element.

property position_context: str

The position context string is the string from the knitscript program from which this element was parsed. The context string will begin at the start of this element and continue to the end of the line of knitscript or a semicolon on new line are reached.

Returns:

The string used to contextualize this element in the knitscript program.

Return type:

str

pre_scope_action(context)

An action taken before execution of the subscope statement (e.g., assigning local variables).

This method should be overridden by subclasses that take actions before executing the sub-scoped statement(s).

Parameters:

context (Knit_Script_Context) – The current execution context of the knit script interpreter.

Returns:

True if the statements should follow execution of the pre-amble, False otherwise.

Return type:

bool