Skip to content

Commit 8667128

Browse files
committed
refactor: extract validation logic into separate functions for clarity
1 parent 194ed46 commit 8667128

1 file changed

Lines changed: 41 additions & 28 deletions

File tree

python/datafusion/html_formatter.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,41 @@
2424
Optional,
2525
Protocol,
2626
runtime_checkable,
27+
TypeVar,
28+
Union,
2729
)
2830

2931

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+
3062
@runtime_checkable
3163
class CellFormatter(Protocol):
3264
"""Protocol for cell value formatters."""
@@ -161,36 +193,17 @@ def __init__(
161193
protocol.
162194
"""
163195
# 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")
183202

184203
# 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")
194207

195208
# Validate custom_css
196209
if custom_css is not None and not isinstance(custom_css, str):

0 commit comments

Comments
 (0)