def resolve_mask(
    data: GTData | list[str],
    expr: PlExpr,
    excl_stub: bool = True,
    excl_group: bool = True,
) -> list[tuple[int, int, str]]:
    """Return data for creating `CellPos`, based on expr"""
    if not isinstance(expr, PlExpr):
        raise ValueError("Only Polars expressions can be passed to the `mask` argument.")

    frame: PlDataFrame = data._tbl_data
    frame_cols = frame.columns

    stub_var = data._boxhead.vars_from_type(ColInfoTypeEnum.stub)
    group_var = data._boxhead.vars_from_type(ColInfoTypeEnum.row_group)
    cols_excl = [*(stub_var if excl_stub else []), *(group_var if excl_group else [])]

    # `df.select()` raises `ColumnNotFoundError` if columns are missing from the original DataFrame.
    masked = frame.select(expr).drop(cols_excl, strict=False)

    # Validate that `masked.columns` exist in the `frame_cols`
    missing = set(masked.columns) - set(frame_cols)
    if missing:
        raise ValueError(
            "The `mask` expression produces extra columns, with names not in the original DataFrame."
            f"\n\nExtra columns: {missing}"
        )

    # Validate that row lengths are equal
    if masked.height != frame.height:
        raise ValueError(
            "The DataFrame length after applying `mask` differs from the original."
            f"\n\n* Original length: {frame.height}"
            f"\n* Mask length: {masked.height}"
        )

    cellpos_data: list[tuple[int, int, str]] = []  # column, row, colname for `CellPos`
    col_idx_map = {colname: frame_cols.index(colname) for colname in frame_cols}
    for row_idx, row_dict in enumerate(masked.iter_rows(named=True)):
        for colname, value in row_dict.items():
            if value:  # select only when `value` is True
                col_idx = col_idx_map[colname]
                cellpos_data.append((col_idx, row_idx, colname))
    return cellpos_data
Return data for creating `CellPos`, based on expr
great_tables/_locations.py