Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to add typing stubs for a namespaced package? #1725

Unanswered
lexdene asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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.

You must be logged in to vote
1 reply
Comment options

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /