mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-14 19:38:02 +00:00
Use stacklevel=2 for warnings to report on callsites (#1219)
Use stacklevel=2 for warnings, as this will correctly show the callsite instead of the line where the warning is reported. 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") ```
This commit is contained in:
@@ -102,7 +102,7 @@ deprecated_classes = {
|
||||
|
||||
def __getattr__(name):
|
||||
if name in deprecated_names:
|
||||
warn(f"{name} is deprecated", DeprecationWarning, stacklevel=1)
|
||||
warn(f"{name} is deprecated", DeprecationWarning, stacklevel=2)
|
||||
return globals()[f"_deprecated_{name}"]
|
||||
if name in deprecated_smart_devices:
|
||||
new_class = deprecated_smart_devices[name]
|
||||
@@ -112,13 +112,13 @@ def __getattr__(name):
|
||||
+ f"from package {package_name} instead or use Discover.discover_single()"
|
||||
+ " and Device.connect() to support new protocols",
|
||||
DeprecationWarning,
|
||||
stacklevel=1,
|
||||
stacklevel=2,
|
||||
)
|
||||
return new_class
|
||||
if name in deprecated_classes:
|
||||
new_class = deprecated_classes[name]
|
||||
msg = f"{name} is deprecated, use {new_class.__name__} instead"
|
||||
warn(msg, DeprecationWarning, stacklevel=1)
|
||||
warn(msg, DeprecationWarning, stacklevel=2)
|
||||
return new_class
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
|
Reference in New Issue
Block a user