From fa08187432c1262367df0f0dd2c3c02bcaef689a Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 5 Oct 2024 10:23:09 +0900 Subject: [PATCH] Adopt `ruff` formatter and linter. (#632) * Format codebases by `ruff`. * Condense the spacing between lines in `@overload`ed functions. And some small fixes. * Remove redundant `fmt: off`/`on` and `noqa` comments. * Add `tool.ruff.lint` settings to `pyproject.toml`. * Use `ruff` instead of `black` in the CI pipeline. * Add the `Check lint` step to the CI pipeline. * Add the `ruff` badge to `README.md`. --- .github/workflows/autofmt.yml | 28 ++------- README.md | 2 +- comtypes/_comobject.py | 1 - comtypes/_memberspec.py | 2 +- comtypes/_npsupport.py | 3 +- comtypes/_post_coinit/__init__.py | 1 + comtypes/_post_coinit/misc.py | 47 +++----------- comtypes/_post_coinit/unknwn.py | 1 + comtypes/client/__init__.py | 40 ++++-------- comtypes/client/_code_cache.py | 1 + comtypes/git.py | 1 + comtypes/hints.pyi | 8 +-- comtypes/persist.py | 14 ++--- comtypes/server/__init__.py | 1 + comtypes/server/register.py | 1 + comtypes/shelllink.py | 63 ++++++++++--------- comtypes/test/TestComServer.py | 2 +- comtypes/test/TestDispServer.py | 2 +- comtypes/test/test_clear_cache.py | 1 + comtypes/test/test_dispinterface.py | 1 - comtypes/test/test_server.py | 6 ++ comtypes/test/test_urlhistory.py | 1 + comtypes/tools/codegenerator/typeannotator.py | 6 +- comtypes/tools/typedesc.py | 2 +- comtypes/typeinfo.py | 28 +++------ comtypes/util.py | 1 + pyproject.toml | 51 +++++++++++++++ 27 files changed, 155 insertions(+), 160 deletions(-) diff --git a/.github/workflows/autofmt.yml b/.github/workflows/autofmt.yml index 2c0ee712..6a96677a 100644 --- a/.github/workflows/autofmt.yml +++ b/.github/workflows/autofmt.yml @@ -13,25 +13,9 @@ jobs: uses: actions/setup-python@v5 with: python-version: 3.8 - - name: Install black - run: pip install black==22.12.0 - # PRs from the forked repo will trigger format-checking only. - # Auto-commit to the branch from a forked repo will fail without - # any access tokens or permissions. - # So formatting and auto-commit will be triggered by PRs from the - # base repo only. - - if: github.repository != github.event.pull_request.head.repo.full_name - name: Check style - run: python -m black comtypes/. --check --diff --color - - if: github.repository == github.event.pull_request.head.repo.full_name - name: Format - run: python -m black comtypes/. - - if: github.repository == github.event.pull_request.head.repo.full_name - name: Auto-commit - uses: stefanzweifel/git-auto-commit-action@v4 - with: - branch: ${{ github.head_ref }} - commit_message: apply automatic formatter - commit_user_name: github-actions[bot] - commit_user_email: 41898282+github-actions[bot]@users.noreply.github.com - commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> + - name: Install ruff + run: pip install ruff==0.6.9 + - name: Check format + run: python -m ruff format comtypes/. --check --diff + - name: Check lint + run: python -m ruff check --output-format=github comtypes/. diff --git a/README.md b/README.md index cf5568fa..42056fbe 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![Works on Windows only](https://img.shields.io/badge/-Windows-0078D6.svg?logo=windows&style=flat) [![PyPI version](https://badge.fury.io/py/comtypes.svg)](https://pypi.org/project/comtypes/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/comtypes)](https://pypi.org/project/comtypes/) [![PyPI - License](https://img.shields.io/pypi/l/comtypes)](https://pypi.org/project/comtypes/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/comtypes)](https://pypi.org/project/comtypes/) -[![GitHub Repo stars](https://img.shields.io/github/stars/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/stargazers) [![GitHub forks](https://img.shields.io/github/forks/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/network/members) +[![GitHub Repo stars](https://img.shields.io/github/stars/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/stargazers) [![GitHub forks](https://img.shields.io/github/forks/enthought/comtypes?style=social)](https://github.com/enthought/comtypes/network/members) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) [![Tidelift Subscription](https://tidelift.com/badges/package/pypi/comtypes)](https://tidelift.com/subscription/pkg/pypi-comtypes?utm_source=pypi-comtypes&utm_medium=readme) diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index f646cc9a..0ce4512b 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -349,7 +349,6 @@ def _InterlockedDecrement(ob): class LocalServer(object): - _queue = None def run(self, classobjects): diff --git a/comtypes/_memberspec.py b/comtypes/_memberspec.py index 5ba366ec..5d950cc8 100644 --- a/comtypes/_memberspec.py +++ b/comtypes/_memberspec.py @@ -43,7 +43,7 @@ def _unpack_argspec( def _resolve_argspec( - items: Tuple[_ArgSpecElmType, ...] + items: Tuple[_ArgSpecElmType, ...], ) -> Tuple[Tuple[_ParamFlagType, ...], Tuple[Type[_CData], ...]]: """Unpacks and converts from argspec to paramflags and argtypes. diff --git a/comtypes/_npsupport.py b/comtypes/_npsupport.py index 2c5351d1..760463ff 100644 --- a/comtypes/_npsupport.py +++ b/comtypes/_npsupport.py @@ -1,4 +1,5 @@ -""" Consolidation of numpy support utilities. """ +"""Consolidation of numpy support utilities.""" + import sys is_64bits = sys.maxsize > 2**32 diff --git a/comtypes/_post_coinit/__init__.py b/comtypes/_post_coinit/__init__.py index 5f615735..e9eedce1 100644 --- a/comtypes/_post_coinit/__init__.py +++ b/comtypes/_post_coinit/__init__.py @@ -12,4 +12,5 @@ So it is necessary to maintain minimal settings to keep the lightweight action when the package is initialized. """ + from comtypes._post_coinit.unknwn import _shutdown # noqa diff --git a/comtypes/_post_coinit/misc.py b/comtypes/_post_coinit/misc.py index 2e1e21b0..9f811cc6 100644 --- a/comtypes/_post_coinit/misc.py +++ b/comtypes/_post_coinit/misc.py @@ -88,15 +88,9 @@ def QueryService( @overload -def CoGetObject(displayname: str, interface: None) -> IUnknown: - ... - - +def CoGetObject(displayname: str, interface: None) -> IUnknown: ... @overload -def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown: - ... - - +def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown: ... def CoGetObject(displayname: str, interface: Optional[Type[IUnknown]]) -> IUnknown: """Convert a displayname to a moniker, then bind and return the object identified by the moniker.""" @@ -117,20 +111,14 @@ def CoCreateInstance( interface: None = None, clsctx: Optional[int] = None, punkouter: Optional[_pUnkOuter] = None, -) -> IUnknown: - ... - - +) -> IUnknown: ... @overload def CoCreateInstance( clsid: GUID, interface: Type[_T_IUnknown], clsctx: Optional[int] = None, punkouter: Optional[_pUnkOuter] = None, -) -> _T_IUnknown: - ... - - +) -> _T_IUnknown: ... def CoCreateInstance( clsid: GUID, interface: Optional[Type[IUnknown]] = None, @@ -158,17 +146,14 @@ def CoGetClassObject( clsctx: Optional[int] = None, pServerInfo: "Optional[COSERVERINFO]" = None, interface: None = None, - ) -> hints.IClassFactory: - ... - + ) -> hints.IClassFactory: ... @overload def CoGetClassObject( clsid: GUID, clsctx: Optional[int] = None, pServerInfo: "Optional[COSERVERINFO]" = None, interface: Type[_T_IUnknown] = hints.IClassFactory, - ) -> _T_IUnknown: - ... + ) -> _T_IUnknown: ... def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None): @@ -185,15 +170,9 @@ def CoGetClassObject(clsid, clsctx=None, pServerInfo=None, interface=None): @overload -def GetActiveObject(clsid: GUID, interface: None = None) -> IUnknown: - ... - - +def GetActiveObject(clsid: GUID, interface: None = None) -> IUnknown: ... @overload -def GetActiveObject(clsid: GUID, interface: Type[_T_IUnknown]) -> _T_IUnknown: - ... - - +def GetActiveObject(clsid: GUID, interface: Type[_T_IUnknown]) -> _T_IUnknown: ... def GetActiveObject( clsid: GUID, interface: Optional[Type[IUnknown]] = None ) -> IUnknown: @@ -343,10 +322,7 @@ def CoCreateInstanceEx( clsctx: Optional[int] = None, machine: Optional[str] = None, pServerInfo: Optional[COSERVERINFO] = None, -) -> IUnknown: - ... - - +) -> IUnknown: ... @overload def CoCreateInstanceEx( clsid: GUID, @@ -354,10 +330,7 @@ def CoCreateInstanceEx( clsctx: Optional[int] = None, machine: Optional[str] = None, pServerInfo: Optional[COSERVERINFO] = None, -) -> _T_IUnknown: - ... - - +) -> _T_IUnknown: ... def CoCreateInstanceEx( clsid: GUID, interface: Optional[Type[IUnknown]] = None, diff --git a/comtypes/_post_coinit/unknwn.py b/comtypes/_post_coinit/unknwn.py index 18944b8d..85e23d2f 100644 --- a/comtypes/_post_coinit/unknwn.py +++ b/comtypes/_post_coinit/unknwn.py @@ -371,6 +371,7 @@ def _make_methods(self, methods: List[_ComMemberSpec]) -> None: class _compointer_meta(type(c_void_p), _cominterface_meta): "metaclass for COM interface pointer classes" + # no functionality, but needed to avoid a metaclass conflict diff --git a/comtypes/client/__init__.py b/comtypes/client/__init__.py index 3cce75fc..1d7bf436 100644 --- a/comtypes/client/__init__.py +++ b/comtypes/client/__init__.py @@ -138,22 +138,17 @@ def GetBestInterface(punk: Any) -> Any: # Should we do this for POINTER(IUnknown) also? ctypes.POINTER(automation.IDispatch).__ctypes_from_outparam__ = wrap_outparam # type: ignore + ################################################################ # # Object creation # @overload -def GetActiveObject(progid: _UnionT[str, CoClass, GUID]) -> Any: - ... - - +def GetActiveObject(progid: _UnionT[str, CoClass, GUID]) -> Any: ... @overload def GetActiveObject( progid: _UnionT[str, CoClass, GUID], interface: Type[_T_IUnknown] -) -> _T_IUnknown: - ... - - +) -> _T_IUnknown: ... def GetActiveObject( progid: _UnionT[str, CoClass, GUID], interface: Optional[Type[IUnknown]] = None, @@ -198,17 +193,14 @@ def GetClassObject( clsctx: Optional[int] = None, pServerInfo: Optional[comtypes.COSERVERINFO] = None, interface: None = None, - ) -> hints.IClassFactory: - ... - + ) -> hints.IClassFactory: ... @overload def GetClassObject( progid: _UnionT[str, CoClass, GUID], clsctx: Optional[int] = None, pServerInfo: Optional[comtypes.COSERVERINFO] = None, interface: Type[_T_IUnknown] = hints.IClassFactory, - ) -> _T_IUnknown: - ... + ) -> _T_IUnknown: ... def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None): @@ -224,10 +216,7 @@ def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None): @overload -def CreateObject(progid: _UnionT[str, Type[CoClass], GUID]) -> Any: - ... - - +def CreateObject(progid: _UnionT[str, Type[CoClass], GUID]) -> Any: ... @overload def CreateObject( progid: _UnionT[str, Type[CoClass], GUID], @@ -236,10 +225,7 @@ def CreateObject( interface: Optional[Type[_T_IUnknown]] = None, dynamic: bool = ..., pServerInfo: Optional[comtypes.COSERVERINFO] = None, -) -> _T_IUnknown: - ... - - +) -> _T_IUnknown: ... def CreateObject( progid: _UnionT[str, Type[CoClass], GUID], # which object to create clsctx: Optional[int] = None, # how to create the object @@ -305,15 +291,11 @@ def CreateObject( @overload -def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown: - ... - - +def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown: ... @overload -def CoGetObject(displayname: str, interface: None = None, dynamic: bool = False) -> Any: - ... - - +def CoGetObject( + displayname: str, interface: None = None, dynamic: bool = False +) -> Any: ... def CoGetObject( displayname: str, interface: Optional[Type[comtypes.IUnknown]] = None, diff --git a/comtypes/client/_code_cache.py b/comtypes/client/_code_cache.py index 56328821..7ba7cfbf 100644 --- a/comtypes/client/_code_cache.py +++ b/comtypes/client/_code_cache.py @@ -4,6 +4,7 @@ comtypes.gen package and returns a directory where generated code can be written to. """ + import ctypes, logging, os, sys, tempfile, types from ctypes import wintypes diff --git a/comtypes/git.py b/comtypes/git.py index ff7d63e9..e6975741 100644 --- a/comtypes/git.py +++ b/comtypes/git.py @@ -3,6 +3,7 @@ The global interface table provides a way to marshal interface pointers between different threading appartments. """ + from ctypes import * from comtypes import ( IUnknown, diff --git a/comtypes/hints.pyi b/comtypes/hints.pyi index 9bdb70c0..b67c8ffa 100644 --- a/comtypes/hints.pyi +++ b/comtypes/hints.pyi @@ -253,7 +253,7 @@ _T_E = TypeVar("_T_E") def to_dunder_iter( newenum: _UnionT[ _Descriptor[_T_Inst, Iterator[_T_E]], Callable[[_T_Inst], Iterator[_T_E]] - ] + ], ) -> Callable[[_T_Inst], Iterator[_T_E]]: ... @overload def to_dunder_iter(newenum: Any) -> Callable[..., NoReturn]: ... @@ -266,7 +266,7 @@ def to_dunder_call( _GetSetNamedProperty[_T_Inst, _P_Get, _R_Get, ...], _GetOnlyNamedProperty[_T_Inst, _P_Get, _R_Get], Callable[Concatenate[_T_Inst, _P_Get], _R_Get], - ] + ], ) -> Callable[Concatenate[_T_Inst, _P_Get], _R_Get]: ... @overload def to_dunder_call(item: Any) -> Callable[..., NoReturn]: ... @@ -278,7 +278,7 @@ def to_dunder_getitem( _GetSetNamedProperty[_T_Inst, _P_Get, _R_Get, ...], _GetOnlyNamedProperty[_T_Inst, _P_Get, _R_Get], Callable[Concatenate[_T_Inst, _P_Get], _R_Get], - ] + ], ) -> Callable[Concatenate[_T_Inst, _P_Get], _R_Get]: ... @overload def to_dunder_getitem(item: Any) -> Callable[..., NoReturn]: ... @@ -289,7 +289,7 @@ def to_dunder_setitem( item: _UnionT[ _GetSetNamedProperty[_T_Inst, ..., Any, _P_Set], _SetOnlyNamedProperty[_T_Inst, _P_Set], - ] + ], ) -> Callable[Concatenate[_T_Inst, _P_Set], Any]: ... @overload def to_dunder_setitem(item: Any) -> Callable[..., NoReturn]: ... diff --git a/comtypes/persist.py b/comtypes/persist.py index 5eecc984..4fa7c9d6 100644 --- a/comtypes/persist.py +++ b/comtypes/persist.py @@ -9,6 +9,7 @@ The 'DictPropertyBag' class is a class implementing the IPropertyBag interface, useful in client code. """ + from ctypes import c_int, c_ulong, c_ushort, c_wchar_p from ctypes import POINTER, Structure from ctypes.wintypes import WORD, DWORD, BOOL @@ -244,13 +245,12 @@ class IPersistFile(IPersist): ] if TYPE_CHECKING: - # fmt: off - def IsDirty(self) -> hints.Hresult: ... # noqa - def Load(self, pszFileName: str, dwMode: int) -> hints.Hresult: ... # noqa - def Save(self, pszFileName: str, fRemember: bool) -> hints.Hresult: ... # noqa - def SaveCompleted(self, pszFileName: str) -> hints.Hresult: ... # noqa - def GetCurFile(self) -> str: ... # noqa - # fmt: on + + def IsDirty(self) -> hints.Hresult: ... + def Load(self, pszFileName: str, dwMode: int) -> hints.Hresult: ... + def Save(self, pszFileName: str, fRemember: bool) -> hints.Hresult: ... + def SaveCompleted(self, pszFileName: str) -> hints.Hresult: ... + def GetCurFile(self) -> str: ... from comtypes import COMObject diff --git a/comtypes/server/__init__.py b/comtypes/server/__init__.py index ad7577e6..5798a191 100644 --- a/comtypes/server/__init__.py +++ b/comtypes/server/__init__.py @@ -1,5 +1,6 @@ import comtypes.client, ctypes + ################################################################ # Interfaces class IClassFactory(comtypes.IUnknown): diff --git a/comtypes/server/register.py b/comtypes/server/register.py index c6b509df..9a9e86b4 100644 --- a/comtypes/server/register.py +++ b/comtypes/server/register.py @@ -35,6 +35,7 @@ python mycomobj.py /nodebug """ + import sys import os import winreg diff --git a/comtypes/shelllink.py b/comtypes/shelllink.py index 1736b696..fe404c76 100644 --- a/comtypes/shelllink.py +++ b/comtypes/shelllink.py @@ -128,25 +128,26 @@ class IShellLinkA(IUnknown): ] if TYPE_CHECKING: - # fmt: off - def GetIDList(self) -> hints.Incomplete: ... # noqa - def SetIDList(self, pidl: hints.Incomplete) -> hints.Incomplete: ... # noqa - def SetDescription(self, pszName: bytes) -> hints.Incomplete: ... # noqa - def SetWorkingDirectory(self, pszDir: bytes) -> hints.Hresult: ... # noqa - def SetArguments(self, pszArgs: bytes) -> hints.Hresult: ... # noqa + + def GetIDList(self) -> hints.Incomplete: ... + def SetIDList(self, pidl: hints.Incomplete) -> hints.Incomplete: ... + def SetDescription(self, pszName: bytes) -> hints.Incomplete: ... + def SetWorkingDirectory(self, pszDir: bytes) -> hints.Hresult: ... + def SetArguments(self, pszArgs: bytes) -> hints.Hresult: ... @property - def Hotkey(self) -> int: ... # noqa + def Hotkey(self) -> int: ... @Hotkey.setter - def Hotkey(self, pwHotkey: int) -> None: ... # noqa + def Hotkey(self, pwHotkey: int) -> None: ... @property - def ShowCmd(self) -> int: ... # noqa + def ShowCmd(self) -> int: ... @ShowCmd.setter - def ShowCmd(self, piShowCmd: int) -> None: ... # noqa - def SetIconLocation(self, pszIconPath: bytes, iIcon: int) -> hints.Hresult: ... # noqa - def SetRelativePath(self, pszPathRel: bytes, dwReserved: hints.Literal[0]) -> hints.Hresult: ... # noqa - def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... # noqa - def SetPath(self, pszFile: bytes) -> hints.Hresult: ... # noqa - # fmt: on + def ShowCmd(self, piShowCmd: int) -> None: ... + def SetIconLocation(self, pszIconPath: bytes, iIcon: int) -> hints.Hresult: ... + def SetRelativePath( + self, pszPathRel: bytes, dwReserved: hints.Literal[0] + ) -> hints.Hresult: ... + def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... + def SetPath(self, pszFile: bytes) -> hints.Hresult: ... def GetPath(self, flags: int = SLGP_SHORTPATH) -> bytes: buf = create_string_buffer(MAX_PATH) @@ -262,25 +263,26 @@ class IShellLinkW(IUnknown): ] if TYPE_CHECKING: - # fmt: off - def GetIDList(self) -> hints.Incomplete: ... # noqa - def SetIDList(self, pidl: hints.Incomplete) -> hints.Incomplete: ... # noqa - def SetDescription(self, pszName: str) -> hints.Incomplete: ... # noqa - def SetWorkingDirectory(self, pszDir: str) -> hints.Hresult: ... # noqa - def SetArguments(self, pszArgs: str) -> hints.Hresult: ... # noqa + + def GetIDList(self) -> hints.Incomplete: ... + def SetIDList(self, pidl: hints.Incomplete) -> hints.Incomplete: ... + def SetDescription(self, pszName: str) -> hints.Incomplete: ... + def SetWorkingDirectory(self, pszDir: str) -> hints.Hresult: ... + def SetArguments(self, pszArgs: str) -> hints.Hresult: ... @property - def Hotkey(self) -> int: ... # noqa + def Hotkey(self) -> int: ... @Hotkey.setter - def Hotkey(self, pwHotkey: int) -> None: ... # noqa + def Hotkey(self, pwHotkey: int) -> None: ... @property - def ShowCmd(self) -> int: ... # noqa + def ShowCmd(self) -> int: ... @ShowCmd.setter - def ShowCmd(self, piShowCmd: int) -> None: ... # noqa - def SetIconLocation(self, pszIconPath: str, iIcon: int) -> hints.Hresult: ... # noqa - def SetRelativePath(self, pszPathRel: str, dwReserved: hints.Literal[0]) -> hints.Hresult: ... # noqa - def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... # noqa - def SetPath(self, pszFile: str) -> hints.Hresult: ... # noqa - # fmt: on + def ShowCmd(self, piShowCmd: int) -> None: ... + def SetIconLocation(self, pszIconPath: str, iIcon: int) -> hints.Hresult: ... + def SetRelativePath( + self, pszPathRel: str, dwReserved: hints.Literal[0] + ) -> hints.Hresult: ... + def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... + def SetPath(self, pszFile: str) -> hints.Hresult: ... def GetPath(self, flags: int = SLGP_SHORTPATH) -> str: buf = create_unicode_buffer(MAX_PATH) @@ -319,7 +321,6 @@ class ShellLink(CoClass): if __name__ == "__main__": - import sys import comtypes from comtypes.client import CreateObject diff --git a/comtypes/test/TestComServer.py b/comtypes/test/TestComServer.py index a0fd8bfd..30a6e578 100644 --- a/comtypes/test/TestComServer.py +++ b/comtypes/test/TestComServer.py @@ -33,6 +33,7 @@ ################################################################ + # Implement the CoClass. Use the coclass from the wrapper as base # class, and use DualDispMixin as base class which provides default # implementations of IDispatch, IProvideClassInfo, IProvideClassInfo2 @@ -42,7 +43,6 @@ class TestComServer( TestComServerLib.TestComServer, # the coclass from the typelib wrapper comtypes.server.connectionpoints.ConnectableObjectMixin, ): - # The default interface from the typelib MUST be the first # interface, other interfaces can follow diff --git a/comtypes/test/TestDispServer.py b/comtypes/test/TestDispServer.py index c693f7ef..985f6bf9 100644 --- a/comtypes/test/TestDispServer.py +++ b/comtypes/test/TestDispServer.py @@ -31,6 +31,7 @@ ################################################################ + # Implement the CoClass by defining a subclass of the # TestDispServerLib.TestDispServer class in the wrapper file. The # COMObject base class provides default implementations of the @@ -42,7 +43,6 @@ class TestDispServer( TestDispServerLib.TestDispServer, # the coclass from the typelib wrapper comtypes.server.connectionpoints.ConnectableObjectMixin, ): - # The default interface from the typelib MUST be the first # interface, other interfaces can follow diff --git a/comtypes/test/test_clear_cache.py b/comtypes/test/test_clear_cache.py index e3cf8147..e7d80382 100644 --- a/comtypes/test/test_clear_cache.py +++ b/comtypes/test/test_clear_cache.py @@ -1,6 +1,7 @@ """ Test for the ``comtypes.clear_cache`` module. """ + import contextlib import runpy from unittest.mock import patch, call diff --git a/comtypes/test/test_dispinterface.py b/comtypes/test/test_dispinterface.py index 91b7a855..575e58b7 100644 --- a/comtypes/test/test_dispinterface.py +++ b/comtypes/test/test_dispinterface.py @@ -16,7 +16,6 @@ def setUpModule(): class Test(unittest.TestCase): - if is_resource_enabled("pythoncom"): def test_win32com(self): diff --git a/comtypes/test/test_server.py b/comtypes/test/test_server.py index eefb004d..ca94e2da 100644 --- a/comtypes/test/test_server.py +++ b/comtypes/test/test_server.py @@ -125,6 +125,7 @@ class MyServer(comtypes.CoClass, ConnectableObjectMixin): ) # implementation Name = "foo" + # test def test_Name(self): p = wrap(self.create()) @@ -137,6 +138,7 @@ def test_Name(self): itf.add( "[id(101)] HRESULT MixedInOut([in] int a, [out] int *b, [in] int c, [out] int *d);" ) + # implementation def MixedInOut(self, a, c): return a + 1, c + 1 @@ -151,6 +153,7 @@ def test_MixedInOut(self): itf.add( "[id(102)] HRESULT MultiInOutArgs([in, out] int *pa, [in, out] int *pb);" ) + # implementation def MultiInOutArgs(self, pa, pb): return pa[0] * 3, pb[0] * 4 @@ -174,6 +177,7 @@ def test_MultiInOutArgs(self): ################ # definition itf.add("HRESULT MultiInOutArgs3([out] int *pa, [out] int *pb);") + # implementation def MultiInOutArgs3(self): return 42, 43 @@ -186,6 +190,7 @@ def test_MultiInOutArgs3(self): ################ # definition itf.add("HRESULT MultiInOutArgs4([out] int *pa, [in, out] int *pb);") + # implementation def MultiInOutArgs4(self, pb): return pb[0] + 3, pb[0] + 4 @@ -238,6 +243,7 @@ def test_GetStackTrace(self): # Test events. itf.add("""HRESULT DoSomething();""") outgoing.add("""[id(103)] HRESULT OnSomething();""") + # implementation def DoSomething(self): "Implement the DoSomething method" diff --git a/comtypes/test/test_urlhistory.py b/comtypes/test/test_urlhistory.py index ce3bb2b0..eb428d0e 100644 --- a/comtypes/test/test_urlhistory.py +++ b/comtypes/test/test_urlhistory.py @@ -9,6 +9,7 @@ GetModule(os.path.join(os.path.dirname(__file__), "urlhist.tlb")) from comtypes.gen import urlhistLib + # The pwcsTitle and pwcsUrl fields of the _STATURL structure must be # freed by the caller. The only way to do this without patching the # generated code directly is to monkey-patch the diff --git a/comtypes/tools/codegenerator/typeannotator.py b/comtypes/tools/codegenerator/typeannotator.py index 7b665ac4..7a6aef72 100644 --- a/comtypes/tools/codegenerator/typeannotator.py +++ b/comtypes/tools/codegenerator/typeannotator.py @@ -33,8 +33,7 @@ def inarg_specs(self) -> Sequence[Tuple[Any, str, Optional[Any]]]: return result @abc.abstractmethod - def getvalue(self, name: str) -> str: - ... + def getvalue(self, name: str) -> str: ... _CatMths = Tuple[ # categorized methods @@ -47,8 +46,7 @@ def __init__(self) -> None: self.data: List[str] = [] @abc.abstractmethod - def to_method_annotator(self, method: _T_MTD) -> _MethodAnnotator[_T_MTD]: - ... + def to_method_annotator(self, method: _T_MTD) -> _MethodAnnotator[_T_MTD]: ... def _iter_methods(self, members: Iterable[_T_MTD]) -> Iterator[_CatMths[_T_MTD]]: methods: Dict[str, List[Optional[_T_MTD]]] = {} diff --git a/comtypes/tools/typedesc.py b/comtypes/tools/typedesc.py index 99869b22..66e6e0cf 100644 --- a/comtypes/tools/typedesc.py +++ b/comtypes/tools/typedesc.py @@ -238,7 +238,7 @@ def add_interface(self, itf: _Interface, idlflags: _ImplTypeFlags) -> None: def groupby_impltypeflags( - seq: Sequence[Tuple[_Interface, _ImplTypeFlags]] + seq: Sequence[Tuple[_Interface, _ImplTypeFlags]], ) -> Tuple[_ImplementedInterfaces, _SourceInterfaces]: implemented = [] sources = [] diff --git a/comtypes/typeinfo.py b/comtypes/typeinfo.py index b6578c6e..4a3bf152 100644 --- a/comtypes/typeinfo.py +++ b/comtypes/typeinfo.py @@ -270,15 +270,9 @@ def FindName( @overload -def fix_name(name: None) -> None: - pass - - +def fix_name(name: None) -> None: ... @overload -def fix_name(name: str) -> str: - pass - - +def fix_name(name: str) -> str: ... def fix_name(name): # Some typelibs contain BSTR with embedded NUL characters, # probably the len of the BSTR is wrong. @@ -485,25 +479,23 @@ def GetFieldNames(self, *args: Any) -> List[Optional[str]]: return result if TYPE_CHECKING: - # fmt: off # def RecordInit # def RecordClear - def RecordCopy( # noqa + def RecordCopy( self, pvExisting: hints.Incomplete, pvNew: hints.Incomplete ) -> hints.Hresult: ... - def GetGuid(self) -> GUID: ... # noqa - def GetName(self) -> str: ... # noqa - def GetSize(self) -> int: ... # noqa - def GetTypeInfo(self) -> ITypeInfo: ... # noqa + def GetGuid(self) -> GUID: ... + def GetName(self) -> str: ... + def GetSize(self) -> int: ... + def GetTypeInfo(self) -> ITypeInfo: ... # def GetField # def GetFieldNoCopy # def PutField # def PutFieldNoCopy - def IsMatchingType(self, value: "IRecordInfo") -> bool: ... # noqa + def IsMatchingType(self, value: "IRecordInfo") -> bool: ... # def RecordCreate - def RecordCreateCopy(self, pvSource: hints.Incomplete) -> int: ... # noqa - def RecordDestroy(self, pvRecord: hints.Incomplete) -> hints.Hresult: ... # noqa - # fmt: on + def RecordCreateCopy(self, pvSource: hints.Incomplete) -> int: ... + def RecordDestroy(self, pvRecord: hints.Incomplete) -> hints.Hresult: ... IRecordInfo._methods_ = [ diff --git a/comtypes/util.py b/comtypes/util.py index a04118cd..dba15310 100644 --- a/comtypes/util.py +++ b/comtypes/util.py @@ -1,6 +1,7 @@ """This module defines the funtions byref_at(cobj, offset) and cast_field(struct, fieldname, fieldtype). """ + from ctypes import * diff --git a/pyproject.toml b/pyproject.toml index c02b2718..0f77248b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,54 @@ [build-system] requires = ["setuptools>=61.2"] build-backend = "setuptools.build_meta" + +[tool.ruff.lint] +ignore = ["E402"] + +[tool.ruff.lint.per-file-ignores] +# production +"comtypes/_comobject.py" = ["E713", "E722", "F401"] +"comtypes/_npsupport.py" = ["F401"] +"comtypes/connectionpoints.py" = ["F401", "F403", "F405"] +"comtypes/automation.py" = ["F401", "F403", "F405"] +"comtypes/errorinfo.py" = ["F403", "F405"] +"comtypes/git.py" = ["F401", "F403", "F405"] +"comtypes/logutil.py" = ["E401"] +"comtypes/shelllink.py" = ["F401"] +"comtypes/util.py" = ["F403", "F405"] +"comtypes/viewobject.py" = ["F403", "F405"] +"comtypes/_post_coinit/unknwn.py" = ["F821"] +"comtypes/client/__init__.py" = ["F401", "F403"] +"comtypes/client/_code_cache.py" = ["E401", "E711"] +"comtypes/client/_constants.py" = ["F401"] +"comtypes/client/_events.py" = ["F841"] +"comtypes/client/lazybind.py" = ["F821"] +"comtypes/server/__init__.py" = ["E401"] +"comtypes/server/automation.py" = ["F403", "F405"] +"comtypes/server/connectionpoints.py" = ["F401", "F403", "F405"] +"comtypes/server/inprocserver.py" = ["E713", "E722", "F403", "F405", "F841"] +"comtypes/server/localserver.py" = ["F401", "F403", "F405"] +"comtypes/server/register.py" = ["F403", "F405", "E713", "E731"] +"comtypes/tools/codegenerator/codegenerator.py" = ["E713", "E721"] +"comtypes/tools/codegenerator/helpers.py" = ["F401"] +"comtypes/tools/codegenerator/packing.py" = ["F821", "F841"] +"comtypes/tools/tlbparser.py" = ["F401"] +"comtypes/tools/typedesc.py" = ["F403", "F405"] +"comtypes/tools/typedesc_base.py" = ["F401"] +# tests +"comtypes/test/TestComServer.py" = ["E401", "F401", "F403", "F405"] +"comtypes/test/TestDispServer.py" = ["E401"] +"comtypes/test/find_memleak.py" = ["E401", "F401", "F403", "F405"] +"comtypes/test/setup.py" = ["F401"] +"comtypes/test/test_BSTR.py" = ["E401", "F401", "F403", "F405"] +"comtypes/test/test_agilent.py" = ["F401", "F841"] +"comtypes/test/test_client.py" = ["F401"] +"comtypes/test/test_dict.py" = ["F841"] +"comtypes/test/test_ie.py" = ["F403", "F405", "F841"] +"comtypes/test/test_outparam.py" = ["F403", "F405", "F841"] +"comtypes/test/test_sapi.py" = ["E401"] +"comtypes/test/test_server.py" = ["E401", "E722", "F401", "F841"] +"comtypes/test/test_subinterface.py" = ["E401", "F401", "F403", "F405"] +"comtypes/test/test_typeinfo.py" = ["F401"] +"comtypes/test/test_urlhistory.py" = ["E401", "F401", "F403", "F405", "F841"] +"comtypes/test/test_variant.py" = ["F401", "F821", "F841"]