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

1

11

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?

Censor answered 21/4, 2023 at 13:25 Comment(1)
Show a minimal reproducible example, then we can help you fix it. -- Basically adding an entry in MANIFEST.in and enabling include_package_data should be enough.Zoubek
U
11

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

Ulla answered 10/5, 2023 at 1:48 Comment(1)
Thank Vanroy and Hostetter that someone documented this.Concretion

© 2022 - 2024 — McMap. All rights reserved.