From 9a7d938bbe75b4e8786d975388144d469094d513 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:19:02 +0900 Subject: [PATCH 1/2] Small changes to coding style. (#627) * Remove redundant comments in `safearray`. They are just for argument names. * Remove redundant comments in `tools.tlbparser`. They are just for argument names. * Remove redundant parentheses in `test_inout_args`. * Adjust coding style in `GetModule`. * Adjust coding style in `typeinfo`. --- comtypes/client/_generate.py | 9 +++++---- comtypes/safearray.py | 4 +--- comtypes/test/test_inout_args.py | 2 +- comtypes/tools/tlbparser.py | 2 +- comtypes/typeinfo.py | 12 ++++++++---- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/comtypes/client/_generate.py b/comtypes/client/_generate.py index 59785bc3..740209d5 100644 --- a/comtypes/client/_generate.py +++ b/comtypes/client/_generate.py @@ -106,7 +106,8 @@ def GetModule(tlib: _UnionT[Any, typeinfo.ITypeLib]) -> types.ModuleType: frame = sys._getframe(1) _file_: Optional[str] = frame.f_globals.get("__file__", None) pathname, is_abs = _resolve_filename( - tlib_string, _file_ and os.path.dirname(_file_) # type: ignore + tlib_string, + _file_ and os.path.dirname(_file_), # type: ignore ) logger.debug("GetModule(%s), resolved: %s", pathname, is_abs) tlib = _load_tlib(pathname) # don't register @@ -251,9 +252,9 @@ def generate(self) -> types.ModuleType: _ItfIid = str -def _get_known_namespaces() -> Tuple[ - Mapping[_SymbolName, _ModuleName], Mapping[_ItfName, _ItfIid] -]: +def _get_known_namespaces() -> ( + Tuple[Mapping[_SymbolName, _ModuleName], Mapping[_ItfName, _ItfIid]] +): """Returns symbols and interfaces that are already statically defined in `ctypes` and `comtypes`. From `ctypes`, all the names are obtained. diff --git a/comtypes/safearray.py b/comtypes/safearray.py index d6f6aa36..7bcae99f 100644 --- a/comtypes/safearray.py +++ b/comtypes/safearray.py @@ -192,9 +192,7 @@ def create_from_ndarray(cls, value, extra, lBound=0): nitems *= d rgsa[i].cElements = d rgsa[i].lBound = lBound - pa = _safearray.SafeArrayCreateEx( - cls._vartype_, value.ndim, rgsa, extra # cDims # rgsaBound - ) # pvExtra + pa = _safearray.SafeArrayCreateEx(cls._vartype_, value.ndim, rgsa, extra) if not pa: if cls._vartype_ == VT_RECORD and extra is None: raise TypeError( diff --git a/comtypes/test/test_inout_args.py b/comtypes/test/test_inout_args.py index 0a30c786..b9026a9a 100644 --- a/comtypes/test/test_inout_args.py +++ b/comtypes/test/test_inout_args.py @@ -278,7 +278,7 @@ def test_permutations(self): (orig_0th, *orig_call_args), orig_kw = orig.call_args self.assertEqual(orig_kw, {}) self.assertIs(orig_0th, self_) - for ((typ, f, val), orig) in zip( + for (typ, f, val), orig in zip( testing_params.call_args_validators, orig_call_args ): self.assertIsInstance(orig, typ) diff --git a/comtypes/tools/tlbparser.py b/comtypes/tools/tlbparser.py index fe3b66a6..c9ec62d8 100644 --- a/comtypes/tools/tlbparser.py +++ b/comtypes/tools/tlbparser.py @@ -743,7 +743,7 @@ def get_tlib_filename(tlib: typeinfo.ITypeLib) -> Optional[str]: la = tlib.GetLibAttr() name = BSTR() if 0 == windll.oleaut32.QueryPathOfRegTypeLib( - byref(la.guid), la.wMajorVerNum, la.wMinorVerNum, 0, byref(name) # lcid + byref(la.guid), la.wMajorVerNum, la.wMinorVerNum, 0, byref(name) ): full_filename = name.value.split("\0")[0] if not os.path.isabs(full_filename): diff --git a/comtypes/typeinfo.py b/comtypes/typeinfo.py index 98e16db4..b6578c6e 100644 --- a/comtypes/typeinfo.py +++ b/comtypes/typeinfo.py @@ -231,7 +231,8 @@ def ReleaseTLibAttr(self, ptla: "_Pointer[TLIBATTR]") -> int: def GetLibAttr(self) -> "TLIBATTR": """Return type library attributes""" return _deref_with_release( - self._GetLibAttr(), self.ReleaseTLibAttr # type: ignore + self._GetLibAttr(), # type: ignore + self.ReleaseTLibAttr, ) def IsName(self, name: str, lHashVal: int = 0) -> Optional[str]: @@ -345,7 +346,8 @@ def ReleaseVarDesc(self, pVarDesc: "_Pointer[VARDESC]") -> int: def GetTypeAttr(self) -> "TYPEATTR": """Return the TYPEATTR for this type""" return _deref_with_release( - self._GetTypeAttr(), self.ReleaseTypeAttr # type: ignore + self._GetTypeAttr(), # type: ignore + self.ReleaseTypeAttr, ) def GetDocumentation( @@ -358,13 +360,15 @@ def GetDocumentation( def GetFuncDesc(self, index: int) -> "FUNCDESC": """Return FUNCDESC for index""" return _deref_with_release( - self._GetFuncDesc(index), self.ReleaseFuncDesc # type: ignore + self._GetFuncDesc(index), # type: ignore + self.ReleaseFuncDesc, ) def GetVarDesc(self, index: int) -> "VARDESC": """Return VARDESC for index""" return _deref_with_release( - self._GetVarDesc(index), self.ReleaseVarDesc # type: ignore + self._GetVarDesc(index), # type: ignore + self.ReleaseVarDesc, ) def GetNames(self, memid: int, count: int = 1) -> List[str]: From 8613f0311dd1da91dac16a6bdbf8ceee750df0fe Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sun, 29 Sep 2024 21:21:31 +0900 Subject: [PATCH 2/2] Refactor from `%` string formatting to f-strings in `codegenerator.namespaces`. (#628) * Update `DeclaredNamespaces`. * Update `ImportedNamespaces`. --- comtypes/tools/codegenerator/namespaces.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/comtypes/tools/codegenerator/namespaces.py b/comtypes/tools/codegenerator/namespaces.py index d84fe087..b1e79f55 100644 --- a/comtypes/tools/codegenerator/namespaces.py +++ b/comtypes/tools/codegenerator/namespaces.py @@ -86,14 +86,14 @@ def get_symbols(self) -> Set[str]: def _make_line(self, from_: str, imports: Sequence[str]) -> str: import_ = ", ".join(imports) - code = "from %s import %s" % (from_, import_) + code = f"from {from_} import {import_}" if len(code) <= 80: return code wrapper = textwrap.TextWrapper( subsequent_indent=" ", initial_indent=" ", break_long_words=False ) import_ = "\n".join(wrapper.wrap(import_)) - code = "from %s import (\n%s\n)" % (from_, import_) + code = f"from {from_} import (\n{import_}\n)" return code def getvalue(self) -> str: @@ -103,12 +103,12 @@ def getvalue(self) -> str: if val is None: ns[key] = val elif key == "*": - lines.append("from %s import *" % val) + lines.append(f"from {val} import *") else: ns.setdefault(val, set()).add(key) # type: ignore for key, val in ns.items(): if val is None: - lines.append("import %s" % key) + lines.append(f"import {key}") else: names = sorted(val, key=lambda s: s.lower()) lines.append(self._make_line(key, names)) @@ -144,9 +144,9 @@ def get_symbols(self) -> Set[str]: def getvalue(self) -> str: lines = [] for (alias, definition), comment in self.data.items(): - code = "%s = %s" % (alias, definition) + code = f"{alias} = {definition}" if comment: - code = code + " # %s" % comment + code = code + f" # {comment}" lines.append(code) return "\n".join(lines)