knit_script.knit_script_interpreter.statements.Assertion module
Statements for asserting conditions.
This module provides the Assertion statement class, which implements Python-style assertion functionality in knit script programs. It allows developers to test conditions during script execution and raise exceptions when those conditions are not met, providing a mechanism for runtime validation and debugging.
- class Assertion(parser_node, condition, error_str=None)[source]
Bases:
StatementIncludes python style assertions in language.
This class provides assertion functionality similar to Python’s assert statement, allowing conditions to be tested during script execution with optional error messages. Assertions are useful for validating assumptions, checking preconditions, and debugging knit script programs by ensuring that expected conditions hold true during execution.
- When an assertion fails, it raises a Knit_Script_Assertion_Exception with detailed information about the failed condition and any provided error message,
helping developers identify and fix issues in their knit script programs.
- _condition
The condition expression to test for truthiness.
- Type:
- _error_str
Optional error message expression to display when assertion fails.
- Type:
Expression | None
- __init__(parser_node, condition, error_str=None)[source]
Initialize an Assertion statement.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
condition (Expression) – The condition expression to test for truthiness. Will be evaluated and converted to boolean.
error_str (Expression | None, optional) – Optional error message expression to display when assertion fails. If None, a default error message will be generated. Defaults to None.
- execute(context)[source]
Execute the assertion by evaluating the condition.
Evaluates the condition expression and raises an exception if the result is falsy. The assertion follows Python’s truthiness conventions where empty collections, None, zero values, and False are considered false.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Raises:
AssertionError – If the condition evaluates to False or any other falsy value.
- 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:
- 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: