Skip to content

Commit 7cfddd8

Browse files
authored
Fix: support auto_restatement_cron in python models (#5141)
1 parent 54e6b57 commit 7cfddd8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

sqlmesh/core/model/definition.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2866,6 +2866,13 @@ def render_field_value(value: t.Any) -> t.Any:
28662866
for key, value in field_value.items():
28672867
if key in RUNTIME_RENDERED_MODEL_FIELDS:
28682868
rendered_dict[key] = parse_strings_with_macro_refs(value, dialect)
2869+
elif (
2870+
# don't parse kind auto_restatement_cron="@..." kwargs (e.g. @daily) into MacroVar
2871+
key == "auto_restatement_cron"
2872+
and isinstance(value, str)
2873+
and value.lower() in CRON_SHORTCUTS
2874+
):
2875+
rendered_dict[key] = value
28692876
elif (rendered := render_field_value(value)) is not None:
28702877
rendered_dict[key] = rendered
28712878

tests/core/test_model.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,7 +2898,15 @@ def my_model_2(context):
28982898
# no warning with valid kind dict
28992899
with patch.object(get_console(), "log_warning") as mock_logger:
29002900

2901-
@model("kind_valid_dict", kind=dict(name=ModelKindName.FULL), columns={'"COL"': "int"})
2901+
@model(
2902+
"kind_valid_dict",
2903+
kind=dict(
2904+
name=ModelKindName.INCREMENTAL_BY_TIME_RANGE,
2905+
time_column="ds",
2906+
auto_restatement_cron="@hourly",
2907+
),
2908+
columns={'"ds"': "date", '"COL"': "int"},
2909+
)
29022910
def my_model(context):
29032911
pass
29042912

@@ -2907,11 +2915,33 @@ def my_model(context):
29072915
path=Path("."),
29082916
)
29092917

2910-
assert isinstance(python_model.kind, FullKind)
2918+
assert isinstance(python_model.kind, IncrementalByTimeRangeKind)
29112919

29122920
assert not mock_logger.call_args
29132921

29142922

2923+
def test_python_model_decorator_auto_restatement_cron() -> None:
2924+
@model(
2925+
"auto_restatement_model",
2926+
cron="@daily",
2927+
kind=dict(
2928+
name=ModelKindName.INCREMENTAL_BY_TIME_RANGE,
2929+
time_column="ds",
2930+
auto_restatement_cron="@hourly",
2931+
),
2932+
columns={'"ds"': "date", '"COL"': "int"},
2933+
)
2934+
def my_model(context):
2935+
pass
2936+
2937+
python_model = model.get_registry()["auto_restatement_model"].model(
2938+
module_path=Path("."),
2939+
path=Path("."),
2940+
)
2941+
2942+
assert python_model.auto_restatement_cron == "@hourly"
2943+
2944+
29152945
def test_python_model_decorator_col_descriptions() -> None:
29162946
# `columns` and `column_descriptions` column names are different cases, but name normalization makes both lower
29172947
@model("col_descriptions", columns={"col": "int"}, column_descriptions={"COL": "a column"})

0 commit comments

Comments
 (0)