googleapis / python-pubsub

Apache License 2.0
392 stars 206 forks source link

Make sure type checks pass with mypy as well #536

Open plamut opened 2 years ago

plamut commented 2 years ago

500 added type annotation to the codebase, but we only checked that static type analysis passes under pytype. A lot of people, on the other hand, use mypy, but mypy currently reports 97 errors.

If we want users to leverage our type annotations, we should make sure that the checks pass with mypy as well, and then we can also add py.typed file to declare the library type-checked.

An additional requirement of this issue is that a new nox session named mypy needs to be added and enabled by default.

Acceptance criteria:

parthea commented 2 years ago

I reviewed the mypy failures for the generated code and found that the warnings are specific to the customized portion of the generated code.

For example, for google/pubsub_v1/types/__init__.py:82 error: Name "google" is not defined, the specific replacement in owlbot.py is here: https://github.com/googleapis/python-pubsub/blob/8513f539a5dfad8ea22126706f3a4e9580df25dc/owlbot.py#L273-L288

For another error google/pubsub_v1/services/subscriber/client.py:492: error: Incompatible types in assignment (expression has type "partial[SubscriberTransport]", variable has type "Type[SubscriberTransport]"), the specific replacement in owlbot.py is here: https://github.com/googleapis/python-pubsub/blob/8513f539a5dfad8ea22126706f3a4e9580df25dc/owlbot.py#L108-L122

kasium commented 2 years ago

I'm not sure, but isn't the py.typed file missing in google/cloud/pubsub and google/cloud/pubsub_v1?

ckcr4lyf commented 1 year ago

FYI: If someone stumbles across this after struggling with mypy, you can just ignore it:

from google.cloud import pubsub_v1  # type: ignore[attr-defined]
knifewine commented 1 month ago

encountering this with google-cloud-pubsub 2.18.4 -- apologies for the reopen if it's been resolved in a newer version.