Skip to content

Commit 3e3977d

Browse files
authored
Add type annotations to shelllink. (#611)
* Remove wildcard imports in `shelllink`. * Add type annotations to the high-level (overridden) functions. * Add `Literal` to `hints.pyi` * Add `TYPE_CHECKING` methods.
1 parent 4ab97c2 commit 3e3977d

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

comtypes/hints.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ from typing import Callable, Iterator, Sequence
1010

1111
if sys.version_info >= (3, 8):
1212
from typing import Protocol
13+
from typing import Literal as Literal
1314
else:
1415
from typing_extensions import Protocol
16+
from typing_extensions import Literal as Literal
1517
if sys.version_info >= (3, 9):
1618
from typing import Annotated as Annotated
1719
else:

comtypes/shelllink.py

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
from __future__ import print_function
2-
from ctypes import *
2+
from ctypes import c_char_p, c_int, c_short, c_wchar_p
3+
from ctypes import POINTER
4+
from ctypes import byref, create_string_buffer, create_unicode_buffer
35
from ctypes.wintypes import DWORD, WIN32_FIND_DATAA, WIN32_FIND_DATAW, MAX_PATH
6+
from typing import Tuple, TYPE_CHECKING
7+
48
from comtypes import IUnknown, GUID, COMMETHOD, HRESULT, CoClass
59

10+
11+
if TYPE_CHECKING:
12+
from comtypes import hints # type: ignore
13+
14+
615
# for GetPath
716
SLGP_SHORTPATH = 0x1
817
SLGP_UNCPRIORITY = 0x2
@@ -113,31 +122,52 @@ class IShellLinkA(IUnknown):
113122
COMMETHOD([], HRESULT, "SetPath", (["in"], c_char_p, "pszFile")),
114123
]
115124

116-
def GetPath(self, flags=SLGP_SHORTPATH):
125+
if TYPE_CHECKING:
126+
# fmt: off
127+
def GetIDList(self) -> hints.Incomplete: ... # noqa
128+
def SetIDList(self, pidl: hints.Incomplete) -> hints.Incomplete: ... # noqa
129+
def SetDescription(self, pszName: bytes) -> hints.Incomplete: ... # noqa
130+
def SetWorkingDirectory(self, pszDir: bytes) -> hints.Hresult: ... # noqa
131+
def SetArguments(self, pszArgs: bytes) -> hints.Hresult: ... # noqa
132+
@property
133+
def Hotkey(self) -> int: ... # noqa
134+
@Hotkey.setter
135+
def Hotkey(self, pwHotkey: int) -> None: ... # noqa
136+
@property
137+
def ShowCmd(self) -> int: ... # noqa
138+
@ShowCmd.setter
139+
def ShowCmd(self, piShowCmd: int) -> None: ... # noqa
140+
def SetIconLocation(self, pszIconPath: bytes, iIcon: int) -> hints.Hresult: ... # noqa
141+
def SetRelativePath(self, pszPathRel: bytes, dwReserved: hints.Literal[0]) -> hints.Hresult: ... # noqa
142+
def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... # noqa
143+
def SetPath(self, pszFile: bytes) -> hints.Hresult: ... # noqa
144+
# fmt: on
145+
146+
def GetPath(self, flags: int = SLGP_SHORTPATH) -> bytes:
117147
buf = create_string_buffer(MAX_PATH)
118148
# We're not interested in WIN32_FIND_DATA
119-
self.__com_GetPath(buf, MAX_PATH, None, flags)
149+
self.__com_GetPath(buf, MAX_PATH, None, flags) # type: ignore
120150
return buf.value
121151

122-
def GetDescription(self):
152+
def GetDescription(self) -> bytes:
123153
buf = create_string_buffer(1024)
124-
self.__com_GetDescription(buf, 1024)
154+
self.__com_GetDescription(buf, 1024) # type: ignore
125155
return buf.value
126156

127-
def GetWorkingDirectory(self):
157+
def GetWorkingDirectory(self) -> bytes:
128158
buf = create_string_buffer(MAX_PATH)
129-
self.__com_GetWorkingDirectory(buf, MAX_PATH)
159+
self.__com_GetWorkingDirectory(buf, MAX_PATH) # type: ignore
130160
return buf.value
131161

