Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python Tests #183

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7"," 3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
poetry-version: [1.3.1]
os: [ubuntu-18.04, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/implementations/py.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 2

# Python Implementation

The Python implementation of MistQL can be installed via `pip install mistql`. Python 3.7+ is supported.
The Python implementation of MistQL can be installed via `pip install mistql`. Python 3.8+ is supported.

### Example Usage:

Expand Down
7 changes: 5 additions & 2 deletions py/mistql/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from functools import cmp_to_key
from typing import Callable, Dict, List, Tuple, Union

from mistql.exceptions import (MistQLRuntimeError, MistQLTypeError,
OpenAnIssueIfYouGetThisError)
from mistql.exceptions import (
MistQLRuntimeError,
MistQLTypeError,
OpenAnIssueIfYouGetThisError,
)
from mistql.expression import BaseExpression, RefExpression
from mistql.runtime_value import RuntimeValue, RuntimeValueType, assert_type, assert_int
from mistql.stack import Stack, add_runtime_value_to_stack
Expand Down
8 changes: 3 additions & 5 deletions py/mistql/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def main(supplied_args=None):
if args.data:
raw_data = args.data
elif args.file:
with open(args.file, 'rb') as f:
with open(args.file, "rb") as f:
raw_data = f.read()

elif args.file_jsonl:
out = []
with open(args.file_jsonl, 'rb') as f:
with open(args.file_jsonl, "rb") as f:
for item in json_lines.reader(f):
out.append(query(args.query, item))
else:
Expand All @@ -65,9 +65,7 @@ def main(supplied_args=None):
if args.output:
# TODO: Allow alternate output encodings other than utf-8
out_bytes = json.dumps(
out,
indent=2 if args.pretty else None,
ensure_ascii=False
out, indent=2 if args.pretty else None, ensure_ascii=False
).encode("utf-8")
with open(args.output, "wb") as f:
f.write(out_bytes)
Expand Down
5 changes: 2 additions & 3 deletions py/mistql/execute.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Mapping, Union, Callable
from mistql.runtime_value import RuntimeValue, RuntimeValueType
from mistql.expression import (
Expression,
Expand All @@ -15,7 +15,6 @@
add_runtime_value_to_stack,
build_initial_stack,
find_in_stack,
StackFrame
)
from mistql.expression import BaseExpression
from mistql.exceptions import MistQLTypeError, OpenAnIssueIfYouGetThisError
Expand Down Expand Up @@ -77,6 +76,6 @@ def execute(ast: BaseExpression, stack: Stack) -> RuntimeValue:
def execute_outer(
ast: Expression,
data: RuntimeValue,
extras: StackFrame
extras: Mapping[str, Union[Callable, RuntimeValue]],
) -> RuntimeValue:
return execute(ast, build_initial_stack(data, builtins, extras))
11 changes: 8 additions & 3 deletions py/mistql/instance.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from typing import Dict, Union, Callable
from typing import Dict, Union, Callable, Optional, Any

from .execute import execute_outer
from .runtime_value import RuntimeValue
from .gardenwall import input_garden_wall, output_garden_wall
from .parse import parse


ExtrasDict = Dict[str, Union[RuntimeValue, Callable]]


class MistQLInstance:
def __init__(self, extras: Dict[str, Union[RuntimeValue, Callable]] = None):
extras: ExtrasDict

def __init__(self, extras: Optional[ExtrasDict] = None):
self.extras = extras or {}

def query(self, query, data):
def query(self, query: str, data: Any):
ast = parse(query)
data = input_garden_wall(data)
result = execute_outer(ast, data, self.extras)
Expand Down
22 changes: 11 additions & 11 deletions py/mistql/runtime_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RuntimeValueType(Enum):
# Passes tests but is also super gross
UPPER_NUM_FORMATTING_BREAKPOINT = 1e21
LOWER_NUM_FORMATTING_BREAKPOINT = 1e-7
MAX_SAFE_INT = 2 ** 53 - 1
MAX_SAFE_INT = 2**53 - 1


e_zero_regex = re.compile(r"e-0+")
Expand Down Expand Up @@ -80,9 +80,11 @@ def of(value):
RuntimeValueType.Object,
{key: RuntimeValue.of(value[key]) for key in value},
)
elif (isinstance(value, date) or
isinstance(value, datetime) or
isinstance(value, time)):
elif (
isinstance(value, date)
or isinstance(value, datetime)
or isinstance(value, time)
):
return RuntimeValue(RuntimeValueType.String, value.isoformat())
else:
raise ValueError(
Expand Down Expand Up @@ -122,16 +124,13 @@ def from_py_func(py_func: Callable):
def definition(args, stack, exec):
if len(args) < min_arity:
fstr = "Function takes no fewer than {} arguments but {} were provided"
raise MistQLTypeError(
fstr.format(min_arity, len(args))
)
raise MistQLTypeError(fstr.format(min_arity, len(args)))
if max_arity is not None and len(args) > max_arity:
fstr = "Function takes no more than {} arguments but {} were provided"
raise MistQLTypeError(
fstr.format(max_arity, len(args))
)
raise MistQLTypeError(fstr.format(max_arity, len(args)))
py_args = [exec(arg, stack).to_python() for arg in args]
return RuntimeValue.of(py_func(*py_args))

return RuntimeValue.wrap_function_def(definition)

@staticmethod
Expand Down Expand Up @@ -191,7 +190,8 @@ def compare(a, b) -> int:
return (a.value > b.value) - (a.value < b.value)
else:
raise OpenAnIssueIfYouGetThisError(
"Cannot compare MistQL values of type " + str(a.type))
"Cannot compare MistQL values of type " + str(a.type)
)

def comparable(self) -> bool:
"""
Expand Down
4 changes: 2 additions & 2 deletions py/mistql/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def add_runtime_value_to_stack(value: RuntimeValue, stack: Stack):
def build_initial_stack(
data: RuntimeValue,
builtins: Mapping[str, Callable],
extras: Mapping[str, Union[Callable, RuntimeValue]]
extras: Mapping[str, Union[Callable, RuntimeValue]],
) -> Stack:
functions_frame: StackFrame = {}
for key, builtin in builtins.items():
Expand All @@ -41,7 +41,7 @@ def build_initial_stack(
return [
functions_frame,
{"$": RuntimeValue.of(dollar_var_dict)},
make_stack_entry_from_runtime_value(data)
make_stack_entry_from_runtime_value(data),
]


Expand Down
Loading
Loading