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

How to "Create Type Stub" for something that is not a library #355

Closed
psam44 opened this issue Sep 10, 2020 · 2 comments
Closed

How to "Create Type Stub" for something that is not a library #355

psam44 opened this issue Sep 10, 2020 · 2 comments

Comments

@psam44
Copy link

psam44 commented Sep 10, 2020

I want to create stub files for my project, not for an existing library (described in https://github.com/microsoft/pyright/blob/master/docs/type-stubs.md). Part of the project is aimed to be published as a library, eventually, with these typings.

The project is designed for Django. The simplified layout looks like:

root
|_ .env
|_ manage.py
|_ .vscode\
|_ typings\
|_ mysite\
   |_ __init__.py
   |_ settings.py
   |_ myapp\
      |_ __init__.py
      |_ models.py

The goal is to create stubs for myapp.
The manage.py is used to temporary host the import instruction, but any other py file would be fine.
import mysite.myapp doesn't raise an error.

import myapp raises an expected error: Import "myapp" could not be resolved
So, we need to make the module visible. How?

  • I tried the setting of PYTHONPATH in .env, but it doesn't help (seems to have no effect for the purpose)
  • I tried to set Python › Analysis: Extra Paths. It works too much, in the sense that the module could be found and there is no "Stub file not found for ..." error.

My solution is to add a python path file: D:\Python37\Lib\site-packages\tmp.pth, containing D:/root/mysite.
After a "Restart Language Server", I finally have the wanted error:
Stub file not found for "myapp" Pylance (reportMissingTypeStubs)
with the Quick Fix: Create Type Stub For "myapp".

The question is: is there a smoother way to generate the typings?

@erictraut
Copy link
Contributor

The "create stub" feature is intended for installed packages. If you plan to make your package into an installable package, you could do an editable install into your current Python environment. Then Pylance will be able to create stubs from it.

However, if you plan to ship your library as Python source code, I recommend simply annotating your Python code and including a "py.typed" file (as described in PEP 561) in your package. That would save you the effort of creating and maintaining a separate set of type stubs.

@huguesv huguesv closed this as completed Sep 10, 2020
@psam44
Copy link
Author

psam44 commented Sep 10, 2020

Thanks Eric for your quick response,

I give the preference to stub files over inline types. Even Pyright, in type-stubs.md, says "it’s highly recommended that you use type stub files for all external imports.".
About an installable package (pip install --editable, I suppose) I'm not used to this option, but it think it does the same as my mentioned tmp.path, with the regular easy-install.pth.
I wanted to be sure to not miss something simpler, so thanks again.

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

3 participants