132-
def GetArguments(self):
162+
def GetArguments(self) -> bytes:
133163
buf = create_string_buffer(1024)
134-
self.__com_GetArguments(buf, 1024)
164+
self.__com_GetArguments(buf, 1024) # type: ignore
135165
return buf.value
136166

137-
def GetIconLocation(self):
167+
def GetIconLocation(self) -> Tuple[bytes, int]:
138168
iIcon = c_int()
139169
buf = create_string_buffer(MAX_PATH)
140-
self.__com_GetIconLocation(buf, MAX_PATH, byref(iIcon))
170+
self.__com_GetIconLocation(buf, MAX_PATH, byref(iIcon)) # type: ignore
141171
return buf.value, iIcon.value
142172

143173

@@ -226,31 +256,52 @@ class IShellLinkW(IUnknown):
226256
COMMETHOD([], HRESULT, "SetPath", (["in"], c_wchar_p, "pszFile")),
227257
]
228258

229-
def GetPath(self, flags=SLGP_SHORTPATH):
259+
if TYPE_CHECKING:
260+
# fmt: off
261+
def GetIDList(self) -> hints.Incomplete: ... # noqa
262+
def SetIDList(self, pidl: hints.Incomplete) -> hints.Incomplete: ... # noqa
263+
def SetDescription(self, pszName: str) -> hints.Incomplete: ... # noqa
264+
def SetWorkingDirectory(self, pszDir: str) -> hints.Hresult: ... # noqa
265+
def SetArguments(self, pszArgs: str) -> hints.Hresult: ... # noqa
266+
@property
267+
def Hotkey(self) -> int: ... # noqa
268+
@Hotkey.setter
269+
def Hotkey(self, pwHotkey: int) -> None: ... # noqa
270+
@property
271+
def ShowCmd(self) -> int: ... # noqa
272+
@ShowCmd.setter
273+
def ShowCmd(self, piShowCmd: int) -> None: ... # noqa
274+
def SetIconLocation(self, pszIconPath: str, iIcon: int) -> hints.Hresult: ... # noqa
275+
def SetRelativePath(self, pszPathRel: str, dwReserved: hints.Literal[0]) -> hints.Hresult: ... # noqa
276+
def Resolve(self, hwnd: int, fFlags: int) -> hints.Hresult: ... # noqa
277+
def SetPath(self, pszFile: str) -> hints.Hresult: ... # noqa
278+
# fmt: on
279+
280+
def GetPath(self, flags: int = SLGP_SHORTPATH) -> str:
230281
buf = create_unicode_buffer(MAX_PATH)
231282
# We're not interested in WIN32_FIND_DATA
232-
self.__com_GetPath(buf, MAX_PATH, None, flags)
283+
self.__com_GetPath(buf, MAX_PATH, None, flags) # type: ignore
233284
return buf.value
234285

235-
def GetDescription(self):
286+
def GetDescription(self) -> str:
236287
buf = create_unicode_buffer(1024)
237-
self.__com_GetDescription(buf, 1024)
288+
self.__com_GetDescription(buf, 1024) # type: ignore
238289
return buf.value
239290

240-
def GetWorkingDirectory(self):
291+
def GetWorkingDirectory(self) -> str:
241292
buf = create_unicode_buffer(MAX_PATH)
242-
self.__com_GetWorkingDirectory(buf, MAX_PATH)
293+
self.__com_GetWorkingDirectory(buf, MAX_PATH) # type: ignore
243294
return buf.value
244295

245-
def GetArguments(self):
296+
def GetArguments(self) -> str:
246297
buf = create_unicode_buffer(1024)
247-
self.__com_GetArguments(buf, 1024)
298+
self.__com_GetArguments(buf, 1024) # type: ignore
248299
return buf.value
249300

250-
def GetIconLocation(self):
301+
def GetIconLocation(self) -> Tuple[str, int]:
251302
iIcon = c_int()
252303
buf = create_unicode_buffer(MAX_PATH)
253-
self.__com_GetIconLocation(buf, MAX_PATH, byref(iIcon))
304+
self.__com_GetIconLocation(buf, MAX_PATH, byref(iIcon)) # type: ignore
254305
return buf.value, iIcon.value
255306

256307

0 commit comments

Comments
 (0)