@@ -86,6 +86,34 @@ def test_init_with_dataset_id_and_version(self):
8686 assert dataset_dict ["id" ] == "dataset-id-123"
8787 assert dataset_dict ["version" ] == "v2"
8888
89+ def test_init_with_repo_info_does_not_raise (self ):
90+ """Test that passing repo_info to init() doesn't cause an UnboundLocalError.
91+
92+ Regression test for a bug where merged_git_metadata_settings was only
93+ defined in the else branch (when repo_info is falsy), but referenced
94+ unconditionally later in compute_metadata().
95+ ref: https://github.com/braintrustdata/braintrust-sdk-python/issues/8
96+ """
97+ mock_conn = MagicMock ()
98+ mock_conn .post_json .return_value = {
99+ "project" : {"id" : "test-project-id" , "name" : "test-project" },
100+ "experiment" : {"id" : "test-exp-id" , "name" : "test-exp" },
101+ }
102+
103+ from braintrust .git_fields import RepoInfo
104+
105+ repo_info = RepoInfo (commit = "abc123" , branch = "main" , dirty = False )
106+
107+ simulate_login ()
108+ with patch .object (logger ._state , "app_conn" , return_value = mock_conn ):
109+ exp = braintrust .init (project = "test-project" , repo_info = repo_info )
110+
111+ # Force compute_metadata() to execute. This would raise
112+ # UnboundLocalError before the fix.
113+ metadata = exp ._lazy_metadata .get ()
114+
115+ assert metadata .project .id == "test-project-id"
116+ assert metadata .experiment .name == "test-exp"
89117
90118class TestLogger (TestCase ):
91119 def test_extract_attachments_no_op (self ):
0 commit comments