diff --git a/comtypes/__init__.py b/comtypes/__init__.py index 1e8ef35a8..d2a589830 100644 --- a/comtypes/__init__.py +++ b/comtypes/__init__.py @@ -3,7 +3,7 @@ import atexit from ctypes import * -from ctypes import _SimpleCData +from ctypes import _Pointer, _SimpleCData try: from _ctypes import COMError @@ -22,6 +22,36 @@ import sys import types +# fmt: off +from typing import ( + Any, ClassVar, overload, TYPE_CHECKING, TypeVar, + # instead of `builtins`. see PEP585 + Dict, List, Tuple, Type, + # instead of `collections.abc`. see PEP585 + Callable, Iterable, Iterator, + # instead of `A | B` and `None | A`. see PEP604 + Optional, Union as _UnionT, # avoiding confusion with `ctypes.Union` +) +# fmt: on +if TYPE_CHECKING: + from ctypes import _CData # only in `typeshed`, private in runtime + from comtypes import hints as hints # type: ignore +else: + _CData = _SimpleCData.__mro__[:-1][-1] + +from comtypes.GUID import GUID +from comtypes import patcher +from comtypes._npsupport import interop as npsupport +from comtypes._memberspec import ( + ComMemberGenerator, + _ComMemberSpec, + DispMemberGenerator, + _DispMemberSpec, + _encode_idl, + _resolve_argspec, +) + + ################################################################ # fmt: off @@ -63,59 +93,6 @@ def wrapper(cls): return wrapper # fmt: on -################################################################ - -# type hinting symbols -# -# `if TYPE_CHECKING:` code block must not be executed because `TYPE_CHECKING` -# is always `False` in runtime. -# see https://peps.python.org/pep-0484/#runtime-or-type-checking -# -if sys.version_info >= (3, 5): - from typing import TYPE_CHECKING -else: # typehints in this package don't support Py<3.5 due to importing symbols. - TYPE_CHECKING = False -# -# Annotations must be placed in a `# type:` comment in according to PEP484. -# see https://peps.python.org/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code -# - `NameError` never raises by using those symbols. -# - It is not able to use any runtime introspections, such as -# `typing.get_type_hints` or `typing.get_origin`. -# -if TYPE_CHECKING: - from ctypes import _CData # only in `typeshed`, private in runtime - - # _CData = _SimpleCData.__mro__[:-1][-1] # defining in runtime - from ctypes import _Pointer - from typing import Any, ClassVar, overload, TypeVar - - # XXX: symbols for backward compatibility. - # instead of `builtins`. see PEP585. - from typing import Dict, List, Tuple, Type - - # instead of `collections.abc`. see PEP585. - from typing import Callable, Iterable, Iterator - - # instead of `A | B` and `None | A`. see PEP604. - from typing import Union as _UnionT # avoiding confusion with `ctypes.Union` - from typing import Optional - - # utilities or workarounds for annotations. - from comtypes import hints as hints - -################################################################ - -from comtypes.GUID import GUID -from comtypes import patcher -from comtypes._npsupport import interop as npsupport -from comtypes._memberspec import ( - ComMemberGenerator, - _ComMemberSpec, - DispMemberGenerator, - _DispMemberSpec, - _encode_idl, - _resolve_argspec, -) ################################################################ if sys.version_info >= (3, 0): @@ -849,8 +826,9 @@ def COMMETHOD(idlflags, restype, methodname, *argspec): ################################################################ # IUnknown, the root of all evil... +_T_IUnknown = TypeVar("_T_IUnknown", bound="IUnknown") + if TYPE_CHECKING: - _T_IUnknown = TypeVar("_T_IUnknown", bound="IUnknown") class _IUnknown_Base(c_void_p): """This is workaround to avoid false-positive of static type checking. diff --git a/comtypes/_memberspec.py b/comtypes/_memberspec.py index 7548a4933..f9f474cfe 100644 --- a/comtypes/_memberspec.py +++ b/comtypes/_memberspec.py @@ -1,28 +1,26 @@ import ctypes - +from typing import ( + Any, + Callable, + Dict, + Iterator, + List, + Optional, + Tuple, + Type, + Union as _UnionT, +) + +from comtypes import _CData import comtypes -from comtypes import TYPE_CHECKING - -if TYPE_CHECKING: - from comtypes import _CData - from typing import ( - Any, - Callable, - Dict, - Iterator, - List, - Optional, - Tuple, - Type, - Union as _UnionT, - ) - - PositionalParamFlagType = Tuple[int, Optional[str]] - OptionalParamFlagType = Tuple[int, Optional[str], Any] - ParamFlagType = _UnionT[PositionalParamFlagType, OptionalParamFlagType] - PositionalArgSpecElmType = Tuple[List[str], Type[_CData], str] - OptionalArgSpecElmType = Tuple[List[str], Type[_CData], str, Any] - ArgSpecElmType = _UnionT[PositionalArgSpecElmType, OptionalArgSpecElmType] + + +PositionalParamFlagType = Tuple[int, Optional[str]] +OptionalParamFlagType = Tuple[int, Optional[str], Any] +ParamFlagType = _UnionT[PositionalParamFlagType, OptionalParamFlagType] +PositionalArgSpecElmType = Tuple[List[str], Type[_CData], str] +OptionalArgSpecElmType = Tuple[List[str], Type[_CData], str, Any] +ArgSpecElmType = _UnionT[PositionalArgSpecElmType, OptionalArgSpecElmType] _PARAMFLAGS = { diff --git a/comtypes/automation.py b/comtypes/automation.py index d1762a1df..ab2206ea0 100644 --- a/comtypes/automation.py +++ b/comtypes/automation.py @@ -3,24 +3,29 @@ import datetime import decimal import sys - from ctypes import * from ctypes import _Pointer from _ctypes import CopyComPointer -from comtypes import ( - BSTR, - COMError, - COMMETHOD, - GUID, - IID, - IUnknown, - STDMETHOD, +from ctypes.wintypes import DWORD, LONG, UINT, VARIANT_BOOL, WCHAR, WORD +from typing import ( + Any, + Callable, + ClassVar, + List, + Optional, TYPE_CHECKING, + Tuple, + Union as _UnionT, ) + +from comtypes import BSTR, COMError, COMMETHOD, GUID, IID, IUnknown, STDMETHOD from comtypes.hresult import * import comtypes.patcher import comtypes +if TYPE_CHECKING: + from comtypes import hints # type: ignore + try: from comtypes import _safearray except (ImportError, AttributeError): @@ -29,21 +34,6 @@ class _safearray(object): tagSAFEARRAY = None -from ctypes.wintypes import DWORD, LONG, UINT, VARIANT_BOOL, WCHAR, WORD - -if TYPE_CHECKING: - from typing import ( - Any, - Callable, - ClassVar, - List, - Optional, - Tuple, - Union as _UnionT, - ) - from comtypes import hints - - if sys.version_info >= (3, 0): int_types = (int,) str_types = (str,) diff --git a/comtypes/client/__init__.py b/comtypes/client/__init__.py index 5e3f6c7ab..f40642adb 100644 --- a/comtypes/client/__init__.py +++ b/comtypes/client/__init__.py @@ -12,28 +12,35 @@ import logging import os import sys +from typing import ( + Any, + Optional, + overload, + Type, + TYPE_CHECKING, + TypeVar, + Union as _UnionT, +) import comtypes from comtypes.hresult import * -from comtypes import automation, CoClass, GUID, IUnknown, TYPE_CHECKING, typeinfo +from comtypes import automation, CoClass, GUID, IUnknown, typeinfo import comtypes.client.dynamic from comtypes.client._constants import Constants from comtypes.client._events import GetEvents, ShowEvents, PumpEvents from comtypes.client._generate import GetModule from comtypes.client._code_cache import _find_gen_dir +if TYPE_CHECKING: + from comtypes import hints # type: ignore + gen_dir = _find_gen_dir() import comtypes.gen ### for testing ##gen_dir = None -if TYPE_CHECKING: - from typing import Any, Optional, overload, Type, TypeVar, Union as _UnionT - from comtypes import hints - - _T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown) - +_T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown) logger = logging.getLogger(__name__) diff --git a/comtypes/client/_generate.py b/comtypes/client/_generate.py index c81759bfd..841463fb1 100644 --- a/comtypes/client/_generate.py +++ b/comtypes/client/_generate.py @@ -5,6 +5,7 @@ import os import sys import types +from typing import Any, Tuple, List, Optional, Dict, Union as _UnionT if sys.version_info >= (3, 0): base_text_type = str @@ -13,13 +14,10 @@ base_text_type = basestring import _winreg as winreg -from comtypes import GUID, TYPE_CHECKING, typeinfo +from comtypes import GUID, typeinfo import comtypes.client from comtypes.tools import codegenerator, tlbparser -if TYPE_CHECKING: - from typing import Any, Tuple, List, Optional, Dict, Union as _UnionT - logger = logging.getLogger(__name__) diff --git a/comtypes/tools/codegenerator.py b/comtypes/tools/codegenerator.py index 5da3cf5dd..d0beb2f12 100644 --- a/comtypes/tools/codegenerator.py +++ b/comtypes/tools/codegenerator.py @@ -7,6 +7,7 @@ import os import sys import textwrap +from typing import Any, Dict, Iterator, List, Optional, Tuple, Union as _UnionT if sys.version_info >= (3, 0): import io @@ -14,20 +15,9 @@ import cStringIO as io import comtypes -from comtypes import TYPE_CHECKING, typeinfo +from comtypes import typeinfo from comtypes.tools import tlbparser, typedesc -if TYPE_CHECKING: - from typing import ( - Any, - Dict, - Iterator, - List, - Optional, - Tuple, - Union as _UnionT, - ) - version = comtypes.__version__ diff --git a/comtypes/tools/tlbparser.py b/comtypes/tools/tlbparser.py index 24d30e125..c0d5efed2 100644 --- a/comtypes/tools/tlbparser.py +++ b/comtypes/tools/tlbparser.py @@ -1,26 +1,24 @@ from __future__ import print_function import os import sys -from ctypes import alignment, c_void_p, sizeof, windll +from typing import ( + Any, + Callable, + Dict, + List, + Optional, + Type, + TYPE_CHECKING, + TypeVar, + Tuple, + Union as _UnionT, +) +from ctypes import alignment, c_void_p, _Pointer, sizeof, windll -from comtypes import automation, COMError, TYPE_CHECKING, typeinfo +from comtypes import automation, _CData, COMError, typeinfo from comtypes.tools import typedesc from comtypes.client._code_cache import _get_module_filename -if TYPE_CHECKING: - from typing import ( - Any, - Callable, - Dict, - List, - Optional, - Type, - TypeVar, - Tuple, - Union as _UnionT, - ) - from ctypes import _CData, _Pointer - # Is the process 64-bit? is_64bits = sys.maxsize > 2**32 diff --git a/comtypes/tools/typedesc.py b/comtypes/tools/typedesc.py index d828dea99..5fefb6b45 100644 --- a/comtypes/tools/typedesc.py +++ b/comtypes/tools/typedesc.py @@ -2,13 +2,11 @@ # in typedesc_base import ctypes -from comtypes import TYPE_CHECKING +from typing import Any, List, Optional, Tuple, Union as _UnionT + from comtypes.typeinfo import ITypeLib, TLIBATTR from comtypes.tools.typedesc_base import * -if TYPE_CHECKING: - from typing import Any, List, Optional, Tuple, Union as _UnionT - class TypeLib(object): def __init__(self, name, guid, major, minor, doc=None): diff --git a/comtypes/tools/typedesc_base.py b/comtypes/tools/typedesc_base.py index 6e64f5649..11bf5b6e9 100644 --- a/comtypes/tools/typedesc_base.py +++ b/comtypes/tools/typedesc_base.py @@ -1,10 +1,15 @@ # typedesc.py - classes representing C type descriptions +from typing import ( + Any, + List, + Optional, + TYPE_CHECKING, + Tuple, + Union as _UnionT, + SupportsInt, +) import comtypes -from comtypes import TYPE_CHECKING - -if TYPE_CHECKING: - from typing import Any, List, Optional, Tuple, Union as _UnionT, SupportsInt class Argument(object): diff --git a/comtypes/typeinfo.py b/comtypes/typeinfo.py index 73fa149c4..01326caff 100644 --- a/comtypes/typeinfo.py +++ b/comtypes/typeinfo.py @@ -3,14 +3,28 @@ # generated by 'xml2py' # flags '..\tools\windows.xml -m comtypes -m comtypes.automation -w -r .*TypeLibEx -r .*TypeLib -o typeinfo.py' # then hacked manually +from ctypes import _Pointer import os import sys +from typing import ( + Any, + Callable, + List, + Optional, + overload, + Sequence, + Type, + TypeVar, + Tuple, + Union as _UnionT, +) import weakref from ctypes import * from ctypes.wintypes import DWORD, LONG, UINT, ULONG, WCHAR, WORD from comtypes import ( BSTR, + _CData, COMMETHOD, _GUID, GUID, @@ -31,23 +45,8 @@ tagVARIANT, ) -if TYPE_CHECKING: - from ctypes import _CData, _Pointer - from typing import ( - Any, - Callable, - List, - Optional, - overload, - Sequence, - Type, - TypeVar, - Tuple, - Union as _UnionT, - ) - - _CT = TypeVar("_CT", bound=_CData) - _T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown) +_CT = TypeVar("_CT", bound=_CData) +_T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown) is_64_bit = sys.maxsize > 2**32