Typing stubs for pywin32
types-pywin32 is a type stub package providing static type annotations for the pywin32 library. It allows type checkers like Mypy or Pyright to analyze code that uses pywin32, enhancing code quality and developer experience by catching type-related errors before runtime. This version aims to provide accurate annotations for pywin32==311.*. As part of the typeshed project, it receives frequent, often daily, updates to ensure currency with underlying libraries and Python typing standards.
Warnings
- breaking Type stubs are versioned to align with specific `pywin32` major versions. If `pywin32` undergoes a major API change, `types-pywin32` will update to reflect the new API, potentially causing type-checking errors for code using older `pywin32` versions.
- gotcha The underlying `pywin32` library often requires a post-install script (`python -m pywin32_postinstall -install`) to register COM components and place DLLs. Failure to run this, especially in virtual environments or when running as a Windows Service, can lead to runtime errors even if `types-pywin32` is correctly installed.
- gotcha Some `pywin32` functions may exhibit incorrect parameter resolution or `reportCallIssue` warnings in type checkers (e.g., Pylance) due to stubs defining parameters as positional-only when they are commonly used as keyword arguments, or other discrepancies in stub definitions.
- gotcha When bundling applications with tools like PyInstaller, users may encounter `win32ctypes.pywin32.pywintypes.error`. This typically indicates issues with the `pywin32` installation, version incompatibilities with PyInstaller, or incorrect PATH configurations for its DLLs.
- deprecated Users might encounter `DeprecationWarning: getargs: The 'u' format is deprecated. Use 'U' instead` originating from `pywintypes.py` within `pywin32`. This indicates internal usage of an older Python C API format for Unicode, which has a newer, preferred alternative.
Install
-
pip install types-pywin32
Imports
- win32api
import win32api
- win32com.client
import win32com.client
- pywintypes
import pywintypes
Quickstart
import win32api
import win32con
from typing import TYPE_CHECKING
import os
# types-pywin32 provides type information for pywin32 modules like win32api.
# It is not imported directly into your runtime code.
# Its benefits are seen when running a static type checker (e.g., mypy, pyright).
# Example 1: Getting a Windows system directory with type hint
windows_directory: str = win32api.GetWindowsDirectory()
print(f"Windows Directory: {windows_directory}")
# Example 2: Using a constant from win32con and a function from win32api
# The actual MessageBox call is commented out to prevent interactive prompts
# during automated tests or in non-interactive environments.
if TYPE_CHECKING:
# Type checkers will use the stubs for win32api.MessageBox here.
# In a real application, you'd ensure the function call arguments match
# the stub definitions provided by types-pywin32.
pass
else:
try:
# Example of a win32api call (commented out for quickstart execution)
# win32api.MessageBox(0, "Hello from pywin32!", "Pywin32 Example", win32con.MB_OK)
print("MessageBox call skipped in quickstart to avoid UI interaction.")
except Exception as e:
print(f"Could not display message box (expected if not on Windows or interactive): {e}")
print("Static type checking for pywin32 is enhanced by types-pywin32.")