How to package mypy stub files only using setup.cfg

Hi,

I have been trying unsuccessfully for a few days to package a stub project. The project is pyside2-stubs and the idea is to provide mypy stubs only for PySide2 also known as “Qt5 for Python”.

My project has the following layout

setup.cfg
pyproject.toml
README.md
LICENSE
version.py
update_latest_pyside2.py
tests/
    qobject.py
    ...  (several other python test files)
pyside2-stubs/
    __init__.pyi
    QtCore.pyi
   ... (more .pyi files)

My goal is to distribute a package containing only the *.pyi files plus the usual README and LICENSE.

Content of pyproject.toml :

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"

Content of setup.cfg :

[metadata]
name = PySide2-stubs
author = Philippe Fremy
author_email = phil.fremy@free.fr
description = PEP561 stub files for the *PySide2/Qt5 for Python* framework
long_description = file: README.md
long_description_content_type = text/markdown
version = attr: version.TOTO
license = LGPL v2.1
url = https://github.com/python-qt-tools/PySide2-stubs
project_urls =
    Bug Tracker = https://github.com/bluebird75/pyside2-stubs/issues
classifiers =
    Development Status :: 5 - Production/Stable
    Programming Language :: Python :: 3
    License :: OSI Approved :: GNU Lesser General Public License v2 (LGPLv2)
    Operating System :: OS Independent
    Intended Audience :: Developers
    Typing :: Stubs Only

[options]
package_dir =
    = .
python_requires = >=3.6

[options.exclude_package_data]
. = *.py

[options.package_data]
* = *.pyi

My main problem is that the generated wheel contains all the python files located in this directory whereas I don’t want to ship any of them.

I have been looking for documentation and examples, but the use case of a package without any python files is not so common. And the setup.cfg documentation has been unsufficient for me. I would really appreciate any guidance.

The full project is on GitHub, ready to be exercised : GitHub - python-qt-tools/PySide2-stubs: Mypy stubs for pyside2 / Qt5 for Python

In the [options] section, specify

packages = spam-stubs

You can alse remove the [options.exclude_package_data] section, as I don’t think Python modules can be considered package-data

Alternatively, you could put your stubs package pyside2-stubs/ in a src/ directory and specify package_dir = = src (ie use the source layout)

It was right under my eyes, I don’t know how I managed to miss it. Or I know, that’s because the source distribution embarks all the python files and I missed the fact that in the wheel, they are not included.

So, solution was simply to add one more section to my setup.cfg :


[options.packages.find]
exclude =
    tests*
    version.py
    update_latest_pyside2.py

That uses the automatic discovery (ie packages = find:)

Thanks for the quick and useful feedback.

I’ll keep the current layout because there are 4 similar stub projects with the same layout. I just wanted to migrate from setup.py to the recommended setup.cfg .

I am supposed to be a seasoned Python developer but each time I touch packaging, I feel like I am back to school, the classes where nothing of what the teachers says makes sense…