Merge pull request 'Undefined variables and column aggregations' (#43) from fix/undefined-variable into main

Reviewed-on: #43
This commit is contained in:
HEL
2026-07-23 23:36:12 +00:00
4 changed files with 33 additions and 47 deletions
@@ -13,7 +13,6 @@ from midas.checker.types import (
Function,
ParamSpec,
Type,
UnknownType,
)
if TYPE_CHECKING:
@@ -97,11 +96,9 @@ class ColumnGroupByMethodRegistry(MethodRegistry[Call]):
positional=[],
keywords={},
)
if not isinstance(returns, ColumnType):
returns = ColumnType(type=UnknownType())
signature = Function(
params=ParamSpec(mixed=real_params),
returns=returns,
returns=ColumnType(type=returns),
)
result: CallResult = self.dispatcher.get_result(
+2 -4
View File
@@ -364,14 +364,12 @@ class ColumnMethodRegistry(MethodRegistry[Call]):
Type: the result type
"""
returns: Type = ColumnType(type=TopType())
returns: Type = TopType()
if formula:
returns = ColumnType(
type=self._resolve_formula_type(
returns = self._resolve_formula_type(
call,
formula(call.column.type),
)
)
signature = Function(
params=ParamSpec(
kw=[
+16 -1
View File
@@ -97,6 +97,21 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]):
return True
return False
def is_defined(self, name: str) -> bool:
"""Check whether the given variable is defined
Args:
name (str): the name of the variable
Returns:
bool: `True` if the variable is defined in the closest scope where it
is declared, `False` otherwise
"""
for scope in reversed(self.scopes):
if name in scope:
return scope[name]
return False
def resolve_function(self, function: p.Function) -> None:
"""Resolve a function definition
@@ -230,7 +245,7 @@ class Resolver(p.Stmt.Visitor[None], p.Expr.Visitor[None]):
pass
def visit_variable_expr(self, expr: p.VariableExpr) -> None:
if len(self.scopes) != 0 and self.scopes[-1].get(expr.name) is False:
if self.is_declared(expr.name) and not self.is_defined(expr.name):
self.reporter.error(
expr.location,
f"Variable '{expr.name}' is declared but may not be defined",
@@ -4305,9 +4305,7 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {}
}
},
{
"location": {
@@ -4342,9 +4340,7 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {}
}
},
{
"location": {
@@ -4379,11 +4375,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "int"
}
}
},
{
"location": {
@@ -4418,11 +4412,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "float"
}
}
},
{
"location": {
@@ -4457,11 +4449,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "int"
}
}
},
{
"location": {
@@ -4496,11 +4486,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "int"
}
}
},
{
"location": {
@@ -4535,11 +4523,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "int"
}
}
},
{
"location": {
@@ -4574,11 +4560,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "int"
}
}
},
{
"location": {
@@ -4613,11 +4597,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "int"
}
}
},
{
"location": {
@@ -4652,9 +4634,7 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {}
}
},
{
"location": {
@@ -4689,11 +4669,9 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {
"name": "int"
}
}
},
{
"location": {
@@ -4728,9 +4706,7 @@
"arguments": [],
"keywords": {}
},
"type": {
"type": {}
}
},
{
"location": {