CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-qtpy

Provides an abstraction layer on top of the various Qt bindings (PyQt5/6 and PySide2/6).

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

development-tools.mddocs/

Development Tools

Command-line interface and utilities for IDE integration, type checking configuration, and development workflow support with mypy and Pyright. These tools help developers configure their development environment to work effectively with QtPy's binding abstraction.

Capabilities

Version Information

Access to QtPy version information for development and debugging.

def print_version() -> None:
    """Print the current version of QtPy package to stdout."""

Usage example:

from qtpy.cli import print_version

print_version()  # Output: QtPy version 2.4.3

API Status Functions

Functions to determine which Qt binding is currently active and available.

def get_api_status() -> dict[str, bool]:
    """
    Get the status of each Qt API usage.
    
    Returns:
        dict[str, bool]: Mapping of API names to boolean indicating if active
                        Keys: 'pyqt5', 'pyside2', 'pyqt6', 'pyside6' 
                        Values: True if currently active, False otherwise
    """

Usage example:

from qtpy.cli import get_api_status

status = get_api_status()
print(status)  # {'pyqt5': True, 'pyside2': False, 'pyqt6': False, 'pyside6': False}

# Check which binding is active
active_binding = [name for name, active in status.items() if active][0]
print(f"Currently using: {active_binding}")

MyPy Integration

Generate command-line arguments for using mypy with QtPy to enable proper type checking.

def generate_mypy_args() -> str:
    """
    Generate mypy command line arguments for QtPy.
    
    Generates strings like:
    "--always-false=PYQT5 --always-false=PYQT6 --always-true=PYSIDE2 --always-false=PYSIDE6"
    
    This helps guide mypy through which library QtPy would have used
    so that mypy can get the proper underlying type hints.
    
    Returns:
        str: Space-separated mypy arguments for current binding
    """

def print_mypy_args() -> None:
    """Print generated mypy args to stdout."""

Usage example:

from qtpy.cli import generate_mypy_args, print_mypy_args

# Get mypy args as string
args = generate_mypy_args()
print(args)  # "--always-false=PYQT5 --always-false=PYQT6 --always-true=PYSIDE2 --always-false=PYSIDE6"

# Print directly
print_mypy_args()

Command line usage:

# Use in shell command
mypy --package mypackage $(qtpy mypy-args)

# Or get the args directly
qtpy mypy-args

Pyright Configuration

Generate Pyright configuration for type checking with QtPy in various formats.

def generate_pyright_config_json() -> str:
    """
    Generate Pyright config JSON for pyrightconfig.json.
    
    Returns:
        str: JSON string with defineConstant configuration
    """

def generate_pyright_config_toml() -> str:
    """
    Generate Pyright config TOML for pyproject.toml.
    
    Returns:
        str: TOML format configuration section
    """

def print_pyright_config_json() -> None:
    """Print generated Pyright JSON config to stdout."""

def print_pyright_config_toml() -> None:
    """Print generated Pyright TOML config to stdout."""

def print_pyright_configs() -> None:
    """Print both Pyright config formats to stdout."""

Usage example:

from qtpy.cli import (
    generate_pyright_config_json, 
    generate_pyright_config_toml,
    print_pyright_configs
)

# Generate JSON config
json_config = generate_pyright_config_json()
print(json_config)
# Output: {"defineConstant": {"PYQT5": false, "PYSIDE2": true, "PYQT6": false, "PYSIDE6": false}}

# Generate TOML config  
toml_config = generate_pyright_config_toml()
print(toml_config)
# Output:
# [tool.pyright.defineConstant]
# PYQT5 = false
# PYSIDE2 = true
# PYQT6 = false
# PYSIDE6 = false

# Print both formats
print_pyright_configs()

Command line usage:

# Generate Pyright configuration
qtpy pyright-config

CLI Parser Functions

Functions for creating and managing the command-line interface.

def generate_arg_parser():
    """
    Generate the argument parser for the dev CLI for QtPy.
    
    Returns:
        argparse.ArgumentParser: Configured argument parser with subcommands
                                for mypy-args and pyright-config
    """

def main(args: list[str] | None = None) -> None:
    """
    Run the development CLI for QtPy.
    Entry point for the console script.
    
    Args:
        args: Command line arguments, uses sys.argv if None
    """

Usage example:

from qtpy.cli import generate_arg_parser, main

# Create argument parser programmatically
parser = generate_arg_parser()
parsed_args = parser.parse_args(['mypy-args'])

# Run CLI programmatically
main(['--version'])  # Prints version
main(['mypy-args'])  # Prints mypy arguments
main(['pyright-config'])  # Prints Pyright configuration

Command Line Interface

QtPy provides a command-line interface accessible via the qtpy command or python -m qtpy.

Available Commands

# Show version
qtpy --version

# Generate mypy arguments
qtpy mypy-args

# Generate Pyright configuration
qtpy pyright-config

# Show help
qtpy --help

Usage Examples

MyPy Integration:

# Use QtPy with mypy for type checking
mypy --package myproject $(qtpy mypy-args)

# This expands to something like:
# mypy --package myproject --always-false=PYQT5 --always-true=PYSIDE2 --always-false=PYQT6 --always-false=PYSIDE6

Pyright Configuration:

# Generate pyrightconfig.json content
qtpy pyright-config > pyrightconfig.json

# Or manually copy the configuration:
qtpy pyright-config

The generated configuration helps IDEs and type checkers understand which Qt binding QtPy is using, enabling proper type hints and autocompletion.

Integration with Development Workflow

IDE Setup

  1. MyPy Integration: Use qtpy mypy-args to configure mypy for proper type checking
  2. Pyright/Pylance: Use qtpy pyright-config to configure Pyright for VS Code
  3. Type Hints: The CLI ensures IDEs understand which Qt binding is active

Continuous Integration

# Example GitHub Actions workflow
- name: Type check with mypy
  run: mypy --package myproject $(qtpy mypy-args)

Development Environment Detection

# Programmatically check binding in development scripts
from qtpy.cli import get_api_status

def setup_dev_environment():
    status = get_api_status()
    active_binding = next(name for name, active in status.items() if active)
    print(f"Setting up development environment for {active_binding}")
    
    # Configure tools based on active binding
    if active_binding in ['pyqt5', 'pyqt6']:
        setup_pyqt_tools()
    else:  # pyside2, pyside6
        setup_pyside_tools()

Install with Tessl CLI

npx tessl i tessl/pypi-qtpy

docs

compatibility.md

core-package.md

development-tools.md

index.md

qt-modules.md

uic-module.md

tile.json