Skip to content

Let checkers and IDEs know that pywintypes and pythoncom are actually dynamic modules #2418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions com/pythoncom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Magic utility that "redirects" to pythoncomXX.dll
from typing import TYPE_CHECKING, Any

import pywintypes

pywintypes.__import_pywin32_system_module__("pythoncom", globals())

# This module dynamically re-exports from a C-Extension.
# Prevent attribute access issues with checkers and language servers (IDEs)
# External usage should still prefer typeshed stubs
if TYPE_CHECKING:

def __getattr__(name: str) -> Any: ...
2 changes: 1 addition & 1 deletion com/win32com/server/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# Note that we derive from com_error, which derives from builtin Exception
# Also note that we don't support "self.args", as we don't support tuple-unpacking
class COMException(pythoncom.com_error): # type: ignore[name-defined] # Dynamic module
class COMException(pythoncom.com_error): # type: ignore[misc] # Dynamic module
"""An Exception object that is understood by the framework.

If the framework is presented with an exception of type class,
Expand Down
7 changes: 2 additions & 5 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"enableTypeIgnoreComments": true,
// Exclude from scanning when running pyright
"exclude": [
".git/",
".git/", // Avoids scanning git branch names ending in ".py"
"build/",
// Vendored
"Pythonwin/Scintilla/",
Expand All @@ -32,15 +32,12 @@
"reportArgumentType": "none",
"reportAttributeAccessIssue": "none",
// FIXE: These all need to be fixed first and turned back to error
// some of the fixes need to be done in types-pywin32 from typeshed
"reportCallIssue": "warning",
"reportOperatorIssue": "warning",
"reportOptionalCall": "warning",
"reportOptionalIterable": "warning",
"reportOptionalMemberAccess": "warning",
"reportOptionalSubscript": "warning",
// Needs fixes in types-pywin32 and requires Python 3.8 to annotate ambiguous global variables
"reportUnnecessaryComparison": "warning",
// Too many dynamically generated modules. This will require type stubs to properly fix.
"reportMissingImports": "warning",
// IDEM, but happens when pywin32 is not in site-packages but module is found from typeshed.
Expand All @@ -50,6 +47,6 @@
"reportMissingModuleSource": "none",
// External type stubs may not be completable, and this will require type stubs for dynamic modules.
"reportMissingTypeStubs": "information",
// Use Flake8/Pycln/Ruff instead
// Use Pycln/Ruff instead
"reportUnusedImport": "none",
}
8 changes: 8 additions & 0 deletions win32/Lib/pywintypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib.util
import os
import sys
from typing import TYPE_CHECKING, Any


def __import_pywin32_system_module__(modname, globs):
Expand Down Expand Up @@ -122,3 +123,10 @@ def __import_pywin32_system_module__(modname, globs):


__import_pywin32_system_module__("pywintypes", globals())

# This module dynamically re-exports from a C-Extension.
# Prevent attribute access issues with checkers and language servers (IDEs)
# External usage should still prefer typeshed stubs
if TYPE_CHECKING:

def __getattr__(name: str) -> Any: ...