Type-hinted, single file modules, empty optional stubs (but for py.typed), and PEP 561

I’d like to hear everyone’s thoughts on this approach.

Last year I added type annotations to PyShp, traditionally a single file module library (shapefile.py), inline, i.e. within that file.
The current typing guidance derived from PEP 561 takes an opinionated stance against such projects:

The single-file module should be refactored into a package

To preserve the single file module structure (despite both PEP 561’s recommendation, and shapefile.py’s newly increased size), I’ve discovered that I can add an optional dependency, on a nominal library that’s simply an empty folder (shapefile-stubs), solely containing an empty py.typed marker file.

My intention is, that this enables users to opt in to using type checking in their code using the type hints in PyShp (or not) as they wish. If they also have libraries installed that shouldn’t be type checked, they are free to use mypy --ignore-missing-imports but install pyshp[stubs]. Or if they do neither, mypy will find the inline type hints.

I’ve not seen it explicitly mentioned in the spec to say the least (nor in PEP 561). Are there any problems for users of this optional dependency that I have not considered? Will type checkers other than mypy treat such empty ‘stubs’ packages (except for a py.typed file) differently?

1 Like