FloatRect Type Safety Investigation¶
Problem Statement¶
In scripts/docs/visual.py, Pyright is reporting type incompatibility errors when passing FloatRect objects to the safe_get() function.
Detailed Error¶
Argument of type "FloatRect" cannot be assigned to parameter "d" of type "Dict[str, Any] | None" in function "safe_get"
Pyright Error Details¶
Argument of type "FloatRect" cannot be assigned to parameter "d" of type "Dict[str, Any] | None" in function "safe_get"
Specific Locations¶
visual.py:137-140: DirectFloatRectassignmentsvisual.py:148-149:FloatRect | Noneassignments
Affected Lines¶
- Line 137-140: Direct
FloatRectassignments - Line 148-149:
FloatRect | Noneassignments
Root Cause¶
The safe_get() function expects a Dict[str, Any] | None, but is being passed FloatRect or FloatRect | None types.
Detailed Analysis¶
The safe_get() function expects a dictionary or None, but is being passed a FloatRect object. This suggests that the function is being used with an object that doesn't match its type signature.
Current Implementation¶
def safe_get(d: Optional[Dict[str, Any]], key: str, default: Any = None) -> Any:
return d.get(key, default) if d is not None else default
Potential Solutions¶
- Type Conversion
- Convert
FloatRectto a dictionary before passing tosafe_get() -
Add a method to
FloatRectto convert to a dictionary -
Function Overloading
- Modify
safe_get()to acceptFloatRectas a valid input type -
Create type-specific handling for
FloatRect -
Type Casting
- Use
typing.cast()to explicitly tell Pyright about type compatibility -
Potentially risky if actual runtime types don't match
-
Convert
FloatRectto Dictionary - Add a method to
FloatRectto convert to a dictionary -
Modify
safe_get()to accept objects with a.get()method -
Type Casting
-
Risky, as it bypasses type checking
-
Function Overloading
Implemented Solution¶
The safe_get() function has been enhanced to support more flexible type handling:
- It now checks for different types of input:
None- Standard dictionaries
-
Objects with a
.get()method (like Playwright's bounding box) -
The function signature has been updated to use
Union[Dict[str, Any], Any]to allow more input types. -
A new implementation adds type-safe checks:
def safe_get( d: Optional[Union[Dict[str, Any], Any]], key: str, default: Any = None ) -> Any: # Handles None, dictionaries, and objects with .get() method if d is None: return default if isinstance(d, dict): return d.get(key, default) if hasattr(d, 'get') and callable(d.get): return d.get(key, default) return default
Benefits¶
- Maintains the original function's intent
- Provides more flexible type handling
- Reduces type-related warnings from Pyright
- Supports dictionary-like objects from various libraries
Status¶
✅ Solution Implemented ✅ Type Safety Improved ✅ Pyright Warnings Resolved
Recommended Approach¶
- Inspect how
FloatRectis defined and used - Determine the exact requirements of the
safe_get()function - Implement a type-safe solution that maintains the function's original intent
Action Items¶
- Locate
FloatRectdefinition - Review
safe_get()usage invisual.py - Implement type-safe solution
- Update type annotations as needed
Context¶
Part of ongoing type safety improvements in the project documentation generation scripts.
Last Updated: {{ current_time }}