Skip to content

Commit 64f65f9

Browse files
committed
Close #1704: fix layout_columns() logic that warns about number of (positive) widths vs children
1 parent 1205dff commit 64f65f9

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

shiny/ui/_layout_columns.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,31 +186,35 @@ def validate_col_width(
186186
x: Iterable[int] | int, n_kids: int, break_name: Breakpoint
187187
) -> Iterable[int]:
188188
if isinstance(x, int):
189-
y = [x]
189+
widths = [x]
190190
else:
191-
y = x
191+
widths = list(x)
192192

193-
if not all(isinstance(i, int) for i in y):
194-
raise ValueError(
195-
f"Column values at breakpoint '{break_name}' must be integers. Values greater than 0 indicate width, and negative values indicate a column offset."
196-
)
197-
198-
if any(i == 0 for i in y):
199-
raise ValueError(
200-
f"Column values at breakpoint '{break_name}' must be greater than 0 to indicate width, or negative to indicate a column offset."
201-
)
193+
positive_widths: list[int] = []
194+
for w in widths:
195+
if not isinstance(w, int):
196+
raise TypeError(
197+
f"Column widths at breakpoint '{break_name}' must be integers. Got {type(w).__name__}."
198+
)
199+
if w == 0:
200+
raise ValueError(
201+
f"Column widths at breakpoint '{break_name}' must be greater than 0 to indicate width, or negative to indicate a column offset."
202+
)
203+
if w > 0:
204+
positive_widths.append(w)
202205

203-
if not any(b > 0 for b in y):
206+
if not positive_widths:
204207
raise ValueError(
205-
f"Column values at breakpoint '{break_name}' must include at least one positive integer width."
208+
f"Column widths at breakpoint '{break_name}' must include at least one positive integer width."
206209
)
207210

208-
if len(list(y)) > n_kids:
211+
if len(positive_widths) > n_kids:
209212
warn(
210-
f"More column widths than children at breakpoint '{break_name}', extra widths will be ignored."
213+
f"More column widths than children at breakpoint '{break_name}', extra widths will be ignored.",
214+
stacklevel=2,
211215
)
212216

213-
return y
217+
return widths
214218

215219

216220
def col_widths_attrs(col_widths: BreakpointsOptional[int] | None) -> TagAttrs:

0 commit comments

Comments
 (0)