Skip to content

Commit 275984b

Browse files
committed
[pywin32] Improve win32com
* Add `Final` * Add default values * Use `bool | Literal[0, 1]` for boolean params (instead of `int`) * Annotate known parts (Source: https://github.com/mhammond/pywin32/tree/b311/com/win32com)
1 parent 9c855f3 commit 275984b

File tree

12 files changed

+344
-277
lines changed

12 files changed

+344
-277
lines changed

stubs/pywin32/win32com/client/__init__.pyi

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,25 @@ from win32com.client import dynamic
99

1010
_Stringifiable: TypeAlias = object
1111

12-
def GetObject(Pathname: str | None = ..., Class: Incomplete | None = ..., clsctx: Incomplete | None = ...) -> CDispatch: ...
13-
def GetActiveObject(Class, clsctx=...): ...
14-
def Moniker(Pathname, clsctx=...): ...
12+
def GetObject(Pathname: str | None = None, Class=None, clsctx=None) -> CDispatch: ...
13+
def GetActiveObject(Class: str, clsctx=...): ...
14+
def Moniker(Pathname: str, clsctx=...): ...
1515
def Dispatch(
1616
dispatch: str | dynamic.PyIDispatchType | dynamic._GoodDispatchTypes | dynamic.PyIUnknownType,
17-
userName: str | None = ...,
18-
resultCLSID: _Stringifiable | None = ...,
19-
typeinfo: _win32typing.PyITypeInfo | None = ...,
17+
userName: str | None = None,
18+
resultCLSID: _Stringifiable | None = None,
19+
typeinfo: _win32typing.PyITypeInfo | None = None,
2020
clsctx: int = ...,
2121
) -> 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-
): ...
22+
def DispatchEx(clsid, machine=None, userName=None, resultCLSID=None, typeinfo=None, clsctx=None): ...
3023

3124
class CDispatch(dynamic.CDispatch):
32-
def __dir__(self): ...
25+
def __dir__(self) -> list[str]: ...
3326

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

3629
class Constants:
37-
__dicts__: Incomplete
30+
__dicts__: list[Incomplete]
3831
def __getattr__(self, a: str): ...
3932

4033
constants: Final[Constants]
@@ -45,22 +38,22 @@ class EventsProxy:
4538
def __getattr__(self, attr: str): ...
4639
def __setattr__(self, attr: str, val) -> None: ...
4740

48-
def DispatchWithEvents(clsid, user_event_class): ...
41+
def DispatchWithEvents(clsid, user_event_class) -> EventsProxy: ...
4942
def WithEvents(disp, user_event_class): ...
5043
def getevents(clsid): ...
5144
def Record(name, object) -> com_record: ...
5245
def register_record_class(cls) -> None: ...
5346

5447
class DispatchBaseClass:
55-
def __init__(self, oobj: Incomplete | None = ...) -> None: ...
56-
def __dir__(self): ...
57-
def __eq__(self, other): ...
58-
def __ne__(self, other): ...
48+
def __init__(self, oobj=None) -> None: ...
49+
def __dir__(self) -> list[str]: ...
50+
def __eq__(self, other) -> bool: ...
51+
def __ne__(self, other) -> bool: ...
5952
def __getattr__(self, attr: str): ...
6053
def __setattr__(self, attr: str, value) -> None: ...
6154

6255
class CoClassBaseClass:
63-
def __init__(self, oobj: Incomplete | None = ...) -> None: ...
56+
def __init__(self, oobj=None) -> None: ...
6457
def __getattr__(self, attr: str): ...
6558
def __setattr__(self, attr: str, value) -> None: ...
6659
def __call__(self, *args, **kwargs): ...
@@ -73,4 +66,9 @@ class CoClassBaseClass:
7366
class VARIANT:
7467
varianttype: Incomplete
7568
def __init__(self, vt, value) -> None: ...
76-
value: Incomplete
69+
@property
70+
def value(self): ...
71+
@value.setter
72+
def value(self, newval) -> None: ...
73+
@value.deleter
74+
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: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from _typeshed import Incomplete
2-
from typing import Any, Protocol, TypeVar, overload
2+
from typing import Any, Final, Literal, Protocol, TypeVar, overload, type_check_only
33
from typing_extensions import TypeAlias
44

