@@ -27,6 +27,8 @@ class ClientWindowState:
2727 MINIMIZED = "minimized"
2828 NORMAL = "normal"
2929
30+ VALID_STATES = {FULLSCREEN , MAXIMIZED , MINIMIZED , NORMAL }
31+
3032
3133class ClientWindowInfo :
3234 """Represents a client window information."""
@@ -123,16 +125,53 @@ def from_dict(cls, data: dict) -> "ClientWindowInfo":
123125 Returns:
124126 -------
125127 ClientWindowInfo: A new instance of ClientWindowInfo.
128+
129+ Raises:
130+ ------
131+ ValueError: If required fields are missing or have invalid types.
126132 """
127- return cls (
128- client_window = data .get ("clientWindow" ),
129- state = data .get ("state" ),
130- width = data .get ("width" ),
131- height = data .get ("height" ),
132- x = data .get ("x" ),
133- y = data .get ("y" ),
134- active = data .get ("active" ),
135- )
133+ try :
134+ client_window = data ["clientWindow" ]
135+ if not isinstance (client_window , str ):
136+ raise ValueError ("clientWindow must be a string" )
137+
138+ state = data ["state" ]
139+ if not isinstance (state , str ):
140+ raise ValueError ("state must be a string" )
141+ if state not in ClientWindowState .VALID_STATES :
142+ raise ValueError (f"Invalid state: { state } . Must be one of { ClientWindowState .VALID_STATES } " )
143+
144+ width = data ["width" ]
145+ if not isinstance (width , int ) or width < 0 :
146+ raise ValueError (f"width must be a non-negative integer, got { width } " )
147+
148+ height = data ["height" ]
149+ if not isinstance (height , int ) or height < 0 :
150+ raise ValueError (f"height must be a non-negative integer, got { height } " )
151+
152+ x = data ["x" ]
153+ if not isinstance (x , int ):
154+ raise ValueError (f"x must be an integer, got { type (x ).__name__ } " )
155+
156+ y = data ["y" ]
157+ if not isinstance (y , int ):
158+ raise ValueError (f"y must be an integer, got { type (y ).__name__ } " )
159+
160+ active = data ["active" ]
161+ if not isinstance (active , bool ):
162+ raise ValueError ("active must be a boolean" )
163+
164+ return cls (
165+ client_window = client_window ,
166+ state = state ,
167+ width = width ,
168+ height = height ,
169+ x = x ,
170+ y = y ,
171+ active = active ,
172+ )
173+ except (KeyError , TypeError ) as e :
174+ raise ValueError (f"Invalid data format for ClientWindowInfo: { e } " )
136175
137176
138177class Browser :
0 commit comments