1
+ from collections import UserList
1
2
import contextlib
2
3
import uuid
3
4
from abc import abstractmethod
4
- from typing import Any , Dict , Iterator , List , Optional , Sequence , Union , TYPE_CHECKING
5
+ from typing import Any , Dict , Iterator , List , Optional , Sequence , Union , TYPE_CHECKING , overload
6
+ from typing_extensions import Self
5
7
6
8
import attrs
7
9
import discord_typings
@@ -989,10 +991,12 @@ def to_dict(self) -> dict:
989
991
}
990
992
991
993
992
- class ContainerComponent (BaseComponent ):
993
- components : list [
994
+ class ContainerComponent (
995
+ BaseComponent ,
996
+ UserList [
994
997
ActionRow | SectionComponent | TextDisplayComponent | MediaGalleryComponent | FileComponent | SeparatorComponent
995
- ]
998
+ ],
999
+ ):
996
1000
accent_color : Optional [int ] = None
997
1001
spoiler : bool = False
998
1002
@@ -1007,13 +1011,86 @@ def __init__(
1007
1011
accent_color : Optional [int ] = None ,
1008
1012
spoiler : bool = False ,
1009
1013
):
1010
- self .components = list (components )
1014
+ self .data = list (components )
1011
1015
self .accent_color = accent_color
1012
1016
self .spoiler = spoiler
1013
1017
self .type = ComponentType .CONTAINER
1014
1018
1019
+ @property
1020
+ def components (
1021
+ self ,
1022
+ ) -> list [
1023
+ ActionRow | SectionComponent | TextDisplayComponent | MediaGalleryComponent | FileComponent | SeparatorComponent
1024
+ ]:
1025
+ return self .data
1026
+
1027
+ @components .setter
1028
+ def components (
1029
+ self ,
1030
+ value : list [
1031
+ ActionRow
1032
+ | SectionComponent
1033
+ | TextDisplayComponent
1034
+ | MediaGalleryComponent
1035
+ | FileComponent
1036
+ | SeparatorComponent
1037
+ ],
1038
+ ) -> None :
1039
+ self .data = value
1040
+
1041
+ @overload
1042
+ def __getitem__ (
1043
+ self , i : int
1044
+ ) -> (
1045
+ ActionRow | SectionComponent | TextDisplayComponent | MediaGalleryComponent | FileComponent | SeparatorComponent
1046
+ ): ...
1047
+
1048
+ @overload
1049
+ def __getitem__ (self , i : slice ) -> Self : ...
1050
+
1051
+ def __getitem__ (
1052
+ self , i : int | slice
1053
+ ) -> (
1054
+ Self
1055
+ | ActionRow
1056
+ | SectionComponent
1057
+ | TextDisplayComponent
1058
+ | MediaGalleryComponent
1059
+ | FileComponent
1060
+ | SeparatorComponent
1061
+ ):
1062
+ if isinstance (i , slice ):
1063
+ return self .__class__ (* self .data [i ], accent_color = self .accent_color , spoiler = self .spoiler )
1064
+ return self .data [i ]
1065
+
1066
+ def __add__ (self , other : Any ) -> Self :
1067
+ if isinstance (other , ContainerComponent ):
1068
+ return self .__class__ (* (self .data + other .data ), accent_color = self .accent_color , spoiler = self .spoiler )
1069
+ if isinstance (other , UserList ):
1070
+ return self .__class__ (* (self .data + other .data ), accent_color = self .accent_color , spoiler = self .spoiler )
1071
+ if isinstance (other , type (self .data )):
1072
+ return self .__class__ (* (self .data + other ), accent_color = self .accent_color , spoiler = self .spoiler )
1073
+ return self .__class__ (* (self .data + list (other )), accent_color = self .accent_color , spoiler = self .spoiler )
1074
+
1075
+ def __radd__ (self , other : Any ) -> Self :
1076
+ if isinstance (other , ContainerComponent ):
1077
+ return self .__class__ (* (self .data + other .data ), accent_color = other .accent_color , spoiler = other .spoiler )
1078
+ if isinstance (other , UserList ):
1079
+ return self .__class__ (* (other .data + self .data ), accent_color = self .accent_color , spoiler = self .spoiler )
1080
+ if isinstance (other , type (self .data )):
1081
+ return self .__class__ (* (other + self .data ), accent_color = self .accent_color , spoiler = self .spoiler )
1082
+ return self .__class__ (* (list (other ) + self .data ), accent_color = self .accent_color , spoiler = self .spoiler )
1083
+
1084
+ def __mul__ (self , n : int ) -> Self :
1085
+ return self .__class__ (* (self .data * n ), accent_color = self .accent_color , spoiler = self .spoiler )
1086
+
1087
+ __rmul__ = __mul__
1088
+
1089
+ def copy (self ) -> Self :
1090
+ return self .__class__ (* self .data , accent_color = self .accent_color , spoiler = self .spoiler )
1091
+
1015
1092
@classmethod
1016
- def from_dict (cls , data : dict ) -> "ContainerComponent" :
1093
+ def from_dict (cls , data : dict ) -> Self :
1017
1094
return cls (
1018
1095
* [BaseComponent .from_dict_factory (component ) for component in data ["components" ]],
1019
1096
accent_color = data .get ("accent_color" ),
0 commit comments