posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.42k stars 48 forks source link

typing of rows parameter in fmt_* methods does not show that expressions are allowed #347

Open Noghpu opened 1 month ago

Noghpu commented 1 month ago

In version 0.5.2 using polars expressions as the rows argument of fmt_* methods seems to work fine. However, Pylance reports an argument type error because the typing is int | list[int] | None.

import polars as pl
import polars.selectors as cs
from great_tables import GT
from IPython.display import display

df = pl.DataFrame(
    dict(
        rowname=["weight", "age"],
        personA=[75, 60],
        personB=[50, 20],
    )
)
gt = GT(df, rowname_col="rowname").fmt_integer(
    columns=cs.starts_with("person"),
    rows=pl.col("rowname") == "weight", # TypeError 
    pattern="{x} kg",
)

display(gt)

Output as expected:

personA personB
weight 75 kg 50 kg
age 60 20