Repository: slackapi/python-slack-sdk
Pull request #988 (2021-04-02 02:30:18)

Setting a default value for select_options for ViewStateValue
## Summary

This pull request adds a default `selected_options` attribute for `ViewStateValue` objects. This makes it consistent with the other attributes of this class. This is related to [Issue 987](https://github.com/slackapi/python-slack-sdk/issues/987).

### Category (place an `x` in each of the `[ ]`)

- [ ] **slack_sdk.web.WebClient (sync/async)** (Web API client)
- [ ] **slack_sdk.webhook.WebhookClient (sync/async)** (Incoming Webhook, response_url sender)
- [x] **slack_sdk.models** (UI component builders)
- [ ] **slack_sdk.oauth** (OAuth Flow Utilities)
- [ ] **slack_sdk.socket_mode** (Socket Mode client)
- [ ] **slack_sdk.audit_logs** (Audit Logs API client)
- [ ] **slack_sdk.scim** (SCIM API client)
- [ ] **slack_sdk.rtm** (RTM client)
- [ ] **slack_sdk.signature** (Request Signature Verifier)
- [ ] `/docs-src` (Documents, have you run `./docs.sh`?)
- [ ] `/docs-src-v2` (Documents, have you run `./docs-v2.sh`?)
- [ ] `/tutorial` (PythOnBoardingBot tutorial)
- [ ] `tests`/`integration_tests` (Automated tests for this library)

## Requirements (place an `x` in each `[ ]`)

- [x] I've read and understood the [Contributing Guidelines](https://github.com/slackapi/python-slack-sdk/blob/main/.github/contributing.md) and have done my best effort to follow them.
- [x] I've read and agree to the [Code of Conduct](https://slackhq.github.io/code-of-conduct).
- [x] I've run `python setup.py validate` after making the changes.


CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/slackapi/python-slack-sdk?pullRequest=988) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/slackapi/python-slack-sdk?pullRequest=988) before we can accept your contribution.<br/><hr/>**Scott Shields** seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please [add the email address used for this commit to your account](https://help.github.com/articles/why-are-my-commits-linked-to-the-wrong-user/#commits-are-not-linked-to-any-user).<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/slackapi/python-slack-sdk?pullRequest=988) it.</sub>
seratch: @scott-shields-github Thanks a lot for taking the time to make this pull request! The changes already look good to me 👍 but would you mind signing our CLA? Refer to the above comment by the CLA assistant bot for details 🙇 
scott-shields-github: Hi @seratch I signed the CLA yesterday, but it appears to be pending. When I follow the link now it just shows all of my information, including the checked approval box, without a submit button.
seratch: @scott-shields-github as the bot says, perhaps the email address you used for the git commit is not associated with your github account.
scott-shields-github: Ah, I see what happened. I accidentally pushed with the wrong email. Should be all set now. Thanks!
codecov[bot]: # [Codecov](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988?src=pr&el=h1) Report
> Merging [#988](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988?src=pr&el=desc) (be2190c) into [main](https://codecov.io/gh/slackapi/python-slack-sdk/commit/83c6f9c6d24592cdfb1632dc761c4d160512a2b6?el=desc) (83c6f9c) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988/graphs/tree.svg?width=650&height=150&src=pr&token=F0O7meYmtD)](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##             main     #988      +/-   ##
==========================================
+ Coverage   87.26%   87.28%   +0.01%     
==========================================
  Files          94       94              
  Lines        8591     8595       +4     
==========================================
+ Hits         7497     7502       +5     
+ Misses       1094     1093       -1     
```


| [Impacted Files](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [slack\_sdk/models/views/\_\_init\_\_.py](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988/diff?src=pr&el=tree#diff-c2xhY2tfc2RrL21vZGVscy92aWV3cy9fX2luaXRfXy5weQ==) | `91.73% <100.00%> (+1.13%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988?src=pr&el=footer). Last update [83c6f9c...be2190c](https://codecov.io/gh/slackapi/python-slack-sdk/pull/988?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/slack_sdk/models/views/__init__.py b/slack_sdk/models/views/__init__.py
index 94b26bf..f524fa5 100644
--- a/slack_sdk/models/views/__init__.py
+++ b/slack_sdk/models/views/__init__.py
@@ -231,8 +231,13 @@ class ViewStateValue(JsonObject):
         self.selected_conversations = selected_conversations
         self.selected_channels = selected_channels
         self.selected_users = selected_users
-        if selected_options:
-            if isinstance(selected_options, dict):
-                self.selected_options = [Option(**d) for d in selected_options]
-            else:
-                self.selected_options = selected_options
+
+        if isinstance(selected_options, list):
+            self.selected_options = []
+            for option in selected_options:
+                if isinstance(option, Option):
+                    self.selected_options.append(option)
+                elif isinstance(option, dict):
+                    self.selected_options.append(Option(**option))
+        else:
+            self.selected_options = selected_options


Test patch:
diff --git a/tests/slack_sdk/models/test_views.py b/tests/slack_sdk/models/test_views.py
index 500b09c..631093c 100644
--- a/tests/slack_sdk/models/test_views.py
+++ b/tests/slack_sdk/models/test_views.py
@@ -351,6 +351,44 @@ class ViewTests(unittest.TestCase):
         self.assertDictEqual(expected, ViewState(**expected).to_dict())
         self.assertDictEqual(expected, state.to_dict())
 
+    def test_view_state_value_empty_selected_options(self):
+        input = {
+            'type': 'checkboxes',
+            'selected_options': []
+        }
+
+        view_state_value = ViewStateValue(**input)
+        assert view_state_value.selected_options == []
+
+    def test_view_state_value_with_selected_options(self):
+        expected = {
+            "type": "checkboxes",
+            "selected_options": [
+                {
+                    "text": {
+                        "type": "plain_text",
+                        "text": "test_option_text"
+                    },
+                    "value": "test_option_value"
+                }
+            ],
+        }
+
+        input = {
+            "type": "checkboxes",
+            "selected_options": [
+                {
+                    "text": {
+                        "type": "plain_text",
+                        "text": "test_option_text"
+                    },
+                    "value": "test_option_value"
+                }
+            ]
+        }
+
+        self.assertDictEqual(expected, ViewStateValue(**input).to_dict())
+
     def test_load_modal_view_001(self):
         with open("tests/slack_sdk_fixture/view_modal_001.json") as file:
             self.verify_loaded_view_object(file)