feat!: remove Python constraint type
this feature was only partially implemented (parsing) and the syntax was not ideal so this commit removes it entirely
This commit is contained in:
@@ -44,11 +44,6 @@ class BaseType:
|
|||||||
args: tuple[MidasType, ...]
|
args: tuple[MidasType, ...]
|
||||||
|
|
||||||
|
|
||||||
class ConstraintType:
|
|
||||||
type: MidasType
|
|
||||||
constraint: ast.expr
|
|
||||||
|
|
||||||
|
|
||||||
class FrameColumn:
|
class FrameColumn:
|
||||||
name: Optional[str]
|
name: Optional[str]
|
||||||
type: Optional[MidasType]
|
type: Optional[MidasType]
|
||||||
|
|||||||
@@ -18,14 +18,6 @@ class PythonAstPrinter(
|
|||||||
self._write_line(f"base: {node.base}")
|
self._write_line(f"base: {node.base}")
|
||||||
self._write_sequence("args", node.args, last=True)
|
self._write_sequence("args", node.args, last=True)
|
||||||
|
|
||||||
def visit_constraint_type(self, node: p.ConstraintType) -> None:
|
|
||||||
self._write_line("ConstraintType")
|
|
||||||
with self._child_level():
|
|
||||||
self._write_line("type")
|
|
||||||
with self._child_level(single=True):
|
|
||||||
node.type.accept(self)
|
|
||||||
self._write_line(f"constraint: {ast.unparse(node.constraint)}", last=True)
|
|
||||||
|
|
||||||
def visit_frame_column(self, node: p.FrameColumn) -> None:
|
def visit_frame_column(self, node: p.FrameColumn) -> None:
|
||||||
self._write_line("FrameColumn")
|
self._write_line("FrameColumn")
|
||||||
with self._child_level():
|
with self._child_level():
|
||||||
|
|||||||
@@ -52,9 +52,6 @@ class MidasType(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def visit_base_type(self, node: BaseType) -> T: ...
|
def visit_base_type(self, node: BaseType) -> T: ...
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def visit_constraint_type(self, node: ConstraintType) -> T: ...
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def visit_frame_column(self, node: FrameColumn) -> T: ...
|
def visit_frame_column(self, node: FrameColumn) -> T: ...
|
||||||
|
|
||||||
@@ -71,15 +68,6 @@ class BaseType(MidasType):
|
|||||||
return visitor.visit_base_type(self)
|
return visitor.visit_base_type(self)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class ConstraintType(MidasType):
|
|
||||||
type: MidasType
|
|
||||||
constraint: ast.expr
|
|
||||||
|
|
||||||
def accept(self, visitor: MidasType.Visitor[T]) -> T:
|
|
||||||
return visitor.visit_constraint_type(self)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class FrameColumn(MidasType):
|
class FrameColumn(MidasType):
|
||||||
name: Optional[str]
|
name: Optional[str]
|
||||||
|
|||||||
@@ -985,10 +985,6 @@ class PythonTyper(
|
|||||||
return self.types.apply_generic(base, args)
|
return self.types.apply_generic(base, args)
|
||||||
return base
|
return base
|
||||||
|
|
||||||
def visit_constraint_type(self, node: p.ConstraintType) -> Type:
|
|
||||||
self.reporter.warning(node.location, "ConstraintType not yet supported")
|
|
||||||
return UnknownType()
|
|
||||||
|
|
||||||
def visit_frame_column(self, node: p.FrameColumn) -> ColumnType:
|
def visit_frame_column(self, node: p.FrameColumn) -> ColumnType:
|
||||||
return ColumnType(
|
return ColumnType(
|
||||||
type=(
|
type=(
|
||||||
|
|||||||
@@ -437,8 +437,8 @@ def to_annotation(type: Type) -> str:
|
|||||||
case AppliedType(name=name, args=args):
|
case AppliedType(name=name, args=args):
|
||||||
return f"{name}[{', '.join(map(to_annotation, args))}]"
|
return f"{name}[{', '.join(map(to_annotation, args))}]"
|
||||||
|
|
||||||
case ConstraintType():
|
case ConstraintType(type=base):
|
||||||
return str(type)
|
return to_annotation(base)
|
||||||
|
|
||||||
case TupleType(items=items):
|
case TupleType(items=items):
|
||||||
return f"Tuple[{', '.join(map(to_annotation, items))}]"
|
return f"Tuple[{', '.join(map(to_annotation, items))}]"
|
||||||
|
|||||||
@@ -138,10 +138,6 @@ class PythonHighlighter(
|
|||||||
self.wrap(arg, "arg")
|
self.wrap(arg, "arg")
|
||||||
arg.accept(self)
|
arg.accept(self)
|
||||||
|
|
||||||
def visit_constraint_type(self, node: p.ConstraintType) -> None:
|
|
||||||
self.wrap(node, "constraint-type")
|
|
||||||
node.type.accept(self)
|
|
||||||
|
|
||||||
def visit_frame_column(self, node: p.FrameColumn) -> None:
|
def visit_frame_column(self, node: p.FrameColumn) -> None:
|
||||||
self.wrap(node, "frame-column")
|
self.wrap(node, "frame-column")
|
||||||
if node.type is not None:
|
if node.type is not None:
|
||||||
|
|||||||
@@ -7,10 +7,6 @@ span {
|
|||||||
--col: 103, 192, 224;
|
--col: 103, 192, 224;
|
||||||
}
|
}
|
||||||
|
|
||||||
&.constraint-type {
|
|
||||||
--col: 174, 200, 195;
|
|
||||||
}
|
|
||||||
|
|
||||||
&.frame-column {
|
&.frame-column {
|
||||||
--col: 216, 231, 81;
|
--col: 216, 231, 81;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ from midas.ast.python import (
|
|||||||
CallExpr,
|
CallExpr,
|
||||||
CastExpr,
|
CastExpr,
|
||||||
CompareExpr,
|
CompareExpr,
|
||||||
ConstraintType,
|
|
||||||
DictExpr,
|
DictExpr,
|
||||||
Expr,
|
Expr,
|
||||||
ExpressionStmt,
|
ExpressionStmt,
|
||||||
@@ -337,30 +336,6 @@ class PythonParser:
|
|||||||
args=(),
|
args=(),
|
||||||
)
|
)
|
||||||
|
|
||||||
case ast.BinOp(left=left_expr, op=ast.Add(), right=right_expr):
|
|
||||||
left = self._parse_type(left_expr)
|
|
||||||
match left:
|
|
||||||
# If chained constraints, separate base type and rebuild constraint
|
|
||||||
case ConstraintType(type=left_type, constraint=left_constraint):
|
|
||||||
constraint = ast.BinOp(
|
|
||||||
left=left_constraint,
|
|
||||||
op=ast.Add(),
|
|
||||||
right=right_expr,
|
|
||||||
)
|
|
||||||
ast.copy_location(constraint, type_expr)
|
|
||||||
return ConstraintType(
|
|
||||||
location=loc,
|
|
||||||
type=left_type,
|
|
||||||
constraint=constraint,
|
|
||||||
)
|
|
||||||
|
|
||||||
case _:
|
|
||||||
return ConstraintType(
|
|
||||||
location=loc,
|
|
||||||
type=left,
|
|
||||||
constraint=right_expr,
|
|
||||||
)
|
|
||||||
|
|
||||||
case ast.Constant(value=None):
|
case ast.Constant(value=None):
|
||||||
return BaseType(
|
return BaseType(
|
||||||
location=loc,
|
location=loc,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|||||||
df: Frame[
|
df: Frame[
|
||||||
verified: bool,
|
verified: bool,
|
||||||
birth_year: int,
|
birth_year: int,
|
||||||
height: float + ( _ > 0 ) + ( _ < 250 ),
|
height: float,
|
||||||
name: str,
|
name: str,
|
||||||
date: datetime,
|
date: datetime,
|
||||||
float,
|
float,
|
||||||
|
|||||||
@@ -1,19 +1,5 @@
|
|||||||
{
|
{
|
||||||
"diagnostics": [
|
"diagnostics": [
|
||||||
{
|
|
||||||
"type": "Warning",
|
|
||||||
"location": {
|
|
||||||
"start": [
|
|
||||||
8,
|
|
||||||
12
|
|
||||||
],
|
|
||||||
"end": [
|
|
||||||
8,
|
|
||||||
43
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"message": "ConstraintType not yet supported"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "Warning",
|
"type": "Warning",
|
||||||
"location": {
|
"location": {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|||||||
df: Frame[
|
df: Frame[
|
||||||
verified: bool,
|
verified: bool,
|
||||||
birth_year: int,
|
birth_year: int,
|
||||||
height: float + ( _ > 0 ) + ( _ < 250 ),
|
height: float,
|
||||||
name: str,
|
name: str,
|
||||||
date: datetime,
|
date: datetime,
|
||||||
float,
|
float,
|
||||||
|
|||||||
@@ -40,13 +40,9 @@
|
|||||||
"_type": "FrameColumn",
|
"_type": "FrameColumn",
|
||||||
"name": "height",
|
"name": "height",
|
||||||
"type": {
|
"type": {
|
||||||
"_type": "ConstraintType",
|
"_type": "BaseType",
|
||||||
"type": {
|
"base": "float",
|
||||||
"_type": "BaseType",
|
"args": []
|
||||||
"base": "float",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "(_ > 0) + (_ < 250)"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,10 +16,6 @@ lat2: Latitude = lat[1]
|
|||||||
lat_diff: Difference[Latitude] = lat2 - lat1
|
lat_diff: Difference[Latitude] = lat2 - lat1
|
||||||
|
|
||||||
df2: Frame[
|
df2: Frame[
|
||||||
age: int + (_ >= 0),
|
age: int,
|
||||||
height: float + (_ >= 0),
|
height: float,
|
||||||
]
|
|
||||||
df2_bis: Frame[
|
|
||||||
age: int + Positive,
|
|
||||||
height: float + Positive,
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -227,61 +227,18 @@
|
|||||||
"_type": "FrameColumn",
|
"_type": "FrameColumn",
|
||||||
"name": "age",
|
"name": "age",
|
||||||
"type": {
|
"type": {
|
||||||
"_type": "ConstraintType",
|
"_type": "BaseType",
|
||||||
"type": {
|
"base": "int",
|
||||||
"_type": "BaseType",
|
"args": []
|
||||||
"base": "int",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "_ >= 0"
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"_type": "FrameColumn",
|
"_type": "FrameColumn",
|
||||||
"name": "height",
|
"name": "height",
|
||||||
"type": {
|
"type": {
|
||||||
"_type": "ConstraintType",
|
"_type": "BaseType",
|
||||||
"type": {
|
"base": "float",
|
||||||
"_type": "BaseType",
|
"args": []
|
||||||
"base": "float",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "_ >= 0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"_type": "TypeAssign",
|
|
||||||
"name": "df2_bis",
|
|
||||||
"type": {
|
|
||||||
"_type": "FrameType",
|
|
||||||
"columns": [
|
|
||||||
{
|
|
||||||
"_type": "FrameColumn",
|
|
||||||
"name": "age",
|
|
||||||
"type": {
|
|
||||||
"_type": "ConstraintType",
|
|
||||||
"type": {
|
|
||||||
"_type": "BaseType",
|
|
||||||
"base": "int",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "Positive"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"_type": "FrameColumn",
|
|
||||||
"name": "height",
|
|
||||||
"type": {
|
|
||||||
"_type": "ConstraintType",
|
|
||||||
"type": {
|
|
||||||
"_type": "BaseType",
|
|
||||||
"base": "float",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "Positive"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -4,11 +4,10 @@ from __future__ import annotations
|
|||||||
|
|
||||||
|
|
||||||
def func(
|
def func(
|
||||||
col1: Column[float + (0 <= _ <= 1)],
|
col1: Column[float],
|
||||||
col2: Column[float + (0 <= _ <= 1)],
|
col2: Column[float],
|
||||||
) -> Column[float + (0 <= _ <= 2)]:
|
) -> Column[float]:
|
||||||
result: Column[float + (0 <= _ <= 2)] = col1 + col2
|
return col1 + col2
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
def func2(a: int, /, b: float, *, c: str):
|
def func2(a: int, /, b: float, *, c: str):
|
||||||
|
|||||||
@@ -26,13 +26,9 @@
|
|||||||
"base": "Column",
|
"base": "Column",
|
||||||
"args": [
|
"args": [
|
||||||
{
|
{
|
||||||
"_type": "ConstraintType",
|
"_type": "BaseType",
|
||||||
"type": {
|
"base": "float",
|
||||||
"_type": "BaseType",
|
"args": []
|
||||||
"base": "float",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "0 <= _ <= 1"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -45,13 +41,9 @@
|
|||||||
"base": "Column",
|
"base": "Column",
|
||||||
"args": [
|
"args": [
|
||||||
{
|
{
|
||||||
"_type": "ConstraintType",
|
"_type": "BaseType",
|
||||||
"type": {
|
"base": "float",
|
||||||
"_type": "BaseType",
|
"args": []
|
||||||
"base": "float",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "0 <= _ <= 1"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -65,44 +57,15 @@
|
|||||||
"base": "Column",
|
"base": "Column",
|
||||||
"args": [
|
"args": [
|
||||||
{
|
{
|
||||||
"_type": "ConstraintType",
|
"_type": "BaseType",
|
||||||
"type": {
|
"base": "float",
|
||||||
"_type": "BaseType",
|
"args": []
|
||||||
"base": "float",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "0 <= _ <= 2"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"body": [
|
"body": [
|
||||||
{
|
{
|
||||||
"_type": "TypeAssign",
|
"_type": "ReturnStmt",
|
||||||
"name": "result",
|
|
||||||
"type": {
|
|
||||||
"_type": "BaseType",
|
|
||||||
"base": "Column",
|
|
||||||
"args": [
|
|
||||||
{
|
|
||||||
"_type": "ConstraintType",
|
|
||||||
"type": {
|
|
||||||
"_type": "BaseType",
|
|
||||||
"base": "float",
|
|
||||||
"args": []
|
|
||||||
},
|
|
||||||
"constraint": "0 <= _ <= 2"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"_type": "AssignStmt",
|
|
||||||
"targets": [
|
|
||||||
{
|
|
||||||
"_type": "VariableExpr",
|
|
||||||
"name": "result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"value": {
|
"value": {
|
||||||
"_type": "BinaryExpr",
|
"_type": "BinaryExpr",
|
||||||
"left": {
|
"left": {
|
||||||
@@ -115,13 +78,6 @@
|
|||||||
"name": "col2"
|
"name": "col2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
{
|
|
||||||
"_type": "ReturnStmt",
|
|
||||||
"value": {
|
|
||||||
"_type": "VariableExpr",
|
|
||||||
"name": "result"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ from midas.ast.python import (
|
|||||||
CallExpr,
|
CallExpr,
|
||||||
CastExpr,
|
CastExpr,
|
||||||
CompareExpr,
|
CompareExpr,
|
||||||
ConstraintType,
|
|
||||||
DictExpr,
|
DictExpr,
|
||||||
Expr,
|
Expr,
|
||||||
ExpressionStmt,
|
ExpressionStmt,
|
||||||
@@ -106,13 +105,6 @@ class PythonAstJsonSerializer(
|
|||||||
"args": self._serialize_list(node.args),
|
"args": self._serialize_list(node.args),
|
||||||
}
|
}
|
||||||
|
|
||||||
def visit_constraint_type(self, node: ConstraintType) -> dict:
|
|
||||||
return {
|
|
||||||
"_type": "ConstraintType",
|
|
||||||
"type": node.type.accept(self),
|
|
||||||
"constraint": ast.unparse(node.constraint),
|
|
||||||
}
|
|
||||||
|
|
||||||
def visit_frame_column(self, node: FrameColumn) -> dict:
|
def visit_frame_column(self, node: FrameColumn) -> dict:
|
||||||
return {
|
return {
|
||||||
"_type": "FrameColumn",
|
"_type": "FrameColumn",
|
||||||
|
|||||||
Reference in New Issue
Block a user