diff --git a/elementary/messages/block_builders.py b/elementary/messages/block_builders.py index 31c8d9ba2..dbb6401f9 100644 --- a/elementary/messages/block_builders.py +++ b/elementary/messages/block_builders.py @@ -1,5 +1,5 @@ import json -from typing import List, Optional, Tuple, Union +from typing import List, Optional, Sequence, Tuple, Union from .blocks import ( CodeBlock, @@ -16,7 +16,7 @@ ) SimpleInlineBlock = Union[str, Icon] -SimpleLineBlock = Union[str, Icon, List[SimpleInlineBlock]] +SimpleLineBlock = Union[str, Icon, Sequence[SimpleInlineBlock]] def _build_inline_block( @@ -28,9 +28,9 @@ def _build_inline_block( def _build_inlines( content: SimpleLineBlock, style: Optional[TextStyle] = None ) -> List[InlineBlock]: - if isinstance(content, list): - return [_build_inline_block(line, style) for line in content] - return [_build_inline_block(content)] + if isinstance(content, (str, Icon)): + return [_build_inline_block(content, style)] + return [_build_inline_block(line, style) for line in content] def BulletListBlock( @@ -73,7 +73,7 @@ def LinkLineBlock(*, text: str, url: str) -> LineBlock: def SummaryLineBlock( *, - summary: List[Tuple[SimpleLineBlock, SimpleLineBlock]], + summary: Sequence[Tuple[SimpleLineBlock, SimpleLineBlock]], include_empty_values: bool = False, ) -> LineBlock: text_blocks: List[InlineBlock] = [] @@ -89,7 +89,7 @@ def SummaryLineBlock( def FactsBlock( *, - facts: List[ + facts: Sequence[ Tuple[ SimpleLineBlock, SimpleLineBlock, diff --git a/elementary/messages/blocks.py b/elementary/messages/blocks.py index ec0b3201e..a72f5ee4a 100644 --- a/elementary/messages/blocks.py +++ b/elementary/messages/blocks.py @@ -2,6 +2,7 @@ from typing import List, Optional, Union from pydantic import BaseModel +from typing_extensions import Literal class Icon(Enum): @@ -25,6 +26,7 @@ class TextStyle(Enum): class BaseBlock(BaseModel): + type: str pass @@ -33,16 +35,19 @@ class BaseInlineTextBlock(BaseBlock): class TextBlock(BaseInlineTextBlock): + type: Literal["text"] = "text" text: str style: Optional[TextStyle] = None class LinkBlock(BaseInlineTextBlock): + type: Literal["link"] = "link" text: str url: str class IconBlock(BaseInlineTextBlock): + type: Literal["icon"] = "icon" icon: Icon @@ -50,18 +55,21 @@ class IconBlock(BaseInlineTextBlock): class HeaderBlock(BaseBlock): + type: Literal["header"] = "header" text: str class CodeBlock(BaseBlock): + type: Literal["code"] = "code" text: str class DividerBlock(BaseBlock): - pass + type: Literal["divider"] = "divider" class LineBlock(BaseBlock): + type: Literal["line"] = "line" inlines: List[InlineBlock] sep: str = " " @@ -71,19 +79,22 @@ class BaseLinesBlock(BaseBlock): class LinesBlock(BaseLinesBlock): - pass + type: Literal["lines"] = "lines" class FactBlock(BaseBlock): + type: Literal["fact"] = "fact" title: LineBlock value: LineBlock class FactListBlock(BaseBlock): + type: Literal["fact_list"] = "fact_list" facts: List[FactBlock] class ExpandableBlock(BaseBlock): + type: Literal["expandable"] = "expandable" title: str body: List["InExpandableBlock"] expanded: bool = False @@ -98,4 +109,5 @@ class ExpandableBlock(BaseBlock): "ExpandableBlock", ] -ExpandableBlock.model_rebuild() +# Update forward references for recursive types +ExpandableBlock.update_forward_refs() diff --git a/elementary/messages/message_body.py b/elementary/messages/message_body.py index d2958727a..b41a76208 100644 --- a/elementary/messages/message_body.py +++ b/elementary/messages/message_body.py @@ -34,4 +34,4 @@ class MessageBody(BaseModel): color: Optional[Color] = None -MessageBody.model_rebuild() +MessageBody.update_forward_refs()