Skip to content
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

Somewhat random Module "project.foo.bar" has no attribute "baz_pb2" [attr-defined] errors #615

Open
sodul opened this issue May 22, 2024 · 1 comment

Comments

@sodul
Copy link

sodul commented May 22, 2024

We have a private repository with protobufs used by go and python code.

We use the latest releases of python, mypy, mypy-protobuf, protoc, buf, etc... and in recent weeks we are seeing some odd errors on the imports when mypy is analyzing our python code.

If the import is in this format:

from project.foo.bar import baz_pb2

It usually works, but once in a while we get:

Module "project.foo.bar" has no attribute "baz_pb2"  [attr-defined]

Regenerating the *_pb2.py and *_pb2.pyi files from scratch does not seem to help, but this seems to work:

import project.foo.bar.baz_pb2

or

import project.foo.bar.baz_pb2 import baz_pb2

I had this happen for a couple of pb2 imports 2-3 weeks ago, then the problem went away, now it is happening to more _pb2 imports, but not all. The other issues, is that mypy no longer report any typing errors and all the proto objects are pretty much equivalent to Any.

I tried to re-install python from scratch, purge __pycache__ folders, the mypy cache, the pip caches, nothing seems to help besides doing a direct import of the modules.

Fortunately our CI pipelines are not impacted but this makes it very difficult for me to use and trust mypy locally now.

@sodul
Copy link
Author

sodul commented May 23, 2024

It turns out that part of the issue is that the internal tool we use to wrap buf and protoc does not add any __init__.py files, which makes these folders namespaced packages. Adding the files and then adding the parent folder to mypy.ini seems to have addressed the issue.

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

1 participant