1
+ from abc import ABC , abstractmethod
1
2
from collections .abc import Sequence
2
3
from enum import StrEnum
3
4
from typing import Any , Callable
@@ -21,23 +22,17 @@ class SerializeMode(StrEnum):
21
22
GRANITEIO = "graniteio"
22
23
23
24
24
- class PDLContext (Sequence ):
25
+ class PDLContext (ABC , Sequence ):
25
26
26
- def serialize ( self , mode : SerializeMode ) -> list [ dict [ str , Any ]]:
27
- return []
27
+ @ abstractmethod
28
+ def serialize ( self , mode : SerializeMode ) -> list [ dict [ str , Any ]]: ...
28
29
29
30
def __add__ (self , value : "PDLContext" ):
30
31
return IndependentContext ([self , value ])
31
32
32
33
def __mul__ (self , value : "PDLContext" ):
33
34
return DependentContext ([self , value ])
34
35
35
- def __len__ (self ):
36
- return 0
37
-
38
- def __getitem__ (self , index : int | slice ): # pyright: ignore
39
- return []
40
-
41
36
# def to_json(self):
42
37
# return json.dumps(self.serialize(SerializeMode.LITELLM))
43
38
@@ -52,16 +47,14 @@ def serialize(self, mode: SerializeMode) -> list[dict[str, Any]]:
52
47
result = self .message .result ()
53
48
return [result ]
54
49
55
- def __len__ (self ): # pyright: ignore
50
+ def __len__ (self ):
56
51
return 1
57
52
58
53
def __getitem__ (self , index : int | slice ): # pyright: ignore
59
- if index in (0 , - 1 ):
60
- return self .message .result ()
61
- assert False
54
+ return [self .message .result ()][index ]
62
55
63
56
def __repr__ (self ): # pyright: ignore
64
- return str ( self .message .result ())
57
+ return self .message .result (). __repr__ ( )
65
58
66
59
67
60
class IndependentContext (PDLContext ):
@@ -74,6 +67,8 @@ def __init__(self, context: list[PDLContext]):
74
67
ret += item .context .data
75
68
elif isinstance (item , SingletonContext ):
76
69
ret += [item ]
70
+ elif isinstance (item , DependentContext ) and len (item ) == 0 :
71
+ pass
77
72
else :
78
73
# Not all elements of the list are Independent, so return
79
74
self .context = PdlList (context )
@@ -111,6 +106,8 @@ def __init__(self, context: list[PDLContext]):
111
106
ret += item .context .data
112
107
elif isinstance (item , SingletonContext ):
113
108
ret += [item ]
109
+ elif isinstance (item , IndependentContext ) and len (item ) == 0 :
110
+ pass
114
111
else :
115
112
# Not all elements of the list are Dependent, so return
116
113
self .context = PdlList (context )
0 commit comments