Skip to content

Commit dd4e596

Browse files
models: Type add-text color fields as fixed channel tuples
Use explicit channel tuple types for add-text colors: - `text_color_rgb` -> `tuple[RgbChannel, RgbChannel, RgbChannel]` - `text_color_cmyk` -> `tuple[CmykChannel, CmykChannel, CmykChannel, CmykChannel]` Assisted-by: Codex
1 parent 38d8daa commit dd4e596

1 file changed

Lines changed: 16 additions & 18 deletions

File tree

src/pdfrest/models/_internal.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -585,14 +585,15 @@ class ExtractImagesPayload(BaseModel):
585585
CmykChannel = Annotated[int, Field(ge=0, le=100)]
586586

587587

588-
def _validate_rgb_values(value: list[Any] | tuple[Any, ...] | None) -> list[int] | None:
589-
if value is None:
588+
def _validate_rgb_values(value: Any) -> tuple[int, int, int] | None:
589+
normalized = _split_comma_string(value)
590+
if normalized is None:
590591
return None
591-
if len(value) != 3:
592+
if len(normalized) != 3:
592593
msg = "text_color_rgb must have exactly 3 values."
593594
raise ValueError(msg)
594595
channels: list[int] = []
595-
for channel in value:
596+
for channel in normalized:
596597
try:
597598
numeric = int(channel)
598599
except (TypeError, ValueError) as exc:
@@ -602,19 +603,18 @@ def _validate_rgb_values(value: list[Any] | tuple[Any, ...] | None) -> list[int]
602603
msg = "text_color_rgb values must be between 0 and 255."
603604
raise ValueError(msg)
604605
channels.append(numeric)
605-
return channels
606+
return (channels[0], channels[1], channels[2])
606607

607608

608-
def _validate_cmyk_values(
609-
value: list[Any] | tuple[Any, ...] | None,
610-
) -> list[int] | None:
611-
if value is None:
609+
def _validate_cmyk_values(value: Any) -> tuple[int, int, int, int] | None:
610+
normalized = _split_comma_string(value)
611+
if normalized is None:
612612
return None
613-
if len(value) != 4:
613+
if len(normalized) != 4:
614614
msg = "text_color_cmyk must have exactly 4 values."
615615
raise ValueError(msg)
616616
channels: list[int] = []
617-
for channel in value:
617+
for channel in normalized:
618618
try:
619619
numeric = int(channel)
620620
except (TypeError, ValueError) as exc:
@@ -624,7 +624,7 @@ def _validate_cmyk_values(
624624
msg = "text_color_cmyk values must be between 0 and 100."
625625
raise ValueError(msg)
626626
channels.append(numeric)
627-
return channels
627+
return (channels[0], channels[1], channels[2], channels[3])
628628

629629

630630
def _validate_add_text_page(value: str | int) -> str | int:
@@ -1118,17 +1118,15 @@ class PdfAddTextObjectModel(BaseModel):
11181118
rotation: Annotated[float, Field(serialization_alias="rotation")]
11191119
text: Annotated[str, Field(min_length=1, serialization_alias="text")]
11201120
text_color_rgb: Annotated[
1121-
list[int] | tuple[int, ...] | None,
1121+
tuple[RgbChannel, RgbChannel, RgbChannel] | None,
11221122
Field(serialization_alias="text_color_rgb", default=None),
1123-
BeforeValidator(_split_comma_string),
1124-
AfterValidator(_validate_rgb_values),
1123+
BeforeValidator(_validate_rgb_values),
11251124
PlainSerializer(_serialize_as_comma_separated_string),
11261125
] = None
11271126
text_color_cmyk: Annotated[
1128-
list[int] | tuple[int, ...] | None,
1127+
tuple[CmykChannel, CmykChannel, CmykChannel, CmykChannel] | None,
11291128
Field(serialization_alias="text_color_cmyk", default=None),
1130-
BeforeValidator(_split_comma_string),
1131-
AfterValidator(_validate_cmyk_values),
1129+
BeforeValidator(_validate_cmyk_values),
11321130
PlainSerializer(_serialize_as_comma_separated_string),
11331131
] = None
11341132
text_size: Annotated[

0 commit comments

Comments
 (0)