Automatically generate Python Typing stub files for Cython extensions (_p4p, _gw)#212
Automatically generate Python Typing stub files for Cython extensions (_p4p, _gw)#212Monarda wants to merge 30 commits into
Conversation
…nd include in wheel files
…yping_stubs_automate
| import sys | ||
| import platform | ||
| if sys.platform=='linux2' and not sysconfig.get_config_var('Py_DEBUG'): | ||
| if sys.platform.startswith('linux') and not sysconfig.get_config_var('Py_DEBUG'): |
There was a problem hiding this comment.
This appears to fix a small bug in the existing versions of setup.py.
sys.platform will return linux on Python3 so this will not trigger. sys.platform.startswith('linux') makes it compatible with Python 2.7 and 3.
| cxxflags += ['-g0'] | ||
|
|
||
| elif platform.system()=='Darwin': | ||
| elif sys.platform == 'darwin': |
There was a problem hiding this comment.
Made consistent with other usages. Removes the need to import platform.
| cothread setuptools_scm | ||
| if python -c "import sys; sys.exit(0 if sys.version_info >= (3, 8) else 1)"; then |
There was a problem hiding this comment.
pip understands conditionals already, no need for special machinery.
"mypy; python_version>='3.8'"
There was a problem hiding this comment.
Perhaps I'm misunderstanding the syntax here, but isn't this part of build.yml not using the information in pyproject.toml?
Experimenting shows that
pip download $PRE -d output \
--only-binary numpy,Cython \
"${{ matrix.cy || 'Cython' }}" \
"${{ matrix.numpy || 'numpy' }}" \
setuptools_dso pvxslibs wheel ply nose2 \
cothread setuptools_scm mypyproduces a GitHub CI/CD error warning of ERROR: You need Python 3.5 or later to use mypy.
I could replace this with pip download . but that would presumably interfere with the matrix?
There was a problem hiding this comment.
pip download ... accepts the same syntax as would be found in pyproject.toml.
pip download $PRE -d output \
--only-binary numpy,Cython \
"${{ matrix.cy || 'Cython' }}" \
"${{ matrix.numpy || 'numpy' }}" \
setuptools_dso pvxslibs wheel ply nose2 \
cothread setuptools_scm \
"mypy; python_version>='3.8'"There was a problem hiding this comment.
D'oh! I understand; I've made the change now.
| stubgen = shutil.which('stubgen', path=sysconfig.get_path('scripts')) \ | ||
| or shutil.which('stubgen') |
There was a problem hiding this comment.
Does stubgen have an import-able main()? Like pip ... vs. python -m pip .... Enables use of sys.executable as an entry point, which avoids possible confusion with $PATH referencing other environments.
There was a problem hiding this comment.
Good catch, I've switched to using sys.executable as you suggested which means _find_stubgen() is gone. I also experimented with calling stubgen.main() directly, but it wouldn't be any simpler on Windows due to its using multiprocessing.
|
|
||
| # Fully-qualified module names for which .pyi stubs are generated. | ||
| # Must match the Extension name= values below. | ||
| _STUB_MODULES = ['p4p._p4p', 'p4p._gw'] |
There was a problem hiding this comment.
I wasn't sure whether to use a glob here (*.pyi) or specify the files explicitly. I've taken the more conservative approach of being explicit.
This reverts commit d46fa98.
This is a continuation of PR #209, the changes are significant enough that I have made them on a new branch which has necessitated a new PR. Apologies for the churn!
The changes outside of
setup.pyare relatively minor and some have already been noted in #209.mypyhas now been added to the CI/CDbuild.ymlandpyproject.ymlfor Python versions >=3.8.stubgenappears to have been relatively stable since Python 3.9.There are more significant changes to
setup.pywhich has hadGenerateStubsadded. This function is automatically called when a wheel is built through subclassingBuildExt. Lines 314 & 315 are where the chain of logic therefore begins. It is also possible to manually generate stub files by running the commandpython setup.py generate_stubs.GenerateStubs is relatively straightforward, made more complex by needing to be cross-platform compatible and coping with the wheel build environment. It:
.pyifilesAny errors above do not terminate setup but instead print warnings to stderr. I'm happy to change this if you prefer.
Note, this code was generated with Claude and then manually checked and improved. I thought it was important to include this disclaimer.