Skip to content

Commit 9c142c3

Browse files
committed
Add block type enum and update block classes with type annotations
- Introduced `BlockType` enum to define standardized block types - Updated block classes to include explicit type annotations using `Literal` types - Added type hints to base block classes to improve type safety
1 parent 98cef8a commit 9c142c3

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

elementary/messages/blocks.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import List, Optional, Union
33

44
from pydantic import BaseModel
5+
from typing_extensions import Literal
56

67

78
class Icon(Enum):
@@ -24,7 +25,22 @@ class TextStyle(Enum):
2425
ITALIC = "italic"
2526

2627

28+
class BlockType(Enum):
29+
HEADER = "header"
30+
CODE = "code"
31+
LINES = "lines"
32+
LINE = "line"
33+
TEXT = "text"
34+
LINK = "link"
35+
ICON = "icon"
36+
DIVIDER = "divider"
37+
FACT_LIST = "fact_list"
38+
FACT = "fact"
39+
EXPANDABLE = "expandable"
40+
41+
2742
class BaseBlock(BaseModel):
43+
type: BlockType
2844
pass
2945

3046

@@ -33,35 +49,41 @@ class BaseInlineTextBlock(BaseBlock):
3349

3450

3551
class TextBlock(BaseInlineTextBlock):
52+
type: Literal[BlockType.TEXT] = BlockType.TEXT
3653
text: str
3754
style: Optional[TextStyle] = None
3855

3956

4057
class LinkBlock(BaseInlineTextBlock):
58+
type: Literal[BlockType.LINK] = BlockType.LINK
4159
text: str
4260
url: str
4361

4462

4563
class IconBlock(BaseInlineTextBlock):
64+
type: Literal[BlockType.ICON] = BlockType.ICON
4665
icon: Icon
4766

4867

4968
InlineBlock = Union[TextBlock, LinkBlock, IconBlock]
5069

5170

5271
class HeaderBlock(BaseBlock):
72+
type: Literal[BlockType.HEADER] = BlockType.HEADER
5373
text: str
5474

5575

5676
class CodeBlock(BaseBlock):
77+
type: Literal[BlockType.CODE] = BlockType.CODE
5778
text: str
5879

5980

6081
class DividerBlock(BaseBlock):
61-
pass
82+
type: Literal[BlockType.DIVIDER] = BlockType.DIVIDER
6283

6384

6485
class LineBlock(BaseBlock):
86+
type: Literal[BlockType.LINE] = BlockType.LINE
6587
inlines: List[InlineBlock]
6688
sep: str = " "
6789

@@ -71,19 +93,22 @@ class BaseLinesBlock(BaseBlock):
7193

7294

7395
class LinesBlock(BaseLinesBlock):
74-
pass
96+
type: Literal[BlockType.LINES] = BlockType.LINES
7597

7698

7799
class FactBlock(BaseBlock):
100+
type: Literal[BlockType.FACT] = BlockType.FACT
78101
title: LineBlock
79102
value: LineBlock
80103

81104

82105
class FactListBlock(BaseBlock):
106+
type: Literal[BlockType.FACT_LIST] = BlockType.FACT_LIST
83107
facts: List[FactBlock]
84108

85109

86110
class ExpandableBlock(BaseBlock):
111+
type: Literal[BlockType.EXPANDABLE] = BlockType.EXPANDABLE
87112
title: str
88113
body: List["InExpandableBlock"]
89114
expanded: bool = False

0 commit comments

Comments
 (0)