-
Notifications
You must be signed in to change notification settings - Fork 288
-
For example, there are 2 packages in the same namespace:
- somenamespace-foo
- somenamespace-bar
usage:
from somenamespace.foo import xxx
from somenamespace.bar import yyy
somenamespace-foo provides typing annotations but somenamespace-bar does not.
How can I add typing stubs for somenamespace-bar without influencing somenamespace-foo?
I tried but doesnot work:
bar-stubs/setup.py:
from setuptools import setup, find_namespace_packages
ns_name = 'somenamespace'
sub_name = 'bar'
setup(
name=f'{ns_name}.{sub_name}-stubs',
version='0.0.1',
packages=find_namespace_packages(include=[f'{ns_name}.*']),
namespace_packages=[ns_name],
package_data={
ns_name: [
'bar-stubs/say.pyi',
]
},
)
check with mypy:
$ mypy --strict main/
main/main.py:2: error: Skipping analyzing "somenamespace.bar.say": module is installed, but missing library stubs or py.typed marker [import-untyped]
main/main.py:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)
full example here: https://github.com/lexdene/python-typing-stubs-for-namespace-package
And you can see mypy error message inside github actions.
Update:
In the reality case, both somenamespace-foo and somenamespace-bar are not maintained by me.
I cannot update their code.
The only one I can control is somenamespace-bar-stubs.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
It looks like you're very close to getting it to work. You simply need to add another py.typed marker file in the bar directory, like you've done in the foo directory already. PEP 561 says:
For namespace packages, the py.typed file should be in the submodules of the namespace, to avoid conflicts and for clarity.
If I manually add that missing marker file, mypy no longer emits the error you're seeing.
Beta Was this translation helpful? Give feedback.
All reactions
-
I cannot do that.
In the reality case, both somenamespace-foo and somenamespace-bar are not maintained by me.
I cannot update their code.
The only one I can control is somenamespace-bar-stubs.
Beta Was this translation helpful? Give feedback.