Let's say I have very simple package with a following structure:
.
├── foo
│ ├── bar
│ │ └── __init__.py
│ └── __init__.py
└── setup.py
Content of the files:
setup.py
:
from distutils.core import setup
setup(
name='foobar',
version='',
packages=['foo', 'foo.bar'],
url='',
license='Apache License 2.0',
author='foobar',
author_email='',
description=''
)
foo/bar/__init__.py
:
def foobar(x):
return x
The remaining files are empty.
I install the package using pip
:
cd foobar
pip install .
and can confirm it is installed correctly.
Now I want to create a separate package with stub files:
.
├── foo
│ ├── bar
│ │ └── __init__.pyi
│ └── __init__.pyi
└── setup.py
Content of the files:
setup.py
:
from distutils.core import setup
import sys
import pathlib
setup(
name='foobar_annot',
version='',
packages=['foo', 'foo.bar'],
url='',
license='Apache License 2.0',
author='foobar',
author_email='',
description='',
data_files=[
(
'shared/typehints/python{}.{}/foo/bar'.format(*sys.version_info[:2]),
["foo/bar/__init__.pyi"]
),
],
)
foo.bar.__init__.pyi
:
def foobar(x: int) -> int: ...
I can install this package, see that it creates anaconda3/shared/typehints/python3.5/foo/bar/__init__.pyi
in my Anaconda root, but it doesn't look like it is recognized by PyCharm (I get no warnings). When I place pyi
file in the main package everything works OK.
I would be grateful for any hints how to make this work:
pathlib
part seem to offend my version of distutils
.PyCharmX.X/config/python-skeletons
but it didn't help.'Some things that work, but don't resolve the problem:
So the questions: How to create a minimal, distributable package with Python stubs, which will be recognized by existing tools. Based on the experiments I suspect one of two problems:
shared/typehints/pythonX.Y
- if this is true, how should I define data_files
?A stub file is a computer file that appears to the user to be on disk and immediately available for use, but is actually held either in part or entirely on a different storage medium.
To generate a stub file use javah 's -stubs option. Again remember to run javah on the Java class. By default, javah will place the resulting stub file in the same directory as the . class file.
What is a stub file? It is a file with the . pyi extension, which describes the Python type information, both input and return for functions and omits the logical part.
Problem is that you didn't include the foo/__init__.pyi
file in your stub distribution. Even though it's empty, it makes foo
a stub files package, and enables search for foo.bar
.
You can modify the data_files
in your setup.py
to include both
data_files=[
(
'shared/typehints/python{}.{}/foo/bar'.format(*sys.version_info[:2]),
["foo/bar/__init__.pyi"]
),
(
'shared/typehints/python{}.{}/foo'.format(*sys.version_info[:2]),
["foo/__init__.pyi"]
),
],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With