AST Utils
Classes:
-
ASTProcessingError–Internal error encountered while processing AST.
-
ContractError–Base exception for callable contract violations.
-
ScopeAccessError–Raised when a callable attempts to access instances not passed as parameters or when decorated functions attempt
-
NotAFactError–Raised when a callable parameter, or accessed attribute is not a subclass of Fact.
-
CallableSignatureError–Raised when a decorated function has any missing type hints, an incorrect return type, or if a lambda that
-
AttributeTransformer–Transformer to replace static class attribute access with parameterized instances.
-
LambdaTracker–Index entry for tracking the parsing position of lambda functions in source lines.
-
ASTProcessor–This class extracts source code from functions or lambda expressions, parses them into
ASTProcessingError ¶
Bases: RuntimeError
Internal error encountered while processing AST.
ScopeAccessError ¶
Bases: ContractError
Raised when a callable attempts to access instances not passed as parameters or when decorated functions attempt to access class attributes instead of parameter instance attributes.
NotAFactError ¶
Bases: ContractError
Raised when a callable parameter, or accessed attribute is not a subclass of Fact.
CallableSignatureError ¶
Bases: ContractError
Raised when a decorated function has any missing type hints, an incorrect return type, or if a lambda that requires arguments is provided.
AttributeTransformer ¶
Bases: NodeTransformer
Transformer to replace static class attribute access with parameterized instances.
LambdaTracker dataclass ¶
Index entry for tracking the parsing position of lambda functions in source lines.
Attributes:
-
source(str) –The source code string containing lambda functions
-
positions(list[int]) –Positions where lambda functions are found in the source
-
index(int) –The lambda being parsed within the source string.
-
in_use(bool) –Whether this source is currently being processed or not, making it eligible for cache deletion.
ASTProcessor dataclass ¶
This class extracts source code from functions or lambda expressions, parses them into Abstract Syntax Trees (AST), and performs various validations and transformations.
The processor validates that: - Functions have proper type hints for parameters and return types - All parameters are subclasses of Fact - No nested attribute access (e.g., X.y.z) is used - No async functions are processed - Lambda expressions do not contain parameters - No duplicate parameter types in function signatures
For lambda expressions, it automatically transforms attribute access patterns (e.g., ClassName.attribute) into parameterized functions for easier execution.
Note: This class is not thread-safe and should not be used concurrently across multiple threads.
Type Parameters
Attributes:
-
func(T) –The callable to process, a lambda or a function
-
decorator(Callable) –The decorator type that initiated the processing (e.g.,
conditionoraction) -
return_type(type | TypeAliasType) –Expected return type for the callable
-
source(str) –Extracted source code of func (set during post-init)
-
tree(Module) –Parsed AST of the source code (set during post-init)
-
facts(tuple[str, ...]) –Tuple of fact strings discovered in the callable (set during post-init)
Properties
Raises:
-
OSError–When source code cannot be extracted
-
ScopeAccessError–When accessing undefined classes or using nested attributes
-
CallableSignatureError–When function signature doesn't meet requirements
-
NotAFactError–When parameter types are not Fact subclasses
-
ASTProcessingError–When AST processing encounters internal errors