Skip to content

Commit 7294cf4

Browse files
srtaalejzimeg
andauthored
feat(models): add typed model classes for TableBlock cells and column_settings (#1939)
Co-authored-by: Eden Zimbelman <eden.zimbelman@salesforce.com>
1 parent ae87985 commit 7294cf4

4 files changed

Lines changed: 122 additions & 4 deletions

File tree

‎slack_sdk/models/blocks/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
OptionGroup,
1818
PlainTextObject,
1919
RawTextObject,
20+
TableBlockColumnSettings,
2021
TextObject,
2122
)
2223
from .block_elements import (
@@ -92,6 +93,7 @@
9293
"OptionGroup",
9394
"PlainTextObject",
9495
"RawTextObject",
96+
"TableBlockColumnSettings",
9597
"TextObject",
9698
"BlockElement",
9799
"ButtonElement",

‎slack_sdk/models/blocks/basic_components.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,46 @@ def _validate_text_min_length(self):
191191
return len(self.text) >= 1
192192

193193

194+
class TableBlockColumnSettings(JsonObject):
195+
"""Column settings for TableBlock columns."""
196+
197+
@property
198+
def attributes(self) -> Set[str]:
199+
return {"align", "is_wrapped"}
200+
201+
def __init__(
202+
self,
203+
*,
204+
align: Optional[str] = None,
205+
is_wrapped: Optional[bool] = None,
206+
**others: dict,
207+
):
208+
"""Settings for a single column in a table block.
209+
https://docs.slack.dev/reference/block-kit/blocks/table-block
210+
211+
Args:
212+
align: The alignment for items in this column. Can be "left", "center", or "right".
213+
Defaults to "left" if not defined.
214+
is_wrapped: Whether the contents of this column should be wrapped or not.
215+
Defaults to false if not defined.
216+
"""
217+
show_unknown_key_warning(self, others)
218+
self.align = align
219+
self.is_wrapped = is_wrapped
220+
221+
@classmethod
222+
def parse(
223+
cls, settings: Optional[Union[Dict[str, Any], "TableBlockColumnSettings"]]
224+
) -> Optional["TableBlockColumnSettings"]:
225+
if settings is None:
226+
return None
227+
if isinstance(settings, TableBlockColumnSettings):
228+
return settings
229+
if isinstance(settings, dict):
230+
return TableBlockColumnSettings(**settings)
231+
return None
232+
233+
194234
class Option(JsonObject):
195235
"""Option object used in dialogs, legacy message actions (interactivity in attachments),
196236
and blocks. JSON must be retrieved with an explicit option_type - the Slack API has

‎slack_sdk/models/blocks/blocks.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
from slack_sdk.models.basic_objects import JsonObject, JsonValidator
88

99
from ...errors import SlackObjectFormationError
10-
from .basic_components import MarkdownTextObject, PlainTextObject, SlackFile, TextObject
10+
from .basic_components import (
11+
MarkdownTextObject,
12+
PlainTextObject,
13+
RawTextObject,
14+
SlackFile,
15+
TableBlockColumnSettings,
16+
TextObject,
17+
)
1118
from .block_elements import (
1219
BlockElement,
1320
FeedbackButtonsElement,
@@ -756,8 +763,8 @@ def attributes(self) -> Set[str]: # type: ignore[override]
756763
def __init__(
757764
self,
758765
*,
759-
rows: Sequence[Sequence[Dict[str, Any]]],
760-
column_settings: Optional[Sequence[Optional[Dict[str, Any]]]] = None,
766+
rows: Sequence[Sequence[Union[Dict[str, Any], "RawTextObject", "RichTextBlock"]]],
767+
column_settings: Optional[Sequence[Optional[Union[Dict[str, Any], "TableBlockColumnSettings"]]]] = None,
761768
block_id: Optional[str] = None,
762769
**others: dict,
763770
):

‎tests/slack_sdk/models/test_blocks.py‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
SectionBlock,
3636
StaticSelectElement,
3737
TableBlock,
38+
TableBlockColumnSettings,
3839
TaskCardBlock,
3940
VideoBlock,
4041
)
4142
from slack_sdk.models.blocks.basic_components import FeedbackButtonObject, SlackFile
42-
from slack_sdk.models.blocks.block_elements import FeedbackButtonsElement, IconButtonElement, ImageElement
43+
from slack_sdk.models.blocks.block_elements import FeedbackButtonsElement, IconButtonElement
4344

4445
from . import STRING_3001_CHARS
4546

@@ -1462,6 +1463,74 @@ def test_with_block_id(self):
14621463
}
14631464
self.assertDictEqual(input, TableBlock(**input).to_dict())
14641465

1466+
def test_with_column_settings_objects(self):
1467+
"""Test table using typed TableBlockColumnSettings objects"""
1468+
block = TableBlock(
1469+
rows=[[{"type": "raw_text", "text": "A"}, {"type": "raw_text", "text": "B"}]],
1470+
column_settings=[
1471+
TableBlockColumnSettings(align="right", is_wrapped=True),
1472+
TableBlockColumnSettings(align="left"),
1473+
],
1474+
)
1475+
expected = {
1476+
"type": "table",
1477+
"column_settings": [{"align": "right", "is_wrapped": True}, {"align": "left"}],
1478+
"rows": [[{"type": "raw_text", "text": "A"}, {"type": "raw_text", "text": "B"}]],
1479+
}
1480+
self.assertDictEqual(expected, block.to_dict())
1481+
1482+
def test_with_rich_text_cell_objects(self):
1483+
"""Test table using typed RichTextBlock objects"""
1484+
cell = RichTextBlock(
1485+
elements=[RichTextSectionElement(elements=[RichTextElementParts.Text(text="Hello")])]
1486+
)
1487+
block = TableBlock(
1488+
rows=[
1489+
[RawTextObject(text="Header"), cell],
1490+
],
1491+
)
1492+
expected = {
1493+
"type": "table",
1494+
"rows": [
1495+
[
1496+
{"type": "raw_text", "text": "Header"},
1497+
{
1498+
"type": "rich_text",
1499+
"elements": [{"type": "rich_text_section", "elements": [{"type": "text", "text": "Hello"}]}],
1500+
},
1501+
]
1502+
],
1503+
}
1504+
self.assertDictEqual(expected, block.to_dict())
1505+
1506+
def test_mixed_typed_and_dict_cells(self):
1507+
"""Test table accepts a mix of typed objects and plain dicts"""
1508+
block = TableBlock(
1509+
rows=[
1510+
[RawTextObject(text="Col A"), RawTextObject(text="Col B")],
1511+
[
1512+
{"type": "raw_text", "text": "Data"},
1513+
RichTextBlock(elements=[{"type": "rich_text_section", "elements": [{"type": "text", "text": "rich"}]}]),
1514+
],
1515+
],
1516+
column_settings=[TableBlockColumnSettings(align="left"), {"align": "right"}],
1517+
)
1518+
expected = {
1519+
"type": "table",
1520+
"column_settings": [{"align": "left"}, {"align": "right"}],
1521+
"rows": [
1522+
[{"type": "raw_text", "text": "Col A"}, {"type": "raw_text", "text": "Col B"}],
1523+
[
1524+
{"type": "raw_text", "text": "Data"},
1525+
{
1526+
"type": "rich_text",
1527+
"elements": [{"type": "rich_text_section", "elements": [{"type": "text", "text": "rich"}]}],
1528+
},
1529+
],
1530+
],
1531+
}
1532+
self.assertDictEqual(expected, block.to_dict())
1533+
14651534
def test_column_settings_variations(self):
14661535
"""Test various column_settings configurations"""
14671536
# Left align

0 commit comments

Comments
 (0)