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

RichTextElement.elements items are never promoted to a proper Python object type #1468

Closed
1 task done
kezabelle opened this issue Mar 5, 2024 · 4 comments · Fixed by #1492
Closed
1 task done

RichTextElement.elements items are never promoted to a proper Python object type #1468

kezabelle opened this issue Mar 5, 2024 · 4 comments · Fixed by #1492

Comments

@kezabelle
Copy link

Using the rich text editor widget with the following content:
Screenshot 2024-03-05 at 11 39 10
gets you an unordered list, which upon a view_submission event gets posted into the state values as:

JSON representation of state value
{
  "type": "rich_text",
  "elements": [
    {
      "type": "rich_text_list",
      "style": "bullet",
      "indent": 0,
      "elements": [
        {
          "type": "rich_text_section",
          "elements": [
            {
              "type": "text",
              "text": "a"
            }
          ]
        },
        {
          "type": "rich_text_section",
          "elements": [
            {
              "type": "text",
              "text": "b"
            }
          ]
        }
      ],
      "border": 0
    }
  ]
}

which can then be converted into an object like so:

>>> obj = Block.parse(example)
>>> type(obj)
slack_sdk.models.blocks.blocks.RichTextBlock
>>> len(obj.elements)
1
>>> type(obj.elements[0])
slack_sdk.models.blocks.block_elements.RichTextListElement

which is all well and good... But the child elements of the encountered RichTextElement subclass are not converted to the equivalent Python class, and always remain as a dict, effectively looking like the parsing "gave up" at a certain depth (which isn't strictly true):

>>> type(obj.elements[0].elements[0])
dict
>>> obj.elements[0].elements[0]
{'type': 'rich_text_section', 'elements': [{'type': 'text', 'text': 'a'}]}

As far as I can tell, this is because the RichTextListElement, RichTextPreformattedElement, RichTextQuoteElement and RichTextSectionElement never themselves do any parsing, as all of them are defined using:

self.elements = elements

instead of something like:

self.elements = BlockElement.parse_all(elements)

The latter of which would at least make it so that:

>>> type(obj.elements[0].elements[0])
slack_sdk.models.blocks.block_elements.RichTextSectionElement 

That still leaves some child elements unpromoted, because there's nothing which converts {'type': 'text', 'text': 'a'} to a RichTextElementParts.Text etc, but that feels like a somewhat separate issue.

Category

  • slack_sdk.models (UI component builders)
@seratch
Copy link
Member

seratch commented Mar 6, 2024

Hi @kezabelle, thanks for reporting this! We will resolve the issue in the next release.

@k1e1n04
Copy link
Contributor

k1e1n04 commented May 1, 2024

Hello, @seratch
I would like to work on this issue.

I believe I can submit a PR within a few days.

I have made changes to the RichTextListElement, RichTextPreformattedElement, RichTextQuoteElement and RichTextSectionElement classes in the slack_sdk/models/blocks/blocks.py file, and I am planning to update the tests in the tests/slack_sdk/models/test_blocks.py file next. Please let me know if there is anything else I need to consider before proceeding. Thank you.

@seratch
Copy link
Member

seratch commented May 2, 2024

Thank you. If you meant block_elements.py, that should be good to go. Having unit test addition would be also appreciated.

@k1e1n04
Copy link
Contributor

k1e1n04 commented May 2, 2024

@seratch
It was block_elements.py, not blocks.py 🙏
I have created a PR, please check it when you have time.

seratch added a commit that referenced this issue May 3, 2024
…oper Python object type (#1492)

Co-authored-by: Kazuhiro Sera <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants