Hi @coactive-jeffrey,
Thanks for providing the detailed reproduction steps. I wasn't able to re-create the same error error: Module "google.cloud" has no attribute "pubsub_v1", however there is an open issue to address a mypy issue with google.cloud.pubsub_v1. See googleapis/google-cloud-python#15652. Since I wasn't able to create the error error: Module "google.cloud" has no attribute "pubsub_v1" , I also wonder if this could be an environment specific issue.
Following the steps that you provided, I have the following output
(venv) (base) root@b76d9c22dce0:/python-pubsub/samples/snippets# python3 -m mypy subscriber.py
subscriber.py:35: error: Skipping analyzing "google.cloud": module is installed, but missing library stubs or py.typed marker [import-untyped]
subscriber.py:107: error: Skipping analyzing "google.cloud.pubsub_v1.types": module is installed, but missing library stubs or py.typed marker [import-untyped]
subscriber.py:107: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
subscriber.py:363: error: Library stubs not installed for "google.protobuf" [import-untyped]
subscriber.py:363: note: Hint: "python3 -m pip install types-protobuf"
subscriber.py:363: note: (or run "mypy --install-types" to install all missing stub packages)
subscriber.py:537: error: Returning Any from function declared to return "Subscription" [no-any-return]
subscriber.py:582: error: Returning Any from function declared to return "Subscription" [no-any-return]
subscriber.py:765: error: Skipping analyzing "google.cloud.pubsub_v1.subscriber": module is installed, but missing library stubs or py.typed marker [import-untyped]
Found 6 errors in 1 file (checked 1 source file)
After installing types-protobuf as suggested by the hint, the "google.protobuf" specific error was resolved.
I used pip install "types-protobuf==4.24.0.4" (I'm not using the latest version of types-protobuf as there is an open issue related to it python/typeshed#11254 )
The mypy failures also reduced if you use mypy==0.910 which is the version in noxfile.py. I've opened a bug for the team to update to a newer version of mypy: #1061
(venv) (base) root@b76d9c22dce0:/python-pubsub/samples/snippets# python3 -m mypy subscriber.py
subscriber.py:35: error: Skipping analyzing "google.cloud": found module but no type hints or library stubs
subscriber.py:35: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
subscriber.py:107: error: Skipping analyzing "google.cloud.pubsub_v1.types": found module but no type hints or library stubs
subscriber.py:363: error: Library stubs not installed for "google.protobuf" (or incompatible with Python 3.10)
subscriber.py:363: note: Hint: "python3 -m pip install types-protobuf"
subscriber.py:363: note: (or run "mypy --install-types" to install all missing stub packages)
subscriber.py:537: error: Returning Any from function declared to return "Subscription"
subscriber.py:582: error: Returning Any from function declared to return "Subscription"
subscriber.py:765: error: Skipping analyzing "google.cloud.pubsub_v1.subscriber": found module but no type hints or library stubs
Found 6 errors in 1 file (checked 1 source file)
I found more mypy error messages when I run the command from the root of the repository. It looks like the mypy presubmit checks are not currently running for the samples files. I've opened googleapis/google-cloud-python#15646 for the team to take a look.
(venv) (base) root@b76d9c22dce0:/python-pubsub# python3 -m mypy samples/snippets/subscriber.py
samples/snippets/subscriber.py:107: error: Module "google.cloud.pubsub_v1.types" has no attribute "DeadLetterPolicy"
samples/snippets/subscriber.py:172: error: Module has no attribute "PushConfig"
samples/snippets/subscriber.py:208: error: Module has no attribute "PushConfig"
samples/snippets/subscriber.py:209: error: Module has no attribute "PushConfig"
samples/snippets/subscriber.py:337: error: Module has no attribute "BigQueryConfig"
samples/snippets/subscriber.py:375: error: Module has no attribute "CloudStorageConfig"
samples/snippets/subscriber.py:384: error: Module has no attribute "CloudStorageConfig"
samples/snippets/subscriber.py:454: error: Module has no attribute "PushConfig"
samples/snippets/subscriber.py:456: error: Module has no attribute "Subscription"
samples/snippets/subscriber.py:484: error: Module "google.cloud.pubsub_v1.types" has no attribute "DeadLetterPolicy"
samples/snippets/subscriber.py:484: error: Module "google.cloud.pubsub_v1.types" has no attribute "FieldMask"
samples/snippets/subscriber.py:524: error: Module has no attribute "Subscription"
samples/snippets/subscriber.py:546: error: Module "google.cloud.pubsub_v1.types" has no attribute "FieldMask"
samples/snippets/subscriber.py:571: error: Module has no attribute "Subscription"
samples/snippets/subscriber.py:843: error: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
samples/snippets/subscriber.py:915: error: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
samples/snippets/subscriber.py:922: error: On Python 3 '{}'.format(b'abc') produces "b'abc'", not 'abc'; use '{!r}'.format(b'abc') if this is desired behavior
Found 17 errors in 1 file (checked 1 source file)