Skip to content

Commit a837409

Browse files
authored
Fix: Include the module path when diffing python code (#4346)
1 parent 3462a5d commit a837409

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

sqlmesh/core/model/common.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,16 @@ def sort_python_env(python_env: t.Dict[str, Executable]) -> t.List[t.Tuple[str,
361361

362362
def sorted_python_env_payloads(python_env: t.Dict[str, Executable]) -> t.List[str]:
363363
"""Returns the payloads of the sorted python env."""
364-
return [
365-
v.payload if v.is_import or v.is_definition else f"{k} = {v.payload}"
366-
for k, v in sort_python_env(python_env)
367-
]
364+
365+
def _executable_to_str(k: str, v: Executable) -> str:
366+
result = f"# {v.path}\n" if v.path is not None else ""
367+
if v.is_import or v.is_definition:
368+
result += v.payload
369+
else:
370+
result += f"{k} = {v.payload}"
371+
return result
372+
373+
return [_executable_to_str(k, v) for k, v in sort_python_env(python_env)]
368374

369375

370376
expression_validator: t.Callable = field_validator(

tests/core/test_model.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9702,3 +9702,26 @@ def test_scd_time_data_type_does_not_cause_diff_after_deserialization() -> None:
97029702
deserialized_model = SqlModel.parse_raw(model.json())
97039703

97049704
assert model.data_hash == deserialized_model.data_hash
9705+
9706+
9707+
def test_python_env_includes_file_path_in_render_definition():
9708+
@model(
9709+
"db.test_model_path",
9710+
kind=dict(
9711+
name=ModelKindName.SCD_TYPE_2_BY_TIME,
9712+
unique_key=["id"],
9713+
disable_restatement=False,
9714+
),
9715+
columns={"id": "string", "name": "string"},
9716+
optimize_query=False,
9717+
)
9718+
def test_model(context, **kwargs):
9719+
return pd.DataFrame([{"id": context.var("1")}])
9720+
9721+
python_model = model.get_registry()["db.test_model_path"].model(
9722+
module_path=Path("."), path=Path("."), dialect="duckdb"
9723+
)
9724+
9725+
model_executable_str = python_model.render_definition()[1].sql()
9726+
# Make sure the file path is included in the render definition
9727+
assert "# tests/core/test_model.py" in model_executable_str

0 commit comments

Comments
 (0)