Skip to content

Commit d9439d0

Browse files
authored
Chore: Move inline audits ouside the model meta object (#2834)
1 parent 45b95e3 commit d9439d0

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

sqlmesh/core/model/definition.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
JinjaMacroRegistry,
3737
extract_macro_references_and_variables,
3838
)
39+
from sqlmesh.utils.pydantic import field_validator, field_validator_v1_args
3940
from sqlmesh.utils.metaprogramming import (
4041
Executable,
4142
build_env,
@@ -637,6 +638,10 @@ def disable_restatement(self) -> bool:
637638
def wap_supported(self) -> bool:
638639
return self.kind.is_materialized and (self.storage_format or "").lower() == "iceberg"
639640

641+
@property
642+
def inline_audits(self) -> t.Dict[str, ModelAudit]:
643+
return {}
644+
640645
def validate_definition(self) -> None:
641646
"""Validates the model's definition.
642647
@@ -834,11 +839,30 @@ class _SqlBasedModel(_Model):
834839
post_statements_: t.Optional[t.List[exp.Expression]] = Field(
835840
default=None, alias="post_statements"
836841
)
842+
inline_audits_: t.Dict[str, t.Any] = Field(default={}, alias="inline_audits")
837843

838844
__statement_renderers: t.Dict[int, ExpressionRenderer] = {}
839845

840846
_expression_validator = expression_validator
841847

848+
@field_validator("inline_audits_", mode="before")
849+
@field_validator_v1_args
850+
def _inline_audits_validator(cls, v: t.Any, values: t.Dict[str, t.Any]) -> t.Any:
851+
if not isinstance(v, dict):
852+
return {}
853+
854+
from sqlmesh.core.audit import ModelAudit
855+
856+
inline_audits = {}
857+
858+
for name, audit in v.items():
859+
if isinstance(audit, ModelAudit):
860+
inline_audits[name] = audit
861+
elif isinstance(audit, dict):
862+
inline_audits[name] = ModelAudit.parse_obj(audit)
863+
864+
return inline_audits
865+
842866
def render_pre_statements(
843867
self,
844868
*,
@@ -900,6 +924,10 @@ def macro_definitions(self) -> t.List[d.MacroDef]:
900924
"""All macro definitions from the list of expressions."""
901925
return [s for s in self.pre_statements + self.post_statements if isinstance(s, d.MacroDef)]
902926

927+
@property
928+
def inline_audits(self) -> t.Dict[str, ModelAudit]:
929+
return self.inline_audits_
930+
903931
def _render_statements(
904932
self,
905933
statements: t.Iterable[exp.Expression],
@@ -1859,7 +1887,6 @@ def _create_model(
18591887
depends_on: t.Optional[t.Set[str]] = None,
18601888
dialect: t.Optional[str] = None,
18611889
physical_schema_override: t.Optional[t.Dict[str, str]] = None,
1862-
inline_audits: t.Optional[t.Dict[str, ModelAudit]] = None,
18631890
**kwargs: t.Any,
18641891
) -> Model:
18651892
_validate_model_fields(klass, {"name", *kwargs} - {"grain", "table_properties"}, path)
@@ -1880,7 +1907,6 @@ def _create_model(
18801907
try:
18811908
model = klass(
18821909
name=name,
1883-
inline_audits=inline_audits,
18841910
**{
18851911
**(defaults or {}),
18861912
"jinja_macros": jinja_macros or JinjaMacroRegistry(),

sqlmesh/core/model/meta.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class ModelMeta(_Node):
6767
default=None, alias="column_descriptions"
6868
)
6969
audits: t.List[AuditReference] = []
70-
inline_audits: t.Dict[str, t.Any] = {}
7170
grains: t.List[exp.Expression] = []
7271
references: t.List[exp.Expression] = []
7372
physical_schema_override: t.Optional[str] = None
@@ -84,24 +83,6 @@ class ModelMeta(_Node):
8483
_default_catalog_validator = default_catalog_validator
8584
_depends_on_validator = depends_on_validator
8685

87-
@field_validator("inline_audits", mode="before")
88-
@field_validator_v1_args
89-
def _inline_audits_validator(cls, v: t.Any, values: t.Dict[str, t.Any]) -> t.Any:
90-
if not isinstance(v, dict):
91-
return {}
92-
93-
from sqlmesh.core.audit import ModelAudit
94-
95-
inline_audits = {}
96-
97-
for name, audit in v.items():
98-
if isinstance(audit, ModelAudit):
99-
inline_audits[name] = audit
100-
elif isinstance(audit, dict):
101-
inline_audits[name] = ModelAudit.parse_obj(audit)
102-
103-
return inline_audits
104-
10586
@field_validator("audits", mode="before")
10687
def _audits_validator(cls, v: t.Any) -> t.Any:
10788
def extract(v: exp.Expression) -> t.Tuple[str, t.Dict[str, exp.Expression]]:

0 commit comments

Comments
 (0)