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

importing google.cloud.logging causes untyped modules in the namespace to be considered missing #10360

Open
graingert opened this issue Apr 23, 2021 · 9 comments
Labels

Comments

@graingert
Copy link
Contributor

graingert commented Apr 23, 2021

Bug Report

To Reproduce

with the mypy config:

[mypy]

[mypy-google.cloud.*]
ignore_missing_imports=true

and the .py file:

from google.cloud import bigquery
from google.cloud import logging

Actual Behavior

I get

foo.py:1: error: Module "google.cloud" has no attribute "bigquery"

however if I comment out:

from google.cloud import bigquery
# from google.cloud import logging

I get

Success: no issues found in 1 source file

Expected Behavior

I don't expect importing a module in a namespace with stubs to affect other modules in that namespace

(Write what happened.)

Your Environment

  • Mypy version used: 0.812 and d326952
  • Mypy command-line flags: mypy.
  • Mypy configuration options from mypy.ini (and other config files): see above
  • Python version used: Python 3.8.5 (default, Jan 27 2021, 15:41:15)
  • Operating system and version: Ubuntu 20.04.2 LTS
@graingert graingert added the bug mypy got something wrong label Apr 23, 2021
@graingert
Copy link
Contributor Author

this is also annoying when from google.cloud import logging is imported in a different file, because the has no attribute "bigquery" error isn't present when using incremental

@graingert graingert changed the title importing google.cloud.logging causes untyped modules in the namespace to be considdered missing importing google.cloud.logging causes untyped modules in the namespace to be considered missing Apr 23, 2021
@egilewski
Copy link

Still reproducible on current master.

Maybe it will be helpful: mypy check also passes if from google.cloud import logging is replaced with import google.cloud.logging as logging.

@jahas
Copy link

jahas commented Sep 16, 2021

Still reproducible on current master.

Maybe it will be helpful: mypy check also passes if from google.cloud import logging is replaced with import google.cloud.logging as logging.

It has helped me. Thank you for your help!

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jul 15, 2022

Summarising the issue (since there are some minor aspects of the original post I can't repro):

λ mypy --no-incremental --show-error-codes -c 'from google.cloud import logging'
<string>:1: error: Skipping analyzing "google.cloud": module is installed, but missing library stubs or py.typed marker  [import]
<string>:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

λ mypy --no-incremental --show-error-codes -c 'from google.cloud import bigquery
from google.cloud import logging'
<string>:2: error: Module "google.cloud" has no attribute "logging"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

That is, other imports from the same namespace package can affect the error code produced by a faulty import (the import from google-cloud-logging is faulty because it's not py.typed).

egilewski's workaround works well (because with import X form, mypy knows that X has to be a module):

change from google.cloud import logging to import google.cloud.logging as logging

@hauntsaninja hauntsaninja removed their assignment Jul 15, 2022
miketheman added a commit to miketheman/warehouse that referenced this issue Sep 17, 2022
Refs: python/mypy#10360

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
miketheman added a commit to miketheman/warehouse that referenced this issue Sep 19, 2022
Refs: python/mypy#10360

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
miketheman added a commit to miketheman/warehouse that referenced this issue Sep 26, 2022
Refs: python/mypy#10360

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
miketheman added a commit to miketheman/warehouse that referenced this issue Sep 27, 2022
Refs: python/mypy#10360

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
di added a commit to pypi/warehouse that referenced this issue Sep 29, 2022
* chore(deps): upgrade google-cloud-bigquery

Remove version constraint and include the new dependencies of numpy and
pyarrow.

Corrects current broken state where the combination of dependencies
prevents resolution.

Increases overall image size by ~100MB to include these dependencies

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: import Client directly to avoid mypy issue

Refs: python/mypy#10360

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* test: update tests to reflect import change

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: rename variable, formatting

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* Bump pyjwt[crypto] from 2.4.0 to 2.5.0

Bumps [pyjwt[crypto]](https://github.com/jpadilla/pyjwt) from 2.4.0 to 2.5.0.
- [Release notes](https://github.com/jpadilla/pyjwt/releases)
- [Changelog](https://github.com/jpadilla/pyjwt/blob/master/CHANGELOG.rst)
- [Commits](jpadilla/pyjwt@2.4.0...2.5.0)

---
updated-dependencies:
- dependency-name: pyjwt[crypto]
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Dustin Ingram <di@users.noreply.github.com>
DanielRyanSmith added a commit to GoogleChrome/chromium-dashboard that referenced this issue Oct 12, 2022
DanielRyanSmith added a commit to GoogleChrome/chromium-dashboard that referenced this issue Oct 12, 2022
DanielRyanSmith added a commit to GoogleChrome/chromium-dashboard that referenced this issue Oct 16, 2022
DanielRyanSmith added a commit to GoogleChrome/chromium-dashboard that referenced this issue Oct 24, 2022
* run mypy in CI

* more type hint fixing

* Update requirements.txt

* google.cloud import workaround

python/mypy#10360

* Revert "google.cloud import workaround"

This reverts commit e03dbc7.

* mypy google.cloud workaround

python/mypy#12985

* fix regular tests

* remove --install-types flag

* last mypy fix

* type fixes for address_reasons

* move email lists creation

* update for newly merged changes

* Fix regular tests

* Changes suggested by @jrobbins

* Catch invalid requests

* small type hint

* Change methods to properly override

* revert to previous implementation

* revert test

* add back original validate request type method

* remove unused import and rearrange methods

* remove comment string casting; add test back

* add ndb ignore

* Update test_html_rendering.html

* mypy fix

* remove merge addition
@gmc444-b
Copy link
Contributor

gmc444-b commented Dec 4, 2023

I'm seeing a very similar problem with an in-house project where I run mypy 1.7.1 over a project for both the source package and the tests, with only the --strict option. It succeeds the first time, then fails the second time with an error like the one described above, on a package namespace. Removing .mypy_cache allows it to succeed.

@aelamran
Copy link

aelamran commented Apr 8, 2024

I'm seeing a very similar problem with an in-house project where I run mypy 1.7.1 over a project for both the source package and the tests, with only the --strict option. It succeeds the first time, then fails the second time with an error like the one described above, on a package namespace. Removing .mypy_cache allows it to succeed.

Having the exact same issue, any workaround or fix?

@gmc444-b
Copy link
Contributor

gmc444-b commented Apr 8, 2024

Having the exact same issue, any workaround or fix?

For me, the workaround was to remove the .mypy_cache before each run.

@asottile-sentry
Copy link

so I don't lose it from my duplicate #17210:

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 added a commit to getsentry/sentry that referenced this issue May 2, 2024
…0183)

python/mypy#10360 (comment)

fixes:

```console
$ mypy src/sentry/utils/kvstore/bigtable.py  src/sentry/analytics/pubsub.py 
src/sentry/analytics/pubsub.py:8: error: Module "google.cloud" has no attribute "pubsub_v1"  [attr-defined]
Found 1 error in 1 file (checked 2 source files)
```

<!-- Describe your PR here. -->
@ethanhs
Copy link
Collaborator

ethanhs commented May 4, 2024

Ok I dug into this a bit before a flight. Unfortunately wasn't able to identify the correct fix yet, but I have refreshed myself on how the module finding logic works.

The root of the error is that we currently record that the package is ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS in modulefinder, but by the time we look for the attribute/module google.cloud.X where X is some untyped package, we've thrown out this info, we only know that the module is not in our list of modules, so we assume it is a missing attribute, not a module.

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

No branches or pull requests

8 participants