From ae65bf8edab5f76bb85e90a0cbf2655a26bef2f1 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 28 Feb 2024 09:39:40 -0500 Subject: [PATCH] Extracted annotations only changes from #2102 (#2175) --- .github/workflows/main.yml | 1 + Pythonwin/pywin/framework/bitmap.py | 2 +- Pythonwin/pywin/framework/editor/color/coloreditor.py | 2 +- Pythonwin/pywin/framework/editor/editor.py | 2 +- Pythonwin/pywin/framework/mdi_pychecker.py | 2 +- Pythonwin/pywin/framework/sgrepmdi.py | 2 +- Pythonwin/pywin/scintilla/bindings.py | 6 ++++-- Pythonwin/pywin/scintilla/find.py | 4 +++- Pythonwin/pywin/tools/browseProjects.py | 2 +- com/win32com/client/gencache.py | 11 ++++++++--- com/win32com/server/dispatcher.py | 4 +++- com/win32com/test/testvb.py | 1 + com/win32comext/adsi/demos/test.py | 1 + com/win32comext/axscript/client/framework.py | 4 +++- com/win32comext/axscript/client/scriptdispatch.py | 4 +++- com/win32comext/mapi/mapiutil.py | 8 +++++--- isapi/install.py | 8 +++++--- isapi/samples/advanced.py | 2 +- isapi/simple.py | 4 +++- pywin32_postinstall.py | 4 ++-- win32/Lib/win32pdhutil.py | 4 +++- win32/Lib/win32rcparser.py | 5 +++-- win32/Lib/win32timezone.py | 3 ++- win32/scripts/ce/pysynch.py | 1 + 24 files changed, 58 insertions(+), 29 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 260d16047b..65a8120b15 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -95,6 +95,7 @@ jobs: path: | dist/*.whl + # This job can be run locally with the `format_all.bat` script checkers: runs-on: ubuntu-latest steps: diff --git a/Pythonwin/pywin/framework/bitmap.py b/Pythonwin/pywin/framework/bitmap.py index 0099fbe590..7619006f1f 100644 --- a/Pythonwin/pywin/framework/bitmap.py +++ b/Pythonwin/pywin/framework/bitmap.py @@ -128,7 +128,7 @@ def MatchDocType(self, fileName, fileType): # For debugging purposes, when this module may be reloaded many times. try: - win32ui.GetApp().RemoveDocTemplate(bitmapTemplate) + win32ui.GetApp().RemoveDocTemplate(bitmapTemplate) # type: ignore[has-type, used-before-def] except NameError: pass diff --git a/Pythonwin/pywin/framework/editor/color/coloreditor.py b/Pythonwin/pywin/framework/editor/color/coloreditor.py index 6818d82502..6b42de93bf 100644 --- a/Pythonwin/pywin/framework/editor/color/coloreditor.py +++ b/Pythonwin/pywin/framework/editor/color/coloreditor.py @@ -638,7 +638,7 @@ def GetPythonPropertyPages(self): # For debugging purposes, when this module may be reloaded many times. try: - win32ui.GetApp().RemoveDocTemplate(editorTemplate) + win32ui.GetApp().RemoveDocTemplate(editorTemplate) # type: ignore[has-type, used-before-def] except NameError: pass diff --git a/Pythonwin/pywin/framework/editor/editor.py b/Pythonwin/pywin/framework/editor/editor.py index 6c332bd777..a1208cba6f 100644 --- a/Pythonwin/pywin/framework/editor/editor.py +++ b/Pythonwin/pywin/framework/editor/editor.py @@ -503,7 +503,7 @@ def Create(fileName=None, title=None, template=None): if __name__ == prefModule: # For debugging purposes, when this module may be reloaded many times. try: - win32ui.GetApp().RemoveDocTemplate(editorTemplate) + win32ui.GetApp().RemoveDocTemplate(editorTemplate) # type: ignore[has-type, used-before-def] except (NameError, win32ui.error): pass diff --git a/Pythonwin/pywin/framework/mdi_pychecker.py b/Pythonwin/pywin/framework/mdi_pychecker.py index bde43f775b..6bd34905f9 100644 --- a/Pythonwin/pywin/framework/mdi_pychecker.py +++ b/Pythonwin/pywin/framework/mdi_pychecker.py @@ -842,7 +842,7 @@ def getNew(self): try: - win32ui.GetApp().RemoveDocTemplate(greptemplate) + win32ui.GetApp().RemoveDocTemplate(greptemplate) # type: ignore[has-type, used-before-def] except NameError: pass diff --git a/Pythonwin/pywin/framework/sgrepmdi.py b/Pythonwin/pywin/framework/sgrepmdi.py index a4068b5cd2..6383c939d5 100644 --- a/Pythonwin/pywin/framework/sgrepmdi.py +++ b/Pythonwin/pywin/framework/sgrepmdi.py @@ -751,7 +751,7 @@ def getNew(self): try: - win32ui.GetApp().RemoveDocTemplate(greptemplate) + win32ui.GetApp().RemoveDocTemplate(greptemplate) # type: ignore[has-type, used-before-def] except NameError: pass diff --git a/Pythonwin/pywin/scintilla/bindings.py b/Pythonwin/pywin/scintilla/bindings.py index 44644bdfc8..16ed461a87 100644 --- a/Pythonwin/pywin/scintilla/bindings.py +++ b/Pythonwin/pywin/scintilla/bindings.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import traceback import win32api @@ -13,8 +15,8 @@ next_id = 5000 -event_to_commands = {} # dict of integer IDs to event names. -command_to_events = {} # dict of event names to int IDs +event_to_commands: dict[str, int] = {} # dict of event names to IDs +command_to_events: dict[int, str] = {} # dict of IDs to event names def assign_command_id(event, id=0): diff --git a/Pythonwin/pywin/scintilla/find.py b/Pythonwin/pywin/scintilla/find.py index e1d21a5bce..75fcaa16c7 100644 --- a/Pythonwin/pywin/scintilla/find.py +++ b/Pythonwin/pywin/scintilla/find.py @@ -1,4 +1,6 @@ # find.py - Find and Replace +from __future__ import annotations + import afxres import win32api import win32con @@ -35,7 +37,7 @@ def __setattr__(self, attr, val): curDialog = None lastSearch = defaultSearch = SearchParams() -searchHistory = [] +searchHistory: list[str] = [] def ShowFindDialog(): diff --git a/Pythonwin/pywin/tools/browseProjects.py b/Pythonwin/pywin/tools/browseProjects.py index 1a420b5f18..fe308c4f52 100644 --- a/Pythonwin/pywin/tools/browseProjects.py +++ b/Pythonwin/pywin/tools/browseProjects.py @@ -24,7 +24,7 @@ def GetText(self): class HLICLBRItem(hierlist.HierListItem): - def __init__(self, name, file, lineno, suffix=""): + def __init__(self, name: str, file, lineno, suffix=""): # If the 'name' object itself has a .name, use it. Not sure # how this happens, but seems pyclbr related. # See PyWin32 bug 817035 diff --git a/com/win32com/client/gencache.py b/com/win32com/client/gencache.py index cc23afdf7d..296e594e84 100644 --- a/com/win32com/client/gencache.py +++ b/com/win32com/client/gencache.py @@ -21,10 +21,14 @@ Maybe an OLE2 compound file, or a bsddb file? """ +from __future__ import annotations + import glob import os import sys from importlib import reload +from types import ModuleType +from typing import Any import pythoncom import pywintypes @@ -36,11 +40,11 @@ bForDemandDefault = 0 # Default value of bForDemand - toggle this to change the world - see also makepy.py # The global dictionary -clsidToTypelib = {} +clsidToTypelib: dict[str, tuple[str, int, int, int]] = {} # If we have a different version of the typelib generated, this # maps the "requested version" to the "generated version". -versionRedirectMap = {} +versionRedirectMap: dict[tuple[str, int, int, int], ModuleType | None] = {} # There is no reason we *must* be readonly in a .zip, but we are now, # Rather than check for ".zip" or other tricks, PEP302 defines @@ -53,7 +57,8 @@ # A dictionary of ITypeLibrary objects for demand generation explicitly handed to us # Keyed by usual clsid, lcid, major, minor -demandGeneratedTypeLibraries = {} +# Typing as Any because PyITypeLib is not exposed +demandGeneratedTypeLibraries: dict[tuple[str, int, int, int], Any] = {} import pickle as pickle diff --git a/com/win32com/server/dispatcher.py b/com/win32com/server/dispatcher.py index 88c1fa0218..1b2fb4cea8 100644 --- a/com/win32com/server/dispatcher.py +++ b/com/win32com/server/dispatcher.py @@ -3,6 +3,8 @@ Please see policy.py for a discussion on dispatchers and policies """ +from __future__ import annotations + import traceback from sys import exc_info @@ -284,6 +286,6 @@ def _HandleException_(self): try: import win32trace - DefaultDebugDispatcher = DispatcherWin32trace + DefaultDebugDispatcher: type[DispatcherTrace] = DispatcherWin32trace except ImportError: # no win32trace module - just use a print based one. DefaultDebugDispatcher = DispatcherTrace diff --git a/com/win32com/test/testvb.py b/com/win32com/test/testvb.py index bd188ca0c2..02e33b614f 100644 --- a/com/win32com/test/testvb.py +++ b/com/win32com/test/testvb.py @@ -4,6 +4,7 @@ # import traceback +from collections.abc import Callable import pythoncom import win32com.client diff --git a/com/win32comext/adsi/demos/test.py b/com/win32comext/adsi/demos/test.py index 64061ab68d..3d6b47d565 100644 --- a/com/win32comext/adsi/demos/test.py +++ b/com/win32comext/adsi/demos/test.py @@ -1,4 +1,5 @@ import sys +from collections.abc import Callable import pythoncom import win32api diff --git a/com/win32comext/axscript/client/framework.py b/com/win32comext/axscript/client/framework.py index 9dbdfd5533..0b250a71e1 100644 --- a/com/win32comext/axscript/client/framework.py +++ b/com/win32comext/axscript/client/framework.py @@ -7,6 +7,8 @@ There are classes defined for the engine itself, and for ScriptItems """ +from __future__ import annotations + import re import sys @@ -175,7 +177,7 @@ def Build(self, typeinfo, funcdesc): class EventSink: """A set of events against an item. Note this is a COM client for connection points.""" - _public_methods_ = [] + _public_methods_: list[str] = [] def __init__(self, myItem, coDispatch): self.events = {} diff --git a/com/win32comext/axscript/client/scriptdispatch.py b/com/win32comext/axscript/client/scriptdispatch.py index c1989dc286..2c15d74367 100644 --- a/com/win32comext/axscript/client/scriptdispatch.py +++ b/com/win32comext/axscript/client/scriptdispatch.py @@ -5,6 +5,8 @@ this yet, so it is not well tested! """ +from __future__ import annotations + import types import pythoncom @@ -26,7 +28,7 @@ def _is_callable(obj): class ScriptDispatch: - _public_methods_ = [] + _public_methods_: list[str] = [] def __init__(self, engine, scriptNamespace): self.engine = engine diff --git a/com/win32comext/mapi/mapiutil.py b/com/win32comext/mapi/mapiutil.py index bf13788076..8716a650a9 100644 --- a/com/win32comext/mapi/mapiutil.py +++ b/com/win32comext/mapi/mapiutil.py @@ -1,10 +1,12 @@ # General utilities for MAPI and MAPI objects. +from __future__ import annotations + import pythoncom from pywintypes import TimeType from . import mapi, mapitags -prTable = {} +prTable: dict[int, str] = {} def GetPropTagName(pt): @@ -57,7 +59,7 @@ def GetPropTagName(pt): return ret -mapiErrorTable = {} +mapiErrorTable: dict[int, str] = {} def GetScodeString(hr): @@ -68,7 +70,7 @@ def GetScodeString(hr): return mapiErrorTable.get(hr, pythoncom.GetScodeString(hr)) -ptTable = {} +ptTable: dict[int, str] = {} def GetMapiTypeName(propType, rawType=True): diff --git a/isapi/install.py b/isapi/install.py index f28cf2ea0c..b89c521dfa 100644 --- a/isapi/install.py +++ b/isapi/install.py @@ -2,6 +2,8 @@ # this code adapted from "Tomcat JK2 ISAPI redirector", part of Apache # Created July 2004, Mark Hammond. +from __future__ import annotations + import importlib.machinery import os import shutil @@ -73,7 +75,7 @@ class VirtualDirParameters: EnableDirBrowsing = _DEFAULT_ENABLE_DIR_BROWSING EnableDefaultDoc = _DEFAULT_ENABLE_DEFAULT_DOC DefaultDoc = None # Only set in IIS if not None - ScriptMaps = [] + ScriptMaps: list[ScriptMapParams] = [] ScriptMapUpdate = "end" # can be 'start', 'end', 'replace' Server = None @@ -117,8 +119,8 @@ def __str__(self): class ISAPIParameters: ServerName = _DEFAULT_SERVER_NAME # Description = None - Filters = [] - VirtualDirs = [] + Filters: list[FilterParameters] = [] + VirtualDirs: list[VirtualDirParameters] = [] def __init__(self, **kw): self.__dict__.update(kw) diff --git a/isapi/samples/advanced.py b/isapi/samples/advanced.py index 913de024a6..3cce5d885b 100644 --- a/isapi/samples/advanced.py +++ b/isapi/samples/advanced.py @@ -67,7 +67,7 @@ from isapi import InternalReloadException try: - reload_counter += 1 + reload_counter += 1 # type: ignore[used-before-def] except NameError: reload_counter = 0 diff --git a/isapi/simple.py b/isapi/simple.py index b453bbae01..d5fcb04d7c 100644 --- a/isapi/simple.py +++ b/isapi/simple.py @@ -8,6 +8,8 @@ must ensure each of the required methods are implemented. """ +from __future__ import annotations + class SimpleExtension: "Base class for a simple ISAPI extension" @@ -38,7 +40,7 @@ def TerminateExtension(self, status): class SimpleFilter: "Base class for a a simple ISAPI filter" - filter_flags = None + filter_flags: int | None = None def __init__(self): pass diff --git a/pywin32_postinstall.py b/pywin32_postinstall.py index 19b971ba44..86844e95bd 100644 --- a/pywin32_postinstall.py +++ b/pywin32_postinstall.py @@ -72,7 +72,7 @@ def flush(self): # functions which write lines to PythonXX\pywin32-install.log. This is # a list of actions for the uninstaller, the format is inspired by what # the Wise installer also creates. - file_created + file_created # type: ignore[used-before-def] # 3.10 stopped supporting bdist_wininst, but we can still build them with 3.9. # This can be kept until Python 3.9 or exe installers support is dropped. is_bdist_wininst = True @@ -98,7 +98,7 @@ def get_root_hkey(): try: - create_shortcut + create_shortcut # type: ignore[used-before-def] except NameError: # Create a function with the same signature as create_shortcut provided # by bdist_wininst diff --git a/win32/Lib/win32pdhutil.py b/win32/Lib/win32pdhutil.py index 7795dd904e..850e0ab89d 100644 --- a/win32/Lib/win32pdhutil.py +++ b/win32/Lib/win32pdhutil.py @@ -18,6 +18,8 @@ the easiest way is often to simply use PerfMon to find out the names. """ +from __future__ import annotations + import time import win32pdh @@ -27,7 +29,7 @@ # Handle some localization issues. # see http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q287/1/59.asp&NoWebContent=1 # Build a map of english_counter_name: counter_id -counter_english_map = {} +counter_english_map: dict[str, int] = {} def find_pdh_counter_localized_name(english_name, machine_name=None): diff --git a/win32/Lib/win32rcparser.py b/win32/Lib/win32rcparser.py index e76acfc9aa..8f065bb8eb 100644 --- a/win32/Lib/win32rcparser.py +++ b/win32/Lib/win32rcparser.py @@ -7,6 +7,7 @@ This is a parser for Windows .rc files, which are text files which define dialogs and other Windows UI resources. """ +from __future__ import annotations import os import pprint @@ -172,8 +173,8 @@ def __repr__(self): class RCParser: next_id = 1001 - dialogs = {} - _dialogs = {} + dialogs: dict[str, list[list[str | int | None | tuple[str | int, ...]]]] = {} + _dialogs: dict[str, DialogDef] = {} debugEnabled = False token = "" diff --git a/win32/Lib/win32timezone.py b/win32/Lib/win32timezone.py index d27816ed65..dc41514e03 100644 --- a/win32/Lib/win32timezone.py +++ b/win32/Lib/win32timezone.py @@ -231,6 +231,7 @@ datetime.datetime(2011, 11, 6, 1, 0, tzinfo=TimeZoneInfo('Pacific Standard Time')) """ +from __future__ import annotations import datetime import logging @@ -250,7 +251,7 @@ # A couple of objects for working with objects as if they were native C-type # structures. class _SimpleStruct: - _fields_ = [] # must be overridden by subclasses + _fields_: list[tuple[str, type]] = [] # must be overridden by subclasses def __init__(self, *args, **kw): for i, (name, typ) in enumerate(self._fields_): diff --git a/win32/scripts/ce/pysynch.py b/win32/scripts/ce/pysynch.py index 3244ae45dc..f01e7e4b89 100644 --- a/win32/scripts/ce/pysynch.py +++ b/win32/scripts/ce/pysynch.py @@ -4,6 +4,7 @@ import getopt import os import sys +from collections.abc import Callable import win32api import win32con