Skip to content

Commit 2ba3b38

Browse files
committed
PR feedback: refactor conversion to bool for is_metadata
1 parent 6da6fab commit 2ba3b38

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

sqlmesh/core/model/common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def make_python_env(
5454
variables = variables or {}
5555
blueprint_variables = blueprint_variables or {}
5656

57-
used_macros: t.Dict[str, t.Tuple[MacroCallable, t.Optional[bool]]] = {}
57+
used_macros: t.Dict[str, t.Tuple[MacroCallable, bool]] = {}
5858
used_variable_referenced_in_metadata_expression = dict.fromkeys(used_variables or set(), False)
5959

6060
# For an expression like @foo(@v1, @bar(@v1, @v2), @v3), the following mapping would be:
@@ -66,7 +66,7 @@ def make_python_env(
6666
if isinstance(expression_metadata, tuple):
6767
expression, is_metadata = expression_metadata
6868
else:
69-
expression, is_metadata = expression_metadata, None
69+
expression, is_metadata = expression_metadata, False
7070

7171
if isinstance(expression, d.Jinja):
7272
continue
@@ -98,7 +98,7 @@ def make_python_env(
9898
var_name = args[0].this.lower()
9999
used_variable_referenced_in_metadata_expression[var_name] = (
100100
used_variable_referenced_in_metadata_expression.get(var_name, True)
101-
and bool(is_metadata)
101+
and is_metadata
102102
)
103103
else:
104104
for var_ref in _extract_macro_func_variable_references(macro_func_or_var):
@@ -114,7 +114,7 @@ def make_python_env(
114114
elif name in variables or name in blueprint_variables:
115115
used_variable_referenced_in_metadata_expression[name] = (
116116
used_variable_referenced_in_metadata_expression.get(name, True)
117-
and bool(is_metadata)
117+
and is_metadata
118118
)
119119
elif (
120120
isinstance(macro_func_or_var, (exp.Identifier, d.MacroStrReplace, d.MacroSQL))
@@ -126,12 +126,12 @@ def make_python_env(
126126
if var_name in variables or var_name in blueprint_variables:
127127
used_variable_referenced_in_metadata_expression[var_name] = (
128128
used_variable_referenced_in_metadata_expression.get(var_name, True)
129-
and bool(is_metadata)
129+
and is_metadata
130130
)
131131

132132
for macro_ref in jinja_macro_references or set():
133133
if macro_ref.package is None and macro_ref.name in macros:
134-
used_macros[macro_ref.name] = (macros[macro_ref.name], None)
134+
used_macros[macro_ref.name] = (macros[macro_ref.name], False)
135135

136136
for name, (used_macro, is_metadata) in used_macros.items():
137137
if isinstance(used_macro, Executable):

sqlmesh/utils/metaprogramming.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def build_env(
283283
env: t.Dict[str, t.Tuple[t.Any, t.Optional[bool]]],
284284
name: str,
285285
path: Path,
286-
is_metadata_obj: t.Optional[bool] = None,
286+
is_metadata_obj: bool = False,
287287
) -> None:
288288
"""Fills in env dictionary with all globals needed to execute the object.
289289
@@ -299,7 +299,7 @@ def build_env(
299299
# We don't rely on `env` to keep track of visited objects, because it's populated in post-order
300300
visited: t.Set[str] = set()
301301

302-
def walk(obj: t.Any, name: str, is_metadata: t.Optional[bool] = None) -> None:
302+
def walk(obj: t.Any, name: str, is_metadata: bool = False) -> None:
303303
obj_module = inspect.getmodule(obj)
304304
if obj_module and obj_module.__name__ == "builtins":
305305
return
@@ -320,7 +320,7 @@ def walk(obj: t.Any, name: str, is_metadata: t.Optional[bool] = None) -> None:
320320
# The existing object in the env is "metadata only" but we're walking it again as a
321321
# non-"metadata only" dependency, so we update this flag to ensure all transitive
322322
# dependencies are also not marked as "metadata only"
323-
is_metadata = None
323+
is_metadata = False
324324

325325
if hasattr(obj, c.SQLMESH_MACRO):
326326
# We only need to add the undecorated code of @macro() functions in env, which
@@ -380,7 +380,7 @@ def walk(obj: t.Any, name: str, is_metadata: t.Optional[bool] = None) -> None:
380380
)
381381

382382
# The "metadata only" annotation of the object is transitive
383-
walk(obj, name, is_metadata_obj or getattr(obj, c.SQLMESH_METADATA, None))
383+
walk(obj, name, is_metadata_obj or getattr(obj, c.SQLMESH_METADATA, False))
384384

385385

386386
@dataclass
@@ -432,7 +432,11 @@ def value(
432432
cls, v: t.Any, is_metadata: t.Optional[bool] = None, sort_root_dict: bool = False
433433
) -> Executable:
434434
payload = _dict_sort(v) if sort_root_dict else repr(v)
435-
return Executable(payload=payload, kind=ExecutableKind.VALUE, is_metadata=is_metadata)
435+
return Executable(
436+
payload=payload,
437+
kind=ExecutableKind.VALUE,
438+
is_metadata=is_metadata or None,
439+
)
436440

437441

438442
def serialize_env(env: t.Dict[str, t.Any], path: Path) -> t.Dict[str, Executable]:
@@ -447,6 +451,9 @@ def serialize_env(env: t.Dict[str, t.Any], path: Path) -> t.Dict[str, Executable
447451
serialized = {}
448452

449453
for k, (v, is_metadata) in env.items():
454+
# We don't store `False` for `is_metadata` to reduce the pydantic model's payload size
455+
is_metadata = is_metadata or None
456+
450457
if isinstance(v, LITERALS) or v is None:
451458
serialized[k] = Executable.value(v, is_metadata=is_metadata)
452459
elif inspect.ismodule(v):

tests/utils/test_metaprogramming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def function_with_custom_decorator():
406406
"SQLGLOT_META": Executable.value("sqlglot.meta"),
407407
}
408408

409-
assert all(is_metadata is None for (_, is_metadata) in env.values())
409+
assert all(not is_metadata for (_, is_metadata) in env.values())
410410
assert serialized_env == expected_env
411411

412412
# Annotate the entrypoint as "metadata only" to show how it propagates

0 commit comments

Comments
 (0)