Skip to content

Commit 869aa7a

Browse files
committed
Fix typing issues
1 parent c86ec1e commit 869aa7a

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

shiny/render/_data_frame_utils/_html.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from ._types import CellHtml, ReprHtml, SeriesLike
1010

1111
if TYPE_CHECKING:
12+
import narwhals.stable.v1 as nw
13+
1214
from ...session import Session
1315

1416

@@ -41,8 +43,11 @@ def maybe_as_cell_html(
4143
return cast(Jsonifiable, x)
4244

4345

44-
def col_contains_shiny_html(col: SeriesLike) -> bool:
45-
return any(is_shiny_html(val) for _, val in enumerate(col))
46+
def col_contains_shiny_html(col: SeriesLike | nw.Series) -> bool:
47+
for val in col:
48+
if is_shiny_html(val):
49+
return True
50+
return False
4651

4752

4853
# TODO-barret-test; Add test to assert the union type of `TagNode` contains `str` and (HTML | Tagifiable | MetadataNode | ReprHtml). Until a `is tag renderable` method is available in htmltools, we need to check for these types manually and must stay in sync with the `TagNode` union type.

tests/pytest/test_render_data_frame_tbl_data.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,19 @@ class D:
5151
"object": [C(1), D(2)],
5252
}
5353

54+
55+
def polars_df_to_narwhals(df: dict[str, Any]) -> nw.DataFrame[pl.DataFrame]:
56+
return nw.from_native(pl.DataFrame(df), eager_only=True)
57+
58+
59+
def polars_series_to_narwhals(ser: pl.Series) -> nw.Series:
60+
return nw.from_native(ser, series_only=True, strict=True)
61+
62+
5463
params_frames = [
5564
pytest.param(pd.DataFrame, id="pandas"),
5665
pytest.param(pl.DataFrame, id="polars"),
57-
pytest.param(
58-
lambda d: nw.from_native(pl.DataFrame(d), eager_only=True), id="narwhals"
59-
),
66+
pytest.param(polars_df_to_narwhals, id="narwhals"),
6067
]
6168

6269
DataFrameLike: TypeAlias = Union[pd.DataFrame, pl.DataFrame]
@@ -99,7 +106,7 @@ def assert_frame_equal(
99106

100107
def assert_frame_equal2(
101108
src: pd.DataFrame | pl.DataFrame,
102-
target_dict: dict,
109+
target_dict: dict[str, Any],
103110
use_index: bool = False,
104111
):
105112
src = nw.to_native(src, strict=False)
@@ -142,12 +149,8 @@ def test_serialize_dtype(
142149
res_type: str,
143150
):
144151
if isinstance(ser, pl.Series):
145-
assert (
146-
serialize_dtype(nw.from_native(ser, eager_only=True, allow_series=True))[
147-
"type"
148-
]
149-
== res_type
150-
)
152+
nw_ser = polars_series_to_narwhals(ser)
153+
assert serialize_dtype(nw_ser)["type"] == res_type
151154
assert serialize_dtype(ser)["type"] == res_type
152155

153156

0 commit comments

Comments
 (0)