Repository navigation
importing google.cloud.logging causes untyped modules in the namespace to be considered missing #10360
Description
Activity
this is also annoying when
from google.cloud import loggingis imported in a different file, because thehas no attribute "bigquery"error isn't present when using incremental- 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[/+]on Apr 23, 2021 Still reproducible on current
master.Maybe it will be helpful: mypy check also passes if
from google.cloud import loggingis replaced withimport google.cloud.logging as logging.Reacted by Liza Daly, jahas, David Haupt, Benjamin Dornel, Higuchi kokoro, Jack Collins and vaishuanupindiStill reproducible on current
master.Maybe it will be helpful: mypy check also passes if
from google.cloud import loggingis replaced withimport google.cloud.logging as logging.It has helped me. Thank you for your help!
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 Xform, mypy knows that X has to be a module):change
from google.cloud import loggingtoimport google.cloud.logging as loggingReacted by Eugeniusz Gilewski, Thomas Grainger, xuan-zoe and hyperbola- added a commit that references this issue
on Sep 29, 2022 - added 3 commits that reference this issue
on Oct 12, 2022 - added a commit that references this issue
on Oct 24, 2022 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.
Reacted by StephenI'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?
Having the exact same issue, any workaround or fix?
For me, the workaround was to remove the
.mypy_cachebefore each run.Reacted by StephenReacted by hyperbola and sh-at-csso I don't lose it from my duplicate #17210:
I believe this causes the bug -- or at least commenting it out "fixes" my problem --
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 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_HINTSin modulefinder, but by the time we look for the attribute/modulegoogle.cloud.XwhereXis 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.Reacted by sh-at-csReacted by hyperbola and Brian MacPherson- added a commit that references this issue
on Mar 22, 2025 - added a commit that references this issue
on Mar 27, 2026
Bug Report
To Reproduce
with the mypy config:
and the .py file:
Actual Behavior
I get
however if I comment out:
I get
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.mypy.ini(and other config files): see above