Add pyodbc.pyi stub file for type annotations and intellisense code completion #864
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thesrc
directory, and included it insetup.py
as a data file so that it gets installed in the top-level pyodbc directory within thesite-packages
directory. That way, IDEs should be able to find the stub file because it will be alongside thepyodbc.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:and type annotation/checking:
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.