Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stub-package for python modules or extensions? #1

Closed
szabolcsdombi opened this issue Nov 3, 2022 · 4 comments
Closed

stub-package for python modules or extensions? #1

szabolcsdombi opened this issue Nov 3, 2022 · 4 comments

Comments

@szabolcsdombi
Copy link

Hi, thank you very much for providing examples for PEP 561

Just for curiosity I have installed this library with

pip install https://github.com/ethanhs/stub-package/archive/refs/heads/master.zip

and found that my VSCode can see the typehints:

image

despite there is no typedpkg installed.

I am not interested in type hinting packages. I usually make Python extensions or modules with type hints in .pyi stub files.
in this context:

  • packages are folders of python code with or without an __init__.py.
  • modules are modulename.py files.
  • extensions are mymodule.___.pyd or mymodule.___.so files.

This repository is a great example to add stub files for packages, but would it be correct to use it for modules or extensions?

I currently have:

setup(
    name='mymodule',
    ext_modules=[Extension(name='mymodule', ...)],
    data_files=[('.', ['mymodule.pyi'])],
)

This is obviously not correct as '.' is not a package, but it seemed to work and I used it here.

Moving mymodule.pyi to mymodule-stubs/__init__.pyi and changing the setup code to:

setup(
    name='mymodule',
    ext_modules=[Extension(name='mymodule', ...)],
    package_data={'mymodule-stubs': ['__init__.pyi']},
    packages=['mymodule-stubs'],
)

This might be also inappropiate as mymodule is an extension not a package.

@ethanhs
Copy link
Owner

ethanhs commented Nov 3, 2022

Hi, PEP 561 doesn't support single module packages, see https://peps.python.org/pep-0561/#packaging-type-information

I would recommend putting a folder of the same name of the module that includes either an __init__.pyi or making a separate -stubs package if you are worried about the folder conflicting with the module. Hopefully that makes sense?

@szabolcsdombi
Copy link
Author

This PEP does not support distributing typing information as part of module-only distributions or single-file modules within namespace packages.
The single-file module should be refactored into a package and indicate that the package supports typing as described above.

Indeed I missed this information.


I would recommend putting a folder of the same name of the module that includes either an init.pyi or making a separate -stubs package if you are worried about the folder conflicting with the module. Hopefully that makes sense?

Makes sense. The package folder with -stubs ending sounds safer.

This works so far for an extension only distribution.

One thing that still bothers me is the naming. According to pypa/distutils#108 (comment) the folder name shoud contain an underscore while the packages and package_data entries should contain dash.

Is this correct?

@ethanhs
Copy link
Owner

ethanhs commented Nov 3, 2022

Henry is incorrect there, see for example mypy's test data for stub packages: https://github.com/python/mypy/tree/master/test-data/packages/typedpkg-stubs

The point of the -stubs prefix is that it is meant only for type-checkers, it shouldn't be importable.

@szabolcsdombi
Copy link
Author

Thank you for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants