1
+ from typing import List , Union , Optional , Any
2
+
3
+
1
4
def _operation (name , location , ** kwargs ):
2
5
return {"operation" : name , "location" : location , "params" : dict (** kwargs )}
3
6
4
7
5
8
_noop = object ()
6
9
10
+ _KeyType = Union [str , int ]
11
+
7
12
8
- def validate_slice (obj ):
13
+ def validate_slice (obj : Any ):
9
14
if isinstance (obj , slice ):
10
15
raise TypeError ("a slice is not a valid index for patch" )
11
16
@@ -19,7 +24,11 @@ class Patch:
19
24
Supported prop types: Dictionaries and lists.
20
25
"""
21
26
22
- def __init__ (self , location = None , parent = None ):
27
+ def __init__ (
28
+ self ,
29
+ location : Optional [List [_KeyType ]] = None ,
30
+ parent : Optional ["Patch" ] = None ,
31
+ ):
23
32
if location is not None :
24
33
self ._location = location
25
34
else :
@@ -36,11 +45,11 @@ def __getstate__(self):
36
45
def __setstate__ (self , state ):
37
46
vars (self ).update (state )
38
47
39
- def __getitem__ (self , item ) -> "Patch" :
48
+ def __getitem__ (self , item : _KeyType ) -> "Patch" :
40
49
validate_slice (item )
41
50
return Patch (location = self ._location + [item ], parent = self )
42
51
43
- def __getattr__ (self , item ) -> "Patch" :
52
+ def __getattr__ (self , item : _KeyType ) -> "Patch" :
44
53
if item == "tolist" :
45
54
# to_json fix
46
55
raise AttributeError
@@ -50,16 +59,16 @@ def __getattr__(self, item) -> "Patch":
50
59
return self ._operations # type: ignore
51
60
return self .__getitem__ (item )
52
61
53
- def __setattr__ (self , key , value ):
62
+ def __setattr__ (self , key : _KeyType , value : Any ):
54
63
if key in ("_location" , "_operations" ):
55
64
self .__dict__ [key ] = value
56
65
else :
57
66
self .__setitem__ (key , value )
58
67
59
- def __delattr__ (self , item ):
68
+ def __delattr__ (self , item : _KeyType ):
60
69
self .__delitem__ (item )
61
70
62
- def __setitem__ (self , key , value ):
71
+ def __setitem__ (self , key : _KeyType , value : Any ):
63
72
validate_slice (key )
64
73
if value is _noop :
65
74
# The += set themselves.
@@ -72,11 +81,11 @@ def __setitem__(self, key, value):
72
81
)
73
82
)
74
83
75
- def __delitem__ (self , key ):
84
+ def __delitem__ (self , key : _KeyType ):
76
85
validate_slice (key )
77
86
self ._operations .append (_operation ("Delete" , self ._location + [key ]))
78
87
79
- def __iadd__ (self , other ):
88
+ def __iadd__ (self , other : Any ):
80
89
if isinstance (other , (list , tuple )):
81
90
self .extend (other )
82
91
else :
@@ -85,25 +94,25 @@ def __iadd__(self, other):
85
94
return self
86
95
return _noop
87
96
88
- def __isub__ (self , other ):
97
+ def __isub__ (self , other : Any ):
89
98
self ._operations .append (_operation ("Sub" , self ._location , value = other ))
90
99
if not self ._location :
91
100
return self
92
101
return _noop
93
102
94
- def __imul__ (self , other ) :
103
+ def __imul__ (self , other : Any ) -> "Patch" :
95
104
self ._operations .append (_operation ("Mul" , self ._location , value = other ))
96
105
if not self ._location :
97
106
return self
98
107
return _noop
99
108
100
- def __itruediv__ (self , other ):
109
+ def __itruediv__ (self , other : Any ):
101
110
self ._operations .append (_operation ("Div" , self ._location , value = other ))
102
111
if not self ._location :
103
112
return self
104
113
return _noop
105
114
106
- def __ior__ (self , other ):
115
+ def __ior__ (self , other : Any ):
107
116
self .update (E = other )
108
117
if not self ._location :
109
118
return self
@@ -115,39 +124,39 @@ def __iter__(self):
115
124
def __repr__ (self ):
116
125
return f"<write-only dash.Patch object at { self ._location } >"
117
126
118
- def append (self , item ) :
127
+ def append (self , item : Any ) -> None :
119
128
"""Add the item to the end of a list"""
120
129
self ._operations .append (_operation ("Append" , self ._location , value = item ))
121
130
122
- def prepend (self , item ) :
131
+ def prepend (self , item : Any ) -> None :
123
132
"""Add the item to the start of a list"""
124
133
self ._operations .append (_operation ("Prepend" , self ._location , value = item ))
125
134
126
- def insert (self , index , item ) :
135
+ def insert (self , index : int , item : Any ) -> None :
127
136
"""Add the item at the index of a list"""
128
137
self ._operations .append (
129
138
_operation ("Insert" , self ._location , value = item , index = index )
130
139
)
131
140
132
- def clear (self ):
141
+ def clear (self ) -> None :
133
142
"""Remove all items in a list"""
134
143
self ._operations .append (_operation ("Clear" , self ._location ))
135
144
136
- def reverse (self ):
145
+ def reverse (self ) -> None :
137
146
"""Reversal of the order of items in a list"""
138
147
self ._operations .append (_operation ("Reverse" , self ._location ))
139
148
140
- def extend (self , item ) :
149
+ def extend (self , item : Union [ list , tuple ]) -> None :
141
150
"""Add all the items to the end of a list"""
142
151
if not isinstance (item , (list , tuple )):
143
152
raise TypeError (f"{ item } should be a list or tuple" )
144
153
self ._operations .append (_operation ("Extend" , self ._location , value = item ))
145
154
146
- def remove (self , item ) :
155
+ def remove (self , item : Any ) -> None :
147
156
"""filter the item out of a list on the frontend"""
148
157
self ._operations .append (_operation ("Remove" , self ._location , value = item ))
149
158
150
- def update (self , E = None , ** F ):
159
+ def update (self , E : Any = None , ** F ) -> None :
151
160
"""Merge a dict or keyword arguments with another dictionary"""
152
161
value = E or {}
153
162
value .update (F )
@@ -159,7 +168,7 @@ def sort(self):
159
168
"sort is reserved for future use, use brackets to access this key on your object"
160
169
)
161
170
162
- def to_plotly_json (self ):
171
+ def to_plotly_json (self ) -> Any :
163
172
return {
164
173
"__dash_patch_update" : "__dash_patch_update" ,
165
174
"operations" : self ._operations ,
0 commit comments