diff --git a/AutoDuck/makedfromi.py b/AutoDuck/makedfromi.py index 53edf0111a..4db2a09f9b 100644 --- a/AutoDuck/makedfromi.py +++ b/AutoDuck/makedfromi.py @@ -32,7 +32,7 @@ def GetComments(line, lineNo, lines): def make_doc_summary(inFile, outFile): - methods = [] + methods: list[tuple[str, list[str]]] = [] modDoc = "" modName = "" lines = inFile.readlines() diff --git a/AutoDuck/py2d.py b/AutoDuck/py2d.py index 5550fb6b7d..c350a4864d 100644 --- a/AutoDuck/py2d.py +++ b/AutoDuck/py2d.py @@ -49,8 +49,8 @@ def should_build_function(build_info): # docstring aware paragraph generator. Isn't there something in docutils # we can use? -def gen_paras(val): - chunks = [] +def gen_paras(val: str): + chunks: list[str] = [] in_docstring = False for line in val.splitlines(): line = ad_escape(line.strip()) diff --git a/com/win32com/client/dynamic.py b/com/win32com/client/dynamic.py index 3738da9031..c65d7e6aea 100644 --- a/com/win32com/client/dynamic.py +++ b/com/win32com/client/dynamic.py @@ -411,7 +411,7 @@ def _make_method_(self, name): # self._print_details_() codeObject = compile(methodCode, "" % self._username_, "exec") # Exec the code object - tempNameSpace = {} + tempNameSpace: dict[str, object] = {} # "Dispatch" in the exec'd code is win32com.client.Dispatch, not ours. globNameSpace = globals().copy() globNameSpace["Dispatch"] = win32com.client.Dispatch diff --git a/com/win32comext/axdebug/codecontainer.py b/com/win32comext/axdebug/codecontainer.py index bc4ec4af71..26aaf2a09a 100644 --- a/com/win32comext/axdebug/codecontainer.py +++ b/com/win32comext/axdebug/codecontainer.py @@ -129,7 +129,7 @@ def _ProcessToken(self, type, token, spos, epos, line): erow, ecol = epos self.GetText() # Prime us. linenum = srow - 1 # Lines zero based for us too. - realCharPos = self.lineOffsets[linenum] + scol + realCharPos: int = self.lineOffsets[linenum] + scol numskipped = realCharPos - self.lastPos if numskipped == 0: pass @@ -162,7 +162,7 @@ def _ProcessToken(self, type, token, spos, epos, line): def GetSyntaxColorAttributes(self): self.lastPos = 0 - self.attrs = [] + self.attrs: list[tuple[int] | tuple[int, int]] = [] try: for tokens in tokenize.tokenize(self.GetNextLine): self._ProcessToken(*tokens) diff --git a/com/win32comext/axscript/client/pyscript.py b/com/win32comext/axscript/client/pyscript.py index 0e36029436..28b7b59686 100644 --- a/com/win32comext/axscript/client/pyscript.py +++ b/com/win32comext/axscript/client/pyscript.py @@ -7,6 +7,8 @@ command line. """ +from __future__ import annotations + import re import types @@ -286,7 +288,7 @@ def GetScriptDispatch(self, name): ) return self.scriptDispatch - def MakeEventMethodName(self, subItemName, eventName): + def MakeEventMethodName(self, subItemName: str, eventName: str): return ( subItemName[0].upper() + subItemName[1:] diff --git a/mypy.ini b/mypy.ini index 69dca8f198..32f4c92b70 100644 --- a/mypy.ini +++ b/mypy.ini @@ -42,10 +42,8 @@ disable_error_code = ; list-item, ; operator, ; override, - ; str-format, ; type-var, ; union-attr, - ; valid-type, ; var-annotated, ; ; And these only happen when checking against types-pywin32 ; func-returns-value, diff --git a/pythonwin/pywin/debugger/dbgcon.py b/pythonwin/pywin/debugger/dbgcon.py index c3a33c417d..8a1101a2e7 100644 --- a/pythonwin/pywin/debugger/dbgcon.py +++ b/pythonwin/pywin/debugger/dbgcon.py @@ -20,7 +20,7 @@ def DoGetOption(optsDict, optName, default): def LoadDebuggerOptions(): - opts = {} + opts: dict[str, str] = {} DoGetOption(opts, OPT_HIDE, 0) DoGetOption(opts, OPT_STOP_EXCEPTIONS, 1) return opts diff --git a/pythonwin/pywin/framework/help.py b/pythonwin/pywin/framework/help.py index 7467d2f8fb..a91f2e1a22 100644 --- a/pythonwin/pywin/framework/help.py +++ b/pythonwin/pywin/framework/help.py @@ -80,7 +80,7 @@ def ListAllHelpFiles(): def _ListAllHelpFilesInRoot(root): """Returns a list of (helpDesc, helpFname) for all registered help files""" - retList = [] + retList: list[tuple[str, str]] = [] try: key = win32api.RegOpenKey( root, regutil.BuildDefaultPythonKey() + "\\Help", 0, win32con.KEY_READ diff --git a/pythonwin/pywin/framework/interact.py b/pythonwin/pywin/framework/interact.py index 9541f9c5d9..8bcd1dd5c6 100644 --- a/pythonwin/pywin/framework/interact.py +++ b/pythonwin/pywin/framework/interact.py @@ -482,7 +482,7 @@ def GetBlockBoundary(self, lineNo): def ExtractCommand(self, lines): start, end = lines - retList = [] + retList: list[str] = [] while end >= start: thisLine = self.DoGetLine(end) promptLen = len(GetPromptPrefix(thisLine)) diff --git a/pythonwin/pywin/framework/scriptutils.py b/pythonwin/pywin/framework/scriptutils.py index abf5a40c13..676e9742ac 100644 --- a/pythonwin/pywin/framework/scriptutils.py +++ b/pythonwin/pywin/framework/scriptutils.py @@ -439,7 +439,7 @@ def ImportFile(): # meaning sys.modules can change as a side-effect of looking at # module.__file__ - so we must take a copy (ie, list(items())) for key, mod in sys.modules.items(): - if getattr(mod, "__file__", None): + if hasattr(mod, "__file__") and mod.__file__: fname = mod.__file__ base, ext = os.path.splitext(fname) if ext.lower() in (".pyo", ".pyc"): @@ -547,14 +547,13 @@ def RunTabNanny(filename): data = newout.getvalue() if data: try: - lineno = data.split()[1] - lineno = int(lineno) + lineno = int(data.split()[1]) _JumpToPosition(filename, lineno) try: # Try and display whitespace GetActiveEditControl().SCISetViewWS(1) except: pass - win32ui.SetStatusText("The TabNanny found trouble at line %d" % lineno) + win32ui.SetStatusText(f"The TabNanny found trouble at line {lineno}") except (IndexError, TypeError, ValueError): print("The tab nanny complained, but I can't see where!") print(data) diff --git a/pythonwin/pywin/framework/winout.py b/pythonwin/pywin/framework/winout.py index c98c98ff85..6be5a60014 100644 --- a/pythonwin/pywin/framework/winout.py +++ b/pythonwin/pywin/framework/winout.py @@ -380,7 +380,7 @@ def __init__( self.iniSizeSection = None self.defSize = defSize self.currentView = None - self.outputQueue = queue.Queue(-1) + self.outputQueue: queue.Queue[str] = queue.Queue(-1) self.mainThreadId = win32api.GetCurrentThreadId() self.idleHandlerSet = 0 self.SetIdleHandler() @@ -520,7 +520,7 @@ def QueueFlush(self, max=None): self.currentView.dowrite("".join(items)) return rc - def HandleOutput(self, message): + def HandleOutput(self, message: str): # debug("QueueOutput on thread %d, flags %d with '%s'...\n" % (win32api.GetCurrentThreadId(), self.writeQueueing, message )) self.outputQueue.put(message) if win32api.GetCurrentThreadId() != self.mainThreadId: diff --git a/pythonwin/pywin/scintilla/config.py b/pythonwin/pywin/scintilla/config.py index 45c2a095c0..3a4589c538 100644 --- a/pythonwin/pywin/scintilla/config.py +++ b/pythonwin/pywin/scintilla/config.py @@ -8,6 +8,8 @@ # .py file, and put the config info in a docstring. Then # pass a CStringIO file (rather than a filename) to the # config manager. +from __future__ import annotations + import glob import importlib.util import marshal @@ -35,7 +37,7 @@ def trace(*args): compiled_config_version = 3 -def split_line(line, lineno): +def split_line(line: str, lineno: int): comment_pos = line.find("#") if comment_pos >= 0: line = line[:comment_pos] @@ -269,7 +271,7 @@ def _save_data(self, name, data): return data def _load_general(self, sub_section, fp, lineno): - map = {} + map: dict[str, list[str | None]] = {} while 1: line, lineno, bBreak = self._readline(fp, lineno) if bBreak: diff --git a/pythonwin/pywin/scintilla/view.py b/pythonwin/pywin/scintilla/view.py index bc816808fa..0081e9f9aa 100644 --- a/pythonwin/pywin/scintilla/view.py +++ b/pythonwin/pywin/scintilla/view.py @@ -667,8 +667,8 @@ def _GetWordSplit(self, pos=-1, bAllowCalls=0): if pos == -1: pos = self.GetSel()[0] - 1 # Character before current one limit = self.GetTextLength() - before = [] - after = [] + before: list[str] = [] + after: list[str] = [] index = pos - 1 wordbreaks_use = wordbreaks if bAllowCalls: diff --git a/win32/Lib/win32pdhquery.py b/win32/Lib/win32pdhquery.py index e356f89142..ec1a9a2cca 100644 --- a/win32/Lib/win32pdhquery.py +++ b/win32/Lib/win32pdhquery.py @@ -124,10 +124,12 @@ """ # Feb 12, 98 - MH added "rawaddcounter" so caller can get exception details. +from __future__ import annotations import _thread import copy import time +from itertools import chain import win32api import win32pdh @@ -445,14 +447,11 @@ def getinstpaths( cur += 1 except IndexError: # if we went over the end pass - paths = [] - for ind in range(len(temp)): - # can this raise an error? - paths.append( - win32pdh.MakeCounterPath( - (machine, "Process", object, None, ind, counter) - ) - ) + paths = [ + win32pdh.MakeCounterPath((machine, "Process", object, None, ind, counter)) + for ind in range(len(temp)) + ] + return paths # should also return the number of elements for naming purposes def open(self, *args, **namedargs): @@ -467,9 +466,11 @@ def open(self, *args, **namedargs): # do all the normal opening stuff, self._base is now the query object BaseQuery.open(*(self,) + args, **namedargs) # should rewrite getinstpaths to take a single tuple - paths = [] - for tup in self.volatilecounters: - paths[len(paths) :] = self.getinstpaths(*tup) + paths = list( + chain.from_iterable( + self.getinstpaths(*tup) for tup in self.volatilecounters + ) + ) for path in paths: try: self.counters.append(win32pdh.AddCounter(self._base, path)) diff --git a/win32/Lib/win32pdhutil.py b/win32/Lib/win32pdhutil.py index 839cb39291..72a0c99826 100644 --- a/win32/Lib/win32pdhutil.py +++ b/win32/Lib/win32pdhutil.py @@ -100,7 +100,7 @@ def FindPerformanceAttributesByName( instanceName = instanceName.lower() items, instances = win32pdh.EnumObjectItems(None, None, object, -1) # Track multiple instances. - instance_dict = {} + instance_dict: dict[str, int] = {} for instance in instances: try: instance_dict[instance] += 1 @@ -125,7 +125,7 @@ def ShowAllProcesses(): None, None, object, win32pdh.PERF_DETAIL_WIZARD ) # Need to track multiple instances of the same name. - instance_dict = {} + instance_dict: dict[str, int] = {} for instance in instances: try: instance_dict[instance] += 1 diff --git a/win32/Lib/win32serviceutil.py b/win32/Lib/win32serviceutil.py index 3276bc61b0..f93db3e5f2 100644 --- a/win32/Lib/win32serviceutil.py +++ b/win32/Lib/win32serviceutil.py @@ -5,6 +5,7 @@ # (which is win32service.error, pywintypes.error, etc) # when things go wrong - eg, not enough permissions to hit the # registry etc. +from __future__ import annotations import importlib.machinery import os @@ -440,8 +441,8 @@ def ControlService(serviceName, code, machine=None): return status -def __FindSvcDeps(findName): - dict = {} +def __FindSvcDeps(findName: str): + deps_dict: dict[str, list[str]] = {} k = win32api.RegOpenKey( win32con.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services" ) @@ -459,19 +460,19 @@ def __FindSvcDeps(findName): deps = () for dep in deps: dep = dep.lower() - dep_on = dict.get(dep, []) + dep_on = deps_dict.get(dep, []) dep_on.append(svc) - dict[dep] = dep_on + deps_dict[dep] = dep_on - return __ResolveDeps(findName, dict) + return __ResolveDeps(findName, deps_dict) -def __ResolveDeps(findName, dict): - items = dict.get(findName.lower(), []) - retList = [] +def __ResolveDeps(findName: str, deps_dict: dict[str, list[str]]): + items = deps_dict.get(findName.lower(), []) + retList: list[str] = [] for svc in items: retList.insert(0, svc) - retList = __ResolveDeps(svc, dict) + retList + retList = __ResolveDeps(svc, deps_dict) + retList return retList