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

Add pyodbc.pyi stub file for type annotations and intellisense code completion #864

Merged

Conversation

keitherskine
Copy link
Collaborator

@keitherskine keitherskine commented Feb 6, 2021

This PR should address #831.

pyodbc does not currently have any type annotations for its classes and functions. Type annotations are used by mypy and other linters to provide a measure of static type analysis for up-front error checking. Also, pyodbc is a pure C++ extension module which means it's a Python module that doesn't contain any Python. This makes it difficult for IDEs to provide "intellisense" code completion when developers are writing code that uses pyodbc.

By creating a pyodbc.pyi file, I'm hoping to address both of these issues. I added the stub file to the src directory, and included it in setup.py as a data file so that it gets installed in the top-level pyodbc directory within the site-packages directory. That way, IDEs should be able to find the stub file because it will be alongside the pyodbc.py file generated during installation (this is the preferred approach described in PEP-0561). Doing this, intellisense worked for me in VS Code on Windows with the Pylance extension, without the need for me to do anything apart from pip-installing pyodbc. For example, intellisense:

image

and type annotation/checking:

image

Quick aside. Strictly speaking pyodbc is not a Python package, it's just a Python module, so the packaging approach for distribution described in PEP-0561 does not apply here. From PEP-0561 "This PEP does not support distributing typing information as part of module-only distributions.", hence the use of the "data_files" construct in setup.py.

Lastly, whilst in this neck o' the woods, I corrected the name of the "paramstyle" attribute in the pyodbc module doc description.

@keitherskine
Copy link
Collaborator Author

Travis has errored out on one of the jobs with a "502 Bad Gateway" when pulling a docker image from Microsoft. @mkleehammer , if you're able to simply restart that one job, I'm pretty sure if will succeed second time round.

@keitherskine
Copy link
Collaborator Author

@mkleehammer This PR is ready for review.

@alherm7
Copy link

alherm7 commented Aug 11, 2022

I had an issue with the location of the .pyi file. When I installed pyodbc in my virtual environment the .pyi got placed into the top level folder of the virtual environment, i.e. "./.venv/" . Intellisense in VS Code was not able to find this location by default, so I had to manually move it into "./.venv/lib/python3.9/site-packages/" folder. This has also been described by others in issue #925 . Will this issue be fixed in future releases?

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

Successfully merging this pull request may close these issues.

3 participants