|
24 | 24 | Optional, |
25 | 25 | Protocol, |
26 | 26 | runtime_checkable, |
| 27 | + TypeVar, |
| 28 | + Union, |
27 | 29 | ) |
28 | 30 |
|
29 | 31 |
|
| 32 | +def _validate_positive_int(value: Any, param_name: str) -> None: |
| 33 | + """Validate that a parameter is a positive integer. |
| 34 | +
|
| 35 | + Args: |
| 36 | + value: The value to validate |
| 37 | + param_name: Name of the parameter (used in error message) |
| 38 | +
|
| 39 | + Raises: |
| 40 | + ValueError: If the value is not a positive integer |
| 41 | + """ |
| 42 | + if not isinstance(value, int) or value <= 0: |
| 43 | + msg = f"{param_name} must be a positive integer" |
| 44 | + raise ValueError(msg) |
| 45 | + |
| 46 | + |
| 47 | +def _validate_bool(value: Any, param_name: str) -> None: |
| 48 | + """Validate that a parameter is a boolean. |
| 49 | +
|
| 50 | + Args: |
| 51 | + value: The value to validate |
| 52 | + param_name: Name of the parameter (used in error message) |
| 53 | +
|
| 54 | + Raises: |
| 55 | + TypeError: If the value is not a boolean |
| 56 | + """ |
| 57 | + if not isinstance(value, bool): |
| 58 | + msg = f"{param_name} must be a boolean" |
| 59 | + raise TypeError(msg) |
| 60 | + |
| 61 | + |
30 | 62 | @runtime_checkable |
31 | 63 | class CellFormatter(Protocol): |
32 | 64 | """Protocol for cell value formatters.""" |
@@ -161,36 +193,17 @@ def __init__( |
161 | 193 | protocol. |
162 | 194 | """ |
163 | 195 | # Validate numeric parameters |
164 | | - |
165 | | - if not isinstance(max_cell_length, int) or max_cell_length <= 0: |
166 | | - msg = "max_cell_length must be a positive integer" |
167 | | - raise ValueError(msg) |
168 | | - if not isinstance(max_width, int) or max_width <= 0: |
169 | | - msg = "max_width must be a positive integer" |
170 | | - raise ValueError(msg) |
171 | | - if not isinstance(max_height, int) or max_height <= 0: |
172 | | - msg = "max_height must be a positive integer" |
173 | | - raise ValueError(msg) |
174 | | - if not isinstance(max_memory_bytes, int) or max_memory_bytes <= 0: |
175 | | - msg = "max_memory_bytes must be a positive integer" |
176 | | - raise ValueError(msg) |
177 | | - if not isinstance(min_rows_display, int) or min_rows_display <= 0: |
178 | | - msg = "min_rows_display must be a positive integer" |
179 | | - raise ValueError(msg) |
180 | | - if not isinstance(repr_rows, int) or repr_rows <= 0: |
181 | | - msg = "repr_rows must be a positive integer" |
182 | | - raise ValueError(msg) |
| 196 | + _validate_positive_int(max_cell_length, "max_cell_length") |
| 197 | + _validate_positive_int(max_width, "max_width") |
| 198 | + _validate_positive_int(max_height, "max_height") |
| 199 | + _validate_positive_int(max_memory_bytes, "max_memory_bytes") |
| 200 | + _validate_positive_int(min_rows_display, "min_rows_display") |
| 201 | + _validate_positive_int(repr_rows, "repr_rows") |
183 | 202 |
|
184 | 203 | # Validate boolean parameters |
185 | | - if not isinstance(enable_cell_expansion, bool): |
186 | | - msg = "enable_cell_expansion must be a boolean" |
187 | | - raise TypeError(msg) |
188 | | - if not isinstance(show_truncation_message, bool): |
189 | | - msg = "show_truncation_message must be a boolean" |
190 | | - raise TypeError(msg) |
191 | | - if not isinstance(use_shared_styles, bool): |
192 | | - msg = "use_shared_styles must be a boolean" |
193 | | - raise TypeError(msg) |
| 204 | + _validate_bool(enable_cell_expansion, "enable_cell_expansion") |
| 205 | + _validate_bool(show_truncation_message, "show_truncation_message") |
| 206 | + _validate_bool(use_shared_styles, "use_shared_styles") |
194 | 207 |
|
195 | 208 | # Validate custom_css |
196 | 209 | if custom_css is not None and not isinstance(custom_css, str): |
|
0 commit comments