Skip to content
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 setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
},
install_requires=[
"astor; python_version < '3.9.0'",
"astor",
"click",
"croniter",
"duckdb",
Expand Down
12 changes: 3 additions & 9 deletions sqlmesh/utils/metaprogramming.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from enum import Enum
from pathlib import Path

from astor import to_source

from sqlmesh.utils import trim_path, unique
from sqlmesh.utils.errors import SQLMeshError
from sqlmesh.utils.pydantic import PydanticModel
Expand Down Expand Up @@ -132,14 +134,6 @@ def join_source(lnum: int) -> str:
raise SQLMeshError(f"Cannot find source for {obj}")


def unparse(node: ast.Module) -> str:
if sys.version_info < (3, 9):
import astor

return astor.to_source(node).strip()
return ast.unparse(node).strip()


def _parse_source(func: t.Callable) -> ast.Module:
return ast.parse(textwrap.dedent(getsource(func)))

Expand Down Expand Up @@ -198,7 +192,7 @@ def normalize_source(obj: t.Any) -> str:
elif isinstance(node, ast.arg):
node.annotation = None

return unparse(root_node)
return to_source(root_node).strip()


def build_env(obj: t.Any, *, env: t.Dict[str, t.Any], name: str, module: str) -> None:
Expand Down
7 changes: 2 additions & 5 deletions tests/utils/test_metaprogramming.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import typing as t
from dataclasses import dataclass

Expand Down Expand Up @@ -116,8 +115,6 @@ def test_serialize_env() -> None:
build_env(main_func, env=env, name="MAIN", module="tests")
env = serialize_env(env, module="tests") # type: ignore

lambda_no_args_padding = " " if sys.version_info < (3, 11) else ""

assert env == {
"MAIN": Executable(
name="main_func",
Expand Down Expand Up @@ -171,7 +168,7 @@ def baz(self):
"sqlglot": Executable(kind=ExecutableKind.IMPORT, payload="import sqlglot"),
"my_lambda": Executable(
path="test_metaprogramming.py",
payload=f"my_lambda = lambda{lambda_no_args_padding}: print('z')",
payload=f"my_lambda = lambda : print('z')",
),
"other_func": Executable(
path="test_metaprogramming.py",
Expand Down Expand Up @@ -206,7 +203,7 @@ def test_print_exception(mocker: MockerFixture):

expected_message = f"""Traceback (most recent call last):

File "{__file__}", line 203, in test_print_exception
File "{__file__}", line 200, in test_print_exception
eval("test_fun()", env)

File "<string>", line 1, in <module>
Expand Down