Skip to content

Commit a347a98

Browse files
seratchfilmaj
andauthored
Fix #1200 Model to_dict doesn't handle tuple sequences (#1206)
Co-authored-by: Fil Maj <[email protected]>
1 parent 268d7eb commit a347a98

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

slack_sdk/models/basic_objects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABCMeta, abstractmethod
22
from functools import wraps
3-
from typing import Callable, Iterable, Set, Union, Any
3+
from typing import Callable, Iterable, Set, Union, Any, Tuple
44

55
from slack_sdk.errors import SlackObjectFormationError
66

@@ -38,9 +38,9 @@ def get_non_null_attributes(self) -> dict:
3838
"""
3939

4040
def to_dict_compatible(
41-
value: Union[dict, list, object]
41+
value: Union[dict, list, object, Tuple]
4242
) -> Union[dict, list, Any]:
43-
if isinstance(value, list): # skipcq: PYL-R1705
43+
if isinstance(value, (list, Tuple)): # skipcq: PYL-R1705
4444
return [to_dict_compatible(v) for v in value]
4545
else:
4646
to_dict = getattr(value, "to_dict", None)

tests/slack_sdk/models/test_elements.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,28 @@ def test_focus_on_load(self):
641641
}
642642
self.assertDictEqual(input, StaticSelectElement(**input).to_dict())
643643

644+
def test_lists_and_tuples_serialize_to_dict_equally(self):
645+
expected = {
646+
"options": [
647+
{
648+
"text": {"emoji": True, "text": "X", "type": "plain_text"},
649+
"value": "x",
650+
}
651+
],
652+
"type": "static_select",
653+
}
654+
option = Option(value="x", text="X")
655+
# List
656+
self.assertDictEqual(
657+
expected,
658+
StaticSelectElement(options=[option]).to_dict(),
659+
)
660+
# Tuple (this pattern used to be failing)
661+
self.assertDictEqual(
662+
expected,
663+
StaticSelectElement(options=(option,)).to_dict(),
664+
)
665+
644666

645667
# -------------------------------------------------
646668
# External Data Source Select

0 commit comments

Comments
 (0)