Fix __repr__ crash when get_device_info returns SmartErrorCode#1672
Open
loganrosen wants to merge 1 commit intopython-kasa:masterfrom
Open
Fix __repr__ crash when get_device_info returns SmartErrorCode#1672loganrosen wants to merge 1 commit intopython-kasa:masterfrom
loganrosen wants to merge 1 commit intopython-kasa:masterfrom
Conversation
When a device returns INTERNAL_QUERY_ERROR for get_device_info, the response is stored as a SmartErrorCode enum in _last_update. If another module then raises DeviceError and the error is logged with %s formatting, __repr__ calls self.model which calls _get_device_info, crashing with TypeError: 'SmartErrorCode' object is not subscriptable. Make __repr__ resilient by catching exceptions and falling back to a basic representation without alias/model. This follows the Python convention that __repr__ should never raise. Fixes python-kasa#1671 Co-authored-by: Copilot <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1672 +/- ##
==========================================
+ Coverage 93.22% 93.23% +0.01%
==========================================
Files 157 157
Lines 9815 9815
Branches 1003 1003
==========================================
+ Hits 9150 9151 +1
Misses 472 472
+ Partials 193 192 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Make
__repr__resilient so it never raises, following Python convention.Problem
When a device returns
INTERNAL_QUERY_ERRORforget_device_info, the response is stored as aSmartErrorCodeenum in_last_update. If another module then raisesDeviceErrorand the error is logged with%sformatting,__repr__callsself.model→device_info→_get_device_info, which doesdi["model"]on the enum and crashes with:This creates a cascade of log spam since the logging error itself triggers more logging.
Fix
Wrap the
__repr__body in a try/except that falls back to a basic representation (device type + host) when alias/model access fails.__repr__should never raise per Python convention.Testing
Added
test_device_info_with_smarterrorcode— injectsSmartErrorCode.INTERNAL_QUERY_ERRORinto_last_update["get_device_info"]and verifiesrepr(dev)does not raise.Fixes #1671