Skip to content

Commit 2b0d9e2

Browse files
authored
fix: Declare merged_git_metadata_settings before usage (#10)
1 parent fe1732c commit 2b0d9e2

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

py/src/braintrust/logger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,7 @@ def compute_metadata():
16391639
if description is not None:
16401640
args["description"] = description
16411641

1642+
merged_git_metadata_settings = None
16421643
if repo_info:
16431644
repo_info_arg = repo_info
16441645
else:

py/src/braintrust/test_logger.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

90118
class TestLogger(TestCase):
91119
def test_extract_attachments_no_op(self):

0 commit comments

Comments
 (0)