Skip to content

Commit 51ceb3b

Browse files
committed
refactor: improve docstrings for sphinx
1 parent 02a053e commit 51ceb3b

File tree

5 files changed

+115
-51
lines changed

5 files changed

+115
-51
lines changed

src/firebase_functions/core.py

-17
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,3 @@ class CloudEvent(_typing.Generic[T]):
6565
The resource, provided by source, that this event relates to
6666
"""
6767

68-
69-
@_dataclass.dataclass(frozen=True)
70-
class Change(_typing.Generic[T]):
71-
"""
72-
* The Functions interface for events that change state, such as
73-
* Realtime Database `on_value_written`.
74-
"""
75-
76-
before: T
77-
"""
78-
The state of data before the change.
79-
"""
80-
81-
after: T
82-
"""
83-
The state of data after the change.
84-
"""

src/firebase_functions/db.py

+55-14
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
1614
"""
1715
Module for Cloud Functions that are triggered by the Firebase Realtime Database.
1816
"""
@@ -21,18 +19,36 @@
2119
import functools as _functools
2220
import typing as _typing
2321
import datetime as _dt
24-
import firebase_functions.options as _options
2522
import firebase_functions.private.util as _util
2623
import firebase_functions.core as _core
2724
import cloudevents.http as _ce
28-
from firebase_functions.core import Change # Exported for user ease of typing events
25+
26+
from firebase_functions.options import DatabaseOptions
2927

3028
_event_type_written = "google.firebase.database.ref.v1.written"
3129
_event_type_created = "google.firebase.database.ref.v1.created"
3230
_event_type_updated = "google.firebase.database.ref.v1.updated"
3331
_event_type_deleted = "google.firebase.database.ref.v1.deleted"
3432

3533

34+
@_dataclass.dataclass(frozen=True)
35+
class Change(_typing.Generic[_core.T]):
36+
"""
37+
* The Functions interface for events that change state, such as
38+
* Realtime Database `on_value_written`.
39+
"""
40+
41+
before: _core.T
42+
"""
43+
The state of data before the change.
44+
"""
45+
46+
after: _core.T
47+
"""
48+
The state of data after the change.
49+
"""
50+
51+
3652
@_dataclass.dataclass(frozen=True)
3753
class Event(_core.CloudEvent[_core.T]):
3854
"""
@@ -87,7 +103,7 @@ def _db_endpoint_handler(
87103
# Merge delta into data to generate an 'after' view of the data.
88104
if isinstance(before, dict) and isinstance(after, dict):
89105
after = _util.prune_nones({**before, **after})
90-
database_event_data = _core.Change(
106+
database_event_data = Change(
91107
before=before,
92108
after=after,
93109
)
@@ -111,7 +127,7 @@ def _db_endpoint_handler(
111127
func(database_event)
112128

113129

114-
@_util.copy_func_kwargs(_options.DatabaseOptions)
130+
@_util.copy_func_kwargs(DatabaseOptions)
115131
def on_value_written(**kwargs) -> _typing.Callable[[_C1], _C1]:
116132
"""
117133
Event handler which triggers when data is created, updated, or deleted in Realtime Database.
@@ -124,8 +140,14 @@ def on_value_written(**kwargs) -> _typing.Callable[[_C1], _C1]:
124140
def example(event: Event[Change[object]]) -> None:
125141
pass
126142
143+
:param \\*\\*kwargs: Database options.
144+
:type \\*\\*kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
145+
:rtype: :exc:`typing.Callable`
146+
\\[ \\[ :exc:`firebase_functions.db.Event` \\[
147+
:exc:`firebase_functions.db.Change` \\] \\], `None` \\]
148+
A function that takes a Database Event and returns None.
127149
"""
128-
options = _options.DatabaseOptions(**kwargs)
150+
options = DatabaseOptions(**kwargs)
129151

130152
def on_value_written_inner_decorator(func: _C1):
131153

@@ -143,7 +165,7 @@ def on_value_written_wrapped(raw: _ce.CloudEvent):
143165
return on_value_written_inner_decorator
144166

145167

146-
@_util.copy_func_kwargs(_options.DatabaseOptions)
168+
@_util.copy_func_kwargs(DatabaseOptions)
147169
def on_value_updated(**kwargs) -> _typing.Callable[[_C1], _C1]:
148170
"""
149171
Event handler which triggers when data is updated in Realtime Database.
@@ -156,8 +178,14 @@ def on_value_updated(**kwargs) -> _typing.Callable[[_C1], _C1]:
156178
def example(event: Event[Change[object]]) -> None:
157179
pass
158180
181+
:param \\*\\*kwargs: Database options.
182+
:type \\*\\*kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
183+
:rtype: :exc:`typing.Callable`
184+
\\[ \\[ :exc:`firebase_functions.db.Event` \\[
185+
:exc:`firebase_functions.db.Change` \\] \\], `None` \\]
186+
A function that takes a Database Event and returns None.
159187
"""
160-
options = _options.DatabaseOptions(**kwargs)
188+
options = DatabaseOptions(**kwargs)
161189

162190
def on_value_updated_inner_decorator(func: _C1):
163191

@@ -175,7 +203,7 @@ def on_value_updated_wrapped(raw: _ce.CloudEvent):
175203
return on_value_updated_inner_decorator
176204

177205

178-
@_util.copy_func_kwargs(_options.DatabaseOptions)
206+
@_util.copy_func_kwargs(DatabaseOptions)
179207
def on_value_created(**kwargs) -> _typing.Callable[[_C2], _C2]:
180208
"""
181209
Event handler which triggers when data is created in Realtime Database.
@@ -185,10 +213,17 @@ def on_value_created(**kwargs) -> _typing.Callable[[_C2], _C2]:
185213
.. code-block:: python
186214
187215
@on_value_created(reference="*")
188-
def example(event):
216+
def example(event: Event[object]):
189217
pass
218+
219+
:param \\*\\*kwargs: Database options.
220+
:type \\*\\*kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
221+
:rtype: :exc:`typing.Callable`
222+
\\[ \\[ :exc:`firebase_functions.db.Event` \\[
223+
:exc:`object` \\] \\], `None` \\]
224+
A function that takes a Database Event and returns None.
190225
"""
191-
options = _options.DatabaseOptions(**kwargs)
226+
options = DatabaseOptions(**kwargs)
192227

193228
def on_value_created_inner_decorator(func: _C2):
194229

@@ -206,7 +241,7 @@ def on_value_created_wrapped(raw: _ce.CloudEvent):
206241
return on_value_created_inner_decorator
207242

208243

209-
@_util.copy_func_kwargs(_options.DatabaseOptions)
244+
@_util.copy_func_kwargs(DatabaseOptions)
210245
def on_value_deleted(**kwargs) -> _typing.Callable[[_C2], _C2]:
211246
"""
212247
Event handler which triggers when data is deleted in Realtime Database.
@@ -219,8 +254,14 @@ def on_value_deleted(**kwargs) -> _typing.Callable[[_C2], _C2]:
219254
def example(event: Event[object]) -> None:
220255
pass
221256
257+
:param \\*\\*kwargs: Database options.
258+
:type \\*\\*kwargs: as :exc:`firebase_functions.options.DatabaseOptions`
259+
:rtype: :exc:`typing.Callable`
260+
\\[ \\[ :exc:`firebase_functions.db.Event` \\[
261+
:exc:`object` \\] \\], `None` \\]
262+
A function that takes a Database Event and returns None.
222263
"""
223-
options = _options.DatabaseOptions(**kwargs)
264+
options = DatabaseOptions(**kwargs)
224265

225266
def on_value_deleted_inner_decorator(func: _C2):
226267

src/firebase_functions/https.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
15-
1614
"""Module for Cloud Functions that listen to HTTPS endpoints.
1715
These can be raw web requests and Callable RPCs.
1816
"""
@@ -23,11 +21,11 @@
2321
import typing_extensions as _typing_extensions
2422
import enum as _enum
2523
import json as _json
26-
import firebase_functions.options as _options
2724
import firebase_functions.private.util as _util
2825
import firebase_functions.core as _core
2926
from functions_framework import logging as _logging
3027

28+
from firebase_functions.options import HttpsOptions
3129
from flask import Request, Response, make_response as _make_response, jsonify as _jsonify
3230
from flask_cors import cross_origin as _cross_origin
3331

@@ -407,7 +405,7 @@ def _on_call_handler(func: _C2, request: Request) -> Response:
407405
return _make_response(_jsonify(error=err._as_dict()), status)
408406

409407

410-
@_util.copy_func_kwargs(_options.HttpsOptions)
408+
@_util.copy_func_kwargs(HttpsOptions)
411409
def on_request(**kwargs) -> _typing.Callable[[_C1], _C1]:
412410
"""
413411
Handler which handles HTTPS requests.
@@ -421,8 +419,12 @@ def on_request(**kwargs) -> _typing.Callable[[_C1], _C1]:
421419
def example(request: Request) -> Response:
422420
pass
423421
422+
:param \\*\\*kwargs: Https options.
423+
:type \\*\\*kwargs: as :exc:`firebase_functions.options.HttpsOptions`
424+
:rtype: :exc:`typing.Callable` \\[ \\[ :exc:`flask.Request` \\], :exc:`flask.Response` \\]
425+
A function that takes a :exc:`flask.Request` and returns a :exc:`flask.Response`.
424426
"""
425-
options = _options.HttpsOptions(**kwargs)
427+
options = HttpsOptions(**kwargs)
426428

427429
def on_request_inner_decorator(func: _C1):
428430

@@ -444,7 +446,7 @@ def on_request_wrapped(request: Request) -> Response:
444446
return on_request_inner_decorator
445447

446448

447-
@_util.copy_func_kwargs(_options.HttpsOptions)
449+
@_util.copy_func_kwargs(HttpsOptions)
448450
def on_call(**kwargs) -> _typing.Callable[[_C2], _C2]:
449451
"""
450452
Declares a callable method for clients to call using a Firebase SDK.
@@ -456,10 +458,16 @@ def on_call(**kwargs) -> _typing.Callable[[_C2], _C2]:
456458
457459
@on_call()
458460
def example(request: CallableRequest) -> Any:
459-
pass
461+
return "Hello World"
460462
463+
:param \\*\\*kwargs: Https options.
464+
:type \\*\\*kwargs: as :exc:`firebase_functions.options.HttpsOptions`
465+
:rtype: :exc:`typing.Callable`
466+
\\[ \\[ :exc:`firebase_functions.https.CallableRequest` \\[
467+
:exc:`object` \\] \\], :exc:`object` \\]
468+
A function that takes a CallableRequest and returns an :exc:`object`.
461469
"""
462-
options = _options.HttpsOptions(**kwargs)
470+
options = HttpsOptions(**kwargs)
463471

464472
def on_call_inner_decorator(func: _C2):
465473
origins: _typing.Any = "*"

src/firebase_functions/pubsub.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
import base64 as _base64
2424
import cloudevents.http as _ce
2525

26-
import firebase_functions.options as _options
2726
import firebase_functions.private.util as _util
27+
2828
from firebase_functions.core import CloudEvent, T
29+
from firebase_functions.options import PubSubOptions
2930

3031

3132
@_dataclasses.dataclass(frozen=True)
@@ -153,7 +154,7 @@ def _message_handler(
153154
func(event)
154155

155156

156-
@_util.copy_func_kwargs(_options.PubSubOptions)
157+
@_util.copy_func_kwargs(PubSubOptions)
157158
def on_message_published(**kwargs) -> _typing.Callable[[_C1], _C1]:
158159
"""
159160
Event handler which triggers on a message being published to a Pub/Sub topic.
@@ -166,8 +167,15 @@ def on_message_published(**kwargs) -> _typing.Callable[[_C1], _C1]:
166167
def example(event: CloudEvent[MessagePublishedData[object]]) -> None:
167168
pass
168169
170+
:param \\*\\*kwargs: Pub/Sub options.
171+
:type \\*\\*kwargs: as :exc:`firebase_functions.options.PubSubOptions`
172+
:rtype: :exc:`typing.Callable`
173+
\\[ \\[ :exc:`firebase_functions.core.CloudEvent` \\[
174+
:exc:`firebase_functions.pubsub.MessagePublishedData` \\[
175+
:exc:`typing.Any` \\] \\] \\], `None` \\]
176+
A function that takes a CloudEvent and returns None.
169177
"""
170-
options = _options.PubSubOptions(**kwargs)
178+
options = PubSubOptions(**kwargs)
171179

172180
def on_message_published_inner_decorator(func: _C1):
173181

0 commit comments

Comments
 (0)