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

Make sure type checks pass with mypy as well #536

Open
4 of 6 tasks
plamut opened this issue Nov 11, 2021 · 3 comments
Open
4 of 6 tasks

Make sure type checks pass with mypy as well #536

plamut opened this issue Nov 11, 2021 · 3 comments
Assignees
Labels
api: pubsub Issues related to the googleapis/python-pubsub API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: process A process-related concern. May include testing, release, or the like.

Comments

@plamut
Copy link
Contributor

plamut commented Nov 11, 2021

#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:

@plamut plamut added the type: process A process-related concern. May include testing, release, or the like. label Nov 11, 2021
@plamut plamut self-assigned this Nov 11, 2021
@product-auto-label product-auto-label bot added the api: pubsub Issues related to the googleapis/python-pubsub API. label Nov 11, 2021
@plamut plamut removed their assignment Feb 15, 2022
@parthea
Copy link
Contributor

parthea commented May 18, 2022

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:

python-pubsub/owlbot.py

Lines 273 to 288 in 8513f53

s.replace(
library / f"google/pubsub_{library.name}/types/__init__.py",
r"__all__ = \(\n",
textwrap.dedent(
'''\
TimeoutType = Union[
int,
float,
"google.api_core.timeout.ConstantTimeout",
"google.api_core.timeout.ExponentialTimeout",
]
"""The type of the timeout parameter of publisher client methods."""
\g<0> "TimeoutType",'''
),
)

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:

python-pubsub/owlbot.py

Lines 108 to 122 in 8513f53

count = s.replace(
clients_to_patch,
r"Transport = type\(self\)\.get_transport_class\(transport\)",
"""\g<0>
emulator_host = os.environ.get("PUBSUB_EMULATOR_HOST")
if emulator_host:
if issubclass(Transport, type(self)._transport_registry["grpc"]):
channel = grpc.insecure_channel(target=emulator_host)
else:
channel = grpc.aio.insecure_channel(target=emulator_host)
Transport = functools.partial(Transport, channel=channel)
""",
)

@kasium
Copy link

kasium commented Oct 21, 2022

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

@kamalaboulhosn kamalaboulhosn added the priority: p3 Desirable enhancement or fix. May not be included in next release. label Jul 17, 2023
@ckcr4lyf
Copy link

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the googleapis/python-pubsub API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: process A process-related concern. May include testing, release, or the like.
Projects
None yet
Development

No branches or pull requests

7 participants