Skip to content

Commit

Permalink
Adopt ruff formatter and linter. (#632)
Browse files Browse the repository at this point in the history
* 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`.
  • Loading branch information
junkmd authored Oct 5, 2024
1 parent ca14c63 commit fa08187
Show file tree
Hide file tree
Showing 27 changed files with 155 additions and 160 deletions.
28 changes: 6 additions & 22 deletions .github/workflows/autofmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
1 change: 0 additions & 1 deletion comtypes/_comobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ def _InterlockedDecrement(ob):


class LocalServer(object):

_queue = None

def run(self, classobjects):
Expand Down
2 changes: 1 addition & 1 deletion comtypes/_memberspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion comtypes/_npsupport.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Consolidation of numpy support utilities. """
"""Consolidation of numpy support utilities."""

import sys

is_64bits = sys.maxsize > 2**32
Expand Down
1 change: 1 addition & 0 deletions comtypes/_post_coinit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 10 additions & 37 deletions comtypes/_post_coinit/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -343,21 +322,15 @@ def CoCreateInstanceEx(
clsctx: Optional[int] = None,
machine: Optional[str] = None,
pServerInfo: Optional[COSERVERINFO] = None,
) -> IUnknown:
...


) -> IUnknown: ...
@overload
def CoCreateInstanceEx(
clsid: GUID,
interface: Type[_T_IUnknown],
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,
Expand Down
1 change: 1 addition & 0 deletions comtypes/_post_coinit/unknwn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
40 changes: 11 additions & 29 deletions comtypes/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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],
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions comtypes/client/_code_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions comtypes/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions comtypes/hints.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand All @@ -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]: ...
Expand All @@ -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]: ...
Expand All @@ -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]: ...
14 changes: 7 additions & 7 deletions comtypes/persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions comtypes/server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import comtypes.client, ctypes


################################################################
# Interfaces
class IClassFactory(comtypes.IUnknown):
Expand Down
1 change: 1 addition & 0 deletions comtypes/server/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
python mycomobj.py /nodebug
"""

import sys
import os
import winreg
Expand Down
Loading

0 comments on commit fa08187

Please sign in to comment.