Typing Stubs for setuptools

82.0.0.20260210 · active · verified Sun Mar 29

types-setuptools is a type stub package that provides accurate type annotations for the setuptools library. It enables type checkers like MyPy and Pyright to analyze code using setuptools, ensuring type safety and catching potential errors during development. This package is part of the broader typeshed project, which centralizes high-quality type stubs for many popular Python libraries. The current version aims to provide annotations for setuptools==82.0.* and is frequently updated to reflect changes in setuptools.

Warnings

Install

Imports

Quickstart

To leverage `types-setuptools`, simply install it in your project's environment alongside `setuptools` and your chosen type checker (e.g., MyPy, Pyright). The type checker will automatically discover and apply the provided stubs to your code that uses `setuptools` APIs, allowing for static analysis and early detection of type-related issues in your packaging configuration or custom build scripts. The example demonstrates a minimal `pyproject.toml` and a Python module using `setuptools` imports, highlighting how a type checker benefits from the stubs. Note that `setup()` is rarely called directly in modern `setuptools` workflows.

import subprocess
import sys

# Create a dummy pyproject.toml for setuptools
with open('pyproject.toml', 'w') as f:
    f.write('''
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "my_package"
version = "0.1.0"
description = "A test package"
requires-python = ">=3.10"

[project.optional-dependencies]
dev = ["mypy", "types-setuptools"]
''')

# Create a dummy setup.py (optional, for demonstrating basic setup function)
with open('setup.py', 'w') as f:
    f.write('''
from setuptools import setup, find_packages

setup(
    name='my_package',
    version='0.1.0',
    packages=find_packages(where='src'),
    package_dir={'': 'src'},
)
''')

# Create a simple module for type checking
import os
os.makedirs('src', exist_ok=True)
with open('src/my_module.py', 'w') as f:
    f.write('''
from setuptools import setup # type: ignore

def build_package(name: str, version: str) -> None:
    # In a real scenario, 'setup' would be called via build tools, 
    # but here we use it to show type checking potential. 
    # 'setup' itself is not typically called directly in modern builds.
    print(f"Building {name}-{version}")

# Example of using setuptools types indirectly via a hypothetical build function
build_package("my_app", "1.0.0")
''')

# Try to run mypy (assuming it's installed in the environment)
print("\n--- Running MyPy without types-setuptools (if not in dev deps) ---")
try:
    # This assumes mypy is available. If not, this part will fail but quickstart still illustrates intent.
    subprocess.run([sys.executable, '-m', 'pip', 'install', 'mypy'], check=True, capture_output=True)
    subprocess.run([sys.executable, '-m', 'mypy', '--ignore-missing-imports', 'src/my_module.py'], check=True, capture_output=True)
    print("MyPy ran successfully (ignoring missing imports for setuptools).")
except subprocess.CalledProcessError as e:
    print(f"MyPy failed: {e.stderr.decode()}")
except FileNotFoundError:
    print("MyPy not found. Please install it with 'pip install mypy'.")

print("\n--- To fully leverage types-setuptools, ensure it's installed alongside setuptools and your type checker is configured. ---")
print("e.g. pip install setuptools types-setuptools mypy")

view raw JSON →