Skip to content

Commit

Permalink
fix text expansion in top-level fields
Browse files Browse the repository at this point in the history
  • Loading branch information
imryche committed Mar 20, 2024
1 parent 3779ad4 commit 1cbd9d4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
6 changes: 2 additions & 4 deletions blockkit/components.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
from typing import Any, List, Type
from typing import get_type_hints

from pydantic import BaseModel, model_validator

Expand All @@ -19,9 +18,8 @@ def get_args(tp):
class Component(BaseModel):
@model_validator(mode="after")
def expand_strings(self) -> Any:
hints = get_type_hints(self)
for field_name, types in hints.items():
inner_types = self._get_inner_types(types)
for field_name, field in self.model_fields.items():
inner_types = self._get_inner_types(field.annotation)
if not inner_types:
continue

Expand Down
6 changes: 4 additions & 2 deletions blockkit/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"RichTextQuote",
"RichTextSection",
"RichTextList",
"FileInput"
"FileInput",
]


Expand Down Expand Up @@ -719,6 +719,7 @@ def __init__(
):
super().__init__(elements=elements, style=style, indent=indent)


class FileInput(ActionableComponent):
type: str = "file_input"
filetypes: Optional[List[str]] = Field(None)
Expand All @@ -735,4 +736,5 @@ def __init__(
action_id=action_id,
filetypes=filetypes,
max_files=max_files,
)
)

10 changes: 10 additions & 0 deletions blockkit/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class MarkdownText(Component):
def __init__(self, *, text: str, verbatim: Optional[bool] = None):
super().__init__(text=text, verbatim=verbatim)

def __eq__(self, other):
if not isinstance(other, type(self)):
return False
return self.text == other.text


class PlainText(Component):
type: str = "plain_text"
Expand All @@ -38,6 +43,11 @@ class PlainText(Component):
def __init__(self, *, text: str, emoji: Optional[bool] = None):
super().__init__(text=text, emoji=emoji)

def __eq__(self, other):
if not isinstance(other, type(self)):
return False
return self.text == other.text


class Style(Component):
bold: Optional[bool] = None
Expand Down
16 changes: 11 additions & 5 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,10 @@ def test_image_excessive_alt_text_raises_exception():

def test_builds_static_select_with_options():
assert StaticSelect(
placeholder=PlainText(text="placeholder"),
placeholder="placeholder",
action_id="action_id",
options=[
PlainOption(text=PlainText(text="option 1"), value="value_1"),
PlainOption(text="option 1", value="value_1"),
PlainOption(text=PlainText(text="option 2"), value="value_2"),
],
initial_option=PlainOption(text=PlainText(text="option 1"), value="value_1"),
Expand All @@ -334,10 +334,13 @@ def test_builds_static_select_with_options():
focus_on_load=True,
).build() == {
"type": "static_select",
"placeholder": {"type": "plain_text", "text": "placeholder"},
"placeholder": {"type": "plain_text", "text": "placeholder", "emoji": True},
"action_id": "action_id",
"options": [
{"text": {"type": "plain_text", "text": "option 1"}, "value": "value_1"},
{
"text": {"type": "plain_text", "text": "option 1", "emoji": True},
"value": "value_1",
},
{"text": {"type": "plain_text", "text": "option 2"}, "value": "value_2"},
],
"initial_option": {
Expand Down Expand Up @@ -1559,6 +1562,7 @@ def test_timepicker_excessive_placeholder_raises_exception():
with pytest.raises(ValidationError):
TimePicker(placeholder=PlainText(text="p" * 151))


def test_builds_fileinput():
assert FileInput(
action_id="action_id",
Expand All @@ -1571,6 +1575,8 @@ def test_builds_fileinput():
"max_files": 1,
}


def test_fileinput_excessive_max_files_raises_exception():
with pytest.raises(ValidationError):
FileInput(max_files=11)
FileInput(max_files=11)

0 comments on commit 1cbd9d4

Please sign in to comment.