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

namespace package import error is order-dependent and not silenceable with ignore_missing_imports #17210

Closed
asottile-sentry opened this issue May 2, 2024 · 3 comments
Labels
bug mypy got something wrong

Comments

@asottile-sentry
Copy link

Bug Report

my minimal reproduction unfortunately requires some third party dependencies but I've tried to make it as self-contained as possible:

To Reproduce

#!/usr/bin/env bash
set -euxo pipefail

rm -rf venv .mypy_cache

cat > pyproject.toml <<EOF
[[tool.mypy.overrides]]
module = ["google.*"]
ignore_missing_imports = true
EOF

cat > t.py <<EOF
from google.cloud import pubsub_v1
from google.cloud import bigtable
EOF

virtualenv -q venv
venv/bin/pip install -q google-cloud-bigtable==2.22.0 google-cloud-pubsub==2.19.0 mypy

venv/bin/mypy t.py

Expected Behavior

I expect it to produce no errors

Actual Behavior

$ bash t.sh 
+ rm -rf venv .mypy_cache
+ cat
+ cat
+ virtualenv -q venv
+ venv/bin/pip install -q google-cloud-bigtable==2.22.0 google-cloud-pubsub==2.19.0 mypy
+ venv/bin/mypy t.py
t.py:1: error: Module "google.cloud" has no attribute "pubsub_v1"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

what's even stranger is if the bigtable import is commented out it passes:

$ cat t.py 
from google.cloud import pubsub_v1
# from google.cloud import bigtable
$ rm -rf .mypy_cache/ && venv/bin/mypy t.py
Success: no issues found in 1 source file

I suspect something to do with namespace packages since google.* is a big bunch of them -- it also has various py.typed files which should have some things typed and others not -- google.cloud.pubsub_v1 does not have one of those (not to be confused with google.pubsub_v1 which does have one):

$ find venv/lib/python3.12/site-packages/google -name py.typed | sort
venv/lib/python3.12/site-packages/google/api_core/py.typed
venv/lib/python3.12/site-packages/google/cloud/_helpers/py.typed
venv/lib/python3.12/site-packages/google/cloud/_http/py.typed
venv/lib/python3.12/site-packages/google/cloud/_testing/py.typed
venv/lib/python3.12/site-packages/google/cloud/bigtable/py.typed
venv/lib/python3.12/site-packages/google/cloud/bigtable_admin/py.typed
venv/lib/python3.12/site-packages/google/cloud/bigtable_admin_v2/py.typed
venv/lib/python3.12/site-packages/google/cloud/bigtable_v2/py.typed
venv/lib/python3.12/site-packages/google/cloud/client/py.typed
venv/lib/python3.12/site-packages/google/cloud/environment_vars/py.typed
venv/lib/python3.12/site-packages/google/cloud/exceptions/py.typed
venv/lib/python3.12/site-packages/google/cloud/obsolete/py.typed
venv/lib/python3.12/site-packages/google/cloud/operation/py.typed
venv/lib/python3.12/site-packages/google/pubsub/py.typed
venv/lib/python3.12/site-packages/google/pubsub_v1/py.typed

I don't think the other directories have an effect as I deleted everything but the bigtable and pubsub_v1 directories and it still reproduces

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: see above
  • Mypy configuration options from mypy.ini (and other config files): see above
  • Python version used: 3.12.2 -- also reproduces on 3.11.x -- didn't try any others
@asottile-sentry asottile-sentry added the bug mypy got something wrong label May 2, 2024
@asottile-sentry
Copy link
Author

deleting the bigtable py.typed file causes it to succeed as well, though I can't really do that (but it might hint at a solution?)

@asottile-sentry
Copy link
Author

I believe this causes the bug -- or at least commenting it out "fixes" my problem --

mypy/mypy/modulefinder.py

Lines 543 to 549 in fb31409

# Finally, we may be asked to produce an ancestor for an
# installed package with a py.typed marker that is a
# subpackage of a namespace package. We only fess up to these
# if we would otherwise return "not found".
ancestor = self.ns_ancestors.get(id)
if ancestor is not None:
return ancestor

@asottile-sentry
Copy link
Author

appears to be a duplicate of #10360

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

No branches or pull requests

1 participant