55
import _win32typing
@@ -9,23 +9,24 @@ from win32com.client import build
99
_T_co = TypeVar("_T_co", covariant=True)
1010
_T = TypeVar("_T")
1111

12+
@type_check_only
1213
class _DispatchCreateClass(Protocol[_T_co]):
1314
@staticmethod
1415
def __call__(
1516
IDispatch: str | PyIDispatchType | _GoodDispatchTypes | PyIUnknownType,
1617
olerepr: build.DispatchItem | build.LazyDispatchItem,
17-
userName: str | None = ...,
18-
lazydata: Incomplete | None = ...,
18+
userName: str | None = None,
19+
lazydata=None,
1920
) -> _T_co: ...
2021

2122
debugging: int
2223
debugging_attr: int
23-
LCID: int
24-
ERRORS_BAD_CONTEXT: Incomplete
25-
ALL_INVOKE_TYPES: Incomplete
24+
LCID: Final = 0x0
25+
ERRORS_BAD_CONTEXT: Final[list[int]]
26+
ALL_INVOKE_TYPES: Final[list[int]]
2627

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

3031
PyIDispatchType = _win32typing.PyIDispatch
3132
PyIUnknownType = _win32typing.PyIUnknown
@@ -37,32 +38,32 @@ def Dispatch(
3738
IDispatch: str | PyIDispatchType | _GoodDispatchTypes | PyIUnknownType,
3839
userName: str | None,
3940
createClass: _DispatchCreateClass[_T],
40-
typeinfo: _win32typing.PyITypeInfo | None = ...,
41+
typeinfo: _win32typing.PyITypeInfo | None = None,
4142
clsctx: int = ...,
4243
) -> _T: ...
4344
@overload
4445
def Dispatch(
4546
IDispatch: str | PyIDispatchType | _GoodDispatchTypes | PyIUnknownType,
46-
userName: str | None = ...,
47+
userName: str | None = None,
4748
createClass: None = None,
48-
typeinfo: _win32typing.PyITypeInfo | None = ...,
49+
typeinfo: _win32typing.PyITypeInfo | None = None,
4950
clsctx: int = ...,
5051
) -> CDispatch: ...
51-
def MakeOleRepr(IDispatch, typeinfo, typecomp): ...
52-
def DumbDispatch(IDispatch, userName: Incomplete | None = ..., createClass: Incomplete | None = ..., clsctx=...): ...
52+
def MakeOleRepr(IDispatch, typeinfo, typecomp) -> build.DispatchItem | build.LazyDispatchItem: ...
53+
def DumbDispatch(IDispatch, userName=None, createClass=None, clsctx=...): ...
5354

5455
class CDispatch:
55-
def __init__(self, IDispatch, olerepr, userName: Incomplete | None = ..., lazydata: Incomplete | None = ...) -> None: ...
56+
def __init__(self, IDispatch, olerepr, userName=None, lazydata=None) -> None: ...
5657
def __call__(self, *args): ...
5758
def __bool__(self) -> bool: ...
58-
def __dir__(self): ...
59-
def __eq__(self, other): ...
60-
def __ne__(self, other): ...
59+
def __dir__(self) -> list[str]: ...
60+
def __eq__(self, other) -> bool: ...
61+
def __ne__(self, other) -> bool: ...
6162
def __int__(self) -> int: ...
6263
def __len__(self) -> int: ...
6364
def __getitem__(self, index): ...
6465
def __setitem__(self, index, *args) -> None: ...
65-
def __LazyMap__(self, attr): ...
66+
def __LazyMap__(self, attr) -> Literal[0, 1] | None: ...
6667
def __AttrToID__(self, attr): ...
6768
ob: Incomplete
6869
# CDispatch objects are dynamically generated and too complex to type
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from typing import Literal
2+
13
from win32com.client import dynamic
24

