Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 093cabf

Browse files
authored
docs: add type annotations to codebase (#509)
Closes #500. **PR checklist:** - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-pubsub/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary)
1 parent ecbe708 commit 093cabf

24 files changed

+805
-636
lines changed

google/cloud/pubsub_v1/_gapic.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515
from __future__ import absolute_import
1616

1717
import functools
18+
from typing import Callable, Container, Type
1819

1920

20-
def add_methods(source_class, denylist=()):
21+
def add_methods(
22+
source_class: Type, denylist: Container[str] = ()
23+
) -> Callable[[Type], Type]:
2124
"""Add wrapped versions of the `api` member's methods to the class.
2225
2326
Any methods passed in `denylist` are not added.
2427
Additionally, any methods explicitly defined on the wrapped class are
2528
not added.
2629
"""
2730

28-
def wrap(wrapped_fx, lookup_fx):
31+
def wrap(wrapped_fx: Callable, lookup_fx: Callable):
2932
"""Wrap a GAPIC method; preserve its name and docstring."""
3033
# If this is a static or class method, then we do *not*
3134
# send self as the first argument.
@@ -40,7 +43,7 @@ def wrap(wrapped_fx, lookup_fx):
4043
fx = lambda self, *a, **kw: wrapped_fx(self.api, *a, **kw) # noqa
4144
return functools.wraps(wrapped_fx)(fx)
4245

43-
def actual_decorator(cls):
46+
def actual_decorator(cls: Type) -> Type:
4447
# Reflectively iterate over most of the methods on the source class
4548
# (the GAPIC) and make wrapped versions available on this client.
4649
for name in dir(source_class):

google/cloud/pubsub_v1/futures.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from __future__ import absolute_import
1616

1717
import concurrent.futures
18+
from typing import Any, NoReturn
1819

1920
import google.api_core.future
2021

@@ -29,27 +30,24 @@ class Future(concurrent.futures.Future, google.api_core.future.Future):
2930
methods in this library.
3031
"""
3132

32-
def running(self):
33-
"""Return ``True`` if the associated Pub/Sub action has not yet completed.
34-
35-
Returns: bool:
36-
"""
33+
def running(self) -> bool:
34+
"""Return ``True`` if the associated Pub/Sub action has not yet completed."""
3735
return not self.done()
3836

39-
def set_running_or_notify_cancel(self):
37+
def set_running_or_notify_cancel(self) -> NoReturn:
4038
raise NotImplementedError(
4139
"Only used by executors from `concurrent.futures` package."
4240
)
4341

44-
def set_result(self, result):
42+
def set_result(self, result: Any):
4543
"""Set the return value of work associated with the future.
4644
4745
Do not use this method, it should only be used internally by the library and its
4846
unit tests.
4947
"""
5048
return super().set_result(result=result)
5149

52-
def set_exception(self, exception):
50+
def set_exception(self, exception: Exception):
5351
"""Set the result of the future as being the given exception.
5452
5553
Do not use this method, it should only be used internally by the library and its

google/cloud/pubsub_v1/publisher/_batch/base.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616

1717
import abc
1818
import enum
19+
import typing
20+
from typing import Optional, Sequence
21+
22+
23+
if typing.TYPE_CHECKING: # pragma: NO COVER
24+
from google.cloud import pubsub_v1
25+
from google.cloud.pubsub_v1 import types
26+
from google.pubsub_v1 import types as gapic_types
1927

2028

2129
class Batch(metaclass=abc.ABCMeta):
@@ -50,7 +58,7 @@ def __len__(self):
5058

5159
@staticmethod
5260
@abc.abstractmethod
53-
def make_lock(): # pragma: NO COVER
61+
def make_lock() -> None: # pragma: NO COVER
5462
"""Return a lock in the chosen concurrency model.
5563
5664
Returns:
@@ -60,17 +68,17 @@ def make_lock(): # pragma: NO COVER
6068

6169
@property
6270
@abc.abstractmethod
63-
def messages(self): # pragma: NO COVER
71+
def messages(self) -> Sequence["gapic_types.PubsubMessage"]: # pragma: NO COVER
6472
"""Return the messages currently in the batch.
6573
6674
Returns:
67-
Sequence: The messages currently in the batch.
75+
The messages currently in the batch.
6876
"""
6977
raise NotImplementedError
7078

7179
@property
7280
@abc.abstractmethod
73-
def size(self): # pragma: NO COVER
81+
def size(self) -> int: # pragma: NO COVER
7482
"""Return the total size of all of the messages currently in the batch.
7583
7684
The size includes any overhead of the actual ``PublishRequest`` that is
@@ -84,42 +92,45 @@ def size(self): # pragma: NO COVER
8492

8593
@property
8694
@abc.abstractmethod
87-
def settings(self): # pragma: NO COVER
95+
def settings(self) -> "types.BatchSettings": # pragma: NO COVER
8896
"""Return the batch settings.
8997
9098
Returns:
91-
~.pubsub_v1.types.BatchSettings: The batch settings. These are
92-
considered immutable once the batch has been opened.
99+
The batch settings. These are considered immutable once the batch has
100+
been opened.
93101
"""
94102
raise NotImplementedError
95103

96104
@property
97105
@abc.abstractmethod
98-
def status(self): # pragma: NO COVER
106+
def status(self) -> "BatchStatus": # pragma: NO COVER
99107
"""Return the status of this batch.
100108
101109
Returns:
102-
str: The status of this batch. All statuses are human-readable,
103-
all-lowercase strings. The ones represented in the
104-
:class:`BaseBatch.Status` enum are special, but other statuses
105-
are permitted.
110+
The status of this batch. All statuses are human-readable, all-lowercase
111+
strings. The ones represented in the :class:`BaseBatch.Status` enum are
112+
special, but other statuses are permitted.
106113
"""
107114
raise NotImplementedError
108115

109-
def cancel(self, cancellation_reason): # pragma: NO COVER
116+
def cancel(
117+
self, cancellation_reason: "BatchCancellationReason"
118+
) -> None: # pragma: NO COVER
110119
"""Complete pending futures with an exception.
111120
112121
This method must be called before publishing starts (ie: while the
113122
batch is still accepting messages.)
114123
115124
Args:
116-
cancellation_reason (BatchCancellationReason): The reason why this
117-
batch has been cancelled.
125+
cancellation_reason:
126+
The reason why this batch has been cancelled.
118127
"""
119128
raise NotImplementedError
120129

121130
@abc.abstractmethod
122-
def publish(self, message): # pragma: NO COVER
131+
def publish(
132+
self, message: "gapic_types.PubsubMessage"
133+
) -> Optional["pubsub_v1.publisher.futures.Future"]: # pragma: NO COVER
123134
"""Publish a single message.
124135
125136
Add the given message to this object; this will cause it to be
@@ -129,11 +140,12 @@ def publish(self, message): # pragma: NO COVER
129140
This method is called by :meth:`~.PublisherClient.publish`.
130141
131142
Args:
132-
message (~.pubsub_v1.types.PubsubMessage): The Pub/Sub message.
143+
message: The Pub/Sub message.
133144
134145
Returns:
135-
~google.api_core.future.Future: An object conforming to the
136-
:class:`concurrent.futures.Future` interface.
146+
An object conforming to the :class:`concurrent.futures.Future` interface.
147+
If :data:`None` is returned, that signals that the batch cannot
148+
accept a message.
137149
"""
138150
raise NotImplementedError
139151

0 commit comments

Comments
 (0)