Skip to content

Commit 64fc8ad

Browse files
authored
[pywin32] Improve win32com (#15527)
1 parent 282b1a8 commit 64fc8ad

File tree

13 files changed

+408
-288
lines changed

13 files changed

+408
-288
lines changed

stubs/pywin32/@tests/stubtest_allowlist_win32.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ win32com.client.combrowse
88
win32com.client.tlbrowse
99

1010
# Utilities to generate python bindings
11-
win32com.client.build.*
1211
win32com.client.CLSIDToClass
1312
win32com.client.connect
14-
# "dynamic.CDipatch" is necessary for mypy to not throw AssertionError
15-
win32com.client.dynamic.*
16-
win32com.client.gencache.*
1713
win32com.client.genpy
1814
win32com.client.makepy
1915
win32com.client.selecttlb

stubs/pywin32/win32com/client/__init__.pyi

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,30 @@ from typing_extensions import TypeAlias
55

66
import _win32typing
77
from pythoncom import com_record
8+
from win32.lib.pywintypes import IIDType
89
from win32com.client import dynamic
910

1011
_Stringifiable: TypeAlias = object
1112

12-
def GetObject(Pathname: str | None = ..., Class: Incomplete | None = ..., clsctx: Incomplete | None = ...) -> CDispatch: ...
13-
def GetActiveObject(Class, clsctx=...): ...
14-
def Moniker(Pathname, clsctx=...): ...
13+
def GetObject(Pathname: str | None = None, Class=None, clsctx=None) -> CDispatch: ...
14+
def GetActiveObject(Class: str, clsctx=...): ...
15+
def Moniker(Pathname: str, clsctx=...): ...
1516
def Dispatch(
16-
dispatch: str | dynamic.PyIDispatchType | dynamic._GoodDispatchTypes | dynamic.PyIUnknownType,
17-
userName: str | None = ...,
18-
resultCLSID: _Stringifiable | None = ...,
19-
typeinfo: _win32typing.PyITypeInfo | None = ...,
17+
dispatch: str | dynamic.PyIDispatchType | IIDType | dynamic.PyIUnknownType,
18+
userName: str | None = None,
19+
resultCLSID: _Stringifiable | None = None,
20+
typeinfo: _win32typing.PyITypeInfo | None = None,
2021
clsctx: int = ...,
2122
) -> dynamic.CDispatch: ...
22-
def DispatchEx(
23-
clsid,
24-
machine: Incomplete | None = ...,
25-
userName: Incomplete | None = ...,
26-
resultCLSID: Incomplete | None = ...,
27-
typeinfo: Incomplete | None = ...,
28-
clsctx: Incomplete | None = ...,
29-
): ...
23+
def DispatchEx(clsid, machine=None, userName=None, resultCLSID=None, typeinfo=None, clsctx=None): ...
3024

3125
class CDispatch(dynamic.CDispatch):
32-
def __dir__(self): ...
26+
def __dir__(self) -> list[str]: ...
3327

34-
def CastTo(ob, target, typelib: Incomplete | None = ...): ...
28+
def CastTo(ob, target, typelib=None): ...
3529

3630
class Constants:
37-
__dicts__: Incomplete
31+
__dicts__: list[Incomplete]
3832
def __getattr__(self, a: str): ...
3933

4034
constants: Final[Constants]
@@ -45,22 +39,22 @@ class EventsProxy:
4539
def __getattr__(self, attr: str): ...
4640
def __setattr__(self, attr: str, val) -> None: ...
4741

48-
def DispatchWithEvents(clsid, user_event_class): ...
42+
def DispatchWithEvents(clsid, user_event_class) -> EventsProxy: ...
4943
def WithEvents(disp, user_event_class): ...
5044
def getevents(clsid): ...
5145
def Record(name, object) -> com_record: ...
5246
def register_record_class(cls) -> None: ...
5347

5448
class DispatchBaseClass:
55-
def __init__(self, oobj: Incomplete | None = ...) -> None: ...
56-
def __dir__(self): ...
57-
def __eq__(self, other): ...
58-
def __ne__(self, other): ...
49+
def __init__(self, oobj=None) -> None: ...
50+
def __dir__(self) -> list[str]: ...
51+
def __eq__(self, other) -> bool: ...
52+
def __ne__(self, other) -> bool: ...
5953
def __getattr__(self, attr: str): ...
6054
def __setattr__(self, attr: str, value) -> None: ...
6155

6256
class CoClassBaseClass:
63-
def __init__(self, oobj: Incomplete | None = ...) -> None: ...
57+
def __init__(self, oobj=None) -> None: ...
6458
def __getattr__(self, attr: str): ...
6559
def __setattr__(self, attr: str, value) -> None: ...
6660
def __call__(self, *args, **kwargs): ...
@@ -73,4 +67,9 @@ class CoClassBaseClass:
7367
class VARIANT:
7468
varianttype: Incomplete
7569
def __init__(self, vt, value) -> None: ...
76-
value: Incomplete
70+
@property
71+
def value(self): ...
72+
@value.setter
73+
def value(self, newval) -> None: ...
74+
@value.deleter
75+
def value(self) -> None: ...
Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,70 @@
11
from _typeshed import Incomplete
2+
from collections.abc import Iterable
3+
from typing import ClassVar, Final, Literal
4+
5+
class NotSupportedException(Exception): ...
6+
7+
DropIndirection: Final = "DropIndirection"
8+
NoTranslateTypes: Final[list[int]]
9+
NoTranslateMap: Final[set[int]]
10+
11+
class MapEntry:
12+
dispid: Incomplete
13+
desc: Incomplete | None
14+
names: Incomplete
15+
doc: Incomplete
16+
resultCLSID: Incomplete
17+
resultDocumentation: Incomplete | None
18+
wasProperty: Incomplete
19+
hidden: bool | Literal[0, 1]
20+
def __init__(
21+
self, desc_or_id, names=None, doc=None, resultCLSID=..., resultDoc=None, hidden: bool | Literal[0, 1] = 0
22+
) -> None: ...
23+
def GetResultCLSID(self) -> Incomplete | None: ...
24+
def GetResultCLSIDStr(self) -> str: ...
25+
def GetResultName(self) -> Incomplete | None: ...
226

327
class OleItem:
4-
typename: str
28+
typename: ClassVar[str]
529
doc: Incomplete
6-
python_name: Incomplete
7-
bWritten: int
8-
bIsDispatch: int
9-
bIsSink: int
10-
clsid: Incomplete
11-
co_class: Incomplete
12-
def __init__(self, doc: Incomplete | None = ...) -> None: ...
30+
python_name: Incomplete | None
31+
bWritten: bool | Literal[0, 1]
32+
bIsDispatch: bool | Literal[0, 1]
33+
bIsSink: bool | Literal[0, 1]
34+
clsid: Incomplete | None
35+
co_class: Incomplete | None
36+
def __init__(self, doc=None) -> None: ...
1337

1438
class DispatchItem(OleItem):
15-
typename: str
16-
propMap: Incomplete
17-
propMapGet: Incomplete
18-
propMapPut: Incomplete
19-
mapFuncs: Incomplete
20-
defaultDispatchName: Incomplete
21-
hidden: int
22-
def __init__(
23-
self, typeinfo: Incomplete | None = ..., attr: Incomplete | None = ..., doc: Incomplete | None = ..., bForUser: int = ...
24-
) -> None: ...
39+
typename: ClassVar[str]
40+
propMap: dict[Incomplete, Incomplete]
41+
propMapGet: dict[Incomplete, Incomplete]
42+
propMapPut: dict[Incomplete, Incomplete]
43+
mapFuncs: dict[Incomplete, Incomplete]
44+
defaultDispatchName: Incomplete | None
45+
hidden: bool | Literal[0, 1]
46+
def __init__(self, typeinfo=None, attr=None, doc=None, bForUser: bool | Literal[0, 1] = 1) -> None: ...
2547
clsid: Incomplete
26-
bIsDispatch: Incomplete
27-
def Build(self, typeinfo, attr, bForUser: int = ...) -> None: ...
28-
def CountInOutOptArgs(self, argTuple): ...
29-
def MakeFuncMethod(self, entry, name, bMakeClass: int = ...): ...
30-
def MakeDispatchFuncMethod(self, entry, name, bMakeClass: int = ...): ...
31-
def MakeVarArgsFuncMethod(self, entry, name, bMakeClass: int = ...): ...
48+
bIsDispatch: bool | Literal[0, 1]
49+
def Build(self, typeinfo, attr, bForUser: bool | Literal[0, 1] = 1) -> None: ...
50+
def CountInOutOptArgs(self, argTuple: Iterable[Incomplete]) -> tuple[int, int, int]: ...
51+
def MakeFuncMethod(self, entry, name: str, bMakeClass: bool | Literal[0, 1] = 1) -> list[str]: ...
52+
def MakeDispatchFuncMethod(self, entry, name: str, bMakeClass: bool | Literal[0, 1] = 1) -> list[str]: ...
53+
def MakeVarArgsFuncMethod(self, entry, name: str, bMakeClass: bool | Literal[0, 1] = 1) -> list[str]: ...
54+
55+
class VTableItem(DispatchItem):
56+
vtableFuncs: list[tuple[Incomplete, Incomplete, Incomplete]]
57+
def Build(self, typeinfo, attr, bForUser: bool | Literal[0, 1] = 1) -> None: ...
3258

3359
class LazyDispatchItem(DispatchItem):
34-
typename: str
60+
typename: ClassVar[str]
3561
clsid: Incomplete
3662
def __init__(self, attr, doc) -> None: ...
63+
64+
typeSubstMap: Final[dict[int, int]]
65+
valid_identifier_chars: Final[str]
66+
67+
def demunge_leading_underscores(className: str) -> str: ...
68+
def MakePublicAttributeName(className: str, is_global: bool = False) -> str: ...
69+
def MakeDefaultArgRepr(defArgVal) -> str | None: ...
70+
def BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg, is_comment: bool = False) -> str: ...

stubs/pywin32/win32com/client/dynamic.pyi

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from _typeshed import Incomplete
2-
from typing import Any, Protocol, TypeVar, overload
3-
from typing_extensions import TypeAlias
2+
from typing import Any, Final, Literal, Protocol, TypeVar, overload, type_check_only
43

54
import _win32typing
65
from win32.lib.pywintypes import IIDType
@@ -9,60 +8,59 @@ from win32com.client import build
98
_T_co = TypeVar("_T_co", covariant=True)
109
_T = TypeVar("_T")
1110

11+
@type_check_only
1212
class _DispatchCreateClass(Protocol[_T_co]):
1313
@staticmethod
1414
def __call__(
15-
IDispatch: str | PyIDispatchType | _GoodDispatchTypes | PyIUnknownType,
15+
IDispatch: str | PyIDispatchType | IIDType | PyIUnknownType,
1616
olerepr: build.DispatchItem | build.LazyDispatchItem,
17-
userName: str | None = ...,
18-
lazydata: Incomplete | None = ...,
17+
userName: str | None = None,
18+
lazydata=None,
1919
) -> _T_co: ...
2020

2121
debugging: int
2222
debugging_attr: int
23-
LCID: int
24-
ERRORS_BAD_CONTEXT: Incomplete
25-
ALL_INVOKE_TYPES: Incomplete
23+
LCID: Final = 0x0
24+
ERRORS_BAD_CONTEXT: Final[list[int]]
25+
ALL_INVOKE_TYPES: Final[list[int]]
2626

27-
def debug_print(*args) -> None: ...
28-
def debug_attr_print(*args) -> None: ...
27+
def debug_print(*args: object) -> None: ...
28+
def debug_attr_print(*args: object) -> None: ...
2929

3030
PyIDispatchType = _win32typing.PyIDispatch
3131
PyIUnknownType = _win32typing.PyIUnknown
3232

33-
_GoodDispatchTypes: TypeAlias = tuple[type[str], type[IIDType]]
34-
3533
@overload
3634
def Dispatch(
37-
IDispatch: str | PyIDispatchType | _GoodDispatchTypes | PyIUnknownType,
35+
IDispatch: str | PyIDispatchType | IIDType | PyIUnknownType,
3836
userName: str | None,
3937
createClass: _DispatchCreateClass[_T],
40-
typeinfo: _win32typing.PyITypeInfo | None = ...,
38+
typeinfo: _win32typing.PyITypeInfo | None = None,
4139
clsctx: int = ...,
4240
) -> _T: ...
4341
@overload
4442
def Dispatch(
45-
IDispatch: str | PyIDispatchType | _GoodDispatchTypes | PyIUnknownType,
46-
userName: str | None = ...,
43+
IDispatch: str | PyIDispatchType | IIDType | PyIUnknownType,
44+
userName: str | None = None,
4745
createClass: None = None,
48-
typeinfo: _win32typing.PyITypeInfo | None = ...,
46+
typeinfo: _win32typing.PyITypeInfo | None = None,
4947
clsctx: int = ...,
5048
) -> CDispatch: ...
51-
def MakeOleRepr(IDispatch, typeinfo, typecomp): ...
52-
def DumbDispatch(IDispatch, userName: Incomplete | None = ..., createClass: Incomplete | None = ..., clsctx=...): ...
49+
def MakeOleRepr(IDispatch, typeinfo, typecomp) -> build.DispatchItem | build.LazyDispatchItem: ...
50+
def DumbDispatch(IDispatch, userName=None, createClass=None, clsctx=...): ...
5351

5452
class CDispatch:
55-
def __init__(self, IDispatch, olerepr, userName: Incomplete | None = ..., lazydata: Incomplete | None = ...) -> None: ...
53+
def __init__(self, IDispatch, olerepr, userName=None, lazydata=None) -> None: ...
5654
def __call__(self, *args): ...
5755
def __bool__(self) -> bool: ...
58-
def __dir__(self): ...
59-
def __eq__(self, other): ...
60-
def __ne__(self, other): ...
56+
def __dir__(self) -> list[str]: ...
57+
def __eq__(self, other) -> bool: ...
58+
def __ne__(self, other) -> bool: ...
6159
def __int__(self) -> int: ...
6260
def __len__(self) -> int: ...
6361
def __getitem__(self, index): ...
6462
def __setitem__(self, index, *args) -> None: ...
65-
def __LazyMap__(self, attr): ...
63+
def __LazyMap__(self, attr) -> Literal[0, 1] | None: ...
6664
def __AttrToID__(self, attr): ...
6765
ob: Incomplete
6866
# CDispatch objects are dynamically generated and too complex to type
Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
1+
from _typeshed import Incomplete, Unused
2+
from collections.abc import Generator
3+
from contextlib import contextmanager
4+
from types import ModuleType
5+
from typing import Literal, NoReturn
6+
7+
from win32.lib.pywintypes import IIDType
18
from win32com.client import dynamic
29

10+
bForDemandDefault: int
11+
clsidToTypelib: dict[str, tuple[str, int, int, int]]
12+
versionRedirectMap: dict[tuple[str, int, int, int], ModuleType | None]
13+
is_readonly: bool
14+
is_zip: bool
15+
demandGeneratedTypeLibraries: dict[tuple[str, int, int, int], Incomplete]
16+
17+
def __init__() -> None: ...
18+
19+
pickleVersion: int
20+
21+
@contextmanager
22+
def ModuleMutex(module_name: str) -> Generator[None]: ...
23+
def GetGeneratedFileName(clsid, lcid, major, minor) -> str: ...
24+
def SplitGeneratedFileName(fname: str) -> tuple[str, ...]: ...
25+
def GetGeneratePath() -> str: ...
26+
def GetClassForProgID(progid: str) -> type | None: ...
27+
def GetClassForCLSID(clsid) -> type | None: ...
28+
def GetModuleForProgID(progid: str) -> ModuleType | None: ...
29+
def GetModuleForCLSID(clsid) -> ModuleType | None: ...
30+
def GetModuleForTypelib(typelibCLSID, lcid, major, minor) -> ModuleType: ...
31+
def MakeModuleForTypelib(
32+
typelibCLSID,
33+
lcid,
34+
major,
35+
minor,
36+
progressInstance=None,
37+
bForDemand: bool | Literal[0, 1] = ...,
38+
bBuildHidden: bool | Literal[0, 1] = 1,
39+
) -> ModuleType: ...
40+
def MakeModuleForTypelibInterface(
41+
typelib_ob, progressInstance=None, bForDemand: bool | Literal[0, 1] = ..., bBuildHidden: bool | Literal[0, 1] = 1
42+
) -> ModuleType | None: ...
43+
def EnsureModuleForTypelibInterface(
44+
typelib_ob, progressInstance=None, bForDemand: bool | Literal[0, 1] = ..., bBuildHidden: bool | Literal[0, 1] = 1
45+
) -> ModuleType | None: ...
46+
def ForgetAboutTypelibInterface(typelib_ob) -> None: ...
47+
def EnsureModule(
48+
typelibCLSID,
49+
lcid,
50+
major,
51+
minor,
52+
progressInstance=None,
53+
bValidateFile: bool | Literal[0, 1] = ...,
54+
bForDemand: bool | Literal[0, 1] = ...,
55+
bBuildHidden: bool | Literal[0, 1] = 1,
56+
) -> ModuleType | None: ...
357
def EnsureDispatch(
4-
prog_id: str | dynamic.PyIDispatchType | dynamic._GoodDispatchTypes | dynamic.PyIUnknownType, bForDemand: int = ...
58+
prog_id: str | dynamic.PyIDispatchType | IIDType | dynamic.PyIUnknownType, bForDemand: bool | Literal[0, 1] = 1
559
) -> dynamic.CDispatch: ...
60+
def AddModuleToCache(typelibclsid, lcid, major, minor, verbose: Unused = 1, bFlushNow: bool | Literal[0, 1] = ...) -> None: ...
61+
def GetGeneratedInfos() -> list[tuple[Incomplete, Incomplete, Incomplete, Incomplete]]: ...
62+
def Rebuild(verbose: bool | Literal[0, 1] = 1) -> None: ...
63+
def usage() -> NoReturn: ...

0 commit comments

Comments
 (0)