Skip to content

Commit

Permalink
Refactor typings and import blocks in client/__init__.py, `_post_co…
Browse files Browse the repository at this point in the history
…init/misc.py` and `test/test_getactiveobj.py`. (#774)
  • Loading branch information
junkmd authored Jan 27, 2025
1 parent c0fb162 commit 547e736
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
18 changes: 4 additions & 14 deletions comtypes/_post_coinit/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,15 @@
pointer,
)
from ctypes.wintypes import DWORD, LPCWSTR, LPVOID

# fmt: off
from typing import ( # noqa
Any, overload, TYPE_CHECKING, TypeVar,
# instead of `builtins`. see PEP585
Type,
# instead of `collections.abc`. see PEP585
Callable,
# instead of `A | B` and `None | A`. see PEP604
Optional,
)
# fmt: on
if TYPE_CHECKING:
from comtypes import hints as hints # noqa # type: ignore
from typing import TYPE_CHECKING, Any, Callable, Optional, Type, TypeVar, overload

from comtypes import CLSCTX_LOCAL_SERVER, CLSCTX_REMOTE_SERVER, CLSCTX_SERVER, GUID
from comtypes._memberspec import COMMETHOD
from comtypes._post_coinit.unknwn import IUnknown

if TYPE_CHECKING:
from comtypes import hints as hints # noqa # type: ignore


def _is_object(obj):
"""This function determines if the argument is a COM object. It
Expand Down
24 changes: 12 additions & 12 deletions comtypes/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
import logging
import os
import sys
from typing import Any, overload, TypeVar, TYPE_CHECKING
from typing import Optional, Type, Union as _UnionT
from typing import TYPE_CHECKING, Any, Optional, Type, TypeVar, overload
from typing import Union as _UnionT

import comtypes
from comtypes.hresult import *
from comtypes import automation, CoClass, GUID, IUnknown, typeinfo
import comtypes.client.dynamic
from comtypes import GUID, CoClass, IUnknown, automation, typeinfo
from comtypes.client._code_cache import _find_gen_dir
from comtypes.client._constants import Constants
from comtypes.client._events import GetEvents, ShowEvents, PumpEvents
from comtypes.client._events import GetEvents, PumpEvents, ShowEvents
from comtypes.client._generate import GetModule
from comtypes.client._code_cache import _find_gen_dir
from comtypes.hresult import *

if TYPE_CHECKING:
from comtypes import hints # type: ignore
Expand Down Expand Up @@ -144,13 +144,13 @@ def GetBestInterface(punk: Any) -> Any:
# Object creation
#
@overload
def GetActiveObject(progid: _UnionT[str, CoClass, GUID]) -> Any: ...
def GetActiveObject(progid: _UnionT[str, Type[CoClass], GUID]) -> Any: ...
@overload
def GetActiveObject(
progid: _UnionT[str, CoClass, GUID], interface: Type[_T_IUnknown]
progid: _UnionT[str, Type[CoClass], GUID], interface: Type[_T_IUnknown]
) -> _T_IUnknown: ...
def GetActiveObject(
progid: _UnionT[str, CoClass, GUID],
progid: _UnionT[str, Type[CoClass], GUID],
interface: Optional[Type[IUnknown]] = None,
dynamic: bool = False,
) -> Any:
Expand Down Expand Up @@ -189,22 +189,22 @@ def _manage(

@overload
def GetClassObject(
progid: _UnionT[str, CoClass, GUID],
progid: _UnionT[str, Type[CoClass], GUID],
clsctx: Optional[int] = None,
pServerInfo: Optional[comtypes.COSERVERINFO] = None,
interface: None = None,
) -> hints.IClassFactory: ...
@overload
def GetClassObject(
progid: _UnionT[str, CoClass, GUID],
progid: _UnionT[str, Type[CoClass], GUID],
clsctx: Optional[int] = None,
pServerInfo: Optional[comtypes.COSERVERINFO] = None,
interface: Type[_T_IUnknown] = hints.IClassFactory,
) -> _T_IUnknown: ...


def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None):
# type: (_UnionT[str, CoClass, GUID], Optional[int], Optional[comtypes.COSERVERINFO], Optional[Type[IUnknown]]) -> IUnknown
# type: (_UnionT[str, Type[CoClass], GUID], Optional[int], Optional[comtypes.COSERVERINFO], Optional[Type[IUnknown]]) -> IUnknown
"""Create and return the class factory for a COM object.
'clsctx' specifies how to create the object, use the CLSCTX_... constants.
Expand Down
8 changes: 4 additions & 4 deletions comtypes/test/test_getactiveobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
try:
# pass Word libUUID
comtypes.client.GetModule(("{00020905-0000-0000-C000-000000000046}",))
IMPORT_FAILED = False
IMPORT_WORD_FAILED = False
except (ImportError, OSError):
IMPORT_FAILED = True
IMPORT_WORD_FAILED = True


################################################################
Expand All @@ -24,8 +24,8 @@
################################################################


@unittest.skipIf(IMPORT_FAILED, "This depends on Word.")
class Test(unittest.TestCase):
@unittest.skipIf(IMPORT_WORD_FAILED, "This depends on Word.")
class Test_Word(unittest.TestCase):
def setUp(self):
try:
comtypes.client.GetActiveObject("Word.Application")
Expand Down

0 comments on commit 547e736

Please sign in to comment.