Skip to content

Commit 7e77932

Browse files
committed
fix: make TableWidget import-safe when traitlets is missing
- Added a mock fallback for 'traitlets' in bigframes/display/anywidget.py to avoid NameError during class definition when the package is not installed. - Moved 'pytest.importorskip' calls in tests/unit/display/test_anywidget.py to the top to ensure tests are skipped before attempting any imports from the module under test. - Added noqa: E402 to suppress flake8 warnings for imports after importorskip.
1 parent 387246a commit 7e77932

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

bigframes/display/anywidget.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
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()
4650

4751
_WIDGET_BASE: type[Any]
4852
if _ANYWIDGET_INSTALLED:

tests/unit/display/test_anywidget.py

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

21-
import bigframes.dataframe
22-
import bigframes.display.anywidget as anywidget
23-
2421
# Skip if anywidget/traitlets not installed, though they should be in the dev env
2522
pytest.importorskip("anywidget")
2623
pytest.importorskip("traitlets")
2724

25+
import bigframes.dataframe # noqa: E402
26+
import bigframes.display.anywidget as anywidget # noqa: E402
27+
2828

2929
class TestTableWidget:
3030
def test_navigation_to_invalid_page_resets_to_valid_page_without_deadlock(self):

0 commit comments

Comments
 (0)