Skip to content

Fix __repr__ crash when get_device_info returns SmartErrorCode#1672

Open
loganrosen wants to merge 1 commit intopython-kasa:masterfrom
loganrosen:fix/device-info-error-handling
Open

Fix __repr__ crash when get_device_info returns SmartErrorCode#1672
loganrosen wants to merge 1 commit intopython-kasa:masterfrom
loganrosen:fix/device-info-error-handling

Conversation

@loganrosen
Copy link
Copy Markdown

Summary

Make __repr__ resilient so it never raises, following Python convention.

Problem

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.modeldevice_info_get_device_info, which does di["model"] on the enum and crashes with:

TypeError: 'SmartErrorCode' object is not subscriptable

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 — injects SmartErrorCode.INTERNAL_QUERY_ERROR into _last_update["get_device_info"] and verifies repr(dev) does not raise.

Fixes #1671

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
Copy link
Copy Markdown

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.23%. Comparing base (be5ce08) to head (6f11837).
⚠️ Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_get_device_info crashes when get_device_info response is a SmartErrorCode

1 participant