You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhance SDK with type safety, error handling and resource management improvements
User description
fix: Enhance SDK with type safety, error handling and resource management improvements
Added proper type hints and validation across core SDK files
Improved error handling with specific exceptions and cleanup
Enhanced file resource management with proper cleanup
Added request timeouts and better HTTP error handling
Improved documentation with comprehensive docstrings
Fixed potential memory leaks in file operations
PR Type
Enhancement
Description
Added comprehensive type hints and validation across SDK
Enhanced error handling with specific exceptions and cleanup
Improved resource management with proper file cleanup
Added request timeouts and better HTTP error handling
Diagram Walkthrough
flowchart LR
A["Input Validation"] --> B["Type Safety"]
B --> C["HTTP Requests"]
C --> D["Error Handling"]
D --> E["Resource Cleanup"]
E --> F["Response Processing"]
Loading
File Walkthrough
Relevant files
Enhancement
__init__.py
Enhanced main SDK interface with validation
src/lighthouseweb3/init.py
Added input validation for all public methods
Enhanced error handling with descriptive exception messages
Improved docstrings with parameter and exception documentation
The post_files method catches all exceptions and wraps them in RequestException, but doesn't properly handle the case where files is None before attempting to close files in the finally block.
files=Nonetry:
self.parse_url_query(kwargs.get("query"))
files=utils.read_files_for_upload(file)
r=req.post(self.url, headers=headers, files=files, timeout=30) # Added timeoutr.raise_for_status()
# Always ensure files are closediffiles:
utils.close_files_after_upload(files)
try:
returnr.json()
exceptreq.exceptions.JSONDecodeError:
# Handle special case where response contains multiple linestemp=r.text.split("\n")
returnjson.loads(temp[-2]) # Use -2 index instead of len(temp) - 2exceptExceptionase:
# Ensure files are closed even if an error occursiffiles:
utils.close_files_after_upload(files)
raiseRequestException(f"File upload failed: {str(e)}")
The read_files_for_upload function opens file handles but doesn't close them if an exception occurs during the loop iteration, potentially causing resource leaks.
The exception handling in uploadBlob method checks for read/close methods but doesn't verify write method for BufferedWriter in downloadBlob, creating inconsistent validation.
ifnotisinstance(source, io.BufferedReader):
raiseTypeError("source must be an instance of io.BufferedReader")
ifnotfilenameornotisinstance(filename, str):
raiseValueError("filename must be a non-empty string")
ifnot (hasattr(source, 'read') andhasattr(source, 'close')):
raiseTypeError("source must have 'read' and 'close' methods")
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: Enhance SDK with type safety, error handling and resource management improvements