From 9aa39fcfdaa4d5f7c3db98bbba0ca84d5bf13fad Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 26 Jul 2024 15:44:51 -0400 Subject: [PATCH 1/4] Fix reportUnnecessaryComparison that can be resolves through annotations --- Pythonwin/pywin/framework/app.py | 3 ++- Pythonwin/pywin/tools/hierlist.py | 4 ++-- com/win32com/test/testPyComTest.py | 4 ++-- com/win32comext/axdebug/codecontainer.py | 6 ++++-- com/win32comext/mapi/mapiutil.py | 2 +- pyrightconfig.json | 4 ++-- win32/Lib/sspi.py | 2 +- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Pythonwin/pywin/framework/app.py b/Pythonwin/pywin/framework/app.py index f0c4d6d3eb..ec8cedb138 100644 --- a/Pythonwin/pywin/framework/app.py +++ b/Pythonwin/pywin/framework/app.py @@ -6,6 +6,7 @@ import os import sys import traceback +from typing import Literal import regutil import win32api @@ -61,7 +62,7 @@ class MainFrame(window.MDIFrameWnd): win32ui.ID_INDICATOR_COLNUM, ) - def OnCreate(self, cs): + def OnCreate(self, cs) -> Literal[-1, 0, 1]: self._CreateStatusBar() return 0 diff --git a/Pythonwin/pywin/tools/hierlist.py b/Pythonwin/pywin/tools/hierlist.py index 7ec29563a6..1e4f8d7682 100644 --- a/Pythonwin/pywin/tools/hierlist.py +++ b/Pythonwin/pywin/tools/hierlist.py @@ -12,7 +12,7 @@ # If you need to use the Tree Control, you may still find this API a reasonable # choice. However, you should investigate using the tree control directly # to provide maximum flexibility (but with extra work). - +from __future__ import annotations import commctrl import win32api @@ -259,7 +259,7 @@ def GetBitmapColumn(self, item): else: return 4 - def GetSelectedBitmapColumn(self, item): + def GetSelectedBitmapColumn(self, item) -> int | None: return 0 def CheckChangedChildren(self): diff --git a/com/win32com/test/testPyComTest.py b/com/win32com/test/testPyComTest.py index 4964d0a510..3d0a422db1 100644 --- a/com/win32com/test/testPyComTest.py +++ b/com/win32com/test/testPyComTest.py @@ -250,9 +250,9 @@ def TestCommon(o, is_generated): ), f"Property value wrong - got {o.ULongProp} (expected {check})" TestApplyResult(o.Test, ("Unused", 99), 1) # A bool function TestApplyResult(o.Test, ("Unused", -1), 1) # A bool function - TestApplyResult(o.Test, ("Unused", 1 == 1), 1) # A bool function + TestApplyResult(o.Test, ("Unused", True), 1) # A bool function TestApplyResult(o.Test, ("Unused", 0), 0) - TestApplyResult(o.Test, ("Unused", 1 == 0), 0) + TestApplyResult(o.Test, ("Unused", False), 0) assert o.DoubleString("foo") == "foofoo" diff --git a/com/win32comext/axdebug/codecontainer.py b/com/win32comext/axdebug/codecontainer.py index d82ee00089..91caf7711d 100644 --- a/com/win32comext/axdebug/codecontainer.py +++ b/com/win32comext/axdebug/codecontainer.py @@ -4,6 +4,8 @@ to color the text, and also how to translate lines into offsets, and back. """ +from __future__ import annotations + import os import sys import tokenize @@ -26,7 +28,7 @@ class SourceCodeContainer: def __init__( self, - text, + text: str | None, fileName="", sourceContext=0, startLineNumber=0, @@ -34,7 +36,7 @@ def __init__( debugDocument=None, ): self.sourceContext = sourceContext # The source context added by a smart host. - self.text = text + self.text: str | None = text if text: self._buildlines() self.nextLineNo = 0 diff --git a/com/win32comext/mapi/mapiutil.py b/com/win32comext/mapi/mapiutil.py index 896a8b0d0e..5ae387f2ac 100644 --- a/com/win32comext/mapi/mapiutil.py +++ b/com/win32comext/mapi/mapiutil.py @@ -156,7 +156,7 @@ def SetPropertyValue(obj, prop, val): if not isinstance(prop, int): props = ((mapi.PS_PUBLIC_STRINGS, prop),) propIds = obj.GetIDsFromNames(props, mapi.MAPI_CREATE) - if val == (1 == 1) or val == (1 == 0): + if val == True or val == False: type_tag = mapitags.PT_BOOLEAN else: type_tag = _MapiTypeMap.get(type(val)) diff --git a/pyrightconfig.json b/pyrightconfig.json index bc3c26fbc0..d90035dc31 100644 --- a/pyrightconfig.json +++ b/pyrightconfig.json @@ -37,6 +37,8 @@ "reportOptionalIterable": "warning", "reportOptionalMemberAccess": "warning", "reportOptionalSubscript": "warning", + // Needs fixes in types-pywin32 and requires Python 3.8 to annotate ambiguous global variables + "reportUnnecessaryComparison": "warning", // TODO: Leave Unbound/Undefined to their own PR(s) "reportUnboundVariable": "warning", "reportUndefinedVariable": "warning", @@ -49,8 +51,6 @@ "reportMissingModuleSource": "none", // External type stubs may not be completable, and this will require type stubs for dynamic modules. "reportMissingTypeStubs": "information", - // Sometimes used for extra runtime safety - "reportUnnecessaryComparison": "warning", // Use Flake8/Pycln/Ruff instead "reportUnusedImport": "none", } diff --git a/win32/Lib/sspi.py b/win32/Lib/sspi.py index 5b62913543..d2658773ac 100644 --- a/win32/Lib/sspi.py +++ b/win32/Lib/sspi.py @@ -26,7 +26,7 @@ def __init__(self): def reset(self): """Reset everything to an unauthorized state""" - self.ctxt = None + self.ctxt: win32security.PyCtxtHandleType | None = None self.authenticated = False self.initiator_name = None self.service_name = None From 2e8b177dd5aa906f97a71657641b95def7a7915d Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 26 Jul 2024 15:48:10 -0400 Subject: [PATCH 2/4] undo runtime changes --- com/win32com/test/testPyComTest.py | 4 ++-- com/win32comext/mapi/mapiutil.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/com/win32com/test/testPyComTest.py b/com/win32com/test/testPyComTest.py index 3d0a422db1..4964d0a510 100644 --- a/com/win32com/test/testPyComTest.py +++ b/com/win32com/test/testPyComTest.py @@ -250,9 +250,9 @@ def TestCommon(o, is_generated): ), f"Property value wrong - got {o.ULongProp} (expected {check})" TestApplyResult(o.Test, ("Unused", 99), 1) # A bool function TestApplyResult(o.Test, ("Unused", -1), 1) # A bool function - TestApplyResult(o.Test, ("Unused", True), 1) # A bool function + TestApplyResult(o.Test, ("Unused", 1 == 1), 1) # A bool function TestApplyResult(o.Test, ("Unused", 0), 0) - TestApplyResult(o.Test, ("Unused", False), 0) + TestApplyResult(o.Test, ("Unused", 1 == 0), 0) assert o.DoubleString("foo") == "foofoo" diff --git a/com/win32comext/mapi/mapiutil.py b/com/win32comext/mapi/mapiutil.py index 5ae387f2ac..896a8b0d0e 100644 --- a/com/win32comext/mapi/mapiutil.py +++ b/com/win32comext/mapi/mapiutil.py @@ -156,7 +156,7 @@ def SetPropertyValue(obj, prop, val): if not isinstance(prop, int): props = ((mapi.PS_PUBLIC_STRINGS, prop),) propIds = obj.GetIDsFromNames(props, mapi.MAPI_CREATE) - if val == True or val == False: + if val == (1 == 1) or val == (1 == 0): type_tag = mapitags.PT_BOOLEAN else: type_tag = _MapiTypeMap.get(type(val)) From 454892677f72989fb5febbfb4fcf8b1e17a0ddad Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 26 Jul 2024 16:21:33 -0400 Subject: [PATCH 3/4] Fix typing issue --- Pythonwin/pywin/framework/app.py | 5 ++++- com/win32comext/axdebug/codecontainer.py | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Pythonwin/pywin/framework/app.py b/Pythonwin/pywin/framework/app.py index ec8cedb138..87526b1b7d 100644 --- a/Pythonwin/pywin/framework/app.py +++ b/Pythonwin/pywin/framework/app.py @@ -6,7 +6,7 @@ import os import sys import traceback -from typing import Literal +from typing import TYPE_CHECKING import regutil import win32api @@ -17,6 +17,9 @@ from . import scriptutils +if TYPE_CHECKING: + from typing_extensions import Literal + # Helper for writing a Window position by name, and later loading it. def SaveWindowSize(section, rect, state=""): diff --git a/com/win32comext/axdebug/codecontainer.py b/com/win32comext/axdebug/codecontainer.py index 91caf7711d..babbd6fc4b 100644 --- a/com/win32comext/axdebug/codecontainer.py +++ b/com/win32comext/axdebug/codecontainer.py @@ -9,6 +9,7 @@ import os import sys import tokenize +from typing import Any import win32api import winerror @@ -41,7 +42,8 @@ def __init__( self._buildlines() self.nextLineNo = 0 self.fileName = fileName - self.codeContexts = {} + # Any: PyIDispatch type is not statically exposed + self.codeContexts: dict[int, Any] = {} self.site = site self.startLineNumber = startLineNumber self.debugDocument = debugDocument From e8eaecf3384a12b690f00e90f05ff0ceb31bdfb7 Mon Sep 17 00:00:00 2001 From: Avasam Date: Fri, 26 Jul 2024 20:05:06 -0400 Subject: [PATCH 4/4] Add missing from __future__ import annotations --- Pythonwin/pywin/framework/app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Pythonwin/pywin/framework/app.py b/Pythonwin/pywin/framework/app.py index 87526b1b7d..4e0f09b943 100644 --- a/Pythonwin/pywin/framework/app.py +++ b/Pythonwin/pywin/framework/app.py @@ -3,6 +3,8 @@ # # We also grab the FileOpen command, to invoke our Python editor " The PythonWin application code. Manages most aspects of MDI, etc " +from __future__ import annotations + import os import sys import traceback