Search code examples
pythonsetuptoolspyproject.tomltyped

Add py.typed as package data with setuptools in pyproject.toml


From what I read, to make sure that the typing information of your code is distributed alongside your code for linters to read, the py.typed file should be part of your distribution.

I find answers for how to add these to setup.py but it is not clear to me 1. whether it should be included in pyproject.toml (using setuptools), 2. if so, how it should be added.

Scouring their github repository, it seems that this is not added automatically so the question remains how I should add it to my pyproject.toml. I found this general discussion about package_data but it includes reference to include_package_data and a MANIFEST.in and it gets confusing from there what should go where.

Tl;dr: how should I include py.typed in pyproject.toml when using setuptools?


Solution

  • Yes, you should add py.typed to your package's source folder (same level as __init__.py). This informs type checkers, like mypy, that your package supports typing. See PEP 561.

    Here is an example of what is needed in pyproject.toml. Replace pkgname with the name of your package.

    [tool.setuptools.package-data]
    "pkgname" = ["py.typed"]
    

    It is worth noting package discovery using:

    [tool.setuptools.packages.find]
    where = ["src"]
    

    seems to be required, alas package-data wouldn't have any effect, as explained in setuptools docs: https://setuptools.pypa.io/en/latest/userguide/datafiles.html#package-data