35
def EnsureDispatch(
4-
prog_id: str | dynamic.PyIDispatchType | dynamic._GoodDispatchTypes | dynamic.PyIUnknownType, bForDemand: int = ...
6+
prog_id: str | dynamic.PyIDispatchType | dynamic._GoodDispatchTypes | dynamic.PyIUnknownType,
7+
bForDemand: bool | Literal[0, 1] = 1,
58
) -> dynamic.CDispatch: ...

stubs/pywin32/win32com/olectl.pyi

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
1-
FACILITY_CONTROL: int
1+
from typing import Final
2+
3+
FACILITY_CONTROL: Final = 0xA
24

35
def MAKE_SCODE(sev: int, fac: int, code: int) -> int: ...
46
def STD_CTL_SCODE(n: int) -> int: ...
57

6-
CTL_E_ILLEGALFUNCTIONCALL: int
7-
CTL_E_OVERFLOW: int
8-
CTL_E_OUTOFMEMORY: int
9-
CTL_E_DIVISIONBYZERO: int
10-
CTL_E_OUTOFSTRINGSPACE: int
11-
CTL_E_OUTOFSTACKSPACE: int
12-
CTL_E_BADFILENAMEORNUMBER: int
13-
CTL_E_FILENOTFOUND: int
14-
CTL_E_BADFILEMODE: int
15-
CTL_E_FILEALREADYOPEN: int
16-
CTL_E_DEVICEIOERROR: int
17-
CTL_E_FILEALREADYEXISTS: int
18-
CTL_E_BADRECORDLENGTH: int
19-
CTL_E_DISKFULL: int
20-
CTL_E_BADRECORDNUMBER: int
21-
CTL_E_BADFILENAME: int
22-
CTL_E_TOOMANYFILES: int
23-
CTL_E_DEVICEUNAVAILABLE: int
24-
CTL_E_PERMISSIONDENIED: int
25-
CTL_E_DISKNOTREADY: int
26-
CTL_E_PATHFILEACCESSERROR: int
27-
CTL_E_PATHNOTFOUND: int
28-
CTL_E_INVALIDPATTERNSTRING: int
29-
CTL_E_INVALIDUSEOFNULL: int
30-
CTL_E_INVALIDFILEFORMAT: int
31-
CTL_E_INVALIDPROPERTYVALUE: int
32-
CTL_E_INVALIDPROPERTYARRAYINDEX: int
33-
CTL_E_SETNOTSUPPORTEDATRUNTIME: int
34-
CTL_E_SETNOTSUPPORTED: int
35-
CTL_E_NEEDPROPERTYARRAYINDEX: int
36-
CTL_E_SETNOTPERMITTED: int
37-
CTL_E_GETNOTSUPPORTEDATRUNTIME: int
38-
CTL_E_GETNOTSUPPORTED: int
39-
CTL_E_PROPERTYNOTFOUND: int
40-
CTL_E_INVALIDCLIPBOARDFORMAT: int
41-
CTL_E_INVALIDPICTURE: int
42-
CTL_E_PRINTERERROR: int
43-
CTL_E_CANTSAVEFILETOTEMP: int
44-
CTL_E_SEARCHTEXTNOTFOUND: int
45-
CTL_E_REPLACEMENTSTOOLONG: int
46-
CONNECT_E_FIRST: int
47-
CONNECT_E_LAST: int
48-
CONNECT_S_FIRST: int
49-
CONNECT_S_LAST: int
50-
CONNECT_E_NOCONNECTION: int
51-
CONNECT_E_ADVISELIMIT: int
52-
CONNECT_E_CANNOTCONNECT: int
53-
CONNECT_E_OVERRIDDEN: int
54-
CLASS_E_NOTLICENSED: int
8+
CTL_E_ILLEGALFUNCTIONCALL: Final = -2146828283
9+
CTL_E_OVERFLOW: Final = -2146828282
10+
CTL_E_OUTOFMEMORY: Final = -2146828281
11+
CTL_E_DIVISIONBYZERO: Final = -2146828277
12+
CTL_E_OUTOFSTRINGSPACE: Final = -2146828274
13+
CTL_E_OUTOFSTACKSPACE: Final = -2146828260
14+
CTL_E_BADFILENAMEORNUMBER: Final = -2146828236
15+
CTL_E_FILENOTFOUND: Final = -2146828235
16+
CTL_E_BADFILEMODE: Final = -2146828234
17+
CTL_E_FILEALREADYOPEN: Final = -2146828233
18+
CTL_E_DEVICEIOERROR: Final = -2146828231
19+
CTL_E_FILEALREADYEXISTS: Final = -2146828230
20+
CTL_E_BADRECORDLENGTH: Final = -2146828229
21+
CTL_E_DISKFULL: Final = -2146828227
22+
CTL_E_BADRECORDNUMBER: Final = -2146828225
23+
CTL_E_BADFILENAME: Final = -2146828224
24+
CTL_E_TOOMANYFILES: Final = -2146828221
25+
CTL_E_DEVICEUNAVAILABLE: Final = -2146828220
26+
CTL_E_PERMISSIONDENIED: Final = -2146828218
27+
CTL_E_DISKNOTREADY: Final = -2146828217
28+
CTL_E_PATHFILEACCESSERROR: Final = -2146828213
29+
CTL_E_PATHNOTFOUND: Final = -2146828212
30+
CTL_E_INVALIDPATTERNSTRING: Final = -2146828195
31+
CTL_E_INVALIDUSEOFNULL: Final = -2146828194
32+
CTL_E_INVALIDFILEFORMAT: Final = -2146827967
33+
CTL_E_INVALIDPROPERTYVALUE: Final = -2146827908
34+
CTL_E_INVALIDPROPERTYARRAYINDEX: Final = -2146827907
35+
CTL_E_SETNOTSUPPORTEDATRUNTIME: Final = -2146827906
36+
CTL_E_SETNOTSUPPORTED: Final = -2146827905
37+
CTL_E_NEEDPROPERTYARRAYINDEX: Final = -2146827903
38+
CTL_E_SETNOTPERMITTED: Final = -2146827901
39+
CTL_E_GETNOTSUPPORTEDATRUNTIME: Final = -2146827895
40+
CTL_E_GETNOTSUPPORTED: Final = -2146827894
41+
CTL_E_PROPERTYNOTFOUND: Final = -2146827866
42+
CTL_E_INVALIDCLIPBOARDFORMAT: Final = -2146827828
43+
CTL_E_INVALIDPICTURE: Final = -2146827807
44+
CTL_E_PRINTERERROR: Final = -2146827806
45+
CTL_E_CANTSAVEFILETOTEMP: Final = -2146827553
46+
CTL_E_SEARCHTEXTNOTFOUND: Final = -2146827544
47+
CTL_E_REPLACEMENTSTOOLONG: Final = -2146827542
48+
CONNECT_E_FIRST: Final = -2147220992
49+
CONNECT_E_LAST: Final = -2147220977
50+
CONNECT_S_FIRST: Final = 262656
51+
CONNECT_S_LAST: Final = 262671
52+
CONNECT_E_NOCONNECTION: Final = -2147220992
53+
CONNECT_E_ADVISELIMIT: Final = -2147220991
54+
CONNECT_E_CANNOTCONNECT: Final = -2147220990
55+
CONNECT_E_OVERRIDDEN: Final = -2147220989
56+
CLASS_E_NOTLICENSED: Final = -2147221230

0 commit comments

Comments
 (0)