Skip to content
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
41 changes: 22 additions & 19 deletions slack_sdk/models/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def __init__(
self.blocks = Block.parse_all(blocks)
self.private_metadata = private_metadata
self.state = state
if self.state is not None and isinstance(self.state, dict):
self.state = ViewState(**self.state)
self.hash = hash
self.clear_on_close = clear_on_close
self.notify_on_close = notify_on_close
Expand Down Expand Up @@ -155,29 +157,30 @@ def __init__(
):
value_objects: Dict[str, Dict[str, ViewStateValue]] = {}
new_state_values = copy.copy(values)
for block_id, actions in new_state_values.items():
if actions is None: # skipcq: PYL-R1724
continue
elif isinstance(actions, dict):
new_actions = copy.copy(actions)
for action_id, v in actions.items():
if isinstance(v, dict):
d = copy.copy(v)
value_object = ViewStateValue(**d)
elif isinstance(v, ViewStateValue):
value_object = v
else:
self._show_warning_about_unknown(v)
continue
new_actions[action_id] = value_object
value_objects[block_id] = new_actions
else:
self._show_warning_about_unknown(v)
if isinstance(new_state_values, dict): # just in case
for block_id, actions in new_state_values.items():
if actions is None: # skipcq: PYL-R1724
continue
elif isinstance(actions, dict):
new_actions = copy.copy(actions)
for action_id, v in actions.items():
if isinstance(v, dict):
d = copy.copy(v)
value_object = ViewStateValue(**d)
elif isinstance(v, ViewStateValue):
value_object = v
else:
self._show_warning_about_unknown(v)
continue
new_actions[action_id] = value_object
value_objects[block_id] = new_actions
else:
self._show_warning_about_unknown(v)
self.values = value_objects

def to_dict(self, *args) -> Dict[str, Dict[str, Dict[str, dict]]]: # type: ignore
self.validate_json()
if self.values: # skipcq: PYL-R1705
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As we no longer use the code analyzer, we can remove this type of comment.

if self.values is not None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been a potential bug - {} is a falsy value.

dict_values: Dict[str, Dict[str, dict]] = {}
for block_id, actions in self.values.items():
if actions:
Expand Down
2 changes: 1 addition & 1 deletion tests/data/view_modal_009.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"callback_id": "view_4",
"external_id": "some-unique-id",
"state": {
"values": []
"values": {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been a potential issue of test data. The values usually cannot be an array.

},
"hash": "1569362015.55b5e41b",
"clear_on_close": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/data/view_modal_010.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"callback_id": "identify_your_modals",
"external_id": "some-unique-id",
"state": {
"values": []
"values": {}
},
"hash": "156772938.1827394",
"clear_on_close": false,
Expand Down
5 changes: 4 additions & 1 deletion tests/slack_sdk/models/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ def test_document(self):
"type": "timepicker",
"action_id": "timepicker123",
"initial_time": "11:40",
"placeholder": {"type": "plain_text", "text": "Select a time",},
"placeholder": {
"type": "plain_text",
"text": "Select a time",
},
}
self.assertDictEqual(input, TimePickerElement(**input).to_dict())

Expand Down
13 changes: 13 additions & 0 deletions tests/slack_sdk/models/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def setUp(self) -> None:
def verify_loaded_view_object(self, file):
input = json.load(file)
view = View(**input)
self.assertTrue(view.state is None or isinstance(view.state, ViewState))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This assertion used to be failing before the change. Before the change in this PR, the passing one is self.assertTrue(view.state is None or isinstance(view.state, dict)).

self.assertDictEqual(input, view.to_dict())

# --------------------------------
Expand Down Expand Up @@ -110,6 +111,18 @@ def test_valid_construction(self):
),
),
],
state=ViewState(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just in case, added test pattern to pass a ViewState object, not a dict.

values={
"b1": {
"a1": ViewStateValue(type="plain_text_input", value="Title")
},
"b2": {
"a2": ViewStateValue(
type="plain_text_input", value="Description"
)
},
}
),
)
modal_view.validate_json()

Expand Down
2 changes: 1 addition & 1 deletion tests/slack_sdk_fixture/view_modal_009.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"callback_id": "view_4",
"external_id": "some-unique-id",
"state": {
"values": []
"values": {}
},
"hash": "1569362015.55b5e41b",
"clear_on_close": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/slack_sdk_fixture/view_modal_010.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"callback_id": "identify_your_modals",
"external_id": "some-unique-id",
"state": {
"values": []
"values": {}
},
"hash": "156772938.1827394",
"clear_on_close": false,
Expand Down