1111from pytest_mock .plugin import MockerFixture
1212from sqlglot import exp
1313from sqlglot import exp as expressions
14- from sqlglot .expressions import to_table
14+ from sqlglot .expressions import SQLGLOT_META , to_table
1515from sqlglot .optimizer .pushdown_projections import SELECT_ALL
1616
1717import tests .utils .test_date as test_date
@@ -100,13 +100,6 @@ def other_func(a: int) -> int:
100100 return X + a + W
101101
102102
103- def noop_metadata () -> None :
104- return None
105-
106-
107- setattr (noop_metadata , c .SQLMESH_METADATA , True )
108-
109-
110103@contextmanager
111104def test_context_manager ():
112105 yield
@@ -134,8 +127,7 @@ def main_func(y: int, foo=exp.true(), *, bar=expressions.Literal.number(1) + 2)
134127 sqlglot .parse_one ("1" )
135128 MyClass ()
136129 DataClass (x = y )
137- noop_metadata ()
138- normalize_model_name ("test" )
130+ normalize_model_name ("test" + SQLGLOT_META )
139131 fetch_data ()
140132 function_with_custom_decorator ()
141133
@@ -154,7 +146,6 @@ def test_func_globals() -> None:
154146 "Z" : 3 ,
155147 "DataClass" : DataClass ,
156148 "MyClass" : MyClass ,
157- "noop_metadata" : noop_metadata ,
158149 "normalize_model_name" : normalize_model_name ,
159150 "other_func" : other_func ,
160151 "sqlglot" : sqlglot ,
@@ -163,6 +154,7 @@ def test_func_globals() -> None:
163154 "fetch_data" : fetch_data ,
164155 "test_context_manager" : test_context_manager ,
165156 "function_with_custom_decorator" : function_with_custom_decorator ,
157+ "SQLGLOT_META" : SQLGLOT_META ,
166158 }
167159 assert func_globals (other_func ) == {
168160 "X" : 1 ,
@@ -194,8 +186,7 @@ def test_normalize_source() -> None:
194186 sqlglot.parse_one('1')
195187 MyClass()
196188 DataClass(x=y)
197- noop_metadata()
198- normalize_model_name('test')
189+ normalize_model_name('test' + SQLGLOT_META)
199190 fetch_data()
200191 function_with_custom_decorator()
201192
@@ -221,20 +212,21 @@ def closure(z: int):
221212def test_serialize_env_error () -> None :
222213 with pytest .raises (SQLMeshError ):
223214 # pretend to be the module pandas
224- serialize_env ({"test_date" : test_date }, path = Path ("tests/utils" ))
215+ serialize_env ({"test_date" : ( test_date , None ) }, path = Path ("tests/utils" ))
225216
226217 with pytest .raises (SQLMeshError ):
227- serialize_env ({"select_all" : SELECT_ALL }, path = Path ("tests/utils" ))
218+ serialize_env ({"select_all" : ( SELECT_ALL , None ) }, path = Path ("tests/utils" ))
228219
229220
230221def test_serialize_env () -> None :
231- env : t .Dict [str , t .Any ] = {}
232222 path = Path ("tests/utils" )
223+ env : t .Dict [str , t .Tuple [t .Any , t .Optional [bool ]]] = {}
224+
233225 build_env (main_func , env = env , name = "MAIN" , path = path )
234- env = serialize_env (env , path = path ) # type: ignore
226+ serialized_env = serialize_env (env , path = path ) # type: ignore
227+ assert prepare_env (serialized_env )
235228
236- assert prepare_env (env )
237- assert env == {
229+ expected_env = {
238230 "MAIN" : Executable (
239231 name = "main_func" ,
240232 alias = "MAIN" ,
@@ -244,8 +236,7 @@ def test_serialize_env() -> None:
244236 sqlglot.parse_one('1')
245237 MyClass()
246238 DataClass(x=y)
247- noop_metadata()
248- normalize_model_name('test')
239+ normalize_model_name('test' + SQLGLOT_META)
249240 fetch_data()
250241 function_with_custom_decorator()
251242
@@ -319,13 +310,6 @@ def test_context_manager():
319310 path = "test_metaprogramming.py" ,
320311 payload = "my_lambda = lambda : print('z')" ,
321312 ),
322- "noop_metadata" : Executable (
323- name = "noop_metadata" ,
324- path = "test_metaprogramming.py" ,
325- payload = """def noop_metadata():
326- return None""" ,
327- is_metadata = True ,
328- ),
329313 "normalize_model_name" : Executable (
330314 payload = "from sqlmesh.core.dialect import normalize_model_name" ,
331315 kind = ExecutableKind .IMPORT ,
@@ -401,4 +385,23 @@ def function_with_custom_decorator():
401385 return""" ,
402386 alias = "_func" ,
403387 ),
388+ "SQLGLOT_META" : Executable .value ("sqlglot.meta" ),
404389 }
390+
391+ assert all (is_metadata is None for (_ , is_metadata ) in env .values ())
392+ assert serialized_env == expected_env
393+
394+ # Annotate the entrypoint as "metadata only" to show how it propagates
395+ setattr (main_func , c .SQLMESH_METADATA , True )
396+
397+ env = {}
398+
399+ build_env (main_func , env = env , name = "MAIN" , path = path )
400+ serialized_env = serialize_env (env , path = path ) # type: ignore
401+ assert prepare_env (serialized_env )
402+
403+ expected_env = {k : Executable (** v .dict (), is_metadata = True ) for k , v in expected_env .items ()}
404+
405+ # Every object is treated as "metadata only", transitively
406+ assert all (is_metadata for (_ , is_metadata ) in env .values ())
407+ assert serialized_env == expected_env
0 commit comments