Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1258 Tuple value for blocks argument does not work for Web API calls #1259

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions slack_sdk/web/internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import warnings
from ssl import SSLContext
from typing import Dict, Union, Optional, Any, Sequence
from typing import Any, Dict, Optional, Sequence, Union
from urllib.parse import urljoin

from slack_sdk import version
Expand Down Expand Up @@ -189,12 +189,12 @@ def to_dict(obj: Union[Dict, Block, Attachment, Metadata]):
return obj

blocks = kwargs.get("blocks", None)
if blocks is not None and isinstance(blocks, list):
if blocks is not None and isinstance(blocks, Sequence):
dict_blocks = [to_dict(b) for b in blocks]
kwargs.update({"blocks": dict_blocks})

attachments = kwargs.get("attachments", None)
if attachments is not None and isinstance(attachments, list):
if attachments is not None and isinstance(attachments, Sequence):
dict_attachments = [to_dict(a) for a in attachments]
kwargs.update({"attachments": dict_attachments})

Expand Down
41 changes: 40 additions & 1 deletion tests/slack_sdk/web/test_internal_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import unittest
from slack_sdk.web.internal_utils import _build_unexpected_body_error_message
from typing import Dict, Sequence, Union

import pytest

from slack_sdk.models.attachments import Attachment
from slack_sdk.models.blocks import Block
from slack_sdk.web.internal_utils import (
_build_unexpected_body_error_message,
_parse_web_class_objects
)


class TestInternalUtils(unittest.TestCase):
Expand All @@ -16,3 +25,33 @@ def test_build_unexpected_body_error_message(self):
assert message.startswith(
"""Received a response in a non-JSON format: <!DOCTYPE html><html lang="en"><head><meta charset="utf-8">"""
)


@pytest.mark.parametrize("initial_blocks", [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

[Block(block_id="42"), Block(block_id="24")], # list
(Block(block_id="42"), Block(block_id="24"),), # tuple
])
def test_can_parse_sequence_of_blocks(initial_blocks: Sequence[Union[Dict, Block]]):
kwargs = {"blocks": initial_blocks}

_parse_web_class_objects(kwargs)

assert kwargs["blocks"]

for block in kwargs["blocks"]:
assert isinstance(block, Dict)


@pytest.mark.parametrize("initial_attachments", [
[Attachment(text="foo"), Attachment(text="bar")], # list
(Attachment(text="foo"), Attachment(text="bar"),), # tuple
])
def test_can_parse_sequence_of_attachments(initial_attachments: Sequence[Union[Dict, Attachment]]):
kwargs = {"attachments": initial_attachments}

_parse_web_class_objects(kwargs)

assert kwargs["attachments"]

for attachment in kwargs["attachments"]:
assert isinstance(attachment, Dict)