Typing Stubs for setuptools
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
- breaking The `pkg_resources` module's type stubs are no longer included in `types-setuptools` for `setuptools` versions 71.1 and newer. This is because `setuptools` itself now ships with inline type annotations for `pkg_resources`. If you are using an older version of `setuptools` (<71.1) and require types for `pkg_resources`, `types-setuptools` will not provide them.
- gotcha Recent `setuptools` versions (e.g., 78.0.1 and later) introduced strict enforcement of naming conventions in `setup.cfg` files, disallowing dash-separated keys (e.g., `description-file`) in favor of underscore-separated ones (e.g., `description_file`). While this is a change in `setuptools` itself, `types-setuptools` reflects the current API. Using outdated `setup.cfg` syntax with newer `setuptools` and `types-setuptools` can lead to type checker errors or build failures.
- deprecated The `setup.py develop` command and other direct `setup.py` invocations are deprecated in `setuptools` and will be removed. While `types-setuptools` provides stubs for these legacy APIs for compatibility, relying on them is discouraged. Type checkers may flag usage of these deprecated features based on the provided stubs.
Install
-
pip install types-setuptools -
pip install 'setuptools<71.1' types-setuptools # For older setuptools versions needing pkg_resources types
Imports
- setup
from setuptools import setup
- find_packages
from setuptools import find_packages
Quickstart
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")