8000 refactor: enhance DataFrameHtmlFormatter initialization with paramete… · kosiew/datafusion-python@70faac2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70faac2

Browse files
committed
refactor: enhance DataFrameHtmlFormatter initialization with parameter validation
1 parent c837771 commit 70faac2

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

python/datafusion/html_formatter.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
"""HTML formatting utilities for DataFusion DataFrames."""
22

3-
from typing import Any, Callable, Dict, List, Optional, Protocol, Type
4-
5-
3+
from typing import (
4+
Any,
5+
Callable,
6+
Dict,
7+
List,
8+
Optional,
9+
Protocol,
10+
Type,
11+
runtime_checkable,
12+
)
13+
14+
15+
@runtime_checkable
616
class CellFormatter(Protocol):
717
"""Protocol for cell value formatters."""
818

@@ -11,6 +21,7 @@ def __call__(self, value: Any) -> str:
1121
...
1222

1323

24+
@runtime_checkable
1425
class StyleProvider(Protocol):
1526
"""Protocol for HTML style providers."""
1627

@@ -78,6 +89,28 @@ def __init__(
7889
show_truncation_message: bool = True,
7990
style_provider: Optional[StyleProvider] = None,
8091
):
92+
# Validate numeric parameters
93+
if not isinstance(max_cell_length, int) or max_cell_length <= 0:
94+
raise ValueError("max_cell_length must be a positive integer")
95+
if not isinstance(max_width, int) or max_width <= 0:
96+
raise ValueError("max_width must be a positive integer")
97+
if not isinstance(max_height, int) or max_height <= 0:
98+
raise ValueError("max_height must be a positive integer")
99+
100+
# Validate boolean parameters
101+
if not isinstance(enable_cell_expansion, bool):
102+
raise TypeError("enable_cell_expansion must be a boolean")
103+
if not isinstance(show_truncation_message, bool):
104+
raise TypeError("show_truncation_message must be a boolean")
105+
106+
# Validate custom_css
107+
if custom_css is not None and not isinstance(custom_css, str):
108+
raise TypeError("custom_css must be None or a string")
109+
110+
# Validate style_provider
111+
if style_provider is not None and not isinstance(style_provider, StyleProvider):
112+
raise TypeError("style_provider must implement the StyleProvider protocol")
113+
81114
self.max_cell_length = max_cell_length
82115
self.max_width = max_width
83116
self.max_height = max_height

0 commit comments

Comments
 (0)
0