Typing stubs for pywin32

311.0.0.20260408 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates typical usage of `pywin32` and how `types-pywin32` silently provides type annotations for static analysis tools. You do not import `types-pywin32` directly; rather, its presence enables type checkers to understand the `pywin32` API.

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.")

view raw JSON →