Skip to content

Class swarmauri_evaluator_anyusage.AnyTypeUsageEvaluator.AnyTypeVisitor

swarmauri_evaluator_anyusage.AnyTypeUsageEvaluator.AnyTypeVisitor

AnyTypeVisitor()

Bases: NodeVisitor

AST visitor that finds usages of the Any type in Python code.

Source code in swarmauri_evaluator_anyusage/AnyTypeUsageEvaluator.py
178
179
180
def __init__(self):
    self.any_occurrences = []  # List of (line_number, context) tuples
    self.importing_any = False

any_occurrences instance-attribute

any_occurrences = []

importing_any instance-attribute

importing_any = False

visit_ImportFrom

visit_ImportFrom(node)

Visit import from statements to detect 'from typing import Any'.

Source code in swarmauri_evaluator_anyusage/AnyTypeUsageEvaluator.py
182
183
184
185
186
187
188
189
190
191
def visit_ImportFrom(self, node):
    """Visit import from statements to detect 'from typing import Any'."""
    if node.module == "typing":
        for name in node.names:
            if name.name == "Any":
                self.any_occurrences.append(
                    (node.lineno, f"from typing import {name.name}")
                )
                self.importing_any = True
    self.generic_visit(node)

visit_Name

visit_Name(node)

Visit name nodes to find Any identifiers.

Source code in swarmauri_evaluator_anyusage/AnyTypeUsageEvaluator.py
193
194
195
196
197
198
199
def visit_Name(self, node):
    """Visit name nodes to find Any identifiers."""
    if node.id == "Any" and not isinstance(node.ctx, ast.Store):
        # Get the line of code for context
        context = "Any used as identifier"
        self.any_occurrences.append((node.lineno, context))
    self.generic_visit(node)

visit_AnnAssign

visit_AnnAssign(node)

Visit annotated assignments to find Any in type annotations.

Source code in swarmauri_evaluator_anyusage/AnyTypeUsageEvaluator.py
201
202
203
204
205
206
207
def visit_AnnAssign(self, node):
    """Visit annotated assignments to find Any in type annotations."""
    # Check if the annotation contains Any
    if isinstance(node.annotation, ast.Name) and node.annotation.id == "Any":
        context = "variable: Any annotation"
        self.any_occurrences.append((node.lineno, context))
    self.generic_visit(node)

visit_FunctionDef

visit_FunctionDef(node)

Visit function definitions to find Any in return annotations.

Source code in swarmauri_evaluator_anyusage/AnyTypeUsageEvaluator.py
209
210
211
212
213
214
215
216
217
218
def visit_FunctionDef(self, node):
    """Visit function definitions to find Any in return annotations."""
    if (
        node.returns
        and isinstance(node.returns, ast.Name)
        and node.returns.id == "Any"
    ):
        context = f"def {node.name}(...) -> Any"
        self.any_occurrences.append((node.lineno, context))
    self.generic_visit(node)

visit_arg

visit_arg(node)

Visit function arguments to find Any in parameter annotations.

Source code in swarmauri_evaluator_anyusage/AnyTypeUsageEvaluator.py
220
221
222
223
224
225
226
227
228
229
def visit_arg(self, node):
    """Visit function arguments to find Any in parameter annotations."""
    if (
        node.annotation
        and isinstance(node.annotation, ast.Name)
        and node.annotation.id == "Any"
    ):
        context = f"parameter: {node.arg}: Any"
        self.any_occurrences.append((node.lineno, context))
    self.generic_visit(node)

visit_Subscript

visit_Subscript(node)

Visit subscripts to find Any in complex type annotations like List[Any].

Source code in swarmauri_evaluator_anyusage/AnyTypeUsageEvaluator.py
231
232
233
234
235
236
237
238
239
240
241
def visit_Subscript(self, node):
    """Visit subscripts to find Any in complex type annotations like List[Any]."""
    # Check for Any in subscripts like List[Any]
    if (
        isinstance(node.value, ast.Name)
        and isinstance(node.slice, ast.Name)
        and node.slice.id == "Any"
    ):
        context = f"Complex type with Any: {node.value.id}[Any]"
        self.any_occurrences.append((node.lineno, context))
    self.generic_visit(node)