From 1b5691dca7ef4ae74a9671dcc97a8172765da908 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 8 Jul 2026 18:42:28 +0200 Subject: [PATCH 1/4] fix(resolver): properly check if variable is defined --- midas/checker/python.py | 2 +- midas/checker/resolver.py | 60 +- tests/cases/checker/07_variance.py | 32 +- tests/cases/checker/07_variance.py.ref.json | 960 +++++++++ tests/cases/checker/09_frame_ops.py | 5 +- tests/cases/checker/09_frame_ops.py.ref.json | 1944 ++++++++++-------- 6 files changed, 2103 insertions(+), 900 deletions(-) diff --git a/midas/checker/python.py b/midas/checker/python.py index 705498b..2ed55d1 100644 --- a/midas/checker/python.py +++ b/midas/checker/python.py @@ -107,7 +107,7 @@ class PythonTyper( tree: ast.Module = ast.parse(source, filename=path or "") parser = PythonParser() stmts: list[p.Stmt] = parser.parse_module(tree) - resolver = Resolver() + resolver = Resolver(reporter) resolver.resolve(*stmts) self.env = self.global_env diff --git a/midas/checker/resolver.py b/midas/checker/resolver.py index f923fe6..1cc77db 100644 --- a/midas/checker/resolver.py +++ b/midas/checker/resolver.py @@ -1,4 +1,6 @@ import midas.ast.python as p +from midas.ast.location import Location +from midas.checker.reporter import FileReporter class ResolverError(Exception): ... @@ -11,9 +13,10 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): scope is referred to when a variable is referenced """ - def __init__(self): + def __init__(self, reporter: FileReporter): self.locals: dict[p.Expr, int] = {} self.scopes: list[dict[str, bool]] = [{}] + self.reporter: FileReporter = reporter def resolve(self, *objects: p.Stmt | p.Expr) -> None: """Resolve the given statements or expressions""" @@ -25,29 +28,28 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): """Begin a new scope inside the current one""" self.scopes.append({}) - def end_scope(self): - """Close the current scope""" - self.scopes.pop() + def end_scope(self) -> dict[str, bool]: + """Close and return the current scope""" + return self.scopes.pop() - def declare(self, name: str) -> None: + def declare(self, location: Location, name: str) -> None: """Declare a variable in the current scope This method must be called *before* evaluating the variable initializer Args: name (str): the name of the variable - - Raises: - ResolverError: if the variable has already been declared in the current scope """ if len(self.scopes) == 0: return scope: dict[str, bool] = self.scopes[-1] if name in scope: - raise ResolverError( - f"A variable with the name {name} is already declared in this scope" + self.reporter.error( + location, + f"A variable with the name '{name}' is already declared in this scope", ) - scope[name] = False + else: + scope[name] = False def define(self, name: str) -> None: """Define a variable in the current scope @@ -77,7 +79,7 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): self.locals[expr] = i return - def is_defined(self, name: str) -> bool: + def is_declared(self, name: str) -> bool: """Check whether the given variable is defined in any scope Args: @@ -106,7 +108,7 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): self.resolve(param.default) for param in function.params.all: - self.declare(param.name) + self.declare(function.location, param.name) self.define(param.name) self.resolve(*function.body) self.end_scope() @@ -116,14 +118,12 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): def visit_function(self, stmt: p.Function) -> None: # Declare before resolving body to allow recursion - self.declare(stmt.name) + self.declare(stmt.location, stmt.name) self.define(stmt.name) self.resolve_function(stmt) def visit_type_assign(self, stmt: p.TypeAssign) -> None: - self.declare(stmt.name) - # NOTE: resolve type here? - self.define(stmt.name) + self.declare(stmt.location, stmt.name) def visit_assign_stmt(self, stmt: p.AssignStmt) -> None: self.resolve(stmt.value) @@ -133,9 +133,9 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): def _visit_assign(self, target: p.Expr): match target: case p.VariableExpr(name=name): - if not self.is_defined(name): - self.declare(name) - self.define(name) + if not self.is_declared(name): + self.declare(target.location, name) + self.define(name) target.accept(self) case p.GetExpr(): @@ -145,7 +145,9 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): target.accept(self) case _: - raise Exception(f"Unsupported assignment to {target}") + self.reporter.error( + target.location, f"Unsupported assignment to {target}" + ) def visit_return_stmt(self, stmt: p.ReturnStmt) -> None: if stmt.value is not None: @@ -162,12 +164,17 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): # Body self.begin_scope() self.resolve(*stmt.body) - self.end_scope() + body: dict[str, bool] = self.end_scope() # Else self.begin_scope() self.resolve(*stmt.orelse) - self.end_scope() + else_: dict[str, bool] = self.end_scope() + + # Define variables in this scope if it was defined in both body and else blocks + for name, is_defined in body.items(): + if is_defined and else_.get(name, False): + self.define(name) def visit_pass(self, stmt: p.Pass) -> None: pass @@ -188,7 +195,7 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): def _resolve_imports(self, imports: list[p.ImportAlias]) -> None: for import_ in imports: name: str = import_.imported_name - self.declare(name) + self.declare(import_.location, name) self.define(name) def visit_raw_stmt(self, stmt: p.RawStmt) -> None: @@ -220,8 +227,9 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): def visit_variable_expr(self, expr: p.VariableExpr) -> None: if len(self.scopes) != 0 and self.scopes[-1].get(expr.name) is False: - raise ResolverError( - f"Cannot use local variable '{expr.name}' in its own initializer" + self.reporter.error( + expr.location, + f"Variable '{expr.name}' is declared but may not be defined", ) # aka. UnboundLocalError self.resolve_local(expr, expr.name) diff --git a/tests/cases/checker/07_variance.py b/tests/cases/checker/07_variance.py index 9b251da..688e8d7 100644 --- a/tests/cases/checker/07_variance.py +++ b/tests/cases/checker/07_variance.py @@ -11,16 +11,16 @@ from _ import ( Unused, ) -unused: Unused -covariant: Covariant -contravariant: Contravariant -invariant: Invariant -coco: Coco -cocontra: Cocontra -contraco: Contraco -contracontra: Contracontra -t1: T1 -t2: T2 +unused: Unused = object() +covariant: Covariant = object() +contravariant: Contravariant = object() +invariant: Invariant = object() +coco: Coco = object() +cocontra: Cocontra = object() +contraco: Contraco = object() +contracontra: Contracontra = object() +t1: T1 = object() +t2: T2 = object() # Dummy print to prudce judgements for the expressions print( @@ -36,17 +36,17 @@ print( t2, ) -cov1: Covariant[float] -cov2: Covariant[int] +cov1: Covariant[float] = object() +cov2: Covariant[int] = object() cov1 = cov2 # Ok because int <: float => Covariant[int] <: Covariant[float] cov2 = cov1 # Invalid -contra1: Contravariant[float] -contra2: Contravariant[int] +contra1: Contravariant[float] = object() +contra2: Contravariant[int] = object() contra1 = contra2 # Invalid contra2 = contra1 # Ok because int <: float => Covariant[float] <: Covariant[int] -inv1: Invariant[float] -inv2: Invariant[int] +inv1: Invariant[float] = object() +inv2: Invariant[int] = object() inv1 = inv2 # Invalid inv2 = inv1 # Invalid diff --git a/tests/cases/checker/07_variance.py.ref.json b/tests/cases/checker/07_variance.py.ref.json index baac740..568b7de 100644 --- a/tests/cases/checker/07_variance.py.ref.json +++ b/tests/cases/checker/07_variance.py.ref.json @@ -1,5 +1,145 @@ { "diagnostics": [ + { + "type": "Error", + "location": { + "start": [ + 14, + 0 + ], + "end": [ + 14, + 25 + ] + }, + "message": "Cannot assign object to variable 'unused' of type Unused[T]" + }, + { + "type": "Error", + "location": { + "start": [ + 15, + 0 + ], + "end": [ + 15, + 31 + ] + }, + "message": "Cannot assign object to variable 'covariant' of type Covariant[+T]" + }, + { + "type": "Error", + "location": { + "start": [ + 16, + 0 + ], + "end": [ + 16, + 39 + ] + }, + "message": "Cannot assign object to variable 'contravariant' of type Contravariant[-T]" + }, + { + "type": "Error", + "location": { + "start": [ + 17, + 0 + ], + "end": [ + 17, + 31 + ] + }, + "message": "Cannot assign object to variable 'invariant' of type Invariant[T]" + }, + { + "type": "Error", + "location": { + "start": [ + 18, + 0 + ], + "end": [ + 18, + 21 + ] + }, + "message": "Cannot assign object to variable 'coco' of type Coco[+T]" + }, + { + "type": "Error", + "location": { + "start": [ + 19, + 0 + ], + "end": [ + 19, + 29 + ] + }, + "message": "Cannot assign object to variable 'cocontra' of type Cocontra[-T]" + }, + { + "type": "Error", + "location": { + "start": [ + 20, + 0 + ], + "end": [ + 20, + 29 + ] + }, + "message": "Cannot assign object to variable 'contraco' of type Contraco[-T]" + }, + { + "type": "Error", + "location": { + "start": [ + 21, + 0 + ], + "end": [ + 21, + 37 + ] + }, + "message": "Cannot assign object to variable 'contracontra' of type Contracontra[+T]" + }, + { + "type": "Error", + "location": { + "start": [ + 22, + 0 + ], + "end": [ + 22, + 17 + ] + }, + "message": "Cannot assign object to variable 't1' of type T1[T]" + }, + { + "type": "Error", + "location": { + "start": [ + 23, + 0 + ], + "end": [ + 23, + 17 + ] + }, + "message": "Cannot assign object to variable 't2' of type T2[T]" + }, { "type": "Error", "location": { @@ -14,6 +154,34 @@ }, "message": "Too many positional arguments" }, + { + "type": "Error", + "location": { + "start": [ + 39, + 0 + ], + "end": [ + 39, + 33 + ] + }, + "message": "Cannot assign object to variable 'cov1' of type Covariant[float]" + }, + { + "type": "Error", + "location": { + "start": [ + 40, + 0 + ], + "end": [ + 40, + 31 + ] + }, + "message": "Cannot assign object to variable 'cov2' of type Covariant[int]" + }, { "type": "Error", "location": { @@ -28,6 +196,34 @@ }, "message": "Cannot assign Covariant[float] to variable 'cov2' of type Covariant[int]" }, + { + "type": "Error", + "location": { + "start": [ + 44, + 0 + ], + "end": [ + 44, + 40 + ] + }, + "message": "Cannot assign object to variable 'contra1' of type Contravariant[float]" + }, + { + "type": "Error", + "location": { + "start": [ + 45, + 0 + ], + "end": [ + 45, + 38 + ] + }, + "message": "Cannot assign object to variable 'contra2' of type Contravariant[int]" + }, { "type": "Error", "location": { @@ -42,6 +238,34 @@ }, "message": "Cannot assign Contravariant[int] to variable 'contra1' of type Contravariant[float]" }, + { + "type": "Error", + "location": { + "start": [ + 49, + 0 + ], + "end": [ + 49, + 33 + ] + }, + "message": "Cannot assign object to variable 'inv1' of type Invariant[float]" + }, + { + "type": "Error", + "location": { + "start": [ + 50, + 0 + ], + "end": [ + 50, + 31 + ] + }, + "message": "Cannot assign object to variable 'inv2' of type Invariant[int]" + }, { "type": "Error", "location": { @@ -72,6 +296,466 @@ } ], "judgments": [ + { + "location": { + "from": "L14:17", + "to": "L14:23" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L14:17", + "to": "L14:25" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L15:23", + "to": "L15:29" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L15:23", + "to": "L15:31" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L16:31", + "to": "L16:37" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L16:31", + "to": "L16:39" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L17:23", + "to": "L17:29" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L17:23", + "to": "L17:31" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L18:13", + "to": "L18:19" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L18:13", + "to": "L18:21" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L19:21", + "to": "L19:27" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L19:21", + "to": "L19:29" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L20:21", + "to": "L20:27" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L20:21", + "to": "L20:29" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L21:29", + "to": "L21:35" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L21:29", + "to": "L21:37" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L22:9", + "to": "L22:15" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L22:9", + "to": "L22:17" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L23:9", + "to": "L23:15" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L23:9", + "to": "L23:17" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, { "location": { "from": "L27:4", @@ -385,6 +1069,98 @@ }, "type": {} }, + { + "location": { + "from": "L39:25", + "to": "L39:31" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L39:25", + "to": "L39:33" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L40:23", + "to": "L40:29" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L40:23", + "to": "L40:31" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, { "location": { "from": "L41:7", @@ -427,6 +1203,98 @@ } } }, + { + "location": { + "from": "L44:32", + "to": "L44:38" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L44:32", + "to": "L44:40" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L45:30", + "to": "L45:36" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L45:30", + "to": "L45:38" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, { "location": { "from": "L46:10", @@ -469,6 +1337,98 @@ } } }, + { + "location": { + "from": "L49:25", + "to": "L49:31" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L49:25", + "to": "L49:33" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, + { + "location": { + "from": "L50:23", + "to": "L50:29" + }, + "expr": { + "_type": "VariableExpr", + "name": "object" + }, + "type": { + "params": { + "pos": [ + { + "pos": 0, + "name": "object", + "type": {}, + "required": false, + "unsupported": false + } + ], + "mixed": [], + "kw": [] + }, + "returns": { + "name": "object" + } + } + }, + { + "location": { + "from": "L50:23", + "to": "L50:31" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "object" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "name": "object" + } + }, { "location": { "from": "L51:7", diff --git a/tests/cases/checker/09_frame_ops.py b/tests/cases/checker/09_frame_ops.py index 908f4ba..d7a0831 100644 --- a/tests/cases/checker/09_frame_ops.py +++ b/tests/cases/checker/09_frame_ops.py @@ -1,8 +1,9 @@ # type: ignore # ruff: disable [F821] +import pandas as pd -df1: Frame[i:int, a:int, b:float] -df2: Frame[i:int, a:int, b:float] +df1 = cast(Frame[i:int, a:int, b:float], pd.DataFrame()) +df2 = cast(Frame[i:int, a:int, b:float], pd.DataFrame()) _: Any diff --git a/tests/cases/checker/09_frame_ops.py.ref.json b/tests/cases/checker/09_frame_ops.py.ref.json index 189640b..2e6c448 100644 --- a/tests/cases/checker/09_frame_ops.py.ref.json +++ b/tests/cases/checker/09_frame_ops.py.ref.json @@ -3,102 +3,205 @@ "judgments": [ { "location": { - "from": "L10:4", - "to": "L10:7" + "from": "L5:41", + "to": "L5:43" }, "expr": { "_type": "VariableExpr", - "name": "df1" + "name": "pd" }, - "type": { - "columns": [ - { - "index": 0, - "name": "i", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 1, - "name": "a", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 2, - "name": "b", - "type": { - "type": { - "name": "float" - } - } - } - ] - } + "type": {} }, { "location": { - "from": "L10:10", - "to": "L10:13" + "from": "L5:41", + "to": "L5:55" }, "expr": { - "_type": "VariableExpr", - "name": "df2" - }, - "type": { - "columns": [ - { - "index": 0, - "name": "i", - "type": { - "type": { - "name": "int" - } - } + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "pd" }, - { - "index": 1, - "name": "a", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 2, - "name": "b", - "type": { - "type": { - "name": "float" - } - } - } - ] - } - }, - { - "location": { - "from": "L10:4", - "to": "L10:13" - }, - "expr": { - "_type": "BinaryExpr", - "left": { - "_type": "VariableExpr", - "name": "df1" + "name": "DataFrame" }, - "operator": "+", - "right": { - "_type": "VariableExpr", - "name": "df2" - } + "arguments": [], + "keywords": {} + }, + "type": {} + }, + { + "location": { + "from": "L5:6", + "to": "L5:56" + }, + "expr": { + "_type": "CastExpr", + "type": { + "_type": "FrameType", + "columns": [ + { + "_type": "FrameColumn", + "name": "i", + "type": { + "_type": "BaseType", + "base": "int", + "args": [] + } + }, + { + "_type": "FrameColumn", + "name": "a", + "type": { + "_type": "BaseType", + "base": "int", + "args": [] + } + }, + { + "_type": "FrameColumn", + "name": "b", + "type": { + "_type": "BaseType", + "base": "float", + "args": [] + } + } + ] + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "pd" + }, + "name": "DataFrame" + }, + "arguments": [], + "keywords": {} + }, + "unsafe": false + }, + "type": { + "columns": [ + { + "index": 0, + "name": "i", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 2, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] + } + }, + { + "location": { + "from": "L6:41", + "to": "L6:43" + }, + "expr": { + "_type": "VariableExpr", + "name": "pd" + }, + "type": {} + }, + { + "location": { + "from": "L6:41", + "to": "L6:55" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "pd" + }, + "name": "DataFrame" + }, + "arguments": [], + "keywords": {} + }, + "type": {} + }, + { + "location": { + "from": "L6:6", + "to": "L6:56" + }, + "expr": { + "_type": "CastExpr", + "type": { + "_type": "FrameType", + "columns": [ + { + "_type": "FrameColumn", + "name": "i", + "type": { + "_type": "BaseType", + "base": "int", + "args": [] + } + }, + { + "_type": "FrameColumn", + "name": "a", + "type": { + "_type": "BaseType", + "base": "int", + "args": [] + } + }, + { + "_type": "FrameColumn", + "name": "b", + "type": { + "_type": "BaseType", + "base": "float", + "args": [] + } + } + ] + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "pd" + }, + "name": "DataFrame" + }, + "arguments": [], + "keywords": {} + }, + "unsafe": false }, "type": { "columns": [ @@ -225,7 +328,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "-", + "operator": "+", "right": { "_type": "VariableExpr", "name": "df2" @@ -356,7 +459,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "*", + "operator": "-", "right": { "_type": "VariableExpr", "name": "df2" @@ -487,7 +590,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "/", + "operator": "*", "right": { "_type": "VariableExpr", "name": "df2" @@ -500,7 +603,7 @@ "name": "i", "type": { "type": { - "name": "float" + "name": "int" } } }, @@ -509,7 +612,7 @@ "name": "a", "type": { "type": { - "name": "float" + "name": "int" } } }, @@ -568,8 +671,8 @@ }, { "location": { - "from": "L14:11", - "to": "L14:14" + "from": "L14:10", + "to": "L14:13" }, "expr": { "_type": "VariableExpr", @@ -610,7 +713,7 @@ { "location": { "from": "L14:4", - "to": "L14:14" + "to": "L14:13" }, "expr": { "_type": "BinaryExpr", @@ -618,7 +721,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "//", + "operator": "/", "right": { "_type": "VariableExpr", "name": "df2" @@ -631,7 +734,7 @@ "name": "i", "type": { "type": { - "name": "int" + "name": "float" } } }, @@ -640,7 +743,7 @@ "name": "a", "type": { "type": { - "name": "int" + "name": "float" } } }, @@ -699,8 +802,8 @@ }, { "location": { - "from": "L15:10", - "to": "L15:13" + "from": "L15:11", + "to": "L15:14" }, "expr": { "_type": "VariableExpr", @@ -741,7 +844,7 @@ { "location": { "from": "L15:4", - "to": "L15:13" + "to": "L15:14" }, "expr": { "_type": "BinaryExpr", @@ -749,7 +852,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "%", + "operator": "//", "right": { "_type": "VariableExpr", "name": "df2" @@ -830,8 +933,8 @@ }, { "location": { - "from": "L16:9", - "to": "L16:12" + "from": "L16:10", + "to": "L16:13" }, "expr": { "_type": "VariableExpr", @@ -872,7 +975,7 @@ { "location": { "from": "L16:4", - "to": "L16:12" + "to": "L16:13" }, "expr": { "_type": "BinaryExpr", @@ -880,7 +983,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "**", + "operator": "%", "right": { "_type": "VariableExpr", "name": "df2" @@ -910,7 +1013,9 @@ "index": 2, "name": "b", "type": { - "type": {} + "type": { + "name": "float" + } } } ] @@ -918,8 +1023,8 @@ }, { "location": { - "from": "L19:4", - "to": "L19:7" + "from": "L17:4", + "to": "L17:7" }, "expr": { "_type": "VariableExpr", @@ -959,8 +1064,8 @@ }, { "location": { - "from": "L19:10", - "to": "L19:13" + "from": "L17:9", + "to": "L17:12" }, "expr": { "_type": "VariableExpr", @@ -1000,16 +1105,16 @@ }, { "location": { - "from": "L19:4", - "to": "L19:13" + "from": "L17:4", + "to": "L17:12" }, "expr": { - "_type": "CompareExpr", + "_type": "BinaryExpr", "left": { "_type": "VariableExpr", "name": "df1" }, - "operator": "<", + "operator": "**", "right": { "_type": "VariableExpr", "name": "df2" @@ -1022,7 +1127,7 @@ "name": "i", "type": { "type": { - "name": "bool" + "name": "int" } } }, @@ -1031,7 +1136,7 @@ "name": "a", "type": { "type": { - "name": "bool" + "name": "int" } } }, @@ -1039,9 +1144,7 @@ "index": 2, "name": "b", "type": { - "type": { - "name": "bool" - } + "type": {} } } ] @@ -1140,7 +1243,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": ">", + "operator": "<", "right": { "_type": "VariableExpr", "name": "df2" @@ -1221,8 +1324,8 @@ }, { "location": { - "from": "L21:11", - "to": "L21:14" + "from": "L21:10", + "to": "L21:13" }, "expr": { "_type": "VariableExpr", @@ -1263,7 +1366,7 @@ { "location": { "from": "L21:4", - "to": "L21:14" + "to": "L21:13" }, "expr": { "_type": "CompareExpr", @@ -1271,7 +1374,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "<=", + "operator": ">", "right": { "_type": "VariableExpr", "name": "df2" @@ -1402,7 +1505,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": ">=", + "operator": "<=", "right": { "_type": "VariableExpr", "name": "df2" @@ -1533,7 +1636,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "!=", + "operator": ">=", "right": { "_type": "VariableExpr", "name": "df2" @@ -1664,7 +1767,7 @@ "_type": "VariableExpr", "name": "df1" }, - "operator": "==", + "operator": "!=", "right": { "_type": "VariableExpr", "name": "df2" @@ -1704,8 +1807,8 @@ }, { "location": { - "from": "L27:4", - "to": "L27:7" + "from": "L25:4", + "to": "L25:7" }, "expr": { "_type": "VariableExpr", @@ -1745,24 +1848,92 @@ }, { "location": { - "from": "L27:4", - "to": "L27:14" + "from": "L25:11", + "to": "L25:14" }, "expr": { - "_type": "CallExpr", - "callee": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "df1" - }, - "name": "kurt" - }, - "arguments": [], - "keywords": {} + "_type": "VariableExpr", + "name": "df2" }, "type": { - "type": {} + "columns": [ + { + "index": 0, + "name": "i", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 2, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] + } + }, + { + "location": { + "from": "L25:4", + "to": "L25:14" + }, + "expr": { + "_type": "CompareExpr", + "left": { + "_type": "VariableExpr", + "name": "df1" + }, + "operator": "==", + "right": { + "_type": "VariableExpr", + "name": "df2" + } + }, + "type": { + "columns": [ + { + "index": 0, + "name": "i", + "type": { + "type": { + "name": "bool" + } + } + }, + { + "index": 1, + "name": "a", + "type": { + "type": { + "name": "bool" + } + } + }, + { + "index": 2, + "name": "b", + "type": { + "type": { + "name": "bool" + } + } + } + ] } }, { @@ -1809,7 +1980,7 @@ { "location": { "from": "L28:4", - "to": "L28:18" + "to": "L28:14" }, "expr": { "_type": "CallExpr", @@ -1819,7 +1990,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "kurtosis" + "name": "kurt" }, "arguments": [], "keywords": {} @@ -1872,7 +2043,7 @@ { "location": { "from": "L29:4", - "to": "L29:13" + "to": "L29:18" }, "expr": { "_type": "CallExpr", @@ -1882,7 +2053,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "max" + "name": "kurtosis" }, "arguments": [], "keywords": {} @@ -1935,7 +2106,7 @@ { "location": { "from": "L30:4", - "to": "L30:14" + "to": "L30:13" }, "expr": { "_type": "CallExpr", @@ -1945,7 +2116,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "mean" + "name": "max" }, "arguments": [], "keywords": {} @@ -1998,7 +2169,7 @@ { "location": { "from": "L31:4", - "to": "L31:16" + "to": "L31:14" }, "expr": { "_type": "CallExpr", @@ -2008,7 +2179,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "median" + "name": "mean" }, "arguments": [], "keywords": {} @@ -2061,7 +2232,7 @@ { "location": { "from": "L32:4", - "to": "L32:13" + "to": "L32:16" }, "expr": { "_type": "CallExpr", @@ -2071,7 +2242,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "min" + "name": "median" }, "arguments": [], "keywords": {} @@ -2124,7 +2295,7 @@ { "location": { "from": "L33:4", - "to": "L33:14" + "to": "L33:13" }, "expr": { "_type": "CallExpr", @@ -2134,7 +2305,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "mode" + "name": "min" }, "arguments": [], "keywords": {} @@ -2197,7 +2368,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "prod" + "name": "mode" }, "arguments": [], "keywords": {} @@ -2250,7 +2421,7 @@ { "location": { "from": "L35:4", - "to": "L35:17" + "to": "L35:14" }, "expr": { "_type": "CallExpr", @@ -2260,7 +2431,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "product" + "name": "prod" }, "arguments": [], "keywords": {} @@ -2313,7 +2484,7 @@ { "location": { "from": "L36:4", - "to": "L36:13" + "to": "L36:17" }, "expr": { "_type": "CallExpr", @@ -2323,7 +2494,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "std" + "name": "product" }, "arguments": [], "keywords": {} @@ -2386,7 +2557,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "sum" + "name": "std" }, "arguments": [], "keywords": {} @@ -2449,7 +2620,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "var" + "name": "sum" }, "arguments": [], "keywords": {} @@ -2460,21 +2631,8 @@ }, { "location": { - "from": "L41:23", - "to": "L41:26" - }, - "expr": { - "_type": "LiteralExpr", - "value": "i" - }, - "type": { - "name": "str" - } - }, - { - "location": { - "from": "L41:8", - "to": "L41:11" + "from": "L39:4", + "to": "L39:7" }, "expr": { "_type": "VariableExpr", @@ -2514,8 +2672,84 @@ }, { "location": { - "from": "L41:8", - "to": "L41:27" + "from": "L39:4", + "to": "L39:13" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "df1" + }, + "name": "var" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "type": {} + } + }, + { + "location": { + "from": "L42:23", + "to": "L42:26" + }, + "expr": { + "_type": "LiteralExpr", + "value": "i" + }, + "type": { + "name": "str" + } + }, + { + "location": { + "from": "L42:8", + "to": "L42:11" + }, + "expr": { + "_type": "VariableExpr", + "name": "df1" + }, + "type": { + "columns": [ + { + "index": 0, + "name": "i", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 2, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] + } + }, + { + "location": { + "from": "L42:8", + "to": "L42:27" }, "expr": { "_type": "CallExpr", @@ -2560,77 +2794,6 @@ } } }, - { - "location": { - "from": "L43:4", - "to": "L43:9" - }, - "expr": { - "_type": "VariableExpr", - "name": "df_gb" - }, - "type": { - "frame": { - "columns": [ - { - "index": 0, - "name": "a", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 1, - "name": "b", - "type": { - "type": { - "name": "float" - } - } - } - ] - } - } - }, - { - "location": { - "from": "L43:4", - "to": "L43:16" - }, - "expr": { - "_type": "CallExpr", - "callee": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "df_gb" - }, - "name": "kurt" - }, - "arguments": [], - "keywords": {} - }, - "type": { - "columns": [ - { - "index": 0, - "name": "a", - "type": { - "type": {} - } - }, - { - "index": 1, - "name": "b", - "type": { - "type": {} - } - } - ] - } - }, { "location": { "from": "L44:4", @@ -2668,7 +2831,7 @@ { "location": { "from": "L44:4", - "to": "L44:15" + "to": "L44:16" }, "expr": { "_type": "CallExpr", @@ -2678,7 +2841,7 @@ "_type": "VariableExpr", "name": "df_gb" }, - "name": "max" + "name": "kurt" }, "arguments": [], "keywords": {} @@ -2689,18 +2852,14 @@ "index": 0, "name": "a", "type": { - "type": { - "name": "int" - } + "type": {} } }, { "index": 1, "name": "b", "type": { - "type": { - "name": "float" - } + "type": {} } } ] @@ -2743,7 +2902,7 @@ { "location": { "from": "L45:4", - "to": "L45:16" + "to": "L45:15" }, "expr": { "_type": "CallExpr", @@ -2753,7 +2912,7 @@ "_type": "VariableExpr", "name": "df_gb" }, - "name": "mean" + "name": "max" }, "arguments": [], "keywords": {} @@ -2765,7 +2924,7 @@ "name": "a", "type": { "type": { - "name": "float" + "name": "int" } } }, @@ -2818,7 +2977,7 @@ { "location": { "from": "L46:4", - "to": "L46:18" + "to": "L46:16" }, "expr": { "_type": "CallExpr", @@ -2828,7 +2987,7 @@ "_type": "VariableExpr", "name": "df_gb" }, - "name": "median" + "name": "mean" }, "arguments": [], "keywords": {} @@ -2840,7 +2999,7 @@ "name": "a", "type": { "type": { - "name": "int" + "name": "float" } } }, @@ -2893,7 +3052,7 @@ { "location": { "from": "L47:4", - "to": "L47:15" + "to": "L47:18" }, "expr": { "_type": "CallExpr", @@ -2903,7 +3062,7 @@ "_type": "VariableExpr", "name": "df_gb" }, - "name": "min" + "name": "median" }, "arguments": [], "keywords": {} @@ -2968,7 +3127,7 @@ { "location": { "from": "L48:4", - "to": "L48:16" + "to": "L48:15" }, "expr": { "_type": "CallExpr", @@ -2978,7 +3137,7 @@ "_type": "VariableExpr", "name": "df_gb" }, - "name": "prod" + "name": "min" }, "arguments": [], "keywords": {} @@ -3043,7 +3202,7 @@ { "location": { "from": "L49:4", - "to": "L49:15" + "to": "L49:16" }, "expr": { "_type": "CallExpr", @@ -3053,7 +3212,7 @@ "_type": "VariableExpr", "name": "df_gb" }, - "name": "std" + "name": "prod" }, "arguments": [], "keywords": {} @@ -3064,14 +3223,18 @@ "index": 0, "name": "a", "type": { - "type": {} + "type": { + "name": "int" + } } }, { "index": 1, "name": "b", "type": { - "type": {} + "type": { + "name": "float" + } } } ] @@ -3124,7 +3287,7 @@ "_type": "VariableExpr", "name": "df_gb" }, - "name": "sum" + "name": "std" }, "arguments": [], "keywords": {} @@ -3135,18 +3298,14 @@ "index": 0, "name": "a", "type": { - "type": { - "name": "int" - } + "type": {} } }, { "index": 1, "name": "b", "type": { - "type": { - "name": "float" - } + "type": {} } } ] @@ -3191,6 +3350,81 @@ "from": "L51:4", "to": "L51:15" }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "df_gb" + }, + "name": "sum" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "columns": [ + { + "index": 0, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] + } + }, + { + "location": { + "from": "L52:4", + "to": "L52:9" + }, + "expr": { + "_type": "VariableExpr", + "name": "df_gb" + }, + "type": { + "frame": { + "columns": [ + { + "index": 0, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] + } + } + }, + { + "location": { + "from": "L52:4", + "to": "L52:15" + }, "expr": { "_type": "CallExpr", "callee": { @@ -3223,69 +3457,6 @@ ] } }, - { - "location": { - "from": "L56:7", - "to": "L56:10" - }, - "expr": { - "_type": "VariableExpr", - "name": "df1" - }, - "type": { - "columns": [ - { - "index": 0, - "name": "i", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 1, - "name": "a", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 2, - "name": "b", - "type": { - "type": { - "name": "float" - } - } - } - ] - } - }, - { - "location": { - "from": "L56:7", - "to": "L56:15" - }, - "expr": { - "_type": "SubscriptExpr", - "object": { - "_type": "VariableExpr", - "name": "df1" - }, - "index": { - "_type": "LiteralExpr", - "value": "a" - } - }, - "type": { - "type": { - "name": "int" - } - } - }, { "location": { "from": "L57:7", @@ -3351,49 +3522,59 @@ }, { "location": { - "from": "L60:4", - "to": "L60:8" + "from": "L58:7", + "to": "L58:10" }, "expr": { "_type": "VariableExpr", - "name": "col1" + "name": "df1" }, "type": { - "type": { - "name": "int" - } + "columns": [ + { + "index": 0, + "name": "i", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 2, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] } }, { "location": { - "from": "L60:11", - "to": "L60:15" + "from": "L58:7", + "to": "L58:15" }, "expr": { - "_type": "VariableExpr", - "name": "col2" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L60:4", - "to": "L60:15" - }, - "expr": { - "_type": "BinaryExpr", - "left": { + "_type": "SubscriptExpr", + "object": { "_type": "VariableExpr", - "name": "col1" + "name": "df1" }, - "operator": "+", - "right": { - "_type": "VariableExpr", - "name": "col2" + "index": { + "_type": "LiteralExpr", + "value": "a" } }, "type": { @@ -3443,7 +3624,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "-", + "operator": "+", "right": { "_type": "VariableExpr", "name": "col2" @@ -3496,7 +3677,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "*", + "operator": "-", "right": { "_type": "VariableExpr", "name": "col2" @@ -3549,7 +3730,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "/", + "operator": "*", "right": { "_type": "VariableExpr", "name": "col2" @@ -3557,7 +3738,7 @@ }, "type": { "type": { - "name": "float" + "name": "int" } } }, @@ -3578,8 +3759,8 @@ }, { "location": { - "from": "L64:12", - "to": "L64:16" + "from": "L64:11", + "to": "L64:15" }, "expr": { "_type": "VariableExpr", @@ -3594,7 +3775,7 @@ { "location": { "from": "L64:4", - "to": "L64:16" + "to": "L64:15" }, "expr": { "_type": "BinaryExpr", @@ -3602,7 +3783,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "//", + "operator": "/", "right": { "_type": "VariableExpr", "name": "col2" @@ -3610,7 +3791,7 @@ }, "type": { "type": { - "name": "int" + "name": "float" } } }, @@ -3631,8 +3812,8 @@ }, { "location": { - "from": "L65:11", - "to": "L65:15" + "from": "L65:12", + "to": "L65:16" }, "expr": { "_type": "VariableExpr", @@ -3647,7 +3828,7 @@ { "location": { "from": "L65:4", - "to": "L65:15" + "to": "L65:16" }, "expr": { "_type": "BinaryExpr", @@ -3655,7 +3836,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "%", + "operator": "//", "right": { "_type": "VariableExpr", "name": "col2" @@ -3684,8 +3865,8 @@ }, { "location": { - "from": "L66:10", - "to": "L66:14" + "from": "L66:11", + "to": "L66:15" }, "expr": { "_type": "VariableExpr", @@ -3700,7 +3881,60 @@ { "location": { "from": "L66:4", - "to": "L66:14" + "to": "L66:15" + }, + "expr": { + "_type": "BinaryExpr", + "left": { + "_type": "VariableExpr", + "name": "col1" + }, + "operator": "%", + "right": { + "_type": "VariableExpr", + "name": "col2" + } + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L67:4", + "to": "L67:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L67:10", + "to": "L67:14" + }, + "expr": { + "_type": "VariableExpr", + "name": "col2" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L67:4", + "to": "L67:14" }, "expr": { "_type": "BinaryExpr", @@ -3720,59 +3954,6 @@ } } }, - { - "location": { - "from": "L69:4", - "to": "L69:8" - }, - "expr": { - "_type": "VariableExpr", - "name": "col1" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L69:11", - "to": "L69:15" - }, - "expr": { - "_type": "VariableExpr", - "name": "col2" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L69:4", - "to": "L69:15" - }, - "expr": { - "_type": "CompareExpr", - "left": { - "_type": "VariableExpr", - "name": "col1" - }, - "operator": "<", - "right": { - "_type": "VariableExpr", - "name": "col2" - } - }, - "type": { - "type": { - "name": "bool" - } - } - }, { "location": { "from": "L70:4", @@ -3814,7 +3995,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": ">", + "operator": "<", "right": { "_type": "VariableExpr", "name": "col2" @@ -3843,8 +4024,8 @@ }, { "location": { - "from": "L71:12", - "to": "L71:16" + "from": "L71:11", + "to": "L71:15" }, "expr": { "_type": "VariableExpr", @@ -3859,7 +4040,7 @@ { "location": { "from": "L71:4", - "to": "L71:16" + "to": "L71:15" }, "expr": { "_type": "CompareExpr", @@ -3867,7 +4048,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "<=", + "operator": ">", "right": { "_type": "VariableExpr", "name": "col2" @@ -3920,7 +4101,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": ">=", + "operator": "<=", "right": { "_type": "VariableExpr", "name": "col2" @@ -3973,7 +4154,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "!=", + "operator": ">=", "right": { "_type": "VariableExpr", "name": "col2" @@ -4026,7 +4207,7 @@ "_type": "VariableExpr", "name": "col1" }, - "operator": "==", + "operator": "!=", "right": { "_type": "VariableExpr", "name": "col2" @@ -4040,8 +4221,8 @@ }, { "location": { - "from": "L77:4", - "to": "L77:8" + "from": "L75:4", + "to": "L75:8" }, "expr": { "_type": "VariableExpr", @@ -4055,24 +4236,40 @@ }, { "location": { - "from": "L77:4", - "to": "L77:15" + "from": "L75:12", + "to": "L75:16" }, "expr": { - "_type": "CallExpr", - "callee": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "col1" - }, - "name": "kurt" - }, - "arguments": [], - "keywords": {} + "_type": "VariableExpr", + "name": "col2" }, "type": { - "type": {} + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L75:4", + "to": "L75:16" + }, + "expr": { + "_type": "CompareExpr", + "left": { + "_type": "VariableExpr", + "name": "col1" + }, + "operator": "==", + "right": { + "_type": "VariableExpr", + "name": "col2" + } + }, + "type": { + "type": { + "name": "bool" + } } }, { @@ -4093,7 +4290,7 @@ { "location": { "from": "L78:4", - "to": "L78:19" + "to": "L78:15" }, "expr": { "_type": "CallExpr", @@ -4103,7 +4300,7 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "kurtosis" + "name": "kurt" }, "arguments": [], "keywords": {} @@ -4130,7 +4327,44 @@ { "location": { "from": "L79:4", - "to": "L79:14" + "to": "L79:19" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col1" + }, + "name": "kurtosis" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "type": {} + } + }, + { + "location": { + "from": "L80:4", + "to": "L80:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L80:4", + "to": "L80:14" }, "expr": { "_type": "CallExpr", @@ -4153,8 +4387,8 @@ }, { "location": { - "from": "L80:4", - "to": "L80:8" + "from": "L81:4", + "to": "L81:8" }, "expr": { "_type": "VariableExpr", @@ -4168,8 +4402,8 @@ }, { "location": { - "from": "L80:4", - "to": "L80:15" + "from": "L81:4", + "to": "L81:15" }, "expr": { "_type": "CallExpr", @@ -4190,45 +4424,6 @@ } } }, - { - "location": { - "from": "L81:4", - "to": "L81:8" - }, - "expr": { - "_type": "VariableExpr", - "name": "col1" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L81:4", - "to": "L81:17" - }, - "expr": { - "_type": "CallExpr", - "callee": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "col1" - }, - "name": "median" - }, - "arguments": [], - "keywords": {} - }, - "type": { - "type": { - "name": "int" - } - } - }, { "location": { "from": "L82:4", @@ -4247,7 +4442,7 @@ { "location": { "from": "L82:4", - "to": "L82:14" + "to": "L82:17" }, "expr": { "_type": "CallExpr", @@ -4257,7 +4452,7 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "min" + "name": "median" }, "arguments": [], "keywords": {} @@ -4286,7 +4481,7 @@ { "location": { "from": "L83:4", - "to": "L83:15" + "to": "L83:14" }, "expr": { "_type": "CallExpr", @@ -4296,7 +4491,7 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "mode" + "name": "min" }, "arguments": [], "keywords": {} @@ -4335,7 +4530,7 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "prod" + "name": "mode" }, "arguments": [], "keywords": {} @@ -4364,7 +4559,7 @@ { "location": { "from": "L85:4", - "to": "L85:18" + "to": "L85:15" }, "expr": { "_type": "CallExpr", @@ -4374,7 +4569,7 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "product" + "name": "prod" }, "arguments": [], "keywords": {} @@ -4403,7 +4598,7 @@ { "location": { "from": "L86:4", - "to": "L86:14" + "to": "L86:18" }, "expr": { "_type": "CallExpr", @@ -4413,13 +4608,15 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "std" + "name": "product" }, "arguments": [], "keywords": {} }, "type": { - "type": {} + "type": { + "name": "int" + } } }, { @@ -4450,15 +4647,13 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "sum" + "name": "std" }, "arguments": [], "keywords": {} }, "type": { - "type": { - "name": "int" - } + "type": {} } }, { @@ -4489,32 +4684,21 @@ "_type": "VariableExpr", "name": "col1" }, - "name": "var" + "name": "sum" }, "arguments": [], "keywords": {} }, "type": { - "type": {} + "type": { + "name": "int" + } } }, { "location": { - "from": "L91:28", - "to": "L91:29" - }, - "expr": { - "_type": "LiteralExpr", - "value": 0 - }, - "type": { - "name": "int" - } - }, - { - "location": { - "from": "L91:9", - "to": "L91:13" + "from": "L89:4", + "to": "L89:8" }, "expr": { "_type": "VariableExpr", @@ -4528,8 +4712,58 @@ }, { "location": { - "from": "L91:9", - "to": "L91:30" + "from": "L89:4", + "to": "L89:14" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col1" + }, + "name": "var" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "type": {} + } + }, + { + "location": { + "from": "L92:28", + "to": "L92:29" + }, + "expr": { + "_type": "LiteralExpr", + "value": 0 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L92:9", + "to": "L92:13" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L92:9", + "to": "L92:30" }, "expr": { "_type": "CallExpr", @@ -4557,45 +4791,6 @@ } } }, - { - "location": { - "from": "L93:4", - "to": "L93:10" - }, - "expr": { - "_type": "VariableExpr", - "name": "col_gb" - }, - "type": { - "column": { - "type": { - "name": "int" - } - } - } - }, - { - "location": { - "from": "L93:4", - "to": "L93:17" - }, - "expr": { - "_type": "CallExpr", - "callee": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "col_gb" - }, - "name": "kurt" - }, - "arguments": [], - "keywords": {} - }, - "type": { - "type": {} - } - }, { "location": { "from": "L94:4", @@ -4616,7 +4811,7 @@ { "location": { "from": "L94:4", - "to": "L94:16" + "to": "L94:17" }, "expr": { "_type": "CallExpr", @@ -4626,15 +4821,13 @@ "_type": "VariableExpr", "name": "col_gb" }, - "name": "max" + "name": "kurt" }, "arguments": [], "keywords": {} }, "type": { - "type": { - "name": "int" - } + "type": {} } }, { @@ -4657,7 +4850,7 @@ { "location": { "from": "L95:4", - "to": "L95:17" + "to": "L95:16" }, "expr": { "_type": "CallExpr", @@ -4667,14 +4860,14 @@ "_type": "VariableExpr", "name": "col_gb" }, - "name": "mean" + "name": "max" }, "arguments": [], "keywords": {} }, "type": { "type": { - "name": "float" + "name": "int" } } }, @@ -4698,7 +4891,7 @@ { "location": { "from": "L96:4", - "to": "L96:19" + "to": "L96:17" }, "expr": { "_type": "CallExpr", @@ -4708,14 +4901,14 @@ "_type": "VariableExpr", "name": "col_gb" }, - "name": "median" + "name": "mean" }, "arguments": [], "keywords": {} }, "type": { "type": { - "name": "int" + "name": "float" } } }, @@ -4739,7 +4932,7 @@ { "location": { "from": "L97:4", - "to": "L97:16" + "to": "L97:19" }, "expr": { "_type": "CallExpr", @@ -4749,7 +4942,7 @@ "_type": "VariableExpr", "name": "col_gb" }, - "name": "min" + "name": "median" }, "arguments": [], "keywords": {} @@ -4780,7 +4973,7 @@ { "location": { "from": "L98:4", - "to": "L98:17" + "to": "L98:16" }, "expr": { "_type": "CallExpr", @@ -4790,7 +4983,7 @@ "_type": "VariableExpr", "name": "col_gb" }, - "name": "prod" + "name": "min" }, "arguments": [], "keywords": {} @@ -4821,7 +5014,7 @@ { "location": { "from": "L99:4", - "to": "L99:16" + "to": "L99:17" }, "expr": { "_type": "CallExpr", @@ -4831,13 +5024,15 @@ "_type": "VariableExpr", "name": "col_gb" }, - "name": "std" + "name": "prod" }, "arguments": [], "keywords": {} }, "type": { - "type": {} + "type": { + "name": "int" + } } }, { @@ -4870,15 +5065,13 @@ "_type": "VariableExpr", "name": "col_gb" }, - "name": "sum" + "name": "std" }, "arguments": [], "keywords": {} }, "type": { - "type": { - "name": "int" - } + "type": {} } }, { @@ -4903,6 +5096,47 @@ "from": "L101:4", "to": "L101:16" }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col_gb" + }, + "name": "sum" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L102:4", + "to": "L102:10" + }, + "expr": { + "_type": "VariableExpr", + "name": "col_gb" + }, + "type": { + "column": { + "type": { + "name": "int" + } + } + } + }, + { + "location": { + "from": "L102:4", + "to": "L102:16" + }, "expr": { "_type": "CallExpr", "callee": { @@ -4920,64 +5154,6 @@ "type": {} } }, - { - "location": { - "from": "L104:4", - "to": "L104:7" - }, - "expr": { - "_type": "VariableExpr", - "name": "df1" - }, - "type": { - "columns": [ - { - "index": 0, - "name": "i", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 1, - "name": "a", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 2, - "name": "b", - "type": { - "type": { - "name": "float" - } - } - } - ] - } - }, - { - "location": { - "from": "L104:4", - "to": "L104:12" - }, - "expr": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "df1" - }, - "name": "ndim" - }, - "type": { - "name": "int" - } - }, { "location": { "from": "L105:4", @@ -5030,7 +5206,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "size" + "name": "ndim" }, "type": { "name": "int" @@ -5080,7 +5256,7 @@ { "location": { "from": "L106:4", - "to": "L106:13" + "to": "L106:12" }, "expr": { "_type": "GetExpr", @@ -5088,77 +5264,6 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "shape" - }, - "type": { - "items": [ - { - "name": "int" - }, - { - "name": "int" - } - ] - } - }, - { - "location": { - "from": "L107:4", - "to": "L107:8" - }, - "expr": { - "_type": "VariableExpr", - "name": "col1" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L107:4", - "to": "L107:13" - }, - "expr": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "col1" - }, - "name": "ndim" - }, - "type": { - "name": "int" - } - }, - { - "location": { - "from": "L108:4", - "to": "L108:8" - }, - "expr": { - "_type": "VariableExpr", - "name": "col1" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L108:4", - "to": "L108:13" - }, - "expr": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "col1" - }, "name": "size" }, "type": { @@ -5167,78 +5272,8 @@ }, { "location": { - "from": "L109:4", - "to": "L109:8" - }, - "expr": { - "_type": "VariableExpr", - "name": "col1" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L109:4", - "to": "L109:14" - }, - "expr": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "col1" - }, - "name": "shape" - }, - "type": { - "items": [ - { - "name": "int" - } - ] - } - }, - { - "location": { - "from": "L110:4", - "to": "L110:8" - }, - "expr": { - "_type": "VariableExpr", - "name": "col1" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L110:4", - "to": "L110:10" - }, - "expr": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "col1" - }, - "name": "T" - }, - "type": { - "type": { - "name": "int" - } - } - }, - { - "location": { - "from": "L114:4", - "to": "L114:7" + "from": "L107:4", + "to": "L107:7" }, "expr": { "_type": "VariableExpr", @@ -5278,54 +5313,162 @@ }, { "location": { - "from": "L114:4", - "to": "L114:14" + "from": "L107:4", + "to": "L107:13" }, "expr": { - "_type": "CallExpr", - "callee": { - "_type": "GetExpr", - "object": { - "_type": "VariableExpr", - "name": "df1" - }, - "name": "head" + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "df1" }, - "arguments": [], - "keywords": {} + "name": "shape" }, "type": { - "columns": [ + "items": [ { - "index": 0, - "name": "i", - "type": { - "type": { - "name": "int" - } - } + "name": "int" }, { - "index": 1, - "name": "a", - "type": { - "type": { - "name": "int" - } - } - }, - { - "index": 2, - "name": "b", - "type": { - "type": { - "name": "float" - } - } + "name": "int" } ] } }, + { + "location": { + "from": "L108:4", + "to": "L108:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L108:4", + "to": "L108:13" + }, + "expr": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col1" + }, + "name": "ndim" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L109:4", + "to": "L109:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L109:4", + "to": "L109:13" + }, + "expr": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col1" + }, + "name": "size" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L110:4", + "to": "L110:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L110:4", + "to": "L110:14" + }, + "expr": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col1" + }, + "name": "shape" + }, + "type": { + "items": [ + { + "name": "int" + } + ] + } + }, + { + "location": { + "from": "L111:4", + "to": "L111:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L111:4", + "to": "L111:10" + }, + "expr": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col1" + }, + "name": "T" + }, + "type": { + "type": { + "name": "int" + } + } + }, { "location": { "from": "L115:4", @@ -5380,7 +5523,7 @@ "_type": "VariableExpr", "name": "df1" }, - "name": "tail" + "name": "head" }, "arguments": [], "keywords": {} @@ -5420,22 +5563,48 @@ { "location": { "from": "L116:4", - "to": "L116:8" + "to": "L116:7" }, "expr": { "_type": "VariableExpr", - "name": "col1" + "name": "df1" }, "type": { - "type": { - "name": "int" - } + "columns": [ + { + "index": 0, + "name": "i", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 2, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] } }, { "location": { "from": "L116:4", - "to": "L116:15" + "to": "L116:14" }, "expr": { "_type": "CallExpr", @@ -5443,17 +5612,43 @@ "_type": "GetExpr", "object": { "_type": "VariableExpr", - "name": "col1" + "name": "df1" }, - "name": "head" + "name": "tail" }, "arguments": [], "keywords": {} }, "type": { - "type": { - "name": "int" - } + "columns": [ + { + "index": 0, + "name": "i", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 1, + "name": "a", + "type": { + "type": { + "name": "int" + } + } + }, + { + "index": 2, + "name": "b", + "type": { + "type": { + "name": "float" + } + } + } + ] } }, { @@ -5476,6 +5671,45 @@ "from": "L117:4", "to": "L117:15" }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "GetExpr", + "object": { + "_type": "VariableExpr", + "name": "col1" + }, + "name": "head" + }, + "arguments": [], + "keywords": {} + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L118:4", + "to": "L118:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "col1" + }, + "type": { + "type": { + "name": "int" + } + } + }, + { + "location": { + "from": "L118:4", + "to": "L118:15" + }, "expr": { "_type": "CallExpr", "callee": { From 06f71f29456233719312fa8ea894fa6c3c895811 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 8 Jul 2026 19:09:59 +0200 Subject: [PATCH 2/4] tests: add test for variable assignment --- tests/cases/checker/10_variable_defined.py | 40 ++ .../checker/10_variable_defined.py.ref.json | 527 ++++++++++++++++++ 2 files changed, 567 insertions(+) create mode 100644 tests/cases/checker/10_variable_defined.py create mode 100644 tests/cases/checker/10_variable_defined.py.ref.json diff --git a/tests/cases/checker/10_variable_defined.py b/tests/cases/checker/10_variable_defined.py new file mode 100644 index 0000000..41fef83 --- /dev/null +++ b/tests/cases/checker/10_variable_defined.py @@ -0,0 +1,40 @@ +# type: ignore +# ruff: disable[F821] + +_: Any + +_ = undeclared + +declared: int +_ = declared + +half_defined1: int +half_defined2: int +if False: + half_defined1 = 0 +else: + half_defined2 = 1 +_ = half_defined1 +_ = half_defined2 + +fully_defined: int +if False: + fully_defined = 0 +else: + fully_defined = 1 +_ = fully_defined + +defined: int = 0 +_ = defined + +no_annotation = 0 +_ = no_annotation + +self_ref1 = self_ref1 +self_ref2: int = self_ref2 + + +def fact(n: int) -> int: + if n <= 1: + return 1 + return n * fact(n - 1) diff --git a/tests/cases/checker/10_variable_defined.py.ref.json b/tests/cases/checker/10_variable_defined.py.ref.json new file mode 100644 index 0000000..28af712 --- /dev/null +++ b/tests/cases/checker/10_variable_defined.py.ref.json @@ -0,0 +1,527 @@ +{ + "diagnostics": [ + { + "type": "Error", + "location": { + "start": [ + 9, + 4 + ], + "end": [ + 9, + 12 + ] + }, + "message": "Variable 'declared' is declared but may not be defined" + }, + { + "type": "Error", + "location": { + "start": [ + 17, + 4 + ], + "end": [ + 17, + 17 + ] + }, + "message": "Variable 'half_defined1' is declared but may not be defined" + }, + { + "type": "Error", + "location": { + "start": [ + 18, + 4 + ], + "end": [ + 18, + 17 + ] + }, + "message": "Variable 'half_defined2' is declared but may not be defined" + }, + { + "type": "Error", + "location": { + "start": [ + 34, + 17 + ], + "end": [ + 34, + 26 + ] + }, + "message": "Variable 'self_ref2' is declared but may not be defined" + }, + { + "type": "Warning", + "location": { + "start": [ + 6, + 4 + ], + "end": [ + 6, + 14 + ] + }, + "message": "Unknown variable" + }, + { + "type": "Warning", + "location": { + "start": [ + 33, + 12 + ], + "end": [ + 33, + 21 + ] + }, + "message": "Unknown variable" + } + ], + "judgments": [ + { + "location": { + "from": "L6:4", + "to": "L6:14" + }, + "expr": { + "_type": "VariableExpr", + "name": "undeclared" + }, + "type": {} + }, + { + "location": { + "from": "L9:4", + "to": "L9:12" + }, + "expr": { + "_type": "VariableExpr", + "name": "declared" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L13:3", + "to": "L13:8" + }, + "expr": { + "_type": "LiteralExpr", + "value": false + }, + "type": { + "name": "bool" + } + }, + { + "location": { + "from": "L14:20", + "to": "L14:21" + }, + "expr": { + "_type": "LiteralExpr", + "value": 0 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L16:20", + "to": "L16:21" + }, + "expr": { + "_type": "LiteralExpr", + "value": 1 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L17:4", + "to": "L17:17" + }, + "expr": { + "_type": "VariableExpr", + "name": "half_defined1" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L18:4", + "to": "L18:17" + }, + "expr": { + "_type": "VariableExpr", + "name": "half_defined2" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L21:3", + "to": "L21:8" + }, + "expr": { + "_type": "LiteralExpr", + "value": false + }, + "type": { + "name": "bool" + } + }, + { + "location": { + "from": "L22:20", + "to": "L22:21" + }, + "expr": { + "_type": "LiteralExpr", + "value": 0 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L24:20", + "to": "L24:21" + }, + "expr": { + "_type": "LiteralExpr", + "value": 1 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L25:4", + "to": "L25:17" + }, + "expr": { + "_type": "VariableExpr", + "name": "fully_defined" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L27:15", + "to": "L27:16" + }, + "expr": { + "_type": "LiteralExpr", + "value": 0 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L28:4", + "to": "L28:11" + }, + "expr": { + "_type": "VariableExpr", + "name": "defined" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L30:16", + "to": "L30:17" + }, + "expr": { + "_type": "LiteralExpr", + "value": 0 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L31:4", + "to": "L31:17" + }, + "expr": { + "_type": "VariableExpr", + "name": "no_annotation" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L33:12", + "to": "L33:21" + }, + "expr": { + "_type": "VariableExpr", + "name": "self_ref1" + }, + "type": {} + }, + { + "location": { + "from": "L34:17", + "to": "L34:26" + }, + "expr": { + "_type": "VariableExpr", + "name": "self_ref2" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L38:7", + "to": "L38:8" + }, + "expr": { + "_type": "VariableExpr", + "name": "n" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L38:12", + "to": "L38:13" + }, + "expr": { + "_type": "LiteralExpr", + "value": 1 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L38:7", + "to": "L38:13" + }, + "expr": { + "_type": "CompareExpr", + "left": { + "_type": "VariableExpr", + "name": "n" + }, + "operator": "<=", + "right": { + "_type": "LiteralExpr", + "value": 1 + } + }, + "type": { + "name": "bool" + } + }, + { + "location": { + "from": "L39:15", + "to": "L39:16" + }, + "expr": { + "_type": "LiteralExpr", + "value": 1 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L40:11", + "to": "L40:12" + }, + "expr": { + "_type": "VariableExpr", + "name": "n" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L40:20", + "to": "L40:21" + }, + "expr": { + "_type": "VariableExpr", + "name": "n" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L40:24", + "to": "L40:25" + }, + "expr": { + "_type": "LiteralExpr", + "value": 1 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L40:20", + "to": "L40:25" + }, + "expr": { + "_type": "BinaryExpr", + "left": { + "_type": "VariableExpr", + "name": "n" + }, + "operator": "-", + "right": { + "_type": "LiteralExpr", + "value": 1 + } + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L40:15", + "to": "L40:19" + }, + "expr": { + "_type": "VariableExpr", + "name": "fact" + }, + "type": { + "params": { + "pos": [], + "mixed": [ + { + "pos": 0, + "name": "n", + "type": { + "name": "int" + }, + "required": true, + "unsupported": false + } + ], + "kw": [] + }, + "returns": { + "name": "int" + } + } + }, + { + "location": { + "from": "L40:15", + "to": "L40:26" + }, + "expr": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "fact" + }, + "arguments": [ + { + "_type": "BinaryExpr", + "left": { + "_type": "VariableExpr", + "name": "n" + }, + "operator": "-", + "right": { + "_type": "LiteralExpr", + "value": 1 + } + } + ], + "keywords": {} + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L40:11", + "to": "L40:26" + }, + "expr": { + "_type": "BinaryExpr", + "left": { + "_type": "VariableExpr", + "name": "n" + }, + "operator": "*", + "right": { + "_type": "CallExpr", + "callee": { + "_type": "VariableExpr", + "name": "fact" + }, + "arguments": [ + { + "_type": "BinaryExpr", + "left": { + "_type": "VariableExpr", + "name": "n" + }, + "operator": "-", + "right": { + "_type": "LiteralExpr", + "value": 1 + } + } + ], + "keywords": {} + } + }, + "type": { + "name": "int" + } + } + ] +} \ No newline at end of file From ab7012c5386f8fc31cace97fb7c6d812e627ddfb Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 8 Jul 2026 19:21:19 +0200 Subject: [PATCH 3/4] fix(checker): leaking for-loop iterator target --- midas/checker/python.py | 10 +- midas/checker/resolver.py | 2 +- tests/cases/checker/10_variable_defined.py | 5 + .../checker/10_variable_defined.py.ref.json | 124 ++++++++++++++++++ 4 files changed, 138 insertions(+), 3 deletions(-) diff --git a/midas/checker/python.py b/midas/checker/python.py index 2ed55d1..ef8e688 100644 --- a/midas/checker/python.py +++ b/midas/checker/python.py @@ -601,6 +601,10 @@ class PythonTyper( pass def visit_for_stmt(self, stmt: p.ForStmt) -> None: + outer_env: Environment = self.env + inner_env: Environment = Environment(self.env) + self.env = inner_env + item_type: Type = UnknownType() iterator_type: Type = self.type_of(stmt.iterator) if iterator_type != UnknownType(): @@ -614,8 +618,10 @@ class PythonTyper( self._assign(stmt.location, stmt.target, item_type) self.judge(stmt.target, item_type) - env: Environment = Environment(self.env) - body_returned: bool = self.process_block(stmt.body, env) + body_returned: bool = self.process_block(stmt.body, inner_env) + + self.env = outer_env + if body_returned: raise ReturnException() diff --git a/midas/checker/resolver.py b/midas/checker/resolver.py index 1cc77db..16756fb 100644 --- a/midas/checker/resolver.py +++ b/midas/checker/resolver.py @@ -180,9 +180,9 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): pass def visit_for_stmt(self, stmt: p.ForStmt) -> None: + self.begin_scope() self.resolve(stmt.iterator) self._visit_assign(stmt.target) - self.begin_scope() self.resolve(*stmt.body) self.end_scope() diff --git a/tests/cases/checker/10_variable_defined.py b/tests/cases/checker/10_variable_defined.py index 41fef83..47b8d56 100644 --- a/tests/cases/checker/10_variable_defined.py +++ b/tests/cases/checker/10_variable_defined.py @@ -38,3 +38,8 @@ def fact(n: int) -> int: if n <= 1: return 1 return n * fact(n - 1) + + +for i in [1, 2, 3]: + _ = i +_ = i diff --git a/tests/cases/checker/10_variable_defined.py.ref.json b/tests/cases/checker/10_variable_defined.py.ref.json index 28af712..40428e2 100644 --- a/tests/cases/checker/10_variable_defined.py.ref.json +++ b/tests/cases/checker/10_variable_defined.py.ref.json @@ -83,6 +83,20 @@ ] }, "message": "Unknown variable" + }, + { + "type": "Warning", + "location": { + "start": [ + 45, + 4 + ], + "end": [ + 45, + 5 + ] + }, + "message": "Unknown variable" } ], "judgments": [ @@ -522,6 +536,116 @@ "type": { "name": "int" } + }, + { + "location": { + "from": "L43:10", + "to": "L43:11" + }, + "expr": { + "_type": "LiteralExpr", + "value": 1 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L43:13", + "to": "L43:14" + }, + "expr": { + "_type": "LiteralExpr", + "value": 2 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L43:16", + "to": "L43:17" + }, + "expr": { + "_type": "LiteralExpr", + "value": 3 + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L43:9", + "to": "L43:18" + }, + "expr": { + "_type": "ListExpr", + "items": [ + { + "_type": "LiteralExpr", + "value": 1 + }, + { + "_type": "LiteralExpr", + "value": 2 + }, + { + "_type": "LiteralExpr", + "value": 3 + } + ] + }, + "type": { + "name": "list", + "args": [ + { + "name": "int" + } + ], + "body": { + "name": "list" + } + } + }, + { + "location": { + "from": "L43:4", + "to": "L43:5" + }, + "expr": { + "_type": "VariableExpr", + "name": "i" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L44:8", + "to": "L44:9" + }, + "expr": { + "_type": "VariableExpr", + "name": "i" + }, + "type": { + "name": "int" + } + }, + { + "location": { + "from": "L45:4", + "to": "L45:5" + }, + "expr": { + "_type": "VariableExpr", + "name": "i" + }, + "type": {} } ] } \ No newline at end of file From 4ed78d3d7ec41e27682273a7c9f9e3da2e258616 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 8 Jul 2026 19:30:15 +0200 Subject: [PATCH 4/4] chore: add new parameter to docstring --- midas/checker/resolver.py | 1 + 1 file changed, 1 insertion(+) diff --git a/midas/checker/resolver.py b/midas/checker/resolver.py index 16756fb..7c9e4e4 100644 --- a/midas/checker/resolver.py +++ b/midas/checker/resolver.py @@ -38,6 +38,7 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]): This method must be called *before* evaluating the variable initializer Args: + location (Location): the location where the name is declared name (str): the name of the variable """ if len(self.scopes) == 0: