Skip to content

Use stacklevel=2 for warnings to report on callsites #1219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2024

Conversation

rytilahti
Copy link
Member

Use stacklevel=2 for warnings, as this will correctly show the callsite instead of the line where the warning is reported.
Thanks to https://www.youtube.com/watch?v=Ljfn4x8t3Ow by @asottile

Currently:

kasa/__init__.py:110
  /home/tpr/code/python-kasa/kasa/__init__.py:110: DeprecationWarning: SmartDevice is deprecated, use IotDevice from package kasa.iot instead or use Discover.discover_single() and Device.connect() to support new protocols
    warn(

After:

kasa/tests/smart/modules/test_contact.py:3
  /home/tpr/code/python-kasa/kasa/tests/smart/modules/test_contact.py:3: DeprecationWarning: SmartDevice is deprecated, use IotDevice from package kasa.iot instead or use Discover.discover_single() and Device.connect() to support new protocols
    from kasa import Module, SmartDevice

Currently:

kasa/tests/test_lightstrip.py: 56 warnings
  /home/tpr/code/python-kasa/kasa/device.py:559: DeprecationWarning: effect is deprecated, use: Module.LightEffect in device.modules instead
    warn(msg, DeprecationWarning, stacklevel=1)

After:

kasa/tests/test_lightstrip.py::test_effects_lightstrip_set_effect_transition[500-KL430(US)_2.0_1.0.9.json]
  /home/tpr/code/python-kasa/kasa/tests/test_lightstrip.py:62: DeprecationWarning: set_effect is deprecated, use: Module.LightEffect in device.modules instead
    await dev.set_effect("Candy Cane")

@rytilahti rytilahti added the maintenance Project improvements and maintenance label Oct 31, 2024
Copy link

codecov bot commented Oct 31, 2024

Codecov Report

Attention: Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.

Project coverage is 92.48%. Comparing base (530cf4b) to head (27c41fd).
Report is 209 commits behind head on master.

Files with missing lines Patch % Lines
kasa/iot/iotdevice.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1219   +/-   ##
=======================================
  Coverage   92.48%   92.48%           
=======================================
  Files          99       99           
  Lines        6535     6535           
  Branches      700      700           
=======================================
  Hits         6044     6044           
  Misses        374      374           
  Partials      117      117           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sdb9696
Copy link
Collaborator

sdb9696 commented Oct 31, 2024

This makes sense when the warning is coming from __getattr__ etc but I'm wondering whether it makes sense for things like the test framework. What does the after look like then?

@rytilahti
Copy link
Member Author

I think it makes sense there, too:

kasa/tests/test_strip.py::test_children_alias[P300(EU)_1.0_1.0.13.json-SMART]
  /home/tpr/code/python-kasa/kasa/tests/fakeprotocol_smart.py:438: UserWarning: Could not find child fixture 000000000000000000000000000000000000000001 in P300(EU)_1.0_1.0.13.json
    return await self._handle_control_child(request_dict["params"])

vs.

kasa/tests/test_strip.py::test_children_alias[P300(EU)_1.0_1.0.13.json-SMART]
  /home/tpr/code/python-kasa/kasa/tests/fakeprotocol_smart.py:224: UserWarning: Could not find child fixture 000000000000000000000000000000000000000000 in P300(EU)_1.0_1.0.13.json
    warn(

and

kasa/tests/test_smartprotocol.py::test_smart_protocol_lists_multiple_request[50-30]
  /home/tpr/code/python-kasa/kasa/tests/fakeprotocol_smart.py:51: UserWarning: Child is a cameraprotocol which needs to be implemented {'foo': 26}
    self.child_protocols = self._get_child_protocols(

vs.

kasa/tests/test_smartprotocol.py::test_smart_protocol_lists_multiple_request[50-30]
  /home/tpr/code/python-kasa/kasa/tests/fakeprotocol_smart.py:208: UserWarning: Child is a cameraprotocol which needs to be implemented {'foo': 26}
    warn(

Copy link
Collaborator

@sdb9696 sdb9696 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool, let's merge. N.B. Once all the ANN changes are in we might want to consider putting all the deprecated __getattr__ behind if not TYPE_CHECKING:

@rytilahti rytilahti merged commit 5da41fc into master Oct 31, 2024
27 of 28 checks passed
@rytilahti rytilahti deleted the fix/warn_stacklevel branch October 31, 2024 13:12
@sdb9696 sdb9696 added this to the 0.7.7 milestone Nov 4, 2024
@sdb9696 sdb9696 mentioned this pull request Nov 4, 2024
sdb9696 added a commit that referenced this pull request Nov 4, 2024
## [0.7.7](https://github.com/python-kasa/python-kasa/tree/0.7.7) (2024-11-04)

[Full Changelog](0.7.6...0.7.7)

**Release summary:**

- Bugfix for child device device creation error with credentials_hash
- PIR support for iot dimmers and wall switches.
- Various small enhancements and project improvements.

**Implemented enhancements:**

- Add PIR&LAS for wall switches mentioning PIR support [\#1227](#1227) (@rytilahti)
- Expose ambient light setting for iot dimmers [\#1210](#1210) (@rytilahti)
- Expose PIR enabled setting for iot dimmers [\#1174](#1174) (@rytilahti)
- Add childprotection module [\#1141](#1141) (@rytilahti)
- Initial trigger logs implementation [\#900](#900) (@rytilahti)

**Fixed bugs:**

- Fix AES child device creation error [\#1220](#1220) (@sdb9696)

**Project maintenance:**

- Update TC65 fixture [\#1225](#1225) (@rytilahti)
- Update smartcamera fixtures from latest dump\_devinfo [\#1224](#1224) (@sdb9696)
- Add component queries to smartcamera devices [\#1223](#1223) (@sdb9696)
- Update try\_connect\_all to be more efficient and report attempts [\#1222](#1222) (@sdb9696)
- Use stacklevel=2 for warnings to report on callsites [\#1219](#1219) (@rytilahti)
- parse\_pcap\_klap: various code cleanups [\#1138](#1138) (@rytilahti)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Project improvements and maintenance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants