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,23 @@ 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 }, set (), 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 }, set (), path = Path ("tests/utils" ))
228219
229220
230221def test_serialize_env () -> None :
231- env : t .Dict [str , t .Any ] = {}
232222 path = Path ("tests/utils" )
233- build_env (main_func , env = env , name = "MAIN" , path = path )
234- env = serialize_env (env , path = path ) # type: ignore
235223
224+ env : t .Dict [str , t .Any ] = {}
225+ env_metadata : t .Set [str ] = set ()
226+
227+ build_env (main_func , env = env , env_metadata = env_metadata , name = "MAIN" , path = path )
228+ env = serialize_env (env , env_metadata = env_metadata , path = path ) # type: ignore
236229 assert prepare_env (env )
237- assert env == {
230+
231+ expected_env = {
238232 "MAIN" : Executable (
239233 name = "main_func" ,
240234 alias = "MAIN" ,
@@ -244,8 +238,7 @@ def test_serialize_env() -> None:
244238 sqlglot.parse_one('1')
245239 MyClass()
246240 DataClass(x=y)
247- noop_metadata()
248- normalize_model_name('test')
241+ normalize_model_name('test' + SQLGLOT_META)
249242 fetch_data()
250243 function_with_custom_decorator()
251244
@@ -319,13 +312,6 @@ def test_context_manager():
319312 path = "test_metaprogramming.py" ,
320313 payload = "my_lambda = lambda : print('z')" ,
321314 ),
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- ),
329315 "normalize_model_name" : Executable (
330316 payload = "from sqlmesh.core.dialect import normalize_model_name" ,
331317 kind = ExecutableKind .IMPORT ,
@@ -401,4 +387,24 @@ def function_with_custom_decorator():
401387 return""" ,
402388 alias = "_func" ,
403389 ),
390+ "SQLGLOT_META" : Executable .value ("sqlglot.meta" ),
404391 }
392+
393+ assert env_metadata == set ()
394+ assert env == expected_env
395+
396+ # Annotate the entrypoint as "metadata only" to show how it propagates
397+ setattr (main_func , c .SQLMESH_METADATA , True )
398+
399+ env = {}
400+ env_metadata = set ()
401+
402+ build_env (main_func , env = env , env_metadata = env_metadata , name = "MAIN" , path = path )
403+ env = serialize_env (env , env_metadata = env_metadata , path = path ) # type: ignore
404+ assert prepare_env (env )
405+
406+ expected_env = {k : Executable (** v .dict (), is_metadata = True ) for k , v in expected_env .items ()}
407+
408+ # Every object is treated as "metadata only", transitively
409+ assert env_metadata == set (env )
410+ assert env == expected_env
0 commit comments