Skip to content

Commit 49f0fe8

Browse files
committed
Avoid forgetting about stacklevel in warnings
1 parent d764900 commit 49f0fe8

File tree

12 files changed

+27
-13
lines changed

12 files changed

+27
-13
lines changed

Pythonwin/pywin/framework/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ def OnInitDialog(self):
357357
except OSError:
358358
ver = None
359359
if not ver:
360-
warnings.warn(f"Could not read pywin32's version from '{version_path}'")
360+
warnings.warn(
361+
f"Could not read pywin32's version from '{version_path}'", stacklevel=2
362+
)
361363
self.SetDlgItemText(win32ui.IDC_ABOUT_VERSION, ver)
362364
self.HookCommand(self.OnButHomePage, win32ui.IDC_BUTTON1)
363365

Pythonwin/pywin/framework/dbgcommands.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ def OnAdd(self, msg, code):
105105
## for the toolbar button IDC_DBG_ADD fails, since MFC falls back to
106106
## sending a normal command if the UI update command fails.
107107
## win32ui.MessageBox('There is no active window - no breakpoint can be added')
108-
warnings.warn("There is no active window - no breakpoint can be added")
108+
warnings.warn(
109+
"There is no active window - no breakpoint can be added", stacklevel=2
110+
)
109111
return None
110112
pathName = doc.GetPathName()
111113
lineNo = view.LineFromChar(view.GetSel()[0]) + 1

adodbapi/adodbapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
except ImportError:
5353
import warnings
5454

55-
warnings.warn("pywin32 package required for adodbapi.", ImportWarning)
55+
warnings.warn("pywin32 package required for adodbapi.", ImportWarning, stacklevel=2)
5656

5757

5858
def getIndexedValue(obj, index):

com/win32comext/axscript/client/error.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(
101101
warnings.warn(
102102
"`exc_type` and `exc_traceback` were redundant and are now unused.",
103103
category=DeprecationWarning,
104+
stacklevel=2,
104105
)
105106

106107
# And my other values...

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ line-ending = "cr-lf"
77

88
[lint]
99
select = [
10+
"B028", # no-explicit-stacklevel
1011
"C4", # flake8-comprehensions
1112
"F811", # redefined-while-unused
1213
"I", # isort

win32/Lib/afxres.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
warnings.warn(
66
"Importing the global `afxres` module is deprecated. Import from `pywin.mfc.afxres` instead.",
77
category=DeprecationWarning,
8+
stacklevel=2,
89
)

win32/Lib/regcheck.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
warnings.warn(
77
"The regcheck module has been deprecated and pending removal since build 210",
88
category=DeprecationWarning,
9+
stacklevel=2,
910
)
1011

1112
import os

win32/Lib/win2kras.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212
from win32ras import * # nopycln: import
1313

14-
warnings.warn(str(__doc__), category=DeprecationWarning)
14+
warnings.warn(str(__doc__), category=DeprecationWarning, stacklevel=2)

win32/Lib/win32gui_struct.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ def PackMENUITEMINFO(
122122
if dwTypeData is not None:
123123
import warnings
124124

125-
warnings.warn("PackMENUITEMINFO: please use dwItemData instead of dwTypeData")
125+
warnings.warn(
126+
"PackMENUITEMINFO: please use dwItemData instead of dwTypeData",
127+
stacklevel=2,
128+
)
126129
if dwItemData is None:
127130
dwItemData = dwTypeData or 0
128131

win32/Lib/win32serviceutil.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ def InstallService(
261261
)
262262
except (win32service.error, NotImplementedError):
263263
## delayed start only exists on Vista and later - warn only when trying to set delayed to True
264-
warnings.warn("Delayed Start not available on this system")
264+
warnings.warn(
265+
"Delayed Start not available on this system", stacklevel=2
266+
)
265267
win32service.CloseServiceHandle(hs)
266268
finally:
267269
win32service.CloseServiceHandle(hscm)
@@ -345,7 +347,9 @@ def ChangeServiceConfig(
345347
## doensn't exist. On Win2k and XP, will fail with ERROR_INVALID_LEVEL
346348
## Warn only if trying to set delayed to True
347349
if delayedstart:
348-
warnings.warn("Delayed Start not available on this system")
350+
warnings.warn(
351+
"Delayed Start not available on this system", stacklevel=2
352+
)
349353
finally:
350354
win32service.CloseServiceHandle(hs)
351355
finally:

win32/Lib/winerror.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ def __getattr__(name: str) -> int:
4040
"CERTDB_E_JET_ERROR": -2146873344,
4141
}.get(name):
4242
warnings.warn(
43-
DeprecationWarning(
44-
f"Constant '{name}' is no longer part of Windows' SDK and may be removed eventually. "
45-
+ f"If you believe this is incorrect or are still using '{name}', "
46-
+ "please raise an issue at https://github.com/mhammond/pywin32/issues"
47-
),
43+
f"Constant '{name}' is no longer part of Windows' SDK and may be removed eventually. "
44+
+ f"If you believe this is incorrect or are still using '{name}', "
45+
+ "please raise an issue at https://github.com/mhammond/pywin32/issues",
46+
DeprecationWarning,
4847
stacklevel=2,
4948
)
5049
return attr

win32/winxpgui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
)
1111
from win32gui import * # nopycln: import
1212

13-
warnings.warn(str(__doc__), category=DeprecationWarning)
13+
warnings.warn(str(__doc__), category=DeprecationWarning, stacklevel=2)

0 commit comments

Comments
 (0)