Skip to content

Commit b303072

Browse files
committed
fix: refactor test_anywidget imports to avoid NameError when dependencies missing
- Reverted changes to bigframes/display/anywidget.py to keep source code clean (no mock injection). - Refactored tests/unit/display/test_anywidget.py to avoid top-level import of bigframes.display.anywidget. - Imported TableWidget inside the test method, ensuring pytest.importorskip runs first and skips the test file if dependencies are missing, preventing the NameError crash.
1 parent 7e77932 commit b303072

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

bigframes/display/anywidget.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@
4343
_ANYWIDGET_INSTALLED = True
4444
except Exception:
4545
_ANYWIDGET_INSTALLED = False
46-
# Define dummy traitlets to avoid NameError at class definition time
47-
import unittest.mock
48-
49-
traitlets = unittest.mock.MagicMock()
5046

5147
_WIDGET_BASE: type[Any]
5248
if _ANYWIDGET_INSTALLED:

tests/unit/display/test_anywidget.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
import pandas as pd
1919
import pytest
2020

21+
import bigframes.dataframe
22+
2123
# Skip if anywidget/traitlets not installed, though they should be in the dev env
2224
pytest.importorskip("anywidget")
2325
pytest.importorskip("traitlets")
2426

25-
import bigframes.dataframe # noqa: E402
26-
import bigframes.display.anywidget as anywidget # noqa: E402
27-
2827

2928
class TestTableWidget:
3029
def test_navigation_to_invalid_page_resets_to_valid_page_without_deadlock(self):
@@ -34,6 +33,8 @@ def test_navigation_to_invalid_page_resets_to_valid_page_without_deadlock(self):
3433
This behavior relies on _set_table_html releasing the lock before updating self.page,
3534
preventing re-entrancy issues where the observer triggers a new update on the same thread.
3635
"""
36+
from bigframes.display import TableWidget
37+
3738
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame, instance=True)
3839
mock_df.columns = ["col1"]
3940
mock_df.dtypes = {"col1": "object"}
@@ -43,8 +44,8 @@ def test_navigation_to_invalid_page_resets_to_valid_page_without_deadlock(self):
4344
mock_df._block = mock_block
4445

4546
# We mock _initial_load to avoid complex setup
46-
with mock.patch.object(anywidget.TableWidget, "_initial_load"):
47-
widget = anywidget.TableWidget(mock_df)
47+
with mock.patch.object(TableWidget, "_initial_load"):
48+
widget = TableWidget(mock_df)
4849

4950
# Simulate "loaded data but unknown total rows" state
5051
widget.page_size = 10

0 commit comments

Comments
 (0)