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
4 changes: 4 additions & 0 deletions sqlmesh/utils/metaprogramming.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ def serialize_env(env: t.Dict[str, t.Any], module: str) -> t.Dict[str, Executabl
)
elif inspect.ismodule(v):
name = v.__name__
if name.startswith(module):
raise SQLMeshError(
f"Cannot serialize 'import {name}'. Use 'from {name} import ...' instead."
)
postfix = "" if name == k else f" as {k}"
serialized[k] = Executable(
payload=f"import {name}{postfix}",
Expand Down
79 changes: 44 additions & 35 deletions tests/utils/test_metaprogramming.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from dataclasses import dataclass

import pandas as pd
import pytest
import sqlglot
from pytest_mock.plugin import MockerFixture
from sqlglot.expressions import to_table

from sqlmesh.utils.errors import SQLMeshError
from sqlmesh.utils.metaprogramming import (
Executable,
ExecutableKind,
Expand All @@ -17,6 +19,42 @@
serialize_env,
)


def test_print_exception(mocker: MockerFixture):
out_mock = mocker.Mock()

test_env = {
"test_fun": Executable(
name="test_func",
payload="""def test_fun():
raise RuntimeError("error")""",
path="/test/path.py",
),
}
env: t.Dict[str, t.Any] = {}
prepare_env(env, test_env)
try:
eval("test_fun()", env)
except Exception as ex:
print_exception(ex, test_env, out_mock)

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

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

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

File '/test/path.py' (or imported file), line 2, in test_fun
def test_fun():
raise RuntimeError("error")


RuntimeError: error
"""
out_mock.write.assert_called_once_with(expected_message)


X = 1
Y = 2
Z = 3
Expand Down Expand Up @@ -110,6 +148,12 @@ def closure(z):
)


def test_serialize_env_error() -> None:
with pytest.raises(SQLMeshError) as e:
# pretend to be the module pandas
serialize_env({"pandas": pd}, module="pandas")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wouldn't apply to pandas though right since that is a third party module?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, but this test pretends like we are pandas

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it just could be confusing for future people reading this. Maybe make a comment explaining that.



def test_serialize_env() -> None:
env: t.Dict[str, t.Any] = {}
build_env(main_func, env=env, name="MAIN", module="tests")
Expand Down Expand Up @@ -181,38 +225,3 @@ def baz(self):
return X + a""",
),
}


def test_print_exception(mocker: MockerFixture):
out_mock = mocker.Mock()

test_env = {
"test_fun": Executable(
name="test_func",
payload="""def test_fun():
raise RuntimeError("error")""",
path="/test/path.py",
),
}
env: t.Dict[str, t.Any] = {}
prepare_env(env, test_env)
try:
eval("test_fun()", env)
except Exception as ex:
print_exception(ex, test_env, out_mock)

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

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

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

File '/test/path.py' (or imported file), line 2, in test_fun
def test_fun():
raise RuntimeError("error")


RuntimeError: error
"""
out_mock.write.assert_called_once_with(expected_message)