Skip to content

Avoid forgetting about stacklevel in warnings #2594

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Notable changes in recent builds.
Notable changes in recent builds.

Maintained by hand, so what's "notable" is subjective! Contributors are
encouraged to add entries for their work.
Expand All @@ -13,6 +13,7 @@ https://mhammond.github.io/pywin32_installers.html .

Coming in build 311, as yet unreleased
--------------------------------------
* pywin32's own warnings will now refer to the caller, rather than to the internal source of warning itself (#2594, @Avasam)
* Fixed a regression that broke special __dunder__ methods with CoClass. (#1870, #2493, @Avasam, @geppi)
* Fixed `TypeError: cannot unpack non-iterable NoneType object` when registering an axscript client ScriptItem (#2513, @Avasam)
* Fixed a memory leak when SafeArrays are used as out parameters (@the-snork)
Expand Down
4 changes: 3 additions & 1 deletion Pythonwin/pywin/framework/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ def OnInitDialog(self):
except OSError:
ver = None
if not ver:
warnings.warn(f"Could not read pywin32's version from '{version_path}'")
warnings.warn(
f"Could not read pywin32's version from '{version_path}'", stacklevel=2
)
self.SetDlgItemText(win32ui.IDC_ABOUT_VERSION, ver)
self.HookCommand(self.OnButHomePage, win32ui.IDC_BUTTON1)

Expand Down
4 changes: 3 additions & 1 deletion Pythonwin/pywin/framework/dbgcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def OnAdd(self, msg, code):
## for the toolbar button IDC_DBG_ADD fails, since MFC falls back to
## sending a normal command if the UI update command fails.
## win32ui.MessageBox('There is no active window - no breakpoint can be added')
warnings.warn("There is no active window - no breakpoint can be added")
warnings.warn(
"There is no active window - no breakpoint can be added", stacklevel=2
)
return None
pathName = doc.GetPathName()
lineNo = view.LineFromChar(view.GetSel()[0]) + 1
Expand Down
2 changes: 1 addition & 1 deletion adodbapi/adodbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
except ImportError:
import warnings

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


def getIndexedValue(obj, index):
Expand Down
1 change: 1 addition & 0 deletions com/win32comext/axscript/client/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def __init__(
warnings.warn(
"`exc_type` and `exc_traceback` were redundant and are now unused.",
category=DeprecationWarning,
stacklevel=2,
)

# And my other values...
Expand Down
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ line-ending = "cr-lf"

[lint]
select = [
"B028", # no-explicit-stacklevel
"C4", # flake8-comprehensions
"F811", # redefined-while-unused
"I", # isort
Expand Down
1 change: 1 addition & 0 deletions win32/Lib/afxres.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
warnings.warn(
"Importing the global `afxres` module is deprecated. Import from `pywin.mfc.afxres` instead.",
category=DeprecationWarning,
stacklevel=2,
)
1 change: 1 addition & 0 deletions win32/Lib/regcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
warnings.warn(
"The regcheck module has been deprecated and pending removal since build 210",
category=DeprecationWarning,
stacklevel=2,
)

import os
Expand Down
2 changes: 1 addition & 1 deletion win32/Lib/win2kras.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@

from win32ras import * # nopycln: import

warnings.warn(str(__doc__), category=DeprecationWarning)
warnings.warn(str(__doc__), category=DeprecationWarning, stacklevel=2)
5 changes: 4 additions & 1 deletion win32/Lib/win32gui_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def PackMENUITEMINFO(
if dwTypeData is not None:
import warnings

warnings.warn("PackMENUITEMINFO: please use dwItemData instead of dwTypeData")
warnings.warn(
"PackMENUITEMINFO: please use dwItemData instead of dwTypeData",
stacklevel=2,
)
if dwItemData is None:
dwItemData = dwTypeData or 0

Expand Down
8 changes: 6 additions & 2 deletions win32/Lib/win32serviceutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ def InstallService(
)
except (win32service.error, NotImplementedError):
## delayed start only exists on Vista and later - warn only when trying to set delayed to True
warnings.warn("Delayed Start not available on this system")
warnings.warn(
"Delayed Start not available on this system", stacklevel=2
)
win32service.CloseServiceHandle(hs)
finally:
win32service.CloseServiceHandle(hscm)
Expand Down Expand Up @@ -345,7 +347,9 @@ def ChangeServiceConfig(
## doensn't exist. On Win2k and XP, will fail with ERROR_INVALID_LEVEL
## Warn only if trying to set delayed to True
if delayedstart:
warnings.warn("Delayed Start not available on this system")
warnings.warn(
"Delayed Start not available on this system", stacklevel=2
)
finally:
win32service.CloseServiceHandle(hs)
finally:
Expand Down
9 changes: 4 additions & 5 deletions win32/Lib/winerror.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ def __getattr__(name: str) -> int:
"CERTDB_E_JET_ERROR": -2146873344,
}.get(name):
warnings.warn(
DeprecationWarning(
f"Constant '{name}' is no longer part of Windows' SDK and may be removed eventually. "
+ f"If you believe this is incorrect or are still using '{name}', "
+ "please raise an issue at https://github.com/mhammond/pywin32/issues"
),
f"Constant '{name}' is no longer part of Windows' SDK and may be removed eventually. "
+ f"If you believe this is incorrect or are still using '{name}', "
+ "please raise an issue at https://github.com/mhammond/pywin32/issues",
DeprecationWarning,
stacklevel=2,
)
return attr
Expand Down
2 changes: 1 addition & 1 deletion win32/winxpgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
)
from win32gui import * # nopycln: import

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