Skip to content

Commit b9a3bab

Browse files
committed
Fix: use maybe_parse for python model depends_on instead of parse_one
1 parent a35fb91 commit b9a3bab

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

sqlmesh/core/model/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2295,7 +2295,7 @@ def create_python_model(
22952295
else:
22962296
depends_on_rendered = render_expression(
22972297
expression=exp.Array(
2298-
expressions=[d.parse_one(dep, dialect=dialect) for dep in depends_on or []]
2298+
expressions=[exp.maybe_parse(dep, dialect=dialect) for dep in depends_on or []]
22992299
),
23002300
module_path=module_path,
23012301
macros=macros,

tests/core/test_model.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8845,6 +8845,54 @@ def entrypoint(context, *args, **kwargs):
88458845
assert t.cast(pd.DataFrame, list(m.render(context=context))[0]).to_dict() == {"x": {0: 1}}
88468846

88478847

8848+
def test_python_model_depends_on_blueprints(tmp_path: Path) -> None:
8849+
sql_model = tmp_path / "models" / "base_blueprints.sql"
8850+
sql_model.parent.mkdir(parents=True, exist_ok=True)
8851+
sql_model.write_text(
8852+
"""
8853+
MODEL (
8854+
name test_schema1.@{model_name},
8855+
blueprints ((model_name := foo), (model_name := bar)),
8856+
kind FULL
8857+
);
8858+
8859+
SELECT 1 AS id
8860+
"""
8861+
)
8862+
8863+
py_model = tmp_path / "models" / "depends_on_with_blueprint_vars.py"
8864+
py_model.parent.mkdir(parents=True, exist_ok=True)
8865+
py_model.write_text(
8866+
"""
8867+
import pandas as pd
8868+
from sqlmesh import model
8869+
8870+
@model(
8871+
"test_schema2.@model_name",
8872+
columns={
8873+
"id": "int",
8874+
},
8875+
blueprints=[
8876+
{"model_name": "foo"},
8877+
{"model_name": "bar"},
8878+
],
8879+
depends_on=["test_schema1.@{model_name}"],
8880+
)
8881+
def entrypoint(context, *args, **kwargs):
8882+
table = context.resolve_table(f"test_schema1.{context.blueprint_var('model_name')}")
8883+
return context.fetchdf(f"SELECT * FROM {table}")"""
8884+
)
8885+
8886+
ctx = Context(
8887+
config=Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")),
8888+
paths=tmp_path,
8889+
)
8890+
assert len(ctx.models) == 4
8891+
8892+
ctx.plan(no_prompts=True, auto_apply=True)
8893+
assert ctx.fetchdf("SELECT * FROM test_schema2.foo").to_dict() == {"id": {0: 1}}
8894+
8895+
88488896
@time_machine.travel("2020-01-01 00:00:00 UTC")
88498897
def test_dynamic_date_spine_model(assert_exp_eq):
88508898
@macro()

0 commit comments

Comments
 (0)