Pylance does not see names in __all__ attributes of stub files
#7206
Unanswered
nilslindemann
asked this question in
Q&A
Replies: 2 comments
-
|
For the records, pyright also generates a stub file """
This type stub file was generated by pyright.
"""
# ...
class IWshShortcut(comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0.IDispatch):
"""Shortcut Object"""
_case_insensitive_ = ...
_iid_ = ...
_idlflags_ = ...
if TYPE_CHECKING:
FullName = ...
Arguments = ...
Description = ...
Hotkey = ...
IconLocation = ...
RelativePath = ...
TargetPath = ...
WindowStyle = ...
WorkingDirectory = ...
def Load(self, PathLink: hints.Incomplete) -> hints.Hresult:
...
def Save(self) -> hints.Hresult:
...
# ...
__all__ = [
# ...
'IWshShortcut',
# ...
]... but the |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
And here is a concrete usage example from my code, which has that one error ( # typeshed-note:
# the _typeshed module is not available at runtime, so we check if `TYPE_CHECKING` is True.
# https://github.com/python/typeshed/blob/0b1cd5989669544866213807afa833a88f649ee7/stdlib/_typeshed/__init__.pyi#L3-L13
# comtypes-dependecy-note:
# Requires the comtypes package
# https://pypi.org/project/comtypes/
# comtypes-autogen-note:
# `CreateObject(...)` generates the module `comtypes.gen` under the hood. Therefore it has to be run before `comtypes.gen`
# gets imported. The command `py -m comtypes.clear_cache` removes such autogenerated modules.
# https://stackoverflow.com/a/3900683
# https://pypi.org/project/comtypes/
from __future__ import annotations
from typing import TYPE_CHECKING # typeshed-note
if TYPE_CHECKING:
from typing import Any, Self
from _typeshed import StrPath, Unused
from pathlib import Path as PathlibPath
class Path (PathlibPath):
def __init__(self, *args: StrPath, **kwargs: Unused):
super().__init__(*args)
# ...
_wscriptshell: Any = None
def wscriptshell(self) -> Any:
if Path._wscriptshell is None:
from comtypes.client import CreateObject # comtypes-dependecy-note
Path._wscriptshell = CreateObject("WScript.Shell") # comtypes-autogen-note
return Path._wscriptshell
def shelllink_to(self, target: Path, arguments: list[str] = []) -> Self:
shell = self.wscriptshell() # comtypes-autogen-note
from comtypes.gen import IWshRuntimeLibrary # comtypes-autogen-note
shortcut = shell.CreateShortcut(str(self)).QueryInterface(
IWshRuntimeLibrary.IWshShortcut # This is the error
)
shortcut.TargetPath = str(target)
shortcut.WorkingDirectory = str(target.parent)
shortcut.Arguments = " ".join(arguments)
shortcut.Save()
return self |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When, in VS Code, I let Pylance "Quick Fix"-generate stub files for comtypes packages, then afterwards it seems to not see names defined in the
__all__attribute of the stub file.As an example, here is the module generated by
comtypes, stored underC:\python\Lib\site-packages\comtypes\gen\IWshRuntimeLibrary.py:and here is the related stub file generated by pyright, under
C:\nils-python-lib\typings\comtypes\gen\IWshRuntimeLibrary.pyi:Notice that, for example,
IWshShortcutis contained in the imports in the comtypes generated module and in the__all__list. The pyright generated module only contains it in the__all__list but does not import it, which I guess is intended.However, Pylance then does not see that name.
IWshShortcutwill be reported asnot a known attribute of module "comtypes.gen.IWshRuntimeLibrary"in user code.I am not sure if this is a bug (or not done yet, stub generation seems to be a new thing) or if I am doing something wrong, so I report this here.
Beta Was this translation helpful? Give feedback.
All reactions