Skip to content

Mypy cannot find library stubs for protobuf #10986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
schmidma opened this issue Nov 6, 2023 · 5 comments
Closed

Mypy cannot find library stubs for protobuf #10986

schmidma opened this issue Nov 6, 2023 · 5 comments

Comments

@schmidma
Copy link

schmidma commented Nov 6, 2023

I'm having issues with mypy correctly finding type stubs.

After creating a virtual environment on my Fedora machine using virtualenv, I install the following packages:

pip install mypy protobuf types-protobuf

Using mypy to check the following code

import google.protobuf

I get the error messsage:

my_file.py:1: error: Skipping analyzing "google": module is installed, but missing library stubs or py.typed marker  [import-untyped]
my_file.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

The reason for this seems that in the virtual environment there are two independent lib directories (my_env/lib and my_env/lib64). One google directory containing code of the protobuf package is in lib64/google and the types (types-protobuf) are installed to lib/google-stubs.
Either moving the google-stubs directory to the lib64 variant or creating an (empty) google directory in lib resolves the issue.

It seems mypy is unable to find the corresponding type stubs in the respectively other lib directory.

Has anybody an idea on how to approach this? I do not even know whether the respective installation locations of either the protobuf or the types-protobuf package are correct or whether this is an issue with mypy resolving types?!

@brandonchinn178
Copy link

+1 It seems like only google-stubs/protobuf/__init__.pyi exists, so when doing import google.protobuf, mypy makes sure that both import google and import google.protobuf resolve, which fails.

cc @Avasam might be as easy as adding google/__init__.pyi

@Avasam
Copy link
Collaborator

Avasam commented Apr 4, 2025

@brandonchinn178

google/__init__.py does not exist though
Adding google/__init__.pyi would make all untyped google namespaced packages seen as typed

And mypy is (should be) able to resolve typed namespaced packages correctly (it's a core part of the spec).

I also cannot replicate op's situation locally (pretty sure their issue is caused by the my_env/lib vs my_env/lib64 differentiation, but I have no idea how to help or investigate that part)

@brandonchinn178
Copy link

hm I'm not sure about OP's situation, but I'm encountering this issue:

  1. python3 -m venv venv
  2. venv/bin/pip install mypy types-protobuf
  3. echo 'import google.protobuf.descriptor' > test.py
  4. venv/bin/mypy test.py

This shows an error at "Could not find library google". Can you repro that? What would be the recommended workaround?

@srittau
Copy link
Collaborator

srittau commented Apr 4, 2025

I can reproduce the issue locally, but I don't see anything wrong with the installed types-protobuf package. It's a bit curious that this is the first time this comes up (the original issue seems to be related to some environment related weirdness) and we've just recently revamped the way packages are built. But I see no significant change between older types-protobuf packages and the newer one and I could reproduce the issue with types-protobuf==5.29.1.20250208 as well. I tend to assume that this is a mypy bug, possibly related to python/mypy#14057.

@srittau
Copy link
Collaborator

srittau commented Apr 4, 2025

Anyway, I'm closing this issue, because the original issue seems to be unrelated to the problem by @brandonchinn178 and looks to be a weird problem related to the environment of the submitter.

@brandonchinn178: Could you open an issue against mypy, with your problem description?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants