Skip to content

Commit 655a577

Browse files
committed
tests: fix test_runtime.py against polluted sys.modules from earlier tests
test_notebook_oauth.py caches a fake ``databricks.sdk.runtime`` directly in ``sys.modules`` without going through the import machinery, which leaves the ``runtime`` attribute on ``databricks.sdk`` unset. The previous fixture's ``import databricks.sdk.runtime`` then hit the cached fake (skipping the loader), and the follow-up ``importlib.reload( databricks.sdk.runtime)`` died with AttributeError when CI happened to run test_notebook_oauth.py first. Drop the eager import + reload from the fixture; just delitem the stale ``sys.modules`` entry via monkeypatch so the next ``import`` in the test body triggers a fresh load (which correctly sets both ``sys.modules`` and the parent attribute). Verified locally that the suite passes both in isolation and when ordered after test_notebook_oauth.py.
1 parent 11fdf3f commit 655a577

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

tests/test_runtime.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Tests for the import-time behavior of ``databricks.sdk.runtime``."""
22

3-
import importlib
43
import sys
54
import types
65

@@ -36,13 +35,13 @@ def get_namespace_globals(self):
3635
monkeypatch.setenv("DATABRICKS_HOST", "https://test.cloud.databricks.com")
3736
monkeypatch.setenv("DATABRICKS_TOKEN", "test-token")
3837

39-
# Re-execute ``databricks.sdk.runtime``'s module body with the fake ``dbruntime`` in
40-
# place, then restore on teardown by reloading once more without it.
41-
import databricks.sdk.runtime
42-
43-
importlib.reload(databricks.sdk.runtime)
44-
yield
45-
importlib.reload(databricks.sdk.runtime)
38+
# Force ``databricks.sdk.runtime`` to re-execute its module body on next import so it
39+
# picks up the fake ``dbruntime``. Earlier tests (e.g. test_notebook_oauth.py) cache a
40+
# fake module here directly via ``sys.modules`` without going through the import
41+
# machinery, which leaves the ``runtime`` attribute on ``databricks.sdk`` unset —
42+
# dropping the cached entry repairs that on the next real import. ``monkeypatch``
43+
# restores the prior value on teardown.
44+
monkeypatch.delitem(sys.modules, "databricks.sdk.runtime", raising=False)
4645

4746

4847
def test_runtime_import_falls_back_on_spark_connect(spark_connect_runtime):

0 commit comments

Comments
 